-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatHandler.js
130 lines (112 loc) · 3.9 KB
/
ChatHandler.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
// update list see...
export { ChatHandler, chat_names}
const chat_names = ["Sachin Tiwari", "Aditya Lodhi ", "Sanorita", "Rakshit", "Pooja Di", "Balli", "Mummy"];
const chat_names_length = chat_names.length;
const chat_msg = ["How are you doing bro ...",
"Perfect, I am really glad to hear that!...",
"This is what I understand you're telling me..",
"I’m sorry, I don’t have the info on that.."];
const chat_msg_length = chat_msg.length;
const chat_img_length = 7;
class ChatHandler{
constructor(chat_template, chat_list){
this.hashmap = new Map();
this.linked_list = null;// doubly linked list.
this.chat_template = chat_template;
this.chat_list = chat_list;
let clock = new Date();
this.hours = clock.getHours();
this.mins = clock.getMinutes();
}
getTime(){
// Time Stamp creation for messages
this.mins += 1;
if(this.mins === 60){
this.hours += 1;
this.mins = 0;
}
if(this.hours === 24){
this.hours = 0;
}
return ("0" + this.hours).slice(-2)+":"+("0" + this.mins).slice(-2);
}
createNode(id){
// Creating node element
let node = {};
// Pointers to prev and next
node['next'] = null;
node['prev'] = null;
// Create a copy of chat template
let chat_item = this.chat_template.cloneNode(true);
// Setting name, message, image to template item
chat_item.querySelector('#Name').innerText = chat_names[id%chat_names_length];
chat_item.querySelector('#Message').innerText = chat_msg[id%chat_msg_length];
console.log("./images/avatar" + eval(1+(id%chat_img_length)) + ".png");
chat_item.querySelector('#Image').src = "./images/avatar" + eval(1+(id%chat_img_length)) + ".png";
node['chat_item'] = chat_item;
return node;
}
newMsg(id){
let node = null;
if((id in this.hashmap ) === false){
// If node not in linked list
node = this.createNode(id);
this.hashmap[id] = node;
} else{
// If node in linked list
node = this.getNodeFromList(id);
}
if(this.linked_list === null){
// Setting head of empty list
this.linked_list = node;
} else{
// Adding node to head of linked list
node['next'] = this.linked_list;
if(this.linked_list!==null)
this.linked_list['prev'] = node;
this.linked_list = node;
}
this.updateList();
}
deleteMsg(id){
let node = this.getNodeFromList(id);
// No use of node since it has been deleted
delete this.hashmap[id];
// Clear entry from hashmap
this.updateList();
}
getNodeFromList(id){
let node = this.hashmap[id];
let prevNode = node['prev'];
let nextNode = node['next'];
// Update prev and next node pointers
if(prevNode!==null)
prevNode['next'] = nextNode;
if(nextNode!==null)
nextNode['prev'] = prevNode;
// Update head of the linked list
if(node===this.linked_list){
this.linked_list = nextNode;
}
node['next'] = null;
node['prev'] = null;
return node;
}
updateList(){
// Update the contents of the chat list
let innerHTML = '';
let head = this.linked_list;
while(head!==null){
let element = head['chat_item'];
if(head===this.linked_list){
element.className = "ks-item ks-active";
element.querySelector('#Time').innerText = this.getTime();
} else{
element.className = "ks-item";
}
innerHTML += element.outerHTML;
head = head['next'];
}
this.chat_list.innerHTML = innerHTML;
}
}