Completed
Push — master ( 963a90...63e2f0 )
by Ankit
03:19
created

message.js ➔ search_choose   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 1
b 0
f 1
1
var flag = 0;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
var pre = "";
3
4
// Websocket Connection Open
5
var conn = new WebSocket("ws://localhost:8080");
0 ignored issues
show
Bug introduced by
The variable WebSocket seems to be never declared. If this is a global, consider adding a /** global: WebSocket */ comment.

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.

Loading history...
6
conn.onopen = function() {
7
  console.log("Connection established!");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
8
  init();
9
};
10
11
// On Message
12
conn.onmessage = function(e) {
13
  var msg = JSON.parse(e.data);
14
  console.log(msg);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
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
    {
83
      var divE1 = $("<div></div>").addClass("row message-previous");
84
      var divE2 = $("<div></div>").addClass("col-sm-12 previous");
85
      var aElement = $("<a></a>").text("Show Previous Message!");
86
      aElement.attr({
87
        "id": data[0].username,
88
        "name": data[0].load
89
      });
90
      divE2.append(aElement);
91
      divE1.append(divE2);
92
      $("#conversation").append(divE1);
93
    }
94
95
    for (var i = data.length - 1; i >= 1; i--) {
96
      // create element
97
      var divElement1 = $("<div></div>").addClass("row message-body");
98
      var divElement2 = $("<div></div>").addClass("col-sm-12");
99
      var divElement3 = $("<div></div>");
100
      var messageText = $("<div></div>").addClass("message-text").text(data[i].message);
101
      var spanElement = $("<span></span>").addClass("message-time pull-right").text(data[i].time);
102
103
      if (data[i]["sent_by"] !== data[i].start)
104
      {
105
       divElement2.addClass("message-main-receiver");
106
       divElement3.addClass("receiver");
107
      }
108
      else
109
      {
110
        divElement2.addClass("message-main-sender");
111
       divElement3.addClass("sender");
112
      }
113
      divElement3.append(messageText);
114
      divElement3.append(spanElement);
115
      divElement2.append(divElement3);
116
      divElement1.append(divElement2);
117
      $("#conversation").append(divElement1);
118
119
120
      // ele.appendChild(divElement);
121
      // var brElement = document.createElement("br");
122
      // brElement.setAttribute("style", "clear:both;");
123
      // ele.appendChild(brElement);
124
125
      // var pElement = document.createElement("p");
126
      // var pText = document.createTextNode(data[i].message);
127
      // pElement.appendChild(pText);
128
      // divElement.appendChild(pElement);
129
130
      // var h6Element = document.createElement("h6");
131
      // var h6Text = document.createTextNode(data[i].time);
132
      // h6Element.appendChild(h6Text);
133
      // h6Element.setAttribute("class", "message_time");
134
      // pElement.appendChild(h6Element);
135
    }
136
137
    setConversationDetails(data[0]);
138
139
    ele.scrollTop = ele.scrollHeight;
140
  } else {
141
    setConversationDetails(data[0]);
142
  }
