ikraamkb commited on
Commit
0764bc3
ยท
verified ยท
1 Parent(s): 4bca93a

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +54 -70
static/application.js CHANGED
@@ -7,78 +7,65 @@ document.addEventListener("DOMContentLoaded", () => {
7
 
8
  let selectedFile = null;
9
  let filePreviewBubble = null;
10
- // Hidden file inputs
11
- const fileUpload = document.querySelector(".fa-file");
12
- const imageUpload = document.querySelector(".fa-image");
13
-
14
- // Create separate input elements for documents and images
15
- const docInput = document.createElement("input");
16
- docInput.type = "file";
17
- docInput.accept = ".pdf,.docx,.pptx,.xlsx";
18
- docInput.style.display = "none";
19
- document.body.appendChild(docInput);
20
-
21
- const imgInput = document.createElement("input");
22
- imgInput.type = "file";
23
- imgInput.accept = "image/*";
24
- imgInput.style.display = "none";
25
- document.body.appendChild(imgInput);
26
-
27
- fileBtn.addEventListener("click", () => docInput.click());
28
- imageBtn.addEventListener("click", () => imgInput.click());
29
-
30
- // Document input handler
31
- docInput.addEventListener("change", (e) => {
32
- if (e.target.files.length > 0) {
33
- const file = e.target.files[0];
 
 
 
 
 
 
 
34
  const validDocTypes = [
35
  'application/pdf',
36
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
37
  'application/vnd.openxmlformats-officedocument.presentationml.presentation',
38
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
39
  ];
40
-
41
- if (validDocTypes.includes(file.type)) {
42
- selectedFile = file;
43
- if (filePreviewBubble) filePreviewBubble.remove();
44
-
45
- filePreviewBubble = document.createElement("div");
46
- filePreviewBubble.className = "file-preview-bubble bubble right";
47
- filePreviewBubble.textContent = `๐Ÿ“Ž Selected: ${file.name}`;
48
- convo.appendChild(filePreviewBubble);
49
- convo.scrollTop = convo.scrollHeight;
50
- } else {
51
- alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
52
- docInput.value = ''; // Reset the input
53
- }
54
- }
55
- });
56
-
57
- // Image input handler
58
- imgInput.addEventListener("change", (e) => {
59
- if (e.target.files.length > 0) {
60
- const file = e.target.files[0];
61
  const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
62
-
63
- if (validImageTypes.includes(file.type)) {
64
- selectedFile = file;
65
- if (filePreviewBubble) filePreviewBubble.remove();
66
-
67
- filePreviewBubble = document.createElement("div");
68
- filePreviewBubble.className = "file-preview-bubble bubble right";
69
- filePreviewBubble.textContent = `๐Ÿ–ผ๏ธ Selected: ${file.name}`;
70
- convo.appendChild(filePreviewBubble);
71
- convo.scrollTop = convo.scrollHeight;
72
- } else {
73
- alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
74
- imgInput.value = ''; // Reset the input
75
  }
 
 
 
 
 
 
 
 
 
 
76
  }
77
- });
78
 
79
- // Remove the old event listeners since we're using the new inputs
80
- fileUpload.removeEventListener('change');
81
- imageUpload.removeEventListener('change');
82
  function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
83
  const bubble = document.createElement("div");
84
  bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
@@ -155,10 +142,10 @@ imageUpload.removeEventListener('change');
155
  filePreviewBubble = null;
156
  }
157
 
158
- // User message with file info
159
  createMessageBubble(question, "You", null, selectedFile.name);
160
 
161
- // Aidan is thinking...
162
  const thinkingBubble = createMessageBubble("Wait, Let me think ๐Ÿค”...", "Aidan");
163
 
164
  const formData = new FormData();
@@ -175,7 +162,7 @@ imageUpload.removeEventListener('change');
175
  const answerText = result.answer || "No response.";
176
  const audioSrc = result.audio || null;
177
 
178
- // Replace "Let me think..." with actual answer
179
  const message = thinkingBubble.querySelector(".text");
180
  message.innerText = answerText;
181
 
@@ -215,19 +202,16 @@ imageUpload.removeEventListener('change');
215
  message.innerText = "โš ๏ธ Aidan had trouble responding.";
216
  }
217
 
