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

$(document).ready   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
c 2
b 0
f 0
nc 5
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
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
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
    for (var i = noOfMessages; i >= 1; i--) {
199
      // create element
200
      var divElement1 = $("<div></div>").addClass("row message-body");
201
      var divElement2 = $("<div></div>").addClass("col-sm-12");
202
      var divElement3 = $("<div></div>");
203
      var messageText = $("<div></div>").addClass("message-text");
204
      
205
      // Embed YouTube video if link presents in message.
206
      var result = anchorme(data[i].message.replace(/<(?:.|\n)*?>/gm, ''), { attributes:[ { name:"target", value:"_blank" } ] });
207
      var youtube_link = data[i].message.indexOf('https://www.youtube.com');
208
      if(youtube_link !== -1) {
209
          messageText.html(result + '<iframe width="100%" src="https://www.youtube.com/embed/' +
210
            data[i].message.substring(youtube_link + 32, youtube_link + 43) + '" frameborder="0" allowfullscreen></iframe>');
211
      }
212
      else {
213
          youtube_link = data[i].message.indexOf('https://youtu.be/');
214
          if(youtube_link !== -1) {
215
              messageText.html(result + '<iframe width="100%" src="https://www.youtube.com/embed/' +
216
                data[i].message.substring(youtube_link + 17, youtube_link + 28) + '" frameborder="0" allowfullscreen></iframe>');
217
          } 
218
          else {
219
              messageText.html(result);
220
          }             
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
  setConversationDetails(data[0]);
247
}
248
249
// For reply to other messages
250
function reply() {
251
252
  var message = $("#text_reply").val();
253
  var id = $("#text_reply").attr("name");
254
  $("#text_reply").val("");
255
  if (message !== "") {
256
    var msg = {
257
      "name": id,
258
      "reply": message,
259
      "type": "reply"
260
    };
261
    sendTo(msg);
262
  }
263
}
264
265
function notFound(eleId) {
266
  eleId = "#" + eleId;
267
  $(eleId).text("");
268
  var divElement = $("<div></div>").addClass("notFound").text("Not Found");
269
  $(eleId).append(divElement);
270
}
271
272
function composeChoose() {
273
  var text = document.getElementById("composeText").value;
274
  if (text !== "") {
275
    var msg =
276
    {
277
      "value": text,
278
      "type": "Compose"
279
    };
280
    sendTo(msg);
281
  } else {
282
    $("#compose").html("<div class=\"notFound\">Start New Chat</div>");
283
  }
284
}
285
286
//compose messages
287
function composeResult(arr) {
288
  var ele = document.getElementById("compose");
289
  ele.innerHTML = "";
290
291
  if (arr !== "Not Found") {
292
    createSidebarElement(arr, "compose");
293
  } else {
294
    notFound("compose");
295
  }
296
}
297
298
function searchChoose() {
299
  var text = $("#searchText").val();
300
  if (text !== "") {
301
    var msg = {
302
      "value": text,
303
      "type": "Search"
304
    };
305
306
    sendTo(msg);
307
  } else {
308
    sidebarRequest();
309
  }
310
311
}
312
313
function searchResult(arr) {
314
  if (arr !== "Not Found") {
315
    createSidebarElement(arr, "message");
316
  } else {
317
    notFound("message");
318
  }
319
320
}
321
322
// Load previous messages
323
function previous(element) {
324
  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...
325
  var lo = element.name;
326
  heightFrom = $("#conversation")[0].scrollHeight;
327
  newConversation(element, lo);
328
}
329
330
function hideComposeScreen() {
331
  $(".side-two").css({
332
    "left": "-100%"
333
  });
334
}
335
336
// Audio Recognization
337
338
function startDictation() {
339
340
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
341
342
    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...
343
344
    recognition.continuous = false;
345
    recognition.interimResults = false;
346
347
    recognition.lang = "en-IN";
348
    recognition.start();
349
350
    recognition.onresult = function(e) {
351
      document.getElementById("text_reply").value = e.results[0][0].transcript;
352
      recognition.stop();
353
      reply();
354
    };
355
356
    recognition.onerror = function() {
357
      recognition.stop();
358
    }
359
360
  }
361
}
362
363
364
// On Message
365
conn.onmessage = function(e)
366
{
367
  var msg = JSON.parse(e.data);
368
  // console.log(msg);
369
  if (!width()) {
370
    SideBar(msg.sidebar);
371
  } else {
372
    if (!$(".side").hasClass("hide")) {
373
      SideBar(msg.sidebar);
374
    }
375
  }
376
377
  if (typeof(msg.initial) !== "undefined") {
378
    SideBar(msg.initial);
379
  }
380
381
  if (typeof(msg.conversation) !== "undefined") {
382
    updateConversation(msg.conversation);
383
  }
384
385
  if (typeof(msg.reply) !== "undefined") {
386
    var textAreaId = $("#text_reply").attr("name");
387
    if (msg.reply[0].id === textAreaId) {
388
      updateConversation(msg.reply);
389
    }
390
  }
391
392
  if (typeof(msg.Search) !== "undefined") {
393
    searchResult(msg.Search);
394
  }
395
396
  if (typeof(msg.Compose) !== "undefined") {
397
    composeResult(msg.Compose);
398
  }
399
};
400
401
// Event Listeners
402
  $("body").on("click", ".sideBar-body", function() {
403
    newConversation(this,20);
404
    hideComposeScreen();
405
    $("#searchText").val("");
406
    $("#composeText").val("");
407
  });
408
409
  $("body").on("click", ".reply-send",
410
   function() {
411
    reply();
412
  });
413
414
  $("body").on("click", ".reply-recording",
415
   function() {
416
    startDictation();
417
  });
418
419
  $("body").on("click", ".lowerBar-recording",
420
   function() {
421
    startDictation();
422
  });
423
424
  $("body").on("click", ".lowerBar-back",
425
   function() {
426
    toSidebar();
427
    sidebarRequest();
428
  });
429
430
  $("body").on("click", ".previous a",
431
   function() {
432
    previous(this);
433
  });
434
435
  $("body").on("keyup", "#searchText",
436
   function() {
437
    searchChoose();
438
  });
439
440
  $("body").on("keyup", "#composeText",
441
   function() {
442
    composeChoose();
443
  });
444
445
  $(".heading-compose").click(function() {
446
    $(".side-two").css({
447
      "left": "0"
448
    });
449
  });
450
451
  $(".newMessage-back").click(function() {
452
    hideComposeScreen();
453
    $("#composeText").val("");
454
  });
455
456
  $("#conversation").scroll(function() {
457
    var res = $(".message-previous").html();
458
    var scrollTop = $("#conversation").scrollTop();
459
    if (typeof(res) !== "undefined" && scrollTop < 100) {
460
      $(".previous a").click();
461
      if (scrollTop < 3) {
462
        $(".message-previous div").html("").removeClass("col-sm-12 previous").addClass("loader").css({"width" : "20px", "height" : "20px"});
463
      }
464
    }
465
466
  });
467
468
  });
469
470
});
471
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...
472
473
474