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