218
- // Clear inputs after processing is complete
219
  input.value = "";
220
  selectedFile = null;
221
  }
222
 
223
- // Click event for send button
224
  sendBtn.addEventListener("click", sendMessage);
225
 
226
- // Enter key event for input field
227
  input.addEventListener("keydown", (event) => {
228
  if (event.key === "Enter") {
229
- event.preventDefault(); // Prevent form submission
230
  sendMessage();
231
  }
232
  });
233
- });
 
7
 
8
  let selectedFile = null;
9
  let filePreviewBubble = null;
10
+
11
+ // Create hidden file inputs for documents and images
12
+ const fileUpload = document.createElement("input");
13
+ fileUpload.type = "file";
14
+ fileUpload.accept = ".pdf,.docx,.pptx,.xlsx";
15
+ fileUpload.style.display = "none";
16
+ document.body.appendChild(fileUpload);
17
+
18
+ const imageUpload = document.createElement("input");
19
+ imageUpload.type = "file";
20
+ imageUpload.accept = "image/*";
21
+ imageUpload.style.display = "none";
22
+ document.body.appendChild(imageUpload);
23
+
24
+ // Click on hidden inputs when buttons are clicked
25
+ fileBtn.addEventListener("click", () => fileUpload.click());
26
+ imageBtn.addEventListener("click", () => imageUpload.click());
27
+
28
+ // Handle document file selection
29
+ fileUpload.addEventListener("change", (e) => {
30
+ handleFileSelection(e.target.files[0], "document");
31
+ });
32
+
33
+ // Handle image file selection
34
+ imageUpload.addEventListener("change", (e) => {
35
+ handleFileSelection(e.target.files[0], "image");
36
+ });
37
+
38
+ function handleFileSelection(file, type) {
39
+ if (!file) return;
40
+
41
  const validDocTypes = [
42
  'application/pdf',
43
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
44
  'application/vnd.openxmlformats-officedocument.presentationml.presentation',
45
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
46
  ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
48
+
49
+ const isValid = type === "document" ? validDocTypes.includes(file.type) : validImageTypes.includes(file.type);
50
+
51
+ if (!isValid) {
52
+ alert(`Please select a valid ${type === "document" ? "document (PDF, DOCX, PPTX, XLSX)" : "image (JPEG, PNG, GIF, WEBP)"}`);
53
+ if (type === "document") fileUpload.value = '';
54
+ else imageUpload.value = '';
55
+ return;
 
 
 
 
 
56
  }
57
+
58
+ selectedFile = file;
59
+
60
+ if (filePreviewBubble) filePreviewBubble.remove();
61
+
62
+ filePreviewBubble = document.createElement("div");
63
+ filePreviewBubble.className = "file-preview-bubble bubble right";
64
+ filePreviewBubble.textContent = `${type === "document" ? "๐Ÿ“Ž" : "๐Ÿ–ผ๏ธ"} Selected: ${file.name}`;
65
+ convo.appendChild(filePreviewBubble);
66
+ convo.scrollTop = convo.scrollHeight;
67
  }
 
68
 
 
 
 
69
  function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
70
  const bubble = document.createElement("div");
71
  bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
 
142
  filePreviewBubble = null;
143
  }
144
 
145
+ // Create user's message bubble
146
  createMessageBubble(question, "You", null, selectedFile.name);
147
 
148
+ // Thinking bubble
149
  const thinkingBubble = createMessageBubble("Wait, Let me think ๐Ÿค”...", "Aidan");
150
 
151
  const formData = new FormData();
 
162
  const answerText = result.answer || "No response.";
163
  const audioSrc = result.audio || null;
164
 
165
+ // Update the thinking bubble
166
  const message = thinkingBubble.querySelector(".text");
167
  message.innerText = answerText;
168
 
 
202
  message.innerText = "โš ๏ธ Aidan had trouble responding.";
203
  }
204
 
 
205
  input.value = "";
206
  selectedFile = null;
207
  }
208
 
 
209
  sendBtn.addEventListener("click", sendMessage);
210
 
 
211
  input.addEventListener("keydown", (event) => {
212
  if (event.key === "Enter") {
213
+ event.preventDefault();
214
  sendMessage();
215
  }
216
  });
217
+ });