Test Failed
Pull Request — master (#34)
by
unknown
02:18
created

$(document).ready   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 72
rs 6.3883

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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