ikraamkb commited on
Commit
f504a20
ยท
verified ยท
1 Parent(s): b097a6d

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +63 -48
static/application.js CHANGED
@@ -7,20 +7,38 @@ document.addEventListener("DOMContentLoaded", () => {
7
 
8
  let selectedFile = null;
9
  let filePreviewBubble = null;
10
-
11
- // Hidden file input
12
- const fileInput = document.createElement("input");
13
- fileInput.type = "file";
14
- fileInput.accept = ".pdf,.docx,.pptx,.xlsx,image/*";
15
- fileInput.style.display = "none";
16
- document.body.appendChild(fileInput);
17
-
18
- fileBtn.addEventListener("click", () => fileUpload.click());
19
- imageBtn.addEventListener("click", () => imageUpload.click());
20
-
21
- fileInput.addEventListener("change", () => {
22
- const file = fileInput.files[0];
23
- if (file) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  selectedFile = file;
25
  if (filePreviewBubble) filePreviewBubble.remove();
26
 
@@ -29,41 +47,38 @@ document.addEventListener("DOMContentLoaded", () => {
29
  filePreviewBubble.textContent = `๐Ÿ“Ž Selected: ${file.name}`;
30
  convo.appendChild(filePreviewBubble);
31
  convo.scrollTop = convo.scrollHeight;
 
 
 
32
  }
33
- });
34
- fileUpload.addEventListener('change', (e) => {
35
- if (e.target.files.length > 0) {
36
- const file = e.target.files[0];
37
- const validDocTypes = [
38
- 'application/pdf',
39
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
40
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
41
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
42
- ];
43
-
44
- if (validDocTypes.includes(file.type)) {
45
- console.log('Valid document selected:', file.name);
46
- // Handle the document file here
47
- } else {
48
- alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
49
- fileUpload.value = ''; // Reset the input
50
- }
51
- }
52
- });
53
- imageUpload.addEventListener('change', (e) => {
54
- if (e.target.files.length > 0) {
55
- const file = e.target.files[0];
56
- const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
57
-
58
- if (validImageTypes.includes(file.type)) {
59
- console.log('Valid image selected:', file.name);
60
- // Handle the image file here
61
- } else {
62
- alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
63
- imageUpload.value = ''; // Reset the input
64
- }
65
- }
66
- });
67
  function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
68
  const bubble = document.createElement("div");
69
  bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
 
7
 
8
  let selectedFile = null;
9
  let filePreviewBubble = null;
10
+ // Hidden file inputs
11
+ const fileUpload = document.getElementById('file-upload');
12
+ const imageUpload = document.getElementById('image-upload');
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
 
 
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"}`;