143
}
144
145
function setConversationDetails(details)
146
{
147
  $(".heading-name-meta").text(details.name);
148
  $(".heading-name-meta").attr({
149
    "href": "http://localhost/openchat/account.php/" + details.username
150
  });
151
  $(".heading-online").removeClass("show");
152
  if (details.login_status === "1") {
153
    $(".heading-online").addClass("show");
154
  }
155
156
  $("#text_reply").attr({
157
    "name": details.id
158
  });
159
}
160
161
// Creating new Conversation or Loading Conversation
162
function newConversation(element, load) {
163
  mobile("main");
164
  $("#compose_selection").css("visibility", "hidden");
165
  flag = 0;
166
  $("#compose_name").val("");
167
  $("#search_item").val("");
168
  $("#compose_text").hide();
169
170
  var msg = {
171
    "username": element.id,
172
    "load": load,
173
    "newConversation": "Initiated"
174
  };
175
  conn.send(JSON.stringify(msg));
176
177
}
178
179
// For reply to other messages
180
function reply() {
181
  var replyElement = "";
182
  if (width()) {
183
    replyElement = ".text_icon #text_reply";
184
  } else {
185
    replyElement = "#text_reply";
186
  }
187
188
  var message = [$(replyElement).val()];
189
  var id = $(replyElement).attr("name");
190
  $(replyElement).val("");
191
  // console.log(message);
192
  var q = {
193
    "name": id,
194
    "reply": message
195
  };
196
  conn.send(JSON.stringify(q));
197
198
}
199
200
// Compose new and direct message to anyone
201
function compose() {
202
  mobile("compose");
203
  flag = 1;
204
  $("#chat_heading a").remove("a");
205
  document.getElementById("conversation").innerHTML = "";
206
  $("#compose_text").show();
207
}
208
209
function composeChoose() {
210
  var text = document.getElementById("compose_name").value;
211
  if (text !== "") {
212
    var msg = {
213
      "value": text,
214
      "Compose": "Compose"
215
    };
216
    conn.send(JSON.stringify(msg));
217
  } else {
218
    $("#compose_selection").css("visibility", "hidden");
219
  }
220
}
221
222
//compose messages
223
function composeResult(arr) {
224
  var ele = document.getElementById("suggestion");
225
  ele.innerHTML = "";
226
227
  if (arr !== "Not Found") {
228
    for (var i = arr.length - 1; i >= 0; i--) {
229
      var liElement = document.createElement("li");
230
      var aElement = document.createElement("a");
231
      var aText = document.createTextNode(arr[i].name);
232
      aElement.appendChild(aText);
233
      aElement.setAttribute("href", "#");
234
      aElement.setAttribute("onclick", "newConversation(this,10)");
235
      aElement.setAttribute("class", "suggestion");
236
      aElement.setAttribute("id", arr[i].username);
237
      liElement.appendChild(aElement);
238
      ele.appendChild(liElement);
239
    }
240
  } else {
241
    var aElement = $("<a></a>").text("Not Found");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aElement already seems to be declared on line 230. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
242
    var liElement = $("<li></li>").append(aElement);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable liElement already seems to be declared on line 229. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
243
    $("#suggestion").append(liElement);
244
245
    $("#suggestion li a").attr({
246
      "onclick": "myFunction()"
247
    });
248
  }
249
  $("#compose_selection").css("visibility", "visible");
250
}
251
252
function search_choose() {
253
  var text = $("#search_item").val();
254
  if (text !== "") {
255
    var msg = {
256
      "value": text,
257
      "search": "search"
258
    };
259
260
    conn.send(JSON.stringify(msg));
261
  } else {
262
    conn.send("Load Sidebar");
263
  }
264
265
}
266
267
function searchResult(arr) {
268
  if (arr !== "Not Found") {
269
    createSidebarElement(arr);
270
  } else {
271
    $("#message").text("");
272
    var aElement = $("<a></a>").text("Not Found");
273
    $("#message").append(aElement);
274
    $("#message a").addClass("message");
275
  }
276
277
}
278
279
function createSidebarElement(data) {
280
  // organising content according to time
281
  var ele = document.getElementById('message');
282
  ele.innerHTML = "";
283
  var condition = data.length;
284
  for (var i = 0; i < condition; i++)
285
  {
286
287
    var div1 = $("<div></div>").addClass("row sideBar-body");
288
289
    div1.attr({
290
      "id" : data[i].username
291
    });
292
293
    var div2 = $("<div></div>").addClass("col-sm-3 col-xs-3 sideBar-avatar");
294
    var div3 = $("<div></div>").addClass("avatar-icon");
295
    var imgElement = $("<img>").attr({
296
      "src": "../public/assests/img/bg.png"
297
    });
298
    div3.append(imgElement);
299
    div2.append(div3);
300
    div1.append(div2);
301
302
    div2 = $("<div></div>").addClass("col-sm-9 col-xs-9 sideBar-main");
303
    div3 = $("<div></div>").addClass("row");
304
    var div4 = $("<div></div>").addClass("col-sm-8 col-xs-8 sideBar-name");
305
    var spanE = $("<span></span>").addClass("name-meta").text(data[i].name);
306
    div4.append(spanE);
307
    div3.append(div4);
308
309
    div4 = $("<div></div>").addClass("col-sm-4 col-xs-4 pull-right sideBar-time");
310
    spanE = $("<span></span>").addClass("time-meta pull-right").text(data[i].time);
311
     div4.append(spanE);
312
    div3.append(div4);
313
    div2.append(div3);
314
315
    div1.append(div2);
316
    $("#message").append(div1);
317
  }
318
}
319
320
function myFunction() // Hidden compose message input
321
{
322
  $("#compose_selection").css("visibility", "hidden");
323
  init();
324
  flag = 0;
325
  $("#compose_name").val("");
326
  $("#search_item").val("");
327
  $("#compose_text").hide();
328
}
329
330
function previous(element) // Load previous messages
331
{
332
  mobile("previous");
333
  var user = element.id;
0 ignored issues
show
Unused Code introduced by
The variable user seems to be never used. Consider removing it.
Loading history...
334
  var lo = element.name;
335
  newConversation(element, lo);
336
}
337
338
function mobile(ele) {
339
  if (width()) {
340
    mob_hide();
341
    if (ele == "main") {
342
      $(".sidebar").hide();
343
      $(".mob-reply").show();
344
      $(".chat_name").show();
345
      $(".chat_name #chat_heading").show();
346
      if (pre == "") {
347
        $(".main div").remove("div");
348
        $(".main br").remove("br");
349
        $(".chat_name #chat_heading a").remove("a");
350
      }
351
      $(".main").show();
352
    }
353
    if (ele == "compose") {
354
      $(".chat_name").show();
355
      $(".chat_name .compose_text").show();
356
      $(".sidebar").hide();
357
      $("#compose_selection").show();
358
    }
359
    if (ele == "sidebar") {
360
      $(".sidebar").show();
361
    }
362
    if (ele == "previous") {
363
      pre = "1";
364
    } else {
365
      pre = "";
366
    }
367
  }
368
}
369
370
function show_search() {
371
  // console.log("HE0");
372
  mob_hide();
373
  $(".search_message").show();
374
  $(".sidebar").show();
375
}
376
377
function mob_hide() {
378
  $(".search_message").hide();
379
  $(".sidebar").hide();
380
  $(".main").hide();
381
  $(".chat_name").hide();
382
  $(".mob-reply").hide();
383
}
384
385
function width() {
386
  if (window.innerWidth < 500) {
387
    return true;
388
  }
389
  return false;
390
}
391
392
// Audio Recognization
393
394
function startDictation() {
395
396
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
397
398
    var recognition = new webkitSpeechRecognition();
0 ignored issues
show
Bug introduced by
The variable webkitSpeechRecognition seems to be never declared. If this is a global, consider adding a /** global: webkitSpeechRecognition */ comment.

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.

Loading history...
Coding Style Best Practice introduced by
By convention, constructors like webkitSpeechRecognition should be capitalized.
Loading history...
399
400
    recognition.continuous = false;
401
    recognition.interimResults = false;
402
403
    recognition.lang = "en-IN";
404
    recognition.start();
405
406
    recognition.onresult = function(e) {
407
      document.getElementById("text_reply").value = e.results[0][0].transcript;
408
      recognition.stop();
409
      reply();
410
    };
411
412
    recognition.onerror = function() {
413
      recognition.stop();
414
    }
415
416
  }
417
}
418
$(document).ready(function(){
419
  $('body').on('click', '.sideBar-body', function() {
420
    console.log(this);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
421
      newConversation(this,10);
422
  });
423
424
  $('body').on('click', '.reply-send',
425
   function() {
426
    reply();
427
  });
428
429
  $('body').on('click', '.reply-recording',
430
   function() {
431
    startDictation();
432
  });
433
434
  $('body').on('click', '.previous a',
435
   function() {
436
    previous(this);
437
  });
438
});
439
console.log("Hello, Contact me at [email protected]");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...