-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
313 lines (257 loc) · 8.12 KB
/
script.js
File metadata and controls
313 lines (257 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"use strict";
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import {
getDatabase,
ref,
push,
onValue,
remove,
update,
} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
const appSettings = {
databaseURL:
"https://chitpit-prsnl-default-rtdb.asia-southeast1.firebasedatabase.app/",
};
//defining const variables
const app = initializeApp(appSettings);
const database = getDatabase(app);
const chatsInDb = ref(database, "chat");
const onlineStatusInDb = ref(database, "chat/onlineStatus");
const typingStatusInDb = ref(database, "chat/typingStatus");
const main = document.querySelector(".main");
const container = document.querySelector(".container");
const heBtn = document.querySelector("#he-btn");
const sheBtn = document.querySelector("#she-btn");
const sendBtn = document.querySelector(".send-btn");
const inputField = document.querySelector(".input-field");
const chatArea = document.querySelector(".chat-area");
const welcomeScreen = document.querySelector(".welcome");
const onlineStatusDot = document.querySelector(".onlineStatusDot");
const screenWidth = window.screen.width;
// Check if the browser is Chrome
const isChrome =
/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
// Check if the app is running in standalone mode (PWAs)
const isStandalone = window.matchMedia("(display-mode: standalone)").matches;
if (isStandalone) {
main.style.height = "100%";
}
let currentUser = undefined;
let antiUser = undefined;
let typingTimer;
function loadData() {
onValue(chatsInDb, function (snapshot) {
if (snapshot.exists()) {
let chatsArray = Object.entries(snapshot.val());
let onlineStatusObject;
if (chatsArray.length > 2) {
onlineStatusObject = chatsArray[chatsArray.length - 2][1];
} else {
onlineStatusObject = chatsArray[0][1];
}
let currentUserOnlineStatus = onlineStatusObject[currentUser];
let antiUserOnlineStatus = onlineStatusObject[antiUser];
let typingStatusObject = chatsArray[chatsArray.length - 1][1];
let currentUserTypingStatus = typingStatusObject[currentUser];
let antiUserTypingStatus = typingStatusObject[antiUser];
if (chatsArray.length > 1 && antiUserTypingStatus) {
onlineStatusDot.classList.add("typing");
} else {
onlineStatusDot.classList.remove("typing");
}
if (antiUserOnlineStatus) {
onlineStatusDot.style.color = "rgb(0,230,118)";
} else {
onlineStatusDot.style.color = "rgba(0, 0, 0, 0.5)";
}
chatArea.innerHTML = "";
// load only limited messages..
const chatLimit = 100;
if (chatsArray.length > chatLimit) {
for (
let i = chatsArray.length - 3;
i >= chatsArray.length - chatLimit;
i--
) {
createChat(chatsArray, i);
}
} else {
for (let i = chatsArray.length - 3; i >= 0; i--) {
createChat(chatsArray, i);
}
}
scrollToBottom();
// read receipts update
// push seen msg to database if the receiver(currentUser) is active or not
if (currentUserOnlineStatus) {
for (let i = 0; i < chatsArray.length - 2; i++) {
let chatUser = chatsArray[i][1].user;
if (currentUser != chatUser) {
let chatId = chatsArray[i][0];
let chatLocationInDb = ref(database, `chat/${chatId}`);
update(chatLocationInDb, {
seen: true,
});
}
}
}
} else {
console.log("snapshot doesn't exists");
}
});
if (screenWidth > 768) {
inputField.focus();
}
}
function createChat(chatData, index) {
let chatId = chatData[index][0];
let chatUser = chatData[index][1].user;
let chatText = chatData[index][1].text;
let chatDateandTime = chatData[index][1].dateAndTime;
let chatTime = chatDateandTime.split(" ")[4];
let seenStatus = chatData[index][1].seen;
let message_box = document.createElement("div");
message_box.classList.add("message-box");
chatArea.append(message_box);
let message = document.createElement("div");
message.classList.add("message", `${chatUser}`);
message_box.append(message);
let text = document.createElement("div");
text.classList.add("text");
text.innerText = `${chatText}`;
message.append(text);
let spaceSpan = document.createElement("span");
spaceSpan.classList.add("space");
spaceSpan.innerHTML = `          `;
text.append(spaceSpan);
let timeSpan = document.createElement("span");
timeSpan.classList.add("time");
timeSpan.innerHTML = `${chatTime.split(":").slice(0, 2).join(":")} `;
text.append(timeSpan);
let seenTick = document.createElement("i");
seenTick.classList.add("fa-solid", "fa-check-double", "read-receipt");
if (currentUser == chatUser) {
timeSpan.append(seenTick);
message.style.justifyContent = "flex-end";
}
// update msg seen status
if (seenStatus) {
seenTick.style.color = "rgba(0, 195, 255, 0.6)";
}
}
function scrollToBottom() {
chatArea.scrollTop = chatArea.scrollHeight;
}
function toggleActiveStatus() {
// Check if the Page Visibility API is supported
if (typeof document.hidden !== "undefined") {
// Browser supports Page Visibility API
// Handle page visibility change
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === "visible") {
// Page is visible, user is active
update(onlineStatusInDb, {
[currentUser]: true,
});
} else {
// Page is hidden, user is inactive
update(onlineStatusInDb, {
[currentUser]: false,
});
}
});
} else {
// Browser does not support Page Visibility API
// Fallback to focus and blur events
// Handle focus event
window.addEventListener("focus", function () {
// User is active
update(onlineStatusInDb, {
[currentUser]: true,
});
});
// Handle blur event
window.addEventListener("blur", function () {
// User is inactive
update(onlineStatusInDb, {
[currentUser]: false,
});
});
}
}
function sendMsg() {
let inputMsg = inputField.value.trim();
if (inputMsg.length > 0) {
inputField.value = "";
push(chatsInDb, {
user: `${currentUser}`,
text: `${inputMsg}`,
dateAndTime: `${new Date()}`,
seen: false,
});
}
// to prevent input from losing focus after sending a msg
inputField.focus();
sendBtn.classList.remove("active");
}
heBtn.addEventListener("click", () => {
currentUser = "he";
antiUser = "she";
welcomeScreen.classList.remove("active");
container.classList.add("active");
update(onlineStatusInDb, {
[currentUser]: true,
});
loadData();
// check status
toggleActiveStatus();
});
sheBtn.addEventListener("click", () => {
currentUser = "she";
antiUser = "he";
welcomeScreen.classList.remove("active");
container.classList.add("active");
update(onlineStatusInDb, {
[currentUser]: true,
});
loadData();
// check status
toggleActiveStatus();
});
sendBtn.addEventListener("click", sendMsg);
inputField.addEventListener("input", () => {
// to set the typing status
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
// Perform action when the user stops typing
update(typingStatusInDb, {
[currentUser]: false,
});
}, 500);
// Perform action while the user is typing (optional)
update(typingStatusInDb, {
[currentUser]: true,
});
// to enable or disable send button
if (inputField.value.trim().length > 0) {
sendBtn.classList.add("active");
} else {
sendBtn.classList.remove("active");
}
});
inputField.addEventListener("keyup", (e) => {
if (!e.shiftKey && e.key == "Enter" && screenWidth > 500) {
sendMsg();
e.preventDefault();
}
});
inputField.addEventListener("keydown", (e) => {
if (e.shiftKey && e.key == "Enter" && screenWidth > 500) {
inputField.value += "\n";
inputField.scrollTop = inputField.scrollHeight;
e.preventDefault();
}
if (e.key == "Enter" && screenWidth > 500) {
e.preventDefault();
}
});