Test Failed
Pull Request — master (#30)
by
unknown
02:21
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
$(document).ready(function(){
2
  var pre = $("<div class='loader'></div>").css({"width" : "60px", "height" : "60px", "z-index" : 10001, "margin" : "auto"});
0 ignored issues
show
Unused Code introduced by
The variable pre seems to be never used. Consider removing it.
Loading history...
3
4
  $('body').load('../public/assests/partials/app.html', function() {
5
  // $('body').append(pre);
6
7
8
var heightFrom; //  global variable
9
// Websocket Connection Open
10
var conn = new WebSocket("ws://192.168.43.138: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...
11
12
// For send Message to Web Socket Server
13
function sendTo(data)
14
{
15
  conn.send(JSON.stringify(data));
16
}
17
18
// For First time
19
function init()
20
{
21
  var msg = {
22
    "type": "OpenChat initiated..!"
23
  };
24
  sendTo(msg);
25
}
26
27
conn.onopen = function() {
28
  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...
29
  init();
30
};
31
32
// SideBar Load Request
33
function sidebarRequest()
34
{
35
  var msg = {
36
    "type": "Load Sidebar"
37
  };
38
  sendTo(msg);
39
}
40
41
// Create Sidebar
42
function createSidebarElement(data, eleId)
43
{
44
  // organising content according to time
45
  $("#"+eleId).html("");
46
  var condition = data.length;
47
  for (var i = 0; i < condition; i++)
48
  {
49
50
    var div1 = $("<div></div>").addClass("row sideBar-body");
51
52
    div1.attr({
53
      "id" : data[i].username
54
    });
55
56
    var div2 = $("<div></div>").addClass("col-sm-3 col-xs-3 sideBar-avatar");
57
    var div3 = $("<div></div>").addClass("avatar-icon");
58
    var imgElement = $("<img>").attr({
59
      "src": "../public/assests/img/bg.png"
60
    });
61
    div3.append(imgElement);
62
    div2.append(div3);
63
    div1.append(div2);
64
65
    div2 = $("<div></div>").addClass("col-sm-9 col-xs-9 sideBar-main");
66
    div3 = $("<div></div>").addClass("row");
67
    var div4 = $("<div></div>").addClass("col-sm-8 col-xs-8 sideBar-name");
68
    var spanE = $("<span></span>").addClass("name-meta").text(data[i].name);
69
    div4.append(spanE);
70
    div3.append(div4);
71
72
    div4 = $("<div></div>").addClass("col-sm-4 col-xs-4 pull-right sideBar-time");
73
    spanE = $("<span></span>").addClass("time-meta pull-right").text(data[i].time);
74
     div4.append(spanE);
75
    div3.append(div4);
76
    div2.append(div3);
77
78
    div1.append(div2);
79
    $("#"+eleId).append(div1);
80
81
    if (eleId === "message" && $("#text_reply").attr("name") == data[i].login_id) {
82
      $("#"+data[i].username).addClass("active");
83
    }
84
  }
85
}
86
87
88
// For updating Sidebar
89
function SideBar(msg) {
90
  // Getting Div
91
  if (msg != null) {
92
    createSidebarElement(msg, "message");
93
  }
94
}
95
96
function toConversation() {
97
  $(".side").addClass("hide");
98
  $(".message").addClass("show");
99
  $(".lowerBar").css({"display":"inline-flex"});
100
  $(".reply-emojis").addClass("hide");
101
  $(".reply-recording").addClass("hide");
102
}
103
104
function toSidebar() {
105
  $(".side").removeClass("hide");
106
  $(".message").removeClass("show");
107
  $(".lowerBar").css({"display":"none"});
108
  $(".reply-emojis").removeClass("hide");
109
  $(".reply-recording").removeClass("hide");
110
}
111
112
function width() {
113
  if (window.innerWidth < 768) {
114
    return true;
115
  }
116
  return false;
117
}
118
119
// Creating new Conversation or Loading Conversation
120
function newConversation(element, load) {
121
122
  if (width()) {
123
    toConversation();
124
  }
125
126
  var msg = {
127
    "details": element.id,
128
    "load": load,
129
    "type": "Initiated"
130
  };
131
  sendTo(msg);
132
}
133
134
// Set Details
135
function setConversationDetails(details) {
136
  $("#conversationHeading").html("");
137
138
  var headingEle = $("<div></div>").addClass("col-sm-9 col-xs-9 heading-left");
139
  var headingAva = $("<div></div>").addClass("heading-avatar");
140
  var headingImg = $("<img>").attr({"src" : "../public/assests/img/ankit.png"});
141
  headingAva.append(headingImg);
142
  headingEle.append(headingAva);
143
144
  var headingEleName = $("<div></div>").addClass("heading-name");
145
  var headingNameMeta = $("<a></a>").addClass("heading-name-meta").text(details.name);
146
  headingNameMeta.attr({
147
    "href": location.href.substring(0, location.href.lastIndexOf("/") + 1) + "account.php/" + details.username
148
  });
149
  var headingOnline = $("<span></span>").addClass("heading-online");
150
  headingOnline.text("");
151
  if (details.login_status === "1") {
152
    headingOnline.text("Online");
153
  }
154
  headingEleName.append(headingNameMeta);
155
  headingEleName.append(headingOnline);
156
  headingEle.append(headingEleName);
157
158
  var headingRight = $("<div></div>").addClass("col-sm-3 col-xs-3 heading-right");
159
160
  var headingDot = $("<div></div>").addClass("heading-dot");
161
  var headingDotIcon = $("<i></i>").addClass("fa fa-ellipsis-v fa-2x").attr({"aria-hidden" : "true"});
162
  headingDot.append(headingDotIcon);
163
  headingRight.append(headingDot);
164
165
166
  $("#conversationHeading").append(headingEle);
167
  $("#conversationHeading").append(headingRight);
168
169
  $("#text_reply").attr({
170
    "name": details.id
171
  });
172
}
173
174
175
// Update Current Conversation
176
function updateConversation(data) {
177
  var ele = document.getElementById("conversation");
178
  ele.innerHTML = "";
179
180
  if (data[0].type === 1)
181
  {
182
    // For showing previous message
183
    if (data[0].load > 10)
184
    {
185
      var divE1 = $("<div></div>").addClass("row message-previous");
186
      var divE2 = $("<div></div>").addClass("col-sm-12 previous");
187
      var aElement = $("<a></a>").text("Show Previous Message!");
188
      aElement.attr({
189
        "id": data[0].username,
190
        "name": data[0].load
191
      });
192
      divE2.append(aElement);
193
      divE1.append(divE2);
194
      $("#conversation").append(divE1);
195
    }
196
    var noOfMessages = data.length - 1;
197
    for (var i = noOfMessages; i >= 1; i--) {
198
      // create element
199
      var divElement1 = $("<div></div>").addClass("row message-body");
200
      var divElement2 = $("<div></div>").addClass("col-sm-12");
201
      var divElement3 = $("<div></div>");
202
      var messageText = $("<div></div>").addClass("message-text");
203
      
204
      // Embed YouTube video if link presents in message.
205
      var youtube_link = data[i].message.indexOf('https://www.youtube.com');
206
      if(youtube_link !== -1) {
207
          messageText.html(data[i].message + '<iframe width="100%" src="https://www.youtube.com/embed/' +
208
          data[i].message.substring(youtube_link + 32, youtube_link + 43) + '" frameborder="0" allowfullscreen></iframe>');
209
      }
210
      else {
211
          messageText.text(data[i].message);
212
      }
213
      
214
      var spanElement = $("<span></span>").addClass("message-time pull-right").text(data[i].time);
215
216
      if (data[i]["sent_by"] !== data[i].start)
217
      {
218
        divElement2.addClass("message-main-receiver");
219
        divElement3.addClass("receiver");
220
      } else {
221
        divElement2.addClass("message-main-sender");
222
        divElement3.addClass("sender");
223
      }
224
      divElement3.append(messageText);
225
      divElement3.append(spanElement);
226
      divElement2.append(divElement3);
227
      divElement1.append(divElement2);
228
      $("#conversation").append(divElement1);
229
    }
230
231
    if (noOfMessages < 21) {
232
      ele.scrollTop = ele.scrollHeight;
233
    } else {
234
      ele.scrollTop = $("#conversation")[0].scrollHeight - heightFrom;
235
    }
236
  }
237
  
238
  setConversationDetails(data[0]);
239
}
240
241
// For reply to other messages
242
function reply() {
243
244
  var message = $("#text_reply").val();
245
  var id = $("#text_reply").attr("name");
246
  $("#text_reply").val("");
247
  if (message !== "") {
248
    var msg = {
249
      "name": id,
250
      "reply": message,
251
      "type": "reply"
252
    };
253
    sendTo(msg);
254
  }
255
}
256
257
// Typing indication
258
function typing() {
259
  var id = $("#text_reply").attr("name");
260
  var msg = {
261
    "name": id,
262
    "type": "typing"
263
  };
264
  sendTo(msg);
265
}
266
267
function notFound(eleId) {
268
  eleId = "#" + eleId;
269
  $(eleId).text("");
270
  var divElement = $("<div></div>").addClass("notFound").text("Not Found");
271
  $(eleId).append(divElement);
272
}
273
274
function composeChoose() {
275
  var text = document.getElementById("composeText").value;
276
  if (text !== "") {
277
    var msg =
278
    {
279
      "value": text,
280
      "type": "Compose"
281
    };
282
    sendTo(msg);
283
  } else {
284
    $("#compose").html("<div class=\"notFound\">Start New Chat</div>");
285
  }
286
}
287
288
//compose messages
289
function composeResult(arr) {
290
  var ele = document.getElementById("compose");
291
  ele.innerHTML = "";
292
293
  if (arr !== "Not Found") {
294
    createSidebarElement(arr, "compose");
295
  } else {
296
    notFound("compose");
297
  }
298
}
299
300
function searchChoose() {
301
  var text = $("#searchText").val();
302
  if (text !== "") {
303
    var msg = {
304
      "value": text,
305
      "type": "Search"
306
    };
307
308
    sendTo(msg);
309
  } else {
310
    sidebarRequest();
311
  }
312
313
}
314
315
function searchResult(arr) {
316
  if (arr !== "Not Found") {
317
    createSidebarElement(arr, "message");
318
  } else {
319
    notFound("message");
320
  }
321
322
}
323
324
// Load previous messages
325
function previous(element) {
326
  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...
327
  var lo = element.name;
328
  heightFrom = $("#conversation")[0].scrollHeight;
329
  newConversation(element, lo);
330
}
331
332
function hideComposeScreen() {
333
  $(".side-two").css({
334
    "left": "-100%"
335
  });
336
}
337
338
// Audio Recognization
339
340
function startDictation() {
341
342
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
343
344
    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...
345
346
    recognition.continuous = false;
347
    recognition.interimResults = false;
348
349
    recognition.lang = "en-IN";
350
    recognition.start();
351
352
    recognition.onresult = function(e) {
353
      document.getElementById("text_reply").value = e.results[0][0].transcript;
354
      recognition.stop();
355
      reply();
356
    };
357
358
    recognition.onerror = function() {
359
      recognition.stop();
360
    }
361
362
  }
363
}
364
365
366
// On Message
367
conn.onmessage = function(e)
368
{
369
  var msg = JSON.parse(e.data);
370
371
  if (!width()) {
372
    SideBar(msg.sidebar);
373
  } else {
374
    if (!$(".side").hasClass("hide")) {
375
      SideBar(msg.sidebar);
376
    }
377
  }
378
379
  if (typeof(msg.initial) !== "undefined") {
380
    SideBar(msg.initial);
381
  }
382
383
  if (typeof(msg.conversation) !== "undefined") {
384
    updateConversation(msg.conversation);
385
  }
386
387
  if (typeof(msg.reply) !== "undefined") {
388
    var textAreaId = $("#text_reply").attr("name");
389
    if (msg.reply[0].id === textAreaId) {
390
      updateConversation(msg.reply);
391
    }
392
  }
393
394
  if (typeof(msg.Search) !== "undefined") {
395
    searchResult(msg.Search);
396
  }
397
398
  if (typeof(msg.Compose) !== "undefined") {
399
    composeResult(msg.Compose);
400
  }
401
  
402
  if (typeof(msg.typing) !== "undefined") {
403
     if($('#user_typing').length == 0) {
404
        $('#conversation').append('<div class="row message-body" id="user_typing" style=""><div class="col-sm-12 message-main-receiver"><div class="receiver"><span class="message-time pull-right">typing...</span></div></div></div>');
405
        var element = document.getElementById("conversation");
406
        element.scrollTop = element.scrollHeight;
407
        setTimeout(function() { $('#user_typing').remove(); }, 1000);
408
     }
409
  }
410
};
411
412
// Event Listeners
413
  $("body").on("click", ".sideBar-body", function() {
414
    newConversation(this,20);
415
    hideComposeScreen();
416
    $("#searchText").val("");
417
    $("#composeText").val("");
418
  });
419
420
  $("body").on("click", ".reply-send",
421
   function() {
422
    reply();
423
  });
424
  
425
  $("body").on("change keyup paste", "#text_reply",
426
   function() {
427
    typing();
428
  });
429
430
  $("body").on("click", ".reply-recording",
431
   function() {
432
    startDictation();
433
  });
434
435
  $("body").on("click", ".lowerBar-recording",
436
   function() {
437
    startDictation();
438
  });
439
440
  $("body").on("click", ".lowerBar-back",
441
   function() {
442
    toSidebar();
443
    sidebarRequest();
444
  });
445
446
  $("body").on("click", ".previous a",
447
   function() {
448
    previous(this);
449
  });
450
451
  $("body").on("keyup", "#searchText",
452
   function() {
453
    searchChoose();
454
  });
455
456
  $("body").on("keyup", "#composeText",
457
   function() {
458
    composeChoose();
459
  });
460
461
  $(".heading-compose").click(function() {
462
    $(".side-two").css({
463
      "left": "0"
464
    });
465
  });
466
467
  $(".newMessage-back").click(function() {
468
    hideComposeScreen();
469
    $("#composeText").val("");
470
  });
471
472
  $("#conversation").scroll(function() {
473
    var res = $(".message-previous").html();
474
    var scrollTop = $("#conversation").scrollTop();
475
    if (typeof(res) !== "undefined" && scrollTop < 100) {
476
      $(".previous a").click();
477
      if (scrollTop < 3) {
478
        $(".message-previous div").html("").removeClass("col-sm-12 previous").addClass("loader").css({"width" : "20px", "height" : "20px"});
479
      }
480
    }
481
482
  });
483
484
  });
485
486
});
487
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...
488
489
490