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