Completed
Push — master ( 63e2f0...3977a0 )
by Ankit
02:58
created

message.js ➔ toConversation   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
2
// Websocket Connection Open
3
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...
4
5
6
// For First time
7
function init() {
8
  conn.send("OpenChat initiated..!");
9
}
10
11
conn.onopen = function() {
12
  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...
13
  init();
14
};
15
16
// SideBar Load Request
17
function sidebarRequest() {
18
  conn.send("Load Sidebar");
19
}
20
21
// Create Sidebar
22
function createSidebarElement(data, eleId)
23
{
24
  // organising content according to time
25
  $("#"+eleId).html("");
26
  var condition = data.length;
27
  for (var i = 0; i < condition; i++)
28
  {
29
30
    var div1 = $("<div></div>").addClass("row sideBar-body");
31
32
    div1.attr({
33
      "id" : data[i].username
34
    });
35
36
    var div2 = $("<div></div>").addClass("col-sm-3 col-xs-3 sideBar-avatar");
37
    var div3 = $("<div></div>").addClass("avatar-icon");
38
    var imgElement = $("<img>").attr({
39
      "src": "../public/assests/img/bg.png"
40
    });
41
    div3.append(imgElement);
42
    div2.append(div3);
43
    div1.append(div2);
44
45
    div2 = $("<div></div>").addClass("col-sm-9 col-xs-9 sideBar-main");
46
    div3 = $("<div></div>").addClass("row");
47
    var div4 = $("<div></div>").addClass("col-sm-8 col-xs-8 sideBar-name");
48
    var spanE = $("<span></span>").addClass("name-meta").text(data[i].name);
49
    div4.append(spanE);
50
    div3.append(div4);
51
52
    div4 = $("<div></div>").addClass("col-sm-4 col-xs-4 pull-right sideBar-time");
53
    spanE = $("<span></span>").addClass("time-meta pull-right").text(data[i].time);
54
     div4.append(spanE);
55
    div3.append(div4);
56
    div2.append(div3);
57
58
    div1.append(div2);
59
    $("#"+eleId).append(div1);
60
61
    if(eleId === "message" && $("#text_reply").attr("name") == data[i].login_id)
62
    {
63
      $("#"+data[i].username).addClass("active");
64
    }
65
  }
66
}
67
68
69
// For updating Sidebar
70
function SideBar(msg) {
71
  // Getting Div
72
  if (msg != null) {
73
    createSidebarElement(msg, "message");
74
  }
75
}
76
77
function toConversation() {
78
  $(".side").addClass("hide");
79
  $(".message").addClass("show");
80
  $(".lowerBar").addClass("show");
81
  $(".reply-emojis").addClass("hide");
82
  $(".reply-recording").addClass("hide");
83
}
84
85
function toSidebar() {
86
  $(".side").removeClass("hide");
87
  $(".message").removeClass("show");
88
  $(".lowerBar").removeClass("show");
89
  $(".reply-emojis").removeClass("hide");
90
  $(".reply-recording").removeClass("hide");
91
}
92
93
function width() {
94
  if (window.innerWidth < 500) {
95
    return true;
96
  }
97
  return false;
98
}
99
100
// Creating new Conversation or Loading Conversation
101
function newConversation(element, load)
102
{
103
104
  if(width())
105
  {
106
    toConversation();
107
    console.log("ok2");
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...
108
  }
109
110
  var msg = {
111
    "username": element.id,
112
    "load": load,
113
    "newConversation": "Initiated"
114
  };
115
  conn.send(JSON.stringify(msg));
116
}
117
118
// Set Details
119
function setConversationDetails(details)
120
{
121
  $(".heading-name-meta").text(details.name);
122
  $(".heading-name-meta").attr({
123
    "href": location.href.substring(0, location.href.lastIndexOf('/') + 1) + "account.php/" + details.username
124
  });
125
126
  $(".heading-online").removeClass("show");
127
  if (details.login_status === "1") {
128
    $(".heading-online").addClass("show");
129
  }
130
131
  $("#text_reply").attr({
132
    "name": details.id
133
  });
134
}
135
136
137
// Update Current Conversation
138
function updateConversation(data)
139
{
140
141
  var ele = document.getElementById("conversation");
142
  ele.innerHTML = "";
143
144
  if (data[0].type === 1) {
145
    // For showing previous message
146
    if (data[0].load > 10)
147
    {
148
      var divE1 = $("<div></div>").addClass("row message-previous");
149
      var divE2 = $("<div></div>").addClass("col-sm-12 previous");
150
      var aElement = $("<a></a>").text("Show Previous Message!");
151
      aElement.attr({
152
        "id": data[0].username,
153
        "name": data[0].load
154
      });
155
      divE2.append(aElement);
156
      divE1.append(divE2);
157
      $("#conversation").append(divE1);
158
    }
159
160
    for (var i = data.length - 1; i >= 1; i--) {
161
      // create element
162
      var divElement1 = $("<div></div>").addClass("row message-body");
163
      var divElement2 = $("<div></div>").addClass("col-sm-12");
164
      var divElement3 = $("<div></div>");
165
      var messageText = $("<div></div>").addClass("message-text").text(data[i].message);
166
      var spanElement = $("<span></span>").addClass("message-time pull-right").text(data[i].time);
167
168
      if (data[i]["sent_by"] !== data[i].start)
169
      {
170
       divElement2.addClass("message-main-receiver");
171
       divElement3.addClass("receiver");
172
      }
173
      else
174
      {
175
        divElement2.addClass("message-main-sender");
176
       divElement3.addClass("sender");
177
      }
178
      divElement3.append(messageText);
179
      divElement3.append(spanElement);
180
      divElement2.append(divElement3);
181
      divElement1.append(divElement2);
182
      $("#conversation").append(divElement1);
183
    }
184
185
    setConversationDetails(data[0]);
186
187
    ele.scrollTop = ele.scrollHeight;
188
  } else {
189
    setConversationDetails(data[0]);
190
  }
191
}
192
193
// For reply to other messages
194
function reply() {
195
196
  var message = [$("#text_reply").val()];
197
  var id = $("#text_reply").attr("name");
198
  $("#text_reply").val("");
199
  // console.log(message);
200
  var q = {
201
    "name": id,
202
    "reply": message
203
  };
204
  conn.send(JSON.stringify(q));
205
206
}
207
208
function notFound(eleId)
209
{
210
  eleId = "#"+eleId;
211
  $(eleId).text("");
212
  var divElement = $("<div></div>").addClass("notFound").text("Not Found");
213
  $(eleId).append(divElement);
214
}
215
216
function composeChoose() {
217
  var text = document.getElementById("composeText").value;
218
  if (text !== "")
219
  {
220
    var msg =
221
    {
222
      "value": text,
223
      "Compose": "Compose"
224
    };
225
    conn.send(JSON.stringify(msg));
226
  }
227
  else
228
  {
229
    $("#compose").html("");
230
  }
231
}
232
233
//compose messages
234
function composeResult(arr)
235
{
236
  var ele = document.getElementById("compose");
237
  ele.innerHTML = "";
238
239
  if (arr !== "Not Found")
240
  {
241
    createSidebarElement(arr, "compose")
242
  }
243
  else
244
  {
245
    notFound("compose");
246
  }
247
}
248
249
function searchChoose() {
250
  var text = $("#searchText").val();
251
  if (text !== "") {
252
    var msg = {
253
      "value": text,
254
      "search": "search"
255
    };
256
257
    conn.send(JSON.stringify(msg));
258
  }
259
  else
260
  {
261
    conn.send("Load Sidebar");
262
  }
263
264
}
265
266
function searchResult(arr) {
267
  if (arr !== "Not Found") {
268
    createSidebarElement(arr, "message");
269
  }
270
  else
271
  {
272
    notFound("message");
273
  }
274
275
}
276
277
// Load previous messages
278
function previous(element)
279
{
280
  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...
281
  var lo = element.name;
282
  newConversation(element, lo);
283
}
284
285
// Audio Recognization
286
287
function startDictation() {
288
289
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
290
291
    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...
292
293
    recognition.continuous = false;
294
    recognition.interimResults = false;
295
296
    recognition.lang = "en-IN";
297
    recognition.start();
298
299
    recognition.onresult = function(e) {
300
      document.getElementById("text_reply").value = e.results[0][0].transcript;
301
      recognition.stop();
302
      reply();
303
    };
304
305
    recognition.onerror = function() {
306
      recognition.stop();
307
    }
308
309
  }
