ikraamkb commited on
Commit
96cbdd5
·
verified ·
1 Parent(s): b061157

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +80 -1
static/application.js CHANGED
@@ -1,4 +1,4 @@
1
- document.addEventListener("DOMContentLoaded", () => {
2
  const convo = document.querySelector(".convo");
3
  const sendBtn = document.querySelector(".sendingQA");
4
  const questionInput = document.querySelector(".qt");
@@ -70,4 +70,83 @@ document.addEventListener("DOMContentLoaded", () => {
70
  }
71
  }
72
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  });
 
1
+ /*document.addEventListener("DOMContentLoaded", () => {
2
  const convo = document.querySelector(".convo");
3
  const sendBtn = document.querySelector(".sendingQA");
4
  const questionInput = document.querySelector(".qt");
 
70
  }
71
  }
72
  });
73
+ }); */
74
+ // Wait until the DOM is fully loaded
75
+ document.addEventListener("DOMContentLoaded", () => {
76
+ const convo = document.querySelector(".convo");
77
+ const input = document.querySelector(".qt input");
78
+ const sendBtn = document.querySelector(".sendingQA");
79
+ const fileBtn = document.querySelector(".fa-file");
80
+ const imageBtn = document.querySelector(".fa-image");
81
+
82
+ // Create file input dynamically (not visible)
83
+ const fileInput = document.createElement("input");
84
+ fileInput.type = "file";
85
+ fileInput.style.display = "none";
86
+ document.body.appendChild(fileInput);
87
+
88
+ fileBtn.addEventListener("click", () => fileInput.click());
89
+
90
+ fileInput.addEventListener("change", () => {
91
+ const file = fileInput.files[0];
92
+ if (file) {
93
+ const fileBubble = document.createElement("div");
94
+ fileBubble.className = "file-preview-bubble bubble right";
95
+ fileBubble.textContent = `Selected: ${file.name}`;
96
+ convo.appendChild(fileBubble);
97
+ convo.scrollTop = convo.scrollHeight;
98
+ }
99
+ });
100
+
101
+ function createMessageBubble(message, isUser = true, audioSrc = null) {
102
+ const bubble = document.createElement("div");
103
+ bubble.className = `bubble ${isUser ? "right" : "left"}`;
104
+ bubble.innerHTML = `<span>${message}</span>`;
105
+
106
+ if (audioSrc && !isUser) {
107
+ const audio = document.createElement("audio");
108
+ audio.src = audioSrc;
109
+ audio.autoplay = true;
110
+ audio.muted = false;
111
+
112
+ const audioIcon = document.createElement("i");
113
+ audioIcon.className = "fa-solid fa-volume-high audio-toggle";
114
+ audioIcon.style.marginLeft = "10px";
115
+ audioIcon.title = "Click to mute";
116
+
117
+ bubble.appendChild(audio);
118
+ bubble.appendChild(audioIcon);
119
+
120
+ // Add toggle logic
121
+ audioIcon.addEventListener("click", () => {
122
+ audio.muted = !audio.muted;
123
+ if (audio.muted) {
124
+ audioIcon.classList.replace("fa-volume-high", "fa-volume-xmark");
125
+ audioIcon.title = "Click to unmute";
126
+ } else {
127
+ audioIcon.classList.replace("fa-volume-xmark", "fa-volume-high");
128
+ audioIcon.title = "Click to mute";
129
+ }
130
+ });
131
+ }
132
+
133
+ convo.appendChild(bubble);
134
+ convo.scrollTop = convo.scrollHeight;
135
+ }
136
+
137
+ sendBtn.addEventListener("click", () => {
138
+ const question = input.value?.trim();
139
+ if (!question) return;
140
+
141
+ // Add user's question to convo
142
+ createMessageBubble(question, true);
143
+ input.value = "";
144
+
145
+ // Simulate a bot answer for now
146
+ setTimeout(() => {
147
+ const answerText = "Here's a simulated answer from Chris!";
148
+ const audioPath = "/static/audio/response.mp3"; // Replace this with dynamic path if needed
149
+ createMessageBubble(answerText, false, audioPath);
150
+ }, 1000);
151
+ });
152
  });