-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
220 lines (191 loc) · 5.7 KB
/
script.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
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
"use strict";
const sendBtn = document.getElementById("send");
const inputTxt = document.getElementById("msg-input");
const newMessage = document.getElementById("msg-box");
const typingMsgEl = document.getElementById("typing-animation");
const miniBtnEl = document.getElementById("minmalizeBtn");
const closeBtnEl = document.getElementById("closeBtn");
const middleSec = document.getElementById("middle-section");
const lastSec = document.getElementById("last-section");
const firstSec = document.getElementById("first-section");
const botName = document.getElementById("name");
const botAvatar = document.getElementById("avatar");
const btnLeft = document.getElementById("left");
const btnRight = document.getElementById("right");
const firstBot = document.getElementById("0");
const secondBot = document.getElementById("1");
const thirdBot = document.getElementById("2");
let botIndex = 0;
let i = 0;
const data = [
{
name: "Kazik Murowski",
avatar: "url(img/av1.jpg)",
chatavatar: "img/av1.jpg",
responsesArr: [
"Cześć! Co słychać?",
`U mnie ok, robię kurs JS. A Ty?`,
"Brzmi spoko! Co robisz w weekend?",
"Jestem na uczelni",
"Meuszę lecieć, trzymaj się!",
],
},
{
name: "Ojosław Ojutkowski",
avatar: "url(img/Ojutek.jpg)",
chatavatar: "img/Ojutek.jpg",
responsesArr: [
"Cześć! jak sie masz?",
`Mam się super! Ja sobie śpie a TY?`,
"Fajowo! a kiedy przyjdziesz?",
"hurra! to ja czekam!",
"hm hm hm hmmmmmm ♫ ♪ ♫",
],
},
];
//TIMESTAMP
const date = new Date();
console.log(date.toTimeString().slice(0, 5));
//SOUNDS
function playBeep() {
const beep = new Audio("beep.mp3");
beep.play();
beep.volume = 0.1;
}
function playPop() {
const pop = new Audio("pop.mp3");
pop.play();
pop.volume = 0.3;
}
function playType() {
const typeing = new Audio("type.mp3");
typeing.play();
typeing.volume = 0.3;
}
// INSERT OWN MSG
const inserMsg = function () {
// date
const date = new Date();
const curDate = date.toTimeString().slice(0, 5);
// print new msg
newMessage.insertAdjacentHTML(
"beforeend",
`<div class="single-message-container flex-row">
<div class="message-flex flex-column showmsg">
<div class="chat__timestamp">Sent ${curDate}</div>
<div class="chat__message">${inputTxt.value}</div>
</div>
</div>`
);
playPop();
// clear input
inputTxt.value = "";
// scroll to bottom
newMessage.scrollTop = newMessage.scrollHeight;
};
//INSERT REPLIES
const insertReply = function () {
const date = new Date();
const curDate = date.toTimeString().slice(0, 5);
//print "typing animation"
newMessage.insertAdjacentHTML(
"beforeend",
`
<div id="typing-animation" class="single-message-container flex-row--opposite showmsg">
<img class="chat__avatar" src=${data[botIndex].chatavatar} />
<div class="message-flex flex-column">
<div id="typing-wave" class="chat__message opposite typing-wave"><span>.</span><span>.</span><span>.</span>
</div>
</div>
</div>`
);
playType();
newMessage.scrollTop = newMessage.scrollHeight;
setTimeout(() => {
document.getElementById("typing-animation").remove();
}, 2900);
setTimeout(() => {
newMessage.insertAdjacentHTML(
"beforeend",
`
<div class="single-message-container flex-row--opposite showmsg" >
<img class="chat__avatar" src=${data[botIndex].chatavatar} />
<div class="message-flex flex-column">
<div class="chat__timestamp">Sent ${curDate}</div>
<div class="chat__message opposite">
${data[botIndex].responsesArr[i]}
</div>
</div>
</div>`
);
playBeep();
responsesIncrement();
}, 3000);
};
//////////////////// CLICK TO SEND /////////////////////////
// sending messeges function
const sendMsg = sendBtn.addEventListener("click", () => {
if (inputTxt.value == "") {
return;
}
inserMsg();
setTimeout(() => {
insertReply();
}, 1000);
});
//////////////////// ENTER TO SEND /////////////////////////
const sendMsgEnter = inputTxt.addEventListener("keydown", (e) => {
if (e.code === "Enter") {
if (inputTxt.value == "") {
return;
}
inserMsg();
setTimeout(() => {
insertReply();
}, 1000);
}
});
// minimalize
miniBtnEl.addEventListener("click", () => {
middleSec.classList.toggle("hide");
lastSec.classList.toggle("hide");
});
const responsesIncrement = function () {
data[0].responsesArr[i++];
if (i > data[botIndex].responsesArr.length - 1) i = 0;
newMessage.scrollTop = newMessage.scrollHeight;
};
const updatePersonals = function () {
// const botChatAvatar = document.getElementById(".chat__avatar");
// botChatAvatar.style.backgroundImage = data[botIndex].chatavatar;
botName.innerHTML = data[botIndex].name;
botAvatar.style.backgroundImage = data[botIndex].avatar;
newMessage.innerHTML = "";
console.log((botName.innerHTML = data[botIndex].name));
i = 0;
};
/////////// FOR NEXT UPDATES
// btnRight.addEventListener("click", () => {
// data[botIndex++];
// if (botIndex > data.length - 1) botIndex = 0;
// updatePersonals();
// });
// btnLeft.addEventListener("click", () => {
// data[botIndex++];
// if (botIndex > data.length - 1) botIndex = 0;
// updatePersonals();
// });
firstBot.addEventListener("click", () => {
data[(botIndex = 0)];
updatePersonals();
firstBot.classList.add("selected-bot");
if (secondBot.classList.contains("selected-bot"))
secondBot.classList.remove("selected-bot");
});
secondBot.addEventListener("click", () => {
data[(botIndex = 1)];
updatePersonals();
secondBot.classList.add("selected-bot");
if (firstBot.classList.contains("selected-bot"))
firstBot.classList.remove("selected-bot");
});