310
}
311
312
313
// On Message
314
conn.onmessage = function(e) {
315
  var msg = JSON.parse(e.data);
316
  // console.log(msg);
317
  if (!width()) {
318
    SideBar(msg.sidebar);
319
  } else {
320
    if (!$(".side").hasClass("hide")) {
321
      SideBar(msg.sidebar);
322
    }
323
  }
324
325
  if (msg.initial !== undefined) {
326
    SideBar(msg.initial);
327
  }
328
329
  if (msg.conversation !== undefined) {
330
    updateConversation(msg.conversation);
331
  }
332
333
  if (msg.reply !== undefined) {
334
    var textAreaId = $("#text_reply").attr("name");
335
    if (msg.reply[0].id === textAreaId) {
336
      updateConversation(msg.reply);
337
    }
338
  }
339
340
  if (msg.Search !== undefined) {
341
    searchResult(msg.Search);
342
  }
343
344
  if (msg.Compose !== undefined) {
345
    composeResult(msg.Compose);
346
  }
347
};
348
349
// Event Listeners
350
$(document).ready(function(){
351
  $('body').on('click', '.sideBar-body', function() {
352
      newConversation(this,10);
353
      console.log("ok");
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...
354
  });
355
356
  $('body').on('click', '.reply-send',
357
   function() {
358
    reply();
359
  });
360
361
  $('body').on('click', '.reply-recording',
362
   function() {
363
    startDictation();
364
  });
365
366
  $('body').on('click', '.lowerBar-recording',
367
   function() {
368
    startDictation();
369
  });
370
371
  $('body').on('click', '.lowerBar-back',
372
   function() {
373
    toSidebar();
374
    sidebarRequest();
375
  });
376
377
  $('body').on('click', '.previous a',
378
   function() {
379
    previous(this);
380
  });
381
382
  $('body').on('keyup', '#searchText',
383
   function() {
384
    searchChoose();
385
  });
386
387
  $('body').on('keyup', '#composeText',
388
   function() {
389
    composeChoose();
390
  });
391
392
  $(".heading-compose").click(function() {
393
    $(".side-two").css({
394
      "left": "0"
395
    });
396
  });
397
398
  $(".newMessage-back").click(function() {
399
    $(".side-two").css({
400
      "left": "-100%"
401
    });
402
  });
403
404
});
405
406
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...