1
|
|
|
var flag = 0; |
|
|
|
|
2
|
|
|
var pre = ""; |
3
|
|
|
|
4
|
|
|
// Websocket Connection Open |
5
|
|
|
var conn = new WebSocket("ws://localhost:8080"); |
|
|
|
|
6
|
|
|
conn.onopen = function() { |
7
|
|
|
// console.log("Connection established!"); |
8
|
|
|
init(); |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
// On Message |
12
|
|
|
conn.onmessage = function(e) { |
13
|
|
|
var msg = JSON.parse(e.data); |
14
|
|
|
console.log(msg); |
|
|
|
|
15
|
|
|
if (!width()) { |
16
|
|
|
SideBar(msg.sidebar); |
17
|
|
|
} else { |
18
|
|
|
if (document.getElementById("conversation").style.display == "none") { |
19
|
|
|
SideBar(msg.sidebar); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (msg.initial !== undefined) { |
24
|
|
|
SideBar(msg.initial); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (msg.conversation !== undefined) { |
28
|
|
|
updateConversation(msg.conversation); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (msg.reply !== undefined) { |
32
|
|
|
var textAreaId = $("#text_reply").attr("name"); |
33
|
|
|
if (width()) { |
34
|
|
|
textAreaId = $(".text_icon #text_reply").attr("name"); |
35
|
|
|
} |
36
|
|
|
if (msg.reply[0].id === textAreaId) { |
37
|
|
|
updateConversation(msg.reply); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (msg.Search !== undefined) { |
42
|
|
|
searchResult(msg.Search); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (msg.Compose !== undefined) { |
46
|
|
|
composeResult(msg.Compose); |
47
|
|
|
} |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
// For First time |
51
|
|
|
function init() { |
52
|
|
|
conn.send("OpenChat initiated..!"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// For updating Sidebar |
56
|
|
|
function SideBar(msg) { |
57
|
|
|
mobile("sidebar"); |
58
|
|
|
// Getting Div |
59
|
|
|
if (msg != null) { |
60
|
|
|
createSidebarElement(msg); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// SideBar Load Request |
65
|
|
|
function sidebarRequest() { |
66
|
|
|
conn.send("Load Sidebar"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Update Current Conversation |
70
|
|
|
function updateConversation(data) { |
71
|
|
|
|
72
|
|
|
if (!width()) { |
73
|
|
|
sidebarRequest(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
var ele = document.getElementById("conversation"); |
77
|
|
|
ele.innerHTML = ""; |
78
|
|
|
|
79
|
|
|
if (data[0].type === 1) { |
80
|
|
|
// For showing previous message |
81
|
|
|
if (data[0].load > 10) { |
82
|
|
|
var aElement = $("<a></a>").text("Show Previous Message!"); |
83
|
|
|
var divElement = $("<div></div>").append(aElement); |
84
|
|
|
$("#conversation").append(divElement); |
85
|
|
|
$("#conversation div").addClass("previous"); |
86
|
|
|
$("#conversation div a").attr({ |
87
|
|
|
"onclick": "previous(this)", |
88
|
|
|
"id": data[0].username, |
89
|
|
|
"name": data[0].load |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
for (var i = data.length - 1; i >= 1; i--) { |
94
|
|
|
// create element |
95
|
|
|
var divElement = document.createElement("div"); |
|
|
|
|
96
|
|
|
if (data[i]["sent_by"] !== data[i].start) { |
97
|
|
|
divElement.setAttribute("class", "receiver"); |
98
|
|
|
} else { |
99
|
|
|
divElement.setAttribute("class", "sender"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
ele.appendChild(divElement); |
103
|
|
|
var brElement = document.createElement("br"); |
104
|
|
|
brElement.setAttribute("style", "clear:both;"); |
105
|
|
|
ele.appendChild(brElement); |
106
|
|
|
|
107
|
|
|
var pElement = document.createElement("p"); |
108
|
|
|
var pText = document.createTextNode(data[i].message); |
109
|
|
|
pElement.appendChild(pText); |
110
|
|
|
divElement.appendChild(pElement); |
111
|
|
|
|
112
|
|
|
var h6Element = document.createElement("h6"); |
113
|
|
|
var h6Text = document.createTextNode(data[i].time); |
114
|
|
|
h6Element.appendChild(h6Text); |
115
|
|
|
h6Element.setAttribute("class", "message_time"); |
116
|
|
|
pElement.appendChild(h6Element); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
setConversationDetails(data[0]); |
120
|
|
|
|
121
|
|
|
ele.scrollTop = ele.scrollHeight; |
122
|
|
|
} else { |
123
|
|
|
setConversationDetails(data[0]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
function setConversationDetails(details) { |
128
|
|
|
$("#chat_heading a").remove("a"); |
129
|
|
|
var aElement = $("<a></a>").text(details.name); |
130
|
|
|
$("#chat_heading").append(aElement); |
131
|
|
|
$("#chat_heading a").attr({ |
132
|
|
|
"href": "http://localhost/openchat/account.php/" + details.username |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
if (details.login_status === "1") { |
136
|
|
|
var online = document.createElement("p"); |
137
|
|
|
online.setAttribute("class", "online"); |
138
|
|
|
$("#chat_heading a").append(online); |
139
|
|
|
$("#chat_heading a p").css({ |
140
|
|
|
"float": "right" |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (width()) { |
145
|
|
|
$(".text_icon #text_reply").attr({ |
146
|
|
|
"name": details.id |
147
|
|
|
}); |
148
|
|
|
} else { |
149
|
|
|
$("#text_reply").attr({ |
150
|
|
|
"name": details.id |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Creating new Conversation or Loading Conversation |
156
|
|
|
function newConversation(element, load) { |
157
|
|
|
mobile("main"); |
158
|
|
|
$("#compose_selection").css("visibility", "hidden"); |
159
|
|
|
flag = 0; |
160
|
|
|
$("#compose_name").val(""); |
161
|
|
|
$("#search_item").val(""); |
162
|
|
|
$("#compose_text").hide(); |
163
|
|
|
|
164
|
|
|
var msg = { |
165
|
|
|
"username": element.id, |
166
|
|
|
"load": load, |
167
|
|
|
"newConversation": "Initiated" |
168
|
|
|
}; |
169
|
|
|
conn.send(JSON.stringify(msg)); |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// For reply to other messages |
174
|
|
|
function reply() { |
175
|
|
|
var replyElement = ""; |
176
|
|
|
if (width()) { |
177
|
|
|
replyElement = ".text_icon #text_reply"; |
178
|
|
|
} else { |
179
|
|
|
replyElement = "#text_reply"; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
var message = [$(replyElement).val()]; |
183
|
|
|
var id = $(replyElement).attr("name"); |
184
|
|
|
$(replyElement).val(""); |
185
|
|
|
// console.log(message); |
186
|
|
|
var q = { |
187
|
|
|
"name": id, |
188
|
|
|
"reply": message |
189
|
|
|
}; |
190
|
|
|
conn.send(JSON.stringify(q)); |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Compose new and direct message to anyone |
195
|
|
|
function compose() { |
196
|
|
|
mobile("compose"); |
197
|
|
|
flag = 1; |
198
|
|
|
$("#chat_heading a").remove("a"); |
199
|
|
|
document.getElementById("conversation").innerHTML = ""; |
200
|
|
|
$("#compose_text").show(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
function composeChoose() { |
204
|
|
|
var text = document.getElementById("compose_name").value; |
205
|
|
|
if (text !== "") { |
206
|
|
|
var msg = { |
207
|
|
|
"value": text, |
208
|
|
|
"Compose": "Compose" |
209
|
|
|
}; |
210
|
|
|
conn.send(JSON.stringify(msg)); |
211
|
|
|
} else { |
212
|
|
|
$("#compose_selection").css("visibility", "hidden"); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
//compose messages |
217
|
|
|
function composeResult(arr) { |
218
|
|
|
var ele = document.getElementById("suggestion"); |
219
|
|
|
ele.innerHTML = ""; |
220
|
|
|
|
221
|
|
|
if (arr !== "Not Found") { |
222
|
|
|
for (var i = arr.length - 1; i >= 0; i--) { |
223
|
|
|
var liElement = document.createElement("li"); |
224
|
|
|
var aElement = document.createElement("a"); |
225
|
|
|
var aText = document.createTextNode(arr[i].name); |
226
|
|
|
aElement.appendChild(aText); |
227
|
|
|
aElement.setAttribute("href", "#"); |
228
|
|
|
aElement.setAttribute("onclick", "newConversation(this,10)"); |
229
|
|
|
aElement.setAttribute("class", "suggestion"); |
230
|
|
|
aElement.setAttribute("id", arr[i].username); |
231
|
|
|
liElement.appendChild(aElement); |
232
|
|
|
ele.appendChild(liElement); |
233
|
|
|
} |
234
|
|
|
} else { |
235
|
|
|
var aElement = $("<a></a>").text("Not Found"); |
|
|
|
|
236
|
|
|
var liElement = $("<li></li>").append(aElement); |
|
|
|
|
237
|
|
|
$("#suggestion").append(liElement); |
238
|
|
|
|
239
|
|
|
$("#suggestion li a").attr({ |
240
|
|
|
"onclick": "myFunction()" |
241
|
|
|
}); |
242
|
|
|
} |
243
|
|
|
$("#compose_selection").css("visibility", "visible"); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function search_choose() { |
247
|
|
|
var text = $("#search_item").val(); |
248
|
|
|
if (text !== "") { |
249
|
|
|
var msg = { |
250
|
|
|
"value": text, |
251
|
|
|
"search": "search" |
252
|
|
|
}; |
253
|
|
|
|
254
|
|
|
conn.send(JSON.stringify(msg)); |
255
|
|
|
} else { |
256
|
|
|
conn.send("Load Sidebar"); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
function searchResult(arr) { |
262
|
|
|
if (arr !== "Not Found") { |
263
|
|
|
createSidebarElement(arr); |
264
|
|
|
} else { |
265
|
|
|
$("#message").text(""); |
266
|
|
|
var aElement = $("<a></a>").text("Not Found"); |
267
|
|
|
$("#message").append(aElement); |
268
|
|
|
$("#message a").addClass("message"); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
function createSidebarElement(data) { |
274
|
|
|
// organising content according to time |
275
|
|
|
var ele = document.getElementById('message'); |
276
|
|
|
ele.innerHTML = ""; |
277
|
|
|
var condition = data.length; |
278
|
|
|
for (var i = 0; i < condition; i++) { |
279
|
|
|
var aElement = document.createElement("a"); //creating element a |
280
|
|
|
var aText = document.createTextNode(data[i].name); |
281
|
|
|
aElement.appendChild(aText); |
282
|
|
|
aElement.setAttribute("id", data[i].username); |
283
|
|
|
aElement.setAttribute("href", "message.php#" + data[i].username); |
284
|
|
|
aElement.setAttribute("class", "message"); |
285
|
|
|
aElement.setAttribute("onclick", "newConversation(this,10)"); |
286
|
|
|
ele.appendChild(aElement); |
287
|
|
|
|
288
|
|
|
// creating element span for showing time |
289
|
|
|
var spanElement = document.createElement("span"); |
290
|
|
|
var spanText = document.createTextNode(data[i].time); |
291
|
|
|
spanElement.appendChild(spanText); |
292
|
|
|
spanElement.setAttribute("class", "message_time"); |
293
|
|
|
aElement.appendChild(spanElement); |
294
|
|
|
|
295
|
|
|
if (data[i].login_status === "1") { |
296
|
|
|
var online = document.createElement("div"); |
297
|
|
|
online.setAttribute("class", "online"); |
298
|
|
|
aElement.appendChild(online); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
window.ondblclick = myFunction; |
304
|
|
|
|
305
|
|
|
function myFunction() // Hidden compose message input |
306
|
|
|
{ |
307
|
|
|
$("#compose_selection").css("visibility", "hidden"); |
308
|
|
|
init(); |
309
|
|
|
flag = 0; |
310
|
|
|
$("#compose_name").val(""); |
311
|
|
|
$("#search_item").val(""); |
312
|
|
|
$("#compose_text").hide(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
function previous(element) // Load previous messages |
316
|
|
|
{ |
317
|
|
|
mobile("previous"); |
318
|
|
|
var user = element.id; |
|
|
|
|
319
|
|
|
var lo = element.name; |
320
|
|
|
newConversation(element, lo); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
function mobile(ele) { |
324
|
|
|
if (width()) { |
325
|
|
|
mob_hide(); |
326
|
|
|
if (ele == "main") { |
327
|
|
|
$(".sidebar").hide(); |
328
|
|
|
$(".mob-reply").show(); |
329
|
|
|
$(".chat_name").show(); |
330
|
|
|
$(".chat_name #chat_heading").show(); |
331
|
|
|
if (pre == "") { |
332
|
|
|
$(".main div").remove("div"); |
333
|
|
|
$(".main br").remove("br"); |
334
|
|
|
$(".chat_name #chat_heading a").remove("a"); |
335
|
|
|
} |
336
|
|
|
$(".main").show(); |
337
|
|
|
} |
338
|
|
|
if (ele == "compose") { |
339
|
|
|
$(".chat_name").show(); |
340
|
|
|
$(".chat_name .compose_text").show(); |
341
|
|
|
$(".sidebar").hide(); |
342
|
|
|
$("#compose_selection").show(); |
343
|
|
|
} |
344
|
|
|
if (ele == "sidebar") { |
345
|
|
|
$(".sidebar").show(); |
346
|
|
|
} |
347
|
|
|
if (ele == "previous") { |
348
|
|
|
pre = "1"; |
349
|
|
|
} else { |
350
|
|
|
pre = ""; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
function show_search() { |
356
|
|
|
// console.log("HE0"); |
357
|
|
|
mob_hide(); |
358
|
|
|
$(".search_message").show(); |
359
|
|
|
$(".sidebar").show(); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
function mob_hide() { |
363
|
|
|
$(".search_message").hide(); |
364
|
|
|
$(".sidebar").hide(); |
365
|
|
|
$(".main").hide(); |
366
|
|
|
$(".chat_name").hide(); |
367
|
|
|
$(".mob-reply").hide(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
function width() { |
371
|
|
|
if (window.innerWidth < 500) { |
372
|
|
|
return true; |
373
|
|
|
} |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// Audio Recognization |
378
|
|
|
|
379
|
|
|
function startDictation() { |
380
|
|
|
|
381
|
|
|
if (window.hasOwnProperty("webkitSpeechRecognition")) { |
382
|
|
|
|
383
|
|
|
var recognition = new webkitSpeechRecognition(); |
|
|
|
|
384
|
|
|
|
385
|
|
|
recognition.continuous = false; |
386
|
|
|
recognition.interimResults = false; |
387
|
|
|
|
388
|
|
|
recognition.lang = "en-IN"; |
389
|
|
|
recognition.start(); |
390
|
|
|
|
391
|
|
|
recognition.onresult = function(e) { |
392
|
|
|
document.getElementById("text_reply").value = e.results[0][0].transcript; |
393
|
|
|
recognition.stop(); |
394
|
|
|
reply(); |
395
|
|
|
}; |
396
|
|
|
|
397
|
|
|
recognition.onerror = function() { |
398
|
|
|
recognition.stop(); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
console.log("Hello, Contact me at [email protected]"); |
|
|
|
|