Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +49 -0
static/script.js
CHANGED
|
@@ -118,6 +118,17 @@ function submitForm() {
|
|
| 118 |
});
|
| 119 |
}
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
function displayOptions(options, isCategory = false) {
|
| 122 |
const chatMessages = document.getElementById('chatMessages');
|
| 123 |
if (!chatMessages) {
|
|
@@ -125,6 +136,9 @@ function displayOptions(options, isCategory = false) {
|
|
| 125 |
return;
|
| 126 |
}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
options.forEach(opt => {
|
| 129 |
const messageDiv = document.createElement('div');
|
| 130 |
messageDiv.className = 'bot-message';
|
|
@@ -150,6 +164,27 @@ function displayOptions(options, isCategory = false) {
|
|
| 150 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 151 |
}
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
function handleResponse(userInput) {
|
| 154 |
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
|
| 155 |
let botResponse = '';
|
|
@@ -219,13 +254,23 @@ function handleResponse(userInput) {
|
|
| 219 |
return;
|
| 220 |
}
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
// Handle category selection
|
| 223 |
if (categories.some(cat => cat.text.toLowerCase() === lastMessage)) {
|
|
|
|
| 224 |
botResponse = `Here are the questions in the "${userInput}" category:`;
|
| 225 |
addMessage('bot', botResponse);
|
| 226 |
options = faqOptions[userInput] || [];
|
| 227 |
if (options.length > 0) {
|
| 228 |
displayOptions(options); // Display FAQ options for the selected category
|
|
|
|
| 229 |
}
|
| 230 |
return;
|
| 231 |
}
|
|
@@ -307,4 +352,8 @@ function handleResponse(userInput) {
|
|
| 307 |
}
|
| 308 |
|
| 309 |
addMessage('bot', botResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
}
|
|
|
|
| 118 |
});
|
| 119 |
}
|
| 120 |
|
| 121 |
+
function clearOptions() {
|
| 122 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 123 |
+
if (!chatMessages) return;
|
| 124 |
+
const optionButtons = chatMessages.querySelectorAll('.bot-message .option-button');
|
| 125 |
+
optionButtons.forEach(button => {
|
| 126 |
+
if (button.parentElement) {
|
| 127 |
+
button.parentElement.remove();
|
| 128 |
+
}
|
| 129 |
+
});
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
function displayOptions(options, isCategory = false) {
|
| 133 |
const chatMessages = document.getElementById('chatMessages');
|
| 134 |
if (!chatMessages) {
|
|
|
|
| 136 |
return;
|
| 137 |
}
|
| 138 |
|
| 139 |
+
// Clear existing options before displaying new ones
|
| 140 |
+
clearOptions();
|
| 141 |
+
|
| 142 |
options.forEach(opt => {
|
| 143 |
const messageDiv = document.createElement('div');
|
| 144 |
messageDiv.className = 'bot-message';
|
|
|
|
| 164 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 165 |
}
|
| 166 |
|
| 167 |
+
function displayBackToCategoriesButton() {
|
| 168 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 169 |
+
if (!chatMessages) return;
|
| 170 |
+
|
| 171 |
+
const messageDiv = document.createElement('div');
|
| 172 |
+
messageDiv.className = 'bot-message';
|
| 173 |
+
const button = document.createElement('button');
|
| 174 |
+
button.textContent = 'Back to Categories';
|
| 175 |
+
button.className = 'option-button blue';
|
| 176 |
+
|
| 177 |
+
button.onclick = () => {
|
| 178 |
+
addMessage('user', 'Back to Categories');
|
| 179 |
+
conversation.push({ role: 'user', message: 'Back to Categories' });
|
| 180 |
+
setTimeout(() => handleResponse('back to categories'), 500);
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
messageDiv.appendChild(button);
|
| 184 |
+
chatMessages.appendChild(messageDiv);
|
| 185 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
function handleResponse(userInput) {
|
| 189 |
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
|
| 190 |
let botResponse = '';
|
|
|
|
| 254 |
return;
|
| 255 |
}
|
| 256 |
|
| 257 |
+
// Handle "Back to Categories"
|
| 258 |
+
if (lastMessage === 'back to categories') {
|
| 259 |
+
botResponse = 'Please select a category to explore frequently asked questions:';
|
| 260 |
+
addMessage('bot', botResponse);
|
| 261 |
+
displayOptions(categories, true);
|
| 262 |
+
return;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
// Handle category selection
|
| 266 |
if (categories.some(cat => cat.text.toLowerCase() === lastMessage)) {
|
| 267 |
+
currentCategory = userInput;
|
| 268 |
botResponse = `Here are the questions in the "${userInput}" category:`;
|
| 269 |
addMessage('bot', botResponse);
|
| 270 |
options = faqOptions[userInput] || [];
|
| 271 |
if (options.length > 0) {
|
| 272 |
displayOptions(options); // Display FAQ options for the selected category
|
| 273 |
+
displayBackToCategoriesButton(); // Add a "Back to Categories" button
|
| 274 |
}
|
| 275 |
return;
|
| 276 |
}
|
|
|
|
| 352 |
}
|
| 353 |
|
| 354 |
addMessage('bot', botResponse);
|
| 355 |
+
// After displaying a FAQ response, show the "Back to Categories" button
|
| 356 |
+
if (faqOptions[currentCategory]) {
|
| 357 |
+
displayBackToCategoriesButton();
|
| 358 |
+
}
|
| 359 |
}
|