ikraamkb commited on
Commit
68d2211
·
verified ·
1 Parent(s): 186a4d5

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +26 -98
static/application.js CHANGED
@@ -1,77 +1,3 @@
1
- /*document.addEventListener("DOMContentLoaded", () => {
2
- const convo = document.querySelector(".convo");
3
- const sendBtn = document.querySelector(".sendingQA");
4
- const questionInput = document.querySelector(".qt");
5
- const fileInput = document.querySelector("#file-upload");
6
- const imageInput = document.querySelector("#image-upload");
7
-
8
- // Handle text or file submission
9
- function sendQuestion() {
10
- const question = questionInput.value.trim();
11
- if (!question && !fileInput.files.length && !imageInput.files.length) return;
12
-
13
- const userBubble = document.createElement("div");
14
- userBubble.className = "bubble right";
15
- userBubble.textContent = question || "Sent a file/image";
16
- convo.appendChild(userBubble);
17
-
18
- // Preview file name (if any)
19
- const file = fileInput.files[0];
20
- if (file) {
21
- const filePreview = document.createElement("div");
22
- filePreview.className = "file-preview-bubble bubble right";
23
- filePreview.textContent = `Selected: ${file.name}`;
24
- convo.appendChild(filePreview);
25
- }
26
-
27
- // Preview image name (if any)
28
- const image = imageInput.files[0];
29
- if (image) {
30
- const imagePreview = document.createElement("div");
31
- imagePreview.className = "file-preview-bubble bubble right";
32
- imagePreview.textContent = `Selected: ${image.name}`;
33
- convo.appendChild(imagePreview);
34
- }
35
-
36
- convo.scrollTop = convo.scrollHeight;
37
- questionInput.value = "";
38
-
39
- // TODO: Send data to backend here
40
- }
41
-
42
- // Handle send button click
43
- sendBtn.addEventListener("click", sendQuestion);
44
-
45
- // Handle Enter key in input
46
- questionInput.addEventListener("keydown", (e) => {
47
- if (e.key === "Enter") {
48
- e.preventDefault();
49
- sendQuestion();
50
- }
51
- });
52
-
53
- // Toggle audio mute/unmute
54
- convo.addEventListener("click", (e) => {
55
- if (e.target.classList.contains("audio-toggle")) {
56
- const audioIcon = e.target;
57
- const answerBlock = audioIcon.closest(".bubble");
58
- const audioElement = answerBlock.querySelector("audio");
59
-
60
- if (audioElement) {
61
- if (audioElement.muted) {
62
- audioElement.muted = false;
63
- audioIcon.classList.replace("fa-volume-xmark", "fa-volume-high");
64
- audioIcon.title = "Click to mute";
65
- } else {
66
- audioElement.muted = true;
67
- audioIcon.classList.replace("fa-volume-high", "fa-volume-xmark");
68
- audioIcon.title = "Click to unmute";
69
- }
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");
@@ -116,46 +42,48 @@ function createMessageBubble(text, sender = "You", audioSrc = null) {
116
  label.className = "label";
117
  label.innerText = sender;
118
 
119
- const message = document.createElement("div");
120
- message.className = "text";
121
- message.innerText = text;
 
 
 
122
 
123
- bubble.appendChild(label);
124
- bubble.appendChild(message);
125
 
126
- if (audioSrc && sender === "Chris") {
127
- const audio = document.createElement("audio");
128
- audio.src = audioSrc;
129
- audio.autoplay = true;
130
- audio.controls = true;
131
 
 
 
132
  const icon = document.createElement("i");
133
  icon.className = "fa-solid fa-volume-high audio-toggle";
134
  icon.style.cursor = "pointer";
 
135
  icon.title = "Click to mute";
136
- icon.style.marginLeft = "10px";
 
 
 
137
 
138
  icon.addEventListener("click", () => {
139
- audio.muted = !audio.muted;
140
- icon.classList.toggle("fa-volume-high", !audio.muted);
141
- icon.classList.toggle("fa-volume-xmark", audio.muted);
142
- icon.title = audio.muted ? "Click to unmute" : "Click to mute";
 
143
  });
144
 
145
- const audioWrap = document.createElement("div");
146
- audioWrap.style.display = "flex";
147
- audioWrap.style.alignItems = "center";
148
- audioWrap.style.gap = "8px";
149
- audioWrap.appendChild(icon);
150
- audioWrap.appendChild(audio);
151
-
152
- bubble.appendChild(audioWrap);
153
  }
154
 
 
 
 
155
  convo.appendChild(bubble);
156
  convo.scrollTop = convo.scrollHeight;
157
 
158
- return bubble; // ✅ Return the bubble so you can modify it later
159
  }
160
 
161
  sendBtn.addEventListener("click", async () => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  document.addEventListener("DOMContentLoaded", () => {
2
  const convo = document.querySelector(".convo");
3
  const input = document.querySelector(".qt input");
 
42
  label.className = "label";
43
  label.innerText = sender;
44
 
45
+ const messageRow = document.createElement("div");
46
+ messageRow.className = "text";
47
+ messageRow.style.display = "flex";
48
+ messageRow.style.justifyContent = "space-between";
49
+ messageRow.style.alignItems = "center";
50
+ messageRow.style.gap = "8px";
51
 
52
+ const messageText = document.createElement("span");
53
+ messageText.innerText = text;
54
 
55
+ messageRow.appendChild(messageText);
 
 
 
 
56
 
57
+ // Only add icon for Chris's answer
58
+ if (sender === "Chris" && audioSrc) {
59
  const icon = document.createElement("i");
60
  icon.className = "fa-solid fa-volume-high audio-toggle";
61
  icon.style.cursor = "pointer";
62
+ icon.style.fontSize = "18px";
63
  icon.title = "Click to mute";
64
+
65
+ let isMuted = false;
66
+ let audio = new Audio(audioSrc);
67
+ audio.play();
68
 
69
  icon.addEventListener("click", () => {
70
+ isMuted = !isMuted;
71
+ audio.muted = isMuted;
72
+ icon.classList.toggle("fa-volume-high", !isMuted);
73
+ icon.classList.toggle("fa-volume-xmark", isMuted);
74
+ icon.title = isMuted ? "Click to unmute" : "Click to mute";
75
  });
76
 
77
+ messageRow.appendChild(icon);
 
 
 
 
 
 
 
78
  }
79
 
80
+ bubble.appendChild(label);
81
+ bubble.appendChild(messageRow);
82
+
83
  convo.appendChild(bubble);
84
  convo.scrollTop = convo.scrollHeight;
85
 
86
+ return bubble;
87
  }
88
 
89
  sendBtn.addEventListener("click", async () => {