Adieee5 commited on
Commit
eaf64a8
·
verified ·
1 Parent(s): 5374c4d
Files changed (1) hide show
  1. templates/chat.html +209 -112
templates/chat.html CHANGED
@@ -97,36 +97,32 @@
97
  </div>
98
 
99
  <script>
100
- $(document).ready(function () {
101
- // Retrieve chat history from localStorage, or initialize an empty array.
102
- let chatHistory = JSON.parse(localStorage.getItem("chatHistory")) || [];
103
- let currentIndex = chatHistory.length;
104
-
105
- /* -------------------
106
- Predictive Text Setup
107
- ------------------- */
108
- $("#text").on("input", function () {
109
- const input = $(this).val().toLowerCase();
110
- const predictions = getPredictiveText(input);
111
  showPredictiveText(predictions);
112
  });
113
-
114
- $(document).on("click", ".predictive-text p", function () {
115
- const text = $(this).text();
116
  $("#text").val(text);
117
  $(".predictive-text").hide();
118
  });
119
-
120
  function getPredictiveText(input) {
121
- const predictions = [];
122
- const patterns = {
123
  "greeting": ["Hi", "Hey", "How are you", "what's up", "Is anyone there?", "Hello", "Good day"],
124
  "name": ["what is your name", "name", "what's your name", "who are you", "what should I call you"]
125
  };
126
-
127
- // Loop through each pattern group and add matching predictions.
128
- for (let tag in patterns) {
129
- patterns[tag].forEach(function (pattern) {
130
  if (pattern.toLowerCase().startsWith(input) && !predictions.includes(pattern)) {
131
  predictions.push(pattern);
132
  }
@@ -134,11 +130,11 @@
134
  }
135
  return predictions;
136
  }
137
-
138
  function showPredictiveText(predictions) {
139
  if (predictions.length > 0) {
140
- let html = "";
141
- predictions.forEach(function (prediction) {
142
  html += "<p>" + prediction + "</p>";
143
  });
144
  $(".predictive-text").html(html).show();
@@ -146,106 +142,207 @@
146
  $(".predictive-text").hide();
147
  }
148
  }
149
-
150
- /* ------------------------------
151
- Message Submission and Response
152
- ------------------------------ */
153
- $("#messageArea").on("submit", function (event) {
154
- event.preventDefault();
155
- $(".predictive-text").hide();
156
-
157
- // Get current time for timestamp
158
  const date = new Date();
159
  const hour = date.getHours().toString().padStart(2, '0');
160
  const minute = date.getMinutes().toString().padStart(2, '0');
161
  const str_time = hour + ":" + minute;
162
-
163
- // Get and validate user input
164
- const rawText = $("#text").val().trim();
165
- if(rawText === "") return;
166
-
167
- // Update chat history and store it
168
- chatHistory.push(rawText);
169
- localStorage.setItem("chatHistory", JSON.stringify(chatHistory));
170
- currentIndex = chatHistory.length;
171
-
172
- // Build and append the user message
173
- const userHtml = `
174
- <div class="d-flex justify-content-end mb-4">
175
- <div class="msg_container_send">
176
- ${rawText}
177
- <span class="msg_time_send">${str_time}</span>
178
- </div>
179
- <div class="img_cont_msg">
180
- <img src="/static/images/user.png" class="rounded-circle user_img_msg">
181
- </div>
182
- </div>`;
183
- $("#messageFormeight").append(userHtml);
184
- $("#text").val("");
185
- $("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
186
-
187
- // Append a typing animation for the bot
188
- const typingHtml = `
189
- <div id="typingIndicator" class="d-flex justify-content-start mb-4">
190
- <div class="img_cont_msg">
191
- <img src="/static/images/juitlogo.png" class="rounded-circle user_img_msg">
192
- </div>
193
- <div class="msg_container typing-animation">
194
- <span>.</span><span>.</span><span>.</span>
195
- </div>
196
- </div>`;
197
- $("#messageFormeight").append(typingHtml);
198
-
199
- // Send user message to server and process bot's response
200
- $.ajax({
201
- data: { msg: rawText },
202
- type: "POST",
203
- url: "/get"
204
- }).done(function (response) {
205
- // Delay to simulate typing before showing response
206
- setTimeout(function () {
207
- // Remove the typing animation
208
- $("#typingIndicator").remove();
209
-
210
- // Append container for the bot's response
211
- const botHtmlContainer = `
212
- <div class="d-flex justify-content-start mb-4">
213
- <div class="img_cont_msg">
214
- <img src="/static/images/juitlogo.png" class="rounded-circle user_img_msg">
215
- </div>
216
- <div class="msg_container" id="botResponse"></div>
217
  </div>`;
218
- $("#messageFormeight").append(botHtmlContainer);
219
-
220
- // Typewriter effect: append one character at a time.
221
- let index = 0;
222
- function typeWriter() {
223
- if (index < response.length) {
224
- $("#botResponse").append(response.charAt(index));
225
- index++;
226
- setTimeout(typeWriter, 10); // adjust delay (50ms) for speed
227
- } else {
228
- // After finishing, append timestamp and scroll to the bottom.
229
- $("#botResponse").append(`<span class="msg_time">${str_time}</span>`);
230
- $("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
- typeWriter();
234
- }, 1000);
 
235
  });
 
 
236
  });
 
 
 
 
 
 
 
237
 
238
- /* -------------------
239
- Clear Chat Function
240
- ------------------- */
241
- $("#clearChat").click(function () {
242
- $("#messageFormeight").html("");
243
- chatHistory = [];
244
- localStorage.removeItem("chatHistory");
245
- currentIndex = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  });
247
  });
248
- </script>
 
 
249
 
250
 
251
  <script>
 
97
  </div>
98
 
99
  <script>
100
+ $(document).ready(function() {
101
+ let chatHistory = []; // Stores the user's input history
102
+ let currentIndex = -1; // Tracks the current position in the history
103
+
104
+ // Handle predictive text
105
+ $("#text").on("input", function() {
106
+ var input = $(this).val().toLowerCase();
107
+ var predictions = getPredictiveText(input);
 
 
 
108
  showPredictiveText(predictions);
109
  });
110
+
111
+ $(document).on("click", ".predictive-text p", function() {
112
+ var text = $(this).text();
113
  $("#text").val(text);
114
  $(".predictive-text").hide();
115
  });
116
+
117
  function getPredictiveText(input) {
118
+ var predictions = [];
119
+ var patterns = {
120
  "greeting": ["Hi", "Hey", "How are you", "what's up", "Is anyone there?", "Hello", "Good day"],
121
  "name": ["what is your name", "name", "what's your name", "who are you", "what should I call you"]
122
  };
123
+
124
+ for (var tag in patterns) {
125
+ patterns[tag].forEach(function(pattern) {
 
126
  if (pattern.toLowerCase().startsWith(input) && !predictions.includes(pattern)) {
127
  predictions.push(pattern);
128
  }
 
130
  }
131
  return predictions;
132
  }
133
+
134
  function showPredictiveText(predictions) {
135
  if (predictions.length > 0) {
136
+ var html = "";
137
+ predictions.forEach(function(prediction) {
138
  html += "<p>" + prediction + "</p>";
139
  });
140
  $(".predictive-text").html(html).show();
 
142
  $(".predictive-text").hide();
143
  }
144
  }
145
+
146
+ // Handle message submission and user input history
147
+ $("#messageArea").on("submit", function(event) {
148
+ $(".predictive-text").hide(); // Hide predictive suggestions
149
+
 
 
 
 
150
  const date = new Date();
151
  const hour = date.getHours().toString().padStart(2, '0');
152
  const minute = date.getMinutes().toString().padStart(2, '0');
153
  const str_time = hour + ":" + minute;
154
+
155
+ var rawText = $("#text").val();
156
+ chatHistory.push(rawText); // Store the user's message in history
157
+ currentIndex = chatHistory.length - 1; // Update the current index
158
+
159
+ var userHtml = `
160
+ <div class="d-flex justify-content-end mb-4">
161
+ <div class="msg_container_send">${rawText}
162
+ <span class="msg_time_send">${str_time}</span>
163
+ </div>
164
+ <div class="img_cont_msg">
165
+ <img src="/static/userlogo.png" class="rounded-circle user_img_msg">
166
+ </div>
167
+ </div>`;
168
+
169
+ $("#text").val(""); // Clear the input field
170
+ $("#messageFormeight").append(userHtml); // Add the user message to the chat
171
+
172
+ $(document).ready(function () {
173
+ let chatHistory = JSON.parse(localStorage.getItem("chatHistory")) || []; // Load stored history
174
+ let currentIndex = chatHistory.length; // Set index to last message
175
+
176
+ $("#messageArea").on("submit", function (event) {
177
+ event.preventDefault(); // Prevent form submission
178
+
179
+ let rawText = $("#text").val().trim();
180
+ if (rawText === "") return; // Ignore empty input
181
+
182
+ const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
183
+
184
+ // Append user message to chat
185
+ let userHtml = `
186
+ <div class="d-flex justify-content-end mb-4">
187
+ <div class="msg_container_send">${rawText}<span class="msg_time_send">${time}</span></div>
188
+ <div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  </div>`;
190
+ $("#messageFormeight").append(userHtml);
191
+
192
+ // Update chat history
193
+ chatHistory.push(rawText);
194
+ localStorage.setItem("chatHistory", JSON.stringify(chatHistory)); // Save in localStorage
195
+ currentIndex = chatHistory.length; // Reset index to end
196
+
197
+ // Clear input field & scroll down
198
+ $("#text").val("");
199
+ $("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
200
+
201
+ // Send message to server
202
+ $.ajax({
203
+ data: { msg: rawText },
204
+ type: "POST",
205
+ url: "/get",
206
+ }).done(function (response) {
207
+ // --- Typewriter effect for bot response START ---
208
+ // Create the container for the bot message
209
+ let botContainer = $(`
210
+ <div class="d-flex justify-content-start mb-4">
211
+ <div class="img_cont_msg">
212
+ <img src="/static/juitlogo.png" class="rounded-circle user_img_msg">
213
+ </div>
214
+ <div class="msg_container"></div>
215
+ </div>`);
216
+ $("#messageFormeight").append(botContainer);
217
+
218
+ let msgDiv = botContainer.find(".msg_container");
219
+ let i = 0;
220
+ function typeWriterInner() {
221
+ if (i < response.length) {
222
+ msgDiv.append(response.charAt(i));
223
+ i++;
224
+ setTimeout(typeWriterInner, 30);
225
+ } else {
226
+ msgDiv.append(`<span class="msg_time">${time}</span>`);
227
+ $("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
228
+ }
229
  }
230
+ typeWriterInner();
231
+ // --- Typewriter effect for bot response END ---
232
+ });
233
+ });
234
+
235
+ // Clear chat history
236
+ $("#clearChat").click(function () {
237
+ $("#messageFormeight").html(""); // Clears UI
238
+ chatHistory = []; // Clears array
239
+ localStorage.removeItem("chatHistory"); // Clears from storage
240
+ currentIndex = 0;
241
+ });
242
+ });
243
+
244
+ // Send the message to the server via AJAX
245
+ $.ajax({
246
+ data: {
247
+ msg: rawText,
248
+ },
249
+ type: "POST",
250
+ url: "/get",
251
+ }).done(function(data) {
252
+ // --- Typewriter effect for bot response START ---
253
+ var lines = data.split("**");
254
+ var fullText = lines.join(' ');
255
+ // Create the container for the bot message
256
+ var botContainer = $('<div class="d-flex justify-content-start mb-4"></div>');
257
+ var imgDiv = $('<div class="img_cont_msg"><img src="/static/juitlogo.png" class="rounded-circle user_img_msg"></div>');
258
+ var msgDiv = $('<div class="msg_container"></div>');
259
+ botContainer.append(imgDiv);
260
+ botContainer.append(msgDiv);
261
+ $("#messageFormeight").append(botContainer);
262
+
263
+ var i = 0;
264
+ function typeWriter() {
265
+ if (i < fullText.length) {
266
+ msgDiv.append(fullText.charAt(i));
267
+ i++;
268
+ setTimeout(typeWriter, 30); // Adjust delay as needed
269
+ } else {
270
+ msgDiv.append('<span class="msg_time">' + str_time + '</span>');
271
+ // Smooth scroll to the bottom of the chat
272
+ var container = $("#messageFormeight");
273
+ container.animate({ scrollTop: container.prop("scrollHeight") }, 500);
274
  }
275
+ }
276
+ typeWriter();
277
+ // --- Typewriter effect for bot response END ---
278
  });
279
+
280
+ event.preventDefault(); // Prevent form submission
281
  });
282
+ });
283
+ </script>
284
+
285
+ <script>
286
+ document.addEventListener("DOMContentLoaded", () => {
287
+ const themeToggle = document.getElementById("themeToggle");
288
+ const body = document.body;
289
 
290
+ // Check local storage for a saved theme, default to dark
291
+ const savedTheme = localStorage.getItem("theme") || "dark";
292
+ if (savedTheme === "light") {
293
+ body.classList.add("light-mode");
294
+ body.classList.remove("dark-mode");
295
+ } else {
296
+ body.classList.add("dark-mode");
297
+ body.classList.remove("light-mode");
298
+ }
299
+
300
+ updateButton(); // Update button text on load
301
+
302
+ // Toggle theme on button click
303
+ themeToggle.addEventListener("click", () => {
304
+ if (body.classList.contains("light-mode")) {
305
+ // Switch to dark mode
306
+ body.classList.remove("light-mode");
307
+ body.classList.add("dark-mode");
308
+ localStorage.setItem("theme", "dark");
309
+ } else {
310
+ // Switch to light mode
311
+ body.classList.remove("dark-mode");
312
+ body.classList.add("light-mode");
313
+ localStorage.setItem("theme", "light");
314
+ }
315
+ updateButton();
316
+ });
317
+
318
+ function updateButton() {
319
+ themeToggle.textContent = body.classList.contains("light-mode")
320
+ ? "Switch to Dark Mode"
321
+ : "Switch to Light Mode";
322
+ }
323
+ });
324
+
325
+ function showPredictiveText(predictions) {
326
+ if (predictions.length > 0 && $("#text").val().trim() !== "") {
327
+ let html = predictions.map(p => `<p>${p}</p>`).join('');
328
+ $(".predictive-text").html(html).show();
329
+ } else {
330
+ $(".predictive-text").hide();
331
+ }
332
+ }
333
+ function scrollToBottom() {
334
+ $("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
335
+ }
336
+
337
+ $(document).ready(function() {
338
+ $("#clearChat").click(function() {
339
+ $("#messageFormeight").html(""); // Clears the chat area
340
+ localStorage.removeItem("chatHistory"); // Clears stored history
341
  });
342
  });
343
+
344
+
345
+ </script>
346
 
347
 
348
  <script>