Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +29 -5
static/script.js
CHANGED
|
@@ -74,22 +74,46 @@ function handleResponse(userInput) {
|
|
| 74 |
}
|
| 75 |
|
| 76 |
function displayOptions(options) {
|
| 77 |
-
// Function to display options on the UI
|
| 78 |
const optionsContainer = document.getElementById('options-container');
|
| 79 |
if (!optionsContainer) {
|
| 80 |
console.error('Options container not found!');
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
const button = document.createElement('button');
|
| 86 |
-
button.
|
| 87 |
-
button.
|
| 88 |
-
button.onclick = () =>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
optionsContainer.appendChild(button);
|
| 90 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
|
|
|
| 93 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
| 94 |
if (e.key === 'Enter') {
|
| 95 |
sendMessage();
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
function displayOptions(options) {
|
|
|
|
| 77 |
const optionsContainer = document.getElementById('options-container');
|
| 78 |
if (!optionsContainer) {
|
| 79 |
console.error('Options container not found!');
|
| 80 |
return;
|
| 81 |
}
|
| 82 |
|
| 83 |
+
optionsContainer.innerHTML = ''; // Clear previous options if any
|
| 84 |
+
|
| 85 |
+
options.forEach(opt => {
|
| 86 |
const button = document.createElement('button');
|
| 87 |
+
button.textContent = opt.text;
|
| 88 |
+
button.className = `option-button ${opt.class}`;
|
| 89 |
+
button.onclick = () => {
|
| 90 |
+
addMessage('user', opt.text);
|
| 91 |
+
conversation.push({ role: 'user', message: opt.text });
|
| 92 |
+
setTimeout(() => handleResponse(opt.text), 500);
|
| 93 |
+
};
|
| 94 |
optionsContainer.appendChild(button);
|
| 95 |
});
|
| 96 |
+
|
| 97 |
+
optionsContainer.appendChild(document.createElement('br')); // Line break after options
|
| 98 |
+
|
| 99 |
+
// Optional 'Go Back' button functionality
|
| 100 |
+
if (conversation.length > 1) {
|
| 101 |
+
const backButton = document.createElement('button');
|
| 102 |
+
backButton.textContent = 'Go Back';
|
| 103 |
+
backButton.className = 'option-button';
|
| 104 |
+
backButton.onclick = () => {
|
| 105 |
+
if (conversation.length > 1) {
|
| 106 |
+
conversation.pop(); // Remove last message
|
| 107 |
+
chatMessages.innerHTML = ''; // Clear current messages
|
| 108 |
+
conversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render
|
| 109 |
+
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
| 110 |
+
}
|
| 111 |
+
};
|
| 112 |
+
optionsContainer.appendChild(backButton);
|
| 113 |
+
}
|
| 114 |
}
|
| 115 |
|
| 116 |
+
|
| 117 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
| 118 |
if (e.key === 'Enter') {
|
| 119 |
sendMessage();
|