Completed
Push — master ( c5e908...a0379d )
by Ankit
03:06
created

public/assests/js/mobile.js   B

Complexity

Total Complexity 46
Complexity/F 3.07

Size

Lines of Code 365
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 0
loc 365
rs 8.3999
c 0
b 0
f 0
wmc 46
mnd 6
bc 38
fnc 15
bpm 2.5333
cpm 3.0666
noi 18

8 Functions

Rating   Name   Duplication   Size   Complexity  
B mobile.js ➔ reply 0 29 1
A mobile.js ➔ init 0 56 1
A mobile.js ➔ compose_message 0 51 2
B mobile.js ➔ chat 0 111 1
A mobile.js ➔ myFunction 0 10 1
A mobile.js ➔ search_choose 0 49 1
A mobile.js ➔ previous 0 7 1
A mobile.js ➔ compose 0 7 1

How to fix   Complexity   

Complexity

Complex classes like public/assests/js/mobile.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var store='';
2
var recursive;
3
var last_time=''  ;
4
var flag=0;
5
6
7
8
9
// For updating sidebar and load conversation for first time
10
11
function init(index)  
12
{
13
  var q="q=total_messages";
14
  var ele=document.getElementById("message-mob");  // Getting Div
15
  var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
16
  xmlhttp.onreadystatechange = function()                                     // Ajax Call
17
  {
18
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
19
    {
20
      var arr=JSON.parse(xmlhttp.responseText);                                // Response From change.php
21
      // console.log(arr); 
22
      if(arr!=null)         
23
      {                 
24
        var a=arr[arr.length-1].time;
25
        var b=arr[arr.length-1].username;
26
        var c=a+b;
27
        if (last_time!=c) 
28
        {
29
          last_time=c;
30
          $("#message-mob a").remove();
31
32
          for (var i = arr.length - 1; i >= 0; i--)                              // organising content according to time
33
          {
34
            // var sp=$("<span></span>").text(arr[i]['time']);                      // creating element span
35
            // sp.addClass('message-mob_time');
36
            // var para=$("<a></a>").text(arr[i]['name']);                          //creating element a
37
            //  para.append(sp);
38
            // $("#message-mob").append(para);                             
39
            // para.attr({'id':arr[i]['username'],'href':'message-mob.php#'+arr[i]['username'],'class':'message-mob','onclick':'chat(this,10)'});
40
41
          var para=document.createElement("a");                 //creating element a    
42
          var node=document.createTextNode(arr[i]['name']);   
43
          para.appendChild(node);   
44
          para.setAttribute('id',arr[i]['username']);   
45
          para.setAttribute('href','message.php#'+arr[i]['username']);    
46
          para.setAttribute('class','message');   
47
          para.setAttribute('onclick','chat(this,10)');    
48
          ele.appendChild(para);    
49
      
50
          var bre=document.createElement("span");               // creating element span for showing time   
51
          var inp=document.createTextNode(arr[i]['time']);    
52
          bre.appendChild(inp);   
53
          bre.setAttribute('class','message_time');   
54
          para.appendChild(bre);
55
          }
56
57
          if(index==0)
58
            chat(document.getElementById(arr[arr.length-1].username),10);         // Load messgage for the first conversation
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
59
        }
60
      }
61
    }
62
  };
63
  xmlhttp.open("POST", "ajax/change.php", true);                                // ajax request post
64
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
65
  xmlhttp.send(q); 
66
}
67
68
69
70
71
// For loading conversation between two persons
72
73
function chat(element,num)   
74
{
75
  // console.log(num);
76
  $("#compose_selection").css("visibility","hidden");
77
  last_time='';
78
  flag=0;
79
  $("#compose_name").val('');
80
  $("#search_item").val('');
81
  $('#compose_text').hide();
82
83
  stop();                                                                  // stopping previous setinterval call
84
  
85
  recursive =setInterval(repeat,1500);                                     // refresh conversation
86
  function repeat() 
87
  {
88
    // console.log(element);
89
    init(1);
90
91
    var q={"username":element.id,"load":num};
92
    q="q="+JSON.stringify(q);
93
94
    // console.log(q);
95
96
    var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
97
    var ele=document.getElementById("conversation");                                  // ajax call
98
    xmlhttp.onreadystatechange = function() 
99
    {
100
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
101
      {
102
        var arr=xmlhttp.responseText;
103
        arr=JSON.parse(arr);
104
        // console.log(arr);
105
106
        if(arr!=null && flag==0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
        {
108
          // console.log(1);
109
          if (arr[arr.length-1]==1)                                                  // Old User
110
          {
111
            var a=arr[0].id;
112
            if(store!=a)
113
            {
114
              store=a;
115
              // console.log(1);
116
              ele.innerHTML="";
117
118
              if(arr[arr.length-2].load>10)                                     // For showing previous message
119
              {
120
                var txt=$("<a></a>").text("Show Previous Message!");
121
                var te=$("<div></div>").append(txt);
122
                $("#conversation").append(te);
123
                $("#conversation div").addClass("previous");
124
                $("#conversation div a").attr({"onclick":"previous(this)","id":arr[0].username,"name":arr[arr.length-2].load});
125
              }
126
127
              for (var i = arr.length -3; i >= 0; i--) 
128
              {
129
                // create element
130
                var para=document.createElement("div");
131
132
                if(arr[i]['sent_by']!=arr[i]['start'])
133
                 para.setAttribute('class','sender');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
134
                else
135
                  para.setAttribute('class','receiver');
136
137
                ele.appendChild(para);
138
                var bre=document.createElement("br");
139
                bre.setAttribute("style","clear:both;");
140
                ele.appendChild(bre);
141
142
                var info=document.createElement("p");
143
                var node=document.createTextNode(arr[i]['message']);
144
                info.appendChild(node);
145
                para.appendChild(info);
146
147
                var tt=document.createElement("h6");
148
                var inp=document.createTextNode(arr[i]['time']);
149
                tt.appendChild(inp);
150
                tt.setAttribute('class','message_time');
151
                info.appendChild(tt);
152
              }
153
154
              $("#chat_heading a").remove('a');
155
              var txt=$("<a></a>").text(arr[0].name);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable txt already seems to be declared on line 120. 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...
156
              $("#chat_heading").append(txt);
157
              $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr[0].username});
158
              $("#text_reply").attr({'name':arr[0]['identifier_message_number']});
159
              ele.scrollTop = ele.scrollHeight;
160
            }
161
          }
162
          else if(arr['new']==0)                              // New User
163
          {
164
            ele.innerHTML="";
165
            $("#chat_heading a").remove('a');
166
            var txt=$("<a></a>").text(arr.name);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable txt already seems to be declared on line 120. 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...
167
            $("#chat_heading").append(txt);
168
            $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr.username});
169
            $("#text_reply").attr({'name':arr['identifier_message_number']});
170
          } 
171
        }  
172
      }
