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