-
Notifications
You must be signed in to change notification settings - Fork 0
/
design.js
563 lines (516 loc) · 14.4 KB
/
design.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//Global variable
//List of friends
var friends = [
"Dawid Dawcikowski",
"Ewu Dawcikowska",
"Stefka",
"Lola",
"Buka Bukiszon",
"Jebułka",
"Evulins"
];
//List of objects containg firends and my conversations with them
var conversations = {
list: [
{
friend: {
name: 'Dawid',
surname: 'Dawcikowski',
avatar: 'dawid.jpg'
},
messages: [
{
text: 'Dawcik, how are you?',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'I am fine. Just chillin ',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Hejka',
sentAt: 'Tue Feb 04 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
},
{
friend: {
name: 'Stefka',
surname: 'Stefka',
avatar: 'stefka.jpg'
},
messages: [
{
text: 'Miau, where is my food???',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'I dont know :(',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'I have to write really long text, to see if the line will hide',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Lola',
surname: 'Lola',
avatar: 'lola.jpg'
},
messages: [
{
text: 'Hi, where is Stefka? I want to play',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'She is eating her cucumber',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: true,
delivered: true
}
},
{
friend: {
name: 'Cats',
surname: '',
avatar: 'koty.jpg'
},
messages: [
{
text: 'Hi, what time we will meet tonight?',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'at 9pm',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Buka',
surname: 'Bukiszon',
avatar: 'buka.jpg'
},
messages: [
{
text: 'Woof! Woof!',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'Come on girl, lets play',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Jebułka',
surname: 'Bułka',
avatar: 'jebulka.jpg'
},
messages: [
{
text: 'purr, purr',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'Come on kitty, kitty',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Evulins',
surname: 'Sar',
avatar: 'ewu.jpg'
},
messages: [
{
text: 'Dawcik. Dlaczego śmierdzisz kupą?',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'bo nie myłem tyłka od środy',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Mon Jan 29 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Ewu',
surname: 'Dawcikowska',
avatar: 'awatar.jpg'
},
messages: [
{
text: 'Dawcik. Dlaczego masz takie duże oczy?',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'Żeby Cię lepiej widzieć!',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
},
{
friend: {
name: 'Ewu',
surname: 'Dawcikowska',
avatar: 'awatar.jpg'
},
messages: [
{
text: 'Test, textMessage',
sentAt: 'Tue Feb 06 2018 13:02:21 GMT+0100 (CET)',
sentByMe: true,
seen: false,
delivered: false
},
{
text: 'Reply',
sentAt: 'Tue Feb 05 2018 13:02:21 GMT+0100 (CET)',
sentByMe: false,
seen: false,
delivered: false
}
],
lastMessage: {
text: 'Muszę napsiać bardzo długi teskt, żeby sprawdzić, czy się zawija lub skraca.',
sentAt: 'Tue Jan 30 2018 21:39:57 GMT+0100 (CET)',
seen: false,
delivered: true
}
}
],
//Gets all conversations
getAll: function() {
return conversations.list;
},
//Adds message
addMessage: function(friend, message) {
var conversation = conversations.getConversationWith(friend);
var oldMessage = {
text: conversation.lastMessage.text,
sentAt: conversation.lastMessage.sentAt,
sentByMe: conversation.lastMessage.sentByMe,
seen: conversation.lastMessage.seen,
delivered: conversation.lastMessage.delivered
}
conversation.messages.push(oldMessage);
conversation.lastMessage.text = message;
conversation.lastMessage.sentAt = new Date().toString();
conversation.lastMessage.sentByMe = true;
conversation.lastMessage.seen = false;
conversation.lastMessage.delivered = true;
},
//Gets conversations with selected person based on her name and surname
getConversationWith: function(full_name) {
var result = conversations.list.filter(function(element) {
return element.friend.name + " " + element.friend.surname === full_name;
});
return result[0];
},
//Searches all conversation by phrase
searchConversations: function(phrase) {
var results = conversations.list.filter(function(element) {
var name = element.friend.name.toUpperCase();
var surname = element.friend.surname.toUpperCase();
var searchContent = phrase.toUpperCase();
return name.indexOf(searchContent) > -1 || surname.indexOf(searchContent) > -1;
});
return results;
}
};
//Based on the data from conversations object, shows all elements on the list
function showConversations(conversationList) {
for (var i = 0; i < conversationList.length; i++) {
var current = conversationList[i];
var wrappedDate = moment(new Date(current.lastMessage.sentAt));
var conversation = `
<li id="conversation">
<div class="avatar">
<img src="avatars/${current.friend.avatar}">
</div>
<div class="textMessage">
<p class="name"><span>${current.friend.name} ${current.friend.surname}</span></p>
<p class="text"><span>${current.lastMessage.text}</span></p>
</div>
<div class="messageDetails">
<p class="date"><span>${wrappedDate.fromNow()}</span></p>
${getIcon(current.lastMessage)}
</div>
</li>
`;
// Adds all conversations to list #messagesList
$("#messagesList").append(conversation);
//Clicking on the icon "x", closes the chat window. After that main messenger window appears.
$(".topBar #close").click(function(event){
event.preventDefault();
$(".chat").hide();
$(".messengerApp").show();
});
}
}
//Shows conversation with selected person
function showChat(conversation) {
var allMessages = conversation.messages.concat([conversation.lastMessage]);
//Puts messages in date order
allMessages.sort(function(a, b) {
var dateA = new Date(a.sentAt);
var dateB = new Date(b.sentAt);
return dateA - dateB;
});
for (var i = 0; i < allMessages.length; i++) {
var current = allMessages[i];
var wrappedDate = moment(new Date(current.sentAt));
var messageContent;
if (current.text.length > 0) {
messageContent = current.text;
} else {
messageContent = '<div class="fa fa-thumbs-o-up like"></div>';
}
// My Message
var myMessage = `
<div class="me">
<ul>
<p class="date">${wrappedDate.fromNow()}</p>
<li class="myMessage"><span>${messageContent}</span></li>
</ul>
</div>
`;
// Message from friend
var reply = `
<div class="reply">
<p class="date">${wrappedDate.fromNow()}</p>
<div class="avatar">
<img src="avatars/${conversation.friend.avatar}">
</div>
<ul>
<li class="reMessage"><span>${messageContent}</span></li>
</ul>
</div>
`;
$(".chatMessages").append(current.sentByMe ? myMessage : reply);
}
//Scrolls to the lates sent message
$(".chatMessages").scroll();
$(".chatMessages").animate({
scrollTop: 5220
}, 10440);
}
//Clears chat window from previous messages to the other person
function clearChat() {
$(".chatMessages").empty();
}
//Clears list messages
function clearConversations() {
$("ul#messagesList").empty();
}
//Changes name in topbar and shows the full name of person with whom I chat
function changeName(full_name) {
$(".chat .topBar a p.name").text(full_name);
}
//Shows conversations in chat windowe with the person selected from the conversation list
function registerClickConversation() {
$("li#conversation").click(function(event) {
event.preventDefault();
$(".messengerApp").hide();
$(".chat").show();
clearChat();
var person = $(this).find(".textMessage .name span").text();
changeName(person);
var conversation = conversations.getConversationWith(person);
showChat(conversation);
});
}
//Shows different icons based on information if the message has been seen and delivered
function getIcon(message) {
if (message.delivered && message.seen) {
return '<span class="fa fa-check-circle seen"></span>'
} else if (message.delivered && !message.seen) {
return '<span class="fa fa-circle seen"></span>'
} else {
return '<span class="fa fa-circle-o seen"></span>'
}
}
//Activate new message icon and shows empty chat windowe
$(".writeMessage").click(function(event) {
event.preventDefault();
$(".messengerApp").hide();
$(".chatWindow").show();
});
//Closes chat window and show main messanger view
$(".top .tools").click(function(event) {
event.preventDefault();
$(".chatWindow").hide();
$(".messengerApp").show();
});
// This function is activating every time when page is reloding
(function() {
console.log("test")
showConversations(conversations.getAll());
//After selecting person and typing message, sends new message.
$(".newForm").submit(function(event) {
event.preventDefault();
var newMessage = $(".newForm .bottom input").val();
var person = $("select").val();
var minLength = 1;
//If new message has less than one charakter, alert is showing
if (newMessage.length < minLength) {
alert("Your message must have at least " + minLength + " character");
return;
}
//Adds new message to conversation with selected person
conversations.addMessage(person, newMessage);
//Hide window with new message and show chat window with selected person
$(".chatWindow").hide();
$(".chat").show();
clearChat();
var conversation = conversations.getConversationWith(person);
showChat(conversation);
changeName(person);
$(".newForm")[0].reset();
});
//Sends like button and adds it to the new conversation
$(".newLikeButton").click(function(event) {
event.preventDefault();
var newMessage = $(".newLikeButton").val();
var person = $("select").val();
conversations.addMessage(person, newMessage);
$(".chatWindow").hide();
$(".chat").show();
clearChat();
var conversation = conversations.getConversationWith(person);
showChat(conversation);
changeName(person);
});
registerClickConversation();
//Sends message and adds it to the conversation
$(".chatForm").submit(function(event) {
event.preventDefault();
var newMessage = $(".chatForm .bottom input").val();
var person = $(".chat").find(".topBar .name").text();
var minLength = 1;
if (newMessage.length < minLength) {
alert("Your message must have at least " + minLength + " character");
return;
}
clearChat();
conversations.addMessage(person, newMessage);
var conversation = conversations.getConversationWith(person);
showChat(conversation);
$(".chatForm")[0].reset();
});
//Sends like button and adds it to the existing conversation
$(".likeButton").click(function(event) {
event.preventDefault();
clearChat();
var newMessage = "";
var person = $(".chat").find(".topBar .name").text();
conversations.addMessage(person, newMessage);
var conversation = conversations.getConversationWith(person);
showChat(conversation);
});
//Search conversation be phrase and shows the result
$("#searchForm").submit(function(event) {
event.preventDefault();
var input = $("#searchForm input").val();
var result = conversations.searchConversations(input);
clearConversations();
showConversations(result);
registerClickConversation();
});
})();