173
    };
174
    xmlhttp.open("POST", "ajax/chat.php", true);
175
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
176
    xmlhttp.send(q); 
177
  }
178
  function stop()
179
  {
180
      clearInterval(recursive);
181
      // console.log("recursive");
182
  }
183
}
184
185
186
187
188
// For reply to other messages
189
190
function reply()
191
{
192
  var ele=[document.getElementById("text_reply").value];
193
  var id=document.getElementById("text_reply").name;
194
195
  // console.log(ele);
196
  var p='';
0 ignored issues
show
Unused Code introduced by
The variable p seems to be never used. Consider removing it.
Loading history...
197
  var q={"name":id,"reply":ele};
198
  q="q="+JSON.stringify(q);
199
  // console.log(q);
200
  var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
201
  xmlhttp.onreadystatechange = function() {
202
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
203
    {
204
      arr=xmlhttp.responseText;
0 ignored issues
show
Bug introduced by
The variable arr seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.arr.
Loading history...
205
        // console.log(arr);
206
      if (arr=="Messages is sent")                                        // Message is sent
207
      {
208
        document.getElementById("text_reply").value="";
209
      }
210
      else{
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
211
212
      }
213
    }    
214
  };
215
  xmlhttp.open("POST", "ajax/reply.php", true);
216
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
217
  xmlhttp.send(q); 
218
}
219
220
221
222
// Compose new and direct message to anyone
223
224
function compose() 
225
{
226
  flag=1;
227
  $("#chat_heading a").remove('a');
228
  document.getElementById("conversation").innerHTML="";
229
  $('#compose_text').show();
230
}
231
232
233
234
//compose messages
235
236
function compose_message() 
237
{
238
  var q=document.getElementById("compose_name").value;
239
  // console.log(q);
240
  var ele=document.getElementById("suggestion");
241
  ele.innerHTML="";
242
  var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
243
  xmlhttp.onreadystatechange = function() 
244
  {
245
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
246
    {
247
      arr=xmlhttp.responseText;
0 ignored issues
show
Bug introduced by
The variable arr seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.arr.
Loading history...
248
      arr=JSON.parse(arr);
249
      // console.log(arr);
250
251
      if (arr!=[] && arr!="Not Found") 
252
      {
253
        for (var i = arr.length - 1; i >= 0; i--) 
254
        {
255
          var para=document.createElement("li");
256
          var active=document.createElement("a");
257
          var node=document.createTextNode(arr[i].name);
258
          active.appendChild(node);
259
          active.setAttribute("href","#");
260
          active.setAttribute("onclick","chat(this,10)");
261
          active.setAttribute("class","suggestion");
262
          active.setAttribute("id",arr[i].username);
263
          para.appendChild(active);
264
          ele.appendChild(para);
265
        }
266
      }
267
      else if(arr=="Not Found")
268
      {
269
        var txt=$("<a></a>").text('Not Found');
270
        var l=$("<li></li>").append(txt);
271
        $("#suggestion").append(l);
272
        $("#suggestion li a").attr({"onclick":"myFunction()"});
273
274
      }
275
    }
276
    $("#compose_selection").css("visibility","visible");
277
  };
278
  if(q!="")
279
  {
280
    xmlhttp.open("GET", "ajax/suggestion.php?q=" + q, true);
281
    xmlhttp.send();
282
  }
283
  else
284
    $("#compose_selection").css("visibility","hidden");  //for hidding the suggestion
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
285
286
}
287
288
289
290
// For Search
291
292
function search_choose()
293
{
294
  // console.log(1);
295
  var q=$("#search_item").val();
296
  var ele=document.getElementById("message");
297
298
  var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
299
  xmlhttp.onreadystatechange = function() 
300
  {
301
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
302
      arr=JSON.parse(xmlhttp.responseText);                                 // Response From change.php
0 ignored issues
show
Bug introduced by
The variable arr seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.arr.
Loading history...
303
      // console.log(arr);
304
      if($("#search_item").val()=='')
305
          last_time='';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
306
      if (arr!=null) 
307
      {
308
        ele.innerHTML="";
309
        for (var i = arr.length - 1; i >= 0; i--)                         // organising content according to time
310
        {
311
          var para=document.createElement("a");                             //creating element a
312
          var node=document.createTextNode(arr[i]['name']);
313
          para.appendChild(node);
314
          para.setAttribute('id',arr[i]['username']);
315
          para.setAttribute('href','message.php#'+arr[i]['username']);
316
          para.setAttribute('class','message');
317
          para.setAttribute('onclick','chat(this)');
318
          ele.appendChild(para);
319
320
          var bre=document.createElement("span");                                 // creating element span for showing time
321
          var inp=document.createTextNode(arr[i]['time']);
322
          bre.appendChild(inp);
323
          bre.setAttribute('class','message_time');
324
          para.appendChild(bre);
325
        }
326
      }
327
      else
328
      {
329
        $("#message").text('');
330
        // console.log("None");
331
        var txt=$("<a></a>").text("Not Found");
332
        $("#message").append(txt);
333
        $("#message a").addClass('message');
334
      }
335
336
    } 
337
  };  
338
  xmlhttp.open("GET", "ajax/search_item.php?q=" + q, true);
339
  xmlhttp.send(); 
340
}
341
342
343
window.ondblclick=myFunction;
344
345
function myFunction()                                                     // Hidden compose message input
346
{
347
  $("#compose_selection").css("visibility","hidden");
348
  last_time='';
349
  flag=0;
350
  store='';
351
  $("#compose_name").val('');
352
  $("#search_item").val('');
353
  $('#compose_text').hide();
354
}
355
356
357
function previous(element)                                                // Load previous messages
358
{
359
  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...
360
  var lo=element.name;
361
  chat(element,lo);
362
  store='';
363
}
364
365
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...