Completed
Branch master (ebb499)
by Alexey
04:15
created

system/modules/Server/static/js/Server.js   A

Complexity

Total Complexity 27
Complexity/F 4.5

Size

Lines of Code 97
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 2
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 27
mnd 4
bc 30
fnc 6
bpm 5
cpm 4.5
noi 4

2 Functions

Rating   Name   Duplication   Size   Complexity  
B Server.runCommands 0 18 5
C Server.request 0 76 7
1
/**
2
 * Item name
3
 *
4
 * Info
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
function Server() {
13
14
}
15
Server.prototype.runCommands = function (commands) {
16
  for (var key in commands) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
17
    var command = commands[key];
18
    var callPath = command.call.split('.');
19
    var curPath = window;
20
    for (var keyPath in callPath) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
21
      if (typeof curPath[callPath[keyPath]] == 'undefined') {
22
        console.log('undefined call path ' + callPath[keyPath] + ' in ' + command.call);
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...
23
        curPath = null;
24
        break;
25
      }
26
      curPath = curPath[callPath[keyPath]];
27
    }
28
    if (curPath !== null) {
29
      curPath.apply(null, command.params);
30
    }
31
  }
32
}
33
Server.prototype.request = function (options, btn) {
34
  var ajaxOptions = {
35
    url: '',
36
    type: 'GET',
37
    dataType: 'json',
38
    data: {},
39
    async: true,
40
    contentType: false,
41
    cache: false,
42
  };
43
  for (var key in options) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
44
    ajaxOptions[key] = options[key];
45
  }
46
  if (options.url && options.url.indexOf(inji.options.appRoot) !== 0) {
47
    ajaxOptions.url = inji.options.appRoot + (options.url.replace(/^\//g, ''));
48
  }
49
  if (typeof btn != 'undefined') {
50
    $(btn).data('loading-text', 'подождите');
51
    var btn = $(btn).button().button('loading');
52
  }
53
  var callback = null;
54
  if (typeof options.success != 'undefined') {
55
    callback = options.success;
56
  }
57
  ajaxOptions.success = function (data, textStatus, jqXHR) {
58
    if (typeof btn != 'undefined') {
59
      btn.button('reset');
60
    }
61
    if (ajaxOptions.dataType != 'json') {
62
      callback(data, textStatus, jqXHR);
63
    } else {
64
      if (data.success) {
65
        if (data.successMsg) {
66
          noty({text: data.successMsg, type: 'success', timeout: 3500, layout: 'center'});
67
        }
68
        if (typeof data.scripts == 'object') {
69
          inji.loaded = false;
70
          inji.onLoad(function () {
71
            inji.Server.runCommands(data.commands);
72
            if (callback !== null) {
73
              callback(data.content, textStatus, jqXHR)
74
            }
75
          });
76
          if (data.scripts.length > 0) {
77
            inji.loadScripts(data.scripts, 0);
78
          } else {
79
            inji.startCallbacks();
80
          }
81
        } else {
82
          inji.Server.runCommands(data.commands);
83
          if (callback !== null) {
84
            callback(data.content, textStatus, jqXHR);
85
          }
86
        }
87
      } else {
88
        inji.Server.runCommands(data.commands);
89
        noty({text: data.error, type: 'warning', timeout: 3500, layout: 'center'});
90
      }
91
    }
92
  }
93
  var errorCallback = null;
94
  if (typeof options.error != 'undefined') {
95
    errorCallback = options.error;
96
  }
97
  ajaxOptions.error = function (jqXHR, textStatus, errorThrown) {
98
    if (typeof btn != 'undefined') {
99
      btn.button('reset');
100
    }
101
    if (errorCallback != null) {
102
      errorCallback(jqXHR, textStatus, errorThrown);
103
    } else if (textStatus != 'abort') {
104
      noty({text: 'Во время запроса произошла ошибка: ' + textStatus, type: 'warning', timeout: 3500, layout: 'center'});
105
    }
106
  }
107
  return $.ajax(ajaxOptions);
108
};