-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·130 lines (116 loc) · 3.54 KB
/
app.js
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
const timelineDiv = document.querySelector("#timeline");
const messagesDiv = document.querySelector("#messages-inner");
const textInput = document.querySelector("#chatbox");
var currentSpeaker = "right";
var scrollCount;
const scrollToBottom = ()=>{
timelineDiv.scrollTop = timelineDiv.scrollHeight
scrollCount--;
if(scrollCount > 0) setTimeout(scrollToBottom, 10);
};
const switchSpeaker = (elem)=>{
if (currentSpeaker == "left") {
currentSpeaker = "right";
if (elem && elem.classList && elem.classList.contains("typing")) {
elem.classList.remove("messageleft");
elem.classList.remove("messageblue");
elem.classList.add("messageright");
elem.classList.add("messagegreen");
}
} else {
currentSpeaker = "left";
if (elem && elem.classList && elem.classList.contains("typing")) {
elem.classList.remove("messageright");
elem.classList.remove("messagegreen");
elem.classList.add("messageleft");
elem.classList.add("messageblue");
}
}
textInput.focus();
}
const addTyping = ()=>{
let lastChild = messagesDiv.lastChild;
if(lastChild.classList != undefined && lastChild.classList.contains("typing")) return;
let newMessageDiv = makeMessageBox();
newMessageDiv.classList.add("typing");
makeDots(newMessageDiv);
newMessageDiv.addEventListener("mouseup", (event)=>{
switchSpeaker(newMessageDiv);
});
newMessageDiv.addEventListener("touchend", (event)=>{
textInput.focus();
});
messagesDiv.appendChild(newMessageDiv);
}
const removeTyping = ()=>{
let lastChild = messagesDiv.lastChild;
if ((lastChild.classList != undefined && lastChild.classList.contains("typing"))) {
lastChild.remove();
}
}
const makeMessageBox = ()=>{
let newMessageDiv = document.createElement("div");
newMessageDiv.classList.add("messagebox");
if (currentSpeaker == "left") {
newMessageDiv.classList.add("messageleft");
newMessageDiv.classList.add("messageblue");
} else {
newMessageDiv.classList.add("messageright");
newMessageDiv.classList.add("messagegreen");
}
scrollToBottom();
return newMessageDiv;
}
const makeDots = (element)=>{
let newDot = document.createElement("div");
newDot.classList.add("dot");
newDot.classList.add("dot-1");
element.appendChild(newDot);
newDot = document.createElement("div");
newDot.classList.add("dot");
newDot.classList.add("dot-2");
element.appendChild(newDot);
newDot = document.createElement("div");
newDot.classList.add("dot");
newDot.classList.add("dot-3");
element.appendChild(newDot);
}
const sendMessage = ()=>{
if (textInput.value) {
let message = textInput.value;
textInput.value = "";
let lastChild = messagesDiv.lastChild;
let newMessageDiv = undefined;
if (lastChild.classList != undefined && lastChild.classList.contains("typing")) {
newMessageDiv = lastChild;
newMessageDiv.classList.remove("typing");
while (newMessageDiv.firstChild) {
newMessageDiv.removeChild(newMessageDiv.lastChild);
}
} else {
newMessageDiv = makeMessageBox();
messagesDiv.appendChild(newMessageDiv);
}
let newMessageText = document.createTextNode(message);
newMessageDiv.appendChild(newMessageText);
scrollCount = 40;
switchSpeaker();
textInput.focus();
}
};
textInput.addEventListener("keyup", (event)=>{
if (event.keyCode === 13) {
event.preventDefault();
sendMessage();
}
});
textInput.addEventListener("input", (event)=>{
if(textInput.value) {
addTyping();
} else {
removeTyping();
}
});
window.onload = function() {
textInput.focus();
}