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.init = function () { |
16
|
|
|
$.each($('form[csrf]'), function () { |
17
|
|
|
var form = $(this); |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
form.submit(function (ev) { |
21
|
|
|
if (!form.find('[name="csrfToken"]').val()) { |
22
|
|
|
inji.Server.CSRF(function (key, token) { |
23
|
|
|
form.append('<input type="hidden" name ="csrfKey" value="' + key + '" />'); |
24
|
|
|
form.append('<input type="hidden" name ="csrfToken" value="' + token + '" />'); |
25
|
|
|
form.submit(); |
26
|
|
|
}, undefined, form.find('button')); |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
}) |
32
|
|
|
}; |
33
|
|
|
Server.prototype.runCommands = function (commands) { |
34
|
|
|
for (var key in commands) { |
|
|
|
|
35
|
|
|
var command = commands[key]; |
36
|
|
|
var callPath = command.call.split('.'); |
37
|
|
|
var curPath = window; |
38
|
|
|
for (var keyPath in callPath) { |
|
|
|
|
39
|
|
|
if (typeof curPath[callPath[keyPath]] == 'undefined') { |
40
|
|
|
console.log('undefined call path ' + callPath[keyPath] + ' in ' + command.call); |
|
|
|
|
41
|
|
|
curPath = null; |
42
|
|
|
break; |
43
|
|
|
} |
44
|
|
|
curPath = curPath[callPath[keyPath]]; |
45
|
|
|
} |
46
|
|
|
if (curPath !== null) { |
47
|
|
|
curPath.apply(null, command.params); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
}; |
51
|
|
|
Server.prototype.CSRF = function (callback, err, btn) { |
52
|
|
|
var key = inji.randomString(); |
53
|
|
|
this.request({ |
54
|
|
|
url: '/server/csrf/' + key, |
55
|
|
|
success: function (token) { |
56
|
|
|
if (callback !== undefined) { |
57
|
|
|
callback(key, token); |
58
|
|
|
} |
59
|
|
|
}, |
60
|
|
|
error: function (e) { |
61
|
|
|
if (err !== undefined) { |
62
|
|
|
err(e); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
}, btn); |
66
|
|
|
}; |
67
|
|
|
Server.prototype.request = function (options, btn) { |
68
|
|
|
var ajaxOptions = { |
69
|
|
|
url: '', |
70
|
|
|
type: 'GET', |
71
|
|
|
dataType: 'json', |
72
|
|
|
data: {}, |
73
|
|
|
async: true, |
74
|
|
|
contentType: false, |
75
|
|
|
cache: false, |
76
|
|
|
}; |
77
|
|
|
for (var key in options) { |
|
|
|
|
78
|
|
|
ajaxOptions[key] = options[key]; |
79
|
|
|
} |
80
|
|
|
if (options.url && options.url.indexOf('http:') !== 0 && options.url.indexOf('https:') !== 0 && options.url.indexOf(inji.options.appRoot) !== 0) { |
81
|
|
|
ajaxOptions.url = inji.options.appRoot + (options.url.replace(/^\//g, '')); |
82
|
|
|
} |
83
|
|
|
if (typeof btn != 'undefined') { |
84
|
|
|
if (!$(btn).data('loading-text')) { |
85
|
|
|
$(btn).data('loading-text', 'подождите'); |
86
|
|
|
} |
87
|
|
|
var btn = $(btn).button().button('loading'); |
88
|
|
|
} |
89
|
|
|
var callback = null; |
90
|
|
|
if (typeof options.success != 'undefined') { |
91
|
|
|
callback = options.success; |
92
|
|
|
} |
93
|
|
|
ajaxOptions.success = function (data, textStatus, jqXHR) { |
94
|
|
|
if (typeof btn != 'undefined') { |
95
|
|
|
btn.button('reset'); |
96
|
|
|
} |
97
|
|
|
if (ajaxOptions.dataType != 'json') { |
98
|
|
|
callback(data, textStatus, jqXHR); |
99
|
|
|
} else { |
100
|
|
|
if (data.success) { |
101
|
|
|
if (data.successMsg) { |
102
|
|
|
noty({text: data.successMsg, type: 'success', timeout: 3500, layout: 'center'}); |
103
|
|
|
} |
104
|
|
|
if (typeof data.scripts == 'object') { |
105
|
|
|
inji.loaded = false; |
106
|
|
|
inji.onLoad(function () { |
107
|
|
|
inji.Server.runCommands(data.commands); |
108
|
|
|
if (callback !== null) { |
109
|
|
|
callback(data.content, textStatus, jqXHR) |
110
|
|
|
} |
111
|
|
|
}); |
112
|
|
|
if (data.scripts.length > 0) { |
113
|
|
|
inji.loadScripts(data.scripts, 0); |
114
|
|
|
} else { |
115
|
|
|
inji.startCallbacks(); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
inji.Server.runCommands(data.commands); |
119
|
|
|
if (callback !== null) { |
120
|
|
|
callback(data.content, textStatus, jqXHR); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
inji.Server.runCommands(data.commands); |
125
|
|
|
noty({text: data.error, type: 'warning', timeout: 3500, layout: 'center'}); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
var errorCallback = null; |
130
|
|
|
if (typeof options.error != 'undefined') { |
131
|
|
|
errorCallback = options.error; |
132
|
|
|
} |
133
|
|
|
ajaxOptions.error = function (jqXHR, textStatus, errorThrown) { |
134
|
|
|
if (typeof btn != 'undefined') { |
135
|
|
|
btn.button('reset'); |
136
|
|
|
} |
137
|
|
|
if (errorCallback != null) { |
138
|
|
|
errorCallback(jqXHR, textStatus, errorThrown); |
139
|
|
|
} else if (textStatus != 'abort') { |
140
|
|
|
noty({ |
141
|
|
|
text: 'Во время запроса произошла ошибка: ' + textStatus, |
142
|
|
|
type: 'warning', |
143
|
|
|
timeout: 3500, |
144
|
|
|
layout: 'center' |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return $.ajax(ajaxOptions); |
149
|
|
|
}; |
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: