|
1
|
|
|
if (!isnode()) { |
|
2
|
|
|
/** |
|
3
|
|
|
* AJAX runner base |
|
4
|
|
|
*/ |
|
5
|
|
|
var AJAX = null; |
|
6
|
|
|
/** |
|
7
|
|
|
* Ajax dump base |
|
8
|
|
|
*/ |
|
9
|
|
|
var dumpAjax = false; |
|
10
|
|
|
/** |
|
11
|
|
|
* Ajax indicator base |
|
12
|
|
|
*/ |
|
13
|
|
|
var indicatorAjax = false; |
|
14
|
|
|
const ajaxIDLoader = |
|
15
|
|
|
"ajxLoader_" + |
|
16
|
|
|
Math.random().toString(36).substring(2) + |
|
17
|
|
|
Date.now().toString(36); |
|
18
|
|
|
if (!$("#" + ajaxIDLoader).length) { |
|
19
|
|
|
$("body").append( |
|
20
|
|
|
'<div id="' + |
|
21
|
|
|
ajaxIDLoader + |
|
22
|
|
|
'" style="position: fixed;z-index:9999;bottom:5px;left:5px;"><svg enable-background="new 0 0 40 40"height=40px id=loader-1 version=1.1 viewBox="0 0 40 40"width=40px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><path d="M20.201,5.169c-8.254,0-14.946,6.692-14.946,14.946c0,8.255,6.692,14.946,14.946,14.946\ |
|
23
|
|
|
s14.946-6.691,14.946-14.946C35.146,11.861,28.455,5.169,20.201,5.169z M20.201,31.749c-6.425,0-11.634-5.208-11.634-11.634\ |
|
24
|
|
|
c0-6.425,5.209-11.634,11.634-11.634c6.425,0,11.633,5.209,11.633,11.634C31.834,26.541,26.626,31.749,20.201,31.749z"fill=#000 opacity=0.2 /><path d="M26.013,10.047l1.654-2.866c-2.198-1.272-4.743-2.012-7.466-2.012h0v3.312h0\ |
|
25
|
|
|
C22.32,8.481,24.301,9.057,26.013,10.047z"fill=#000><animateTransform attributeName=transform attributeType=xml dur=0.5s from="0 20 20"repeatCount=indefinite to="360 20 20"type=rotate /></path></svg></div>' |
|
26
|
|
|
); |
|
27
|
|
|
$("#" + ajaxIDLoader).fadeOut("fast"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
jQuery.ajaxPrefilter(function (options: JQueryAjaxSettings) { |
|
31
|
|
|
indicatorAjax = |
|
32
|
|
|
typeof options.indicator == "boolean" && options.indicator === true; |
|
33
|
|
|
dumpAjax = typeof options.dump == "boolean" && options.dump === true; |
|
34
|
|
|
/** |
|
35
|
|
|
* Proxying begin |
|
36
|
|
|
*/ |
|
37
|
|
|
if (options.crossDomain && jQuery.support.cors) { |
|
38
|
|
|
var allowed = true; |
|
39
|
|
|
if (options.url.match(/\.html$/g)) { |
|
40
|
|
|
allowed = false; |
|
41
|
|
|
} |
|
42
|
|
|
if (options.url.match(/^\//)) { |
|
43
|
|
|
allowed = false; |
|
44
|
|
|
} |
|
45
|
|
|
if (options.hasOwnProperty("proxy") && !options.proxy) { |
|
46
|
|
|
allowed = false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (allowed) { |
|
50
|
|
|
var http = window.location.protocol === "http:" ? "http:" : "https:"; |
|
51
|
|
|
if (typeof options.proxy == "string") { |
|
52
|
|
|
options.url = |
|
53
|
|
|
options.proxy.replace(/\/{1,99}$/s, "") + "/" + options.url; |
|
54
|
|
|
} else { |
|
55
|
|
|
options.url = http + "//cors-anywhere.herokuapp.com/" + options.url; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
/* |
|
62
|
|
|
$(document).ajaxStart(function () { |
|
63
|
|
|
}); |
|
64
|
|
|
*/ |
|
65
|
|
|
|
|
66
|
|
|
$(document).ajaxError(function (event, jqXHR, settings, errorThrown) { |
|
67
|
|
|
var content_type = jqXHR.getResponseHeader("Content-Type"); |
|
68
|
|
|
if (typeof toastr != "undefined") { |
|
69
|
|
|
if (/json|text\/plain/s.test(content_type)) { |
|
70
|
|
|
toastr.error( |
|
71
|
|
|
"Request failed. (" + |
|
72
|
|
|
jqXHR.status + |
|
73
|
|
|
" " + |
|
74
|
|
|
jqXHR.statusText + |
|
75
|
|
|
") " + |
|
76
|
|
|
errorThrown, |
|
77
|
|
|
"Request Info" |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
$(document).ajaxSend(function (event, xhr, settings) { |
|
84
|
|
|
if (settings.hasOwnProperty("indicator") && settings.indicator) { |
|
85
|
|
|
$("#" + ajaxIDLoader).fadeIn("fast"); |
|
86
|
|
|
} |
|
87
|
|
|
if (dumpAjax) { |
|
88
|
|
|
toastr.info("Requesting...", "Request Info"); |
|
89
|
|
|
} |
|
90
|
|
|
if (!settings.hasOwnProperty("method")) { |
|
91
|
|
|
settings.method = "POST"; |
|
92
|
|
|
} |
|
93
|
|
|
}); |
|
94
|
|
|
|
|
95
|
|
|
$(document).ajaxComplete(function (event: any, xhr, settings) { |
|
96
|
|
|
if (settings.hasOwnProperty("indicator") && settings.indicator) { |
|
97
|
|
|
$("#" + ajaxIDLoader).fadeOut("fast"); |
|
98
|
|
|
} |
|
99
|
|
|
if (dumpAjax) { |
|
100
|
|
|
toastr.success("Request complete", "Request Info"); |
|
101
|
|
|
} |
|
102
|
|
|
AJAX = null; |
|
103
|
|
|
$("#" + ajaxIDLoader).fadeOut("slow"); |
|
104
|
|
|
|
|
105
|
|
|
var content_type = xhr.getResponseHeader("Content-Type"), |
|
106
|
|
|
res; |
|
107
|
|
|
if (xhr.hasOwnProperty("responseJSON")) { |
|
108
|
|
|
res = xhr.responseJSON; |
|
109
|
|
|
} else { |
|
110
|
|
|
res = xhr.responseText; |
|
111
|
|
|
if ( |
|
112
|
|
|
typeof res == "string" && |
|
113
|
|
|
!empty(res) && |
|
114
|
|
|
/json|text\/plain/s.test(content_type) |
|
115
|
|
|
) { |
|
116
|
|
|
//begin decode json |
|
117
|
|
|
if (isJSON(res)) { |
|
118
|
|
|
res = JSON.parse(res); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (typeof res == "object") { |
|
124
|
|
|
if (res.hasOwnProperty("redirect")) { |
|
125
|
|
|
this.location.replace(res.redirect); |
|
126
|
|
|
throw "Disabled"; |
|
127
|
|
|
} |
|
128
|
|
|
if (res.hasOwnProperty("reload")) { |
|
129
|
|
|
location.href = location.href; |
|
130
|
|
|
throw "Disabled"; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
}); |
|
134
|
|
|
|
|
135
|
|
|
$(document).ajaxSuccess(function (event, request, settings) { |
|
136
|
|
|
var res; |
|
137
|
|
|
var content_type = request.getResponseHeader("Content-Type"); |
|
138
|
|
|
if (request.hasOwnProperty("responseJSON")) { |
|
139
|
|
|
res = request.responseJSON; |
|
140
|
|
|
} else { |
|
141
|
|
|
res = request.responseText; |
|
142
|
|
|
} |
|
143
|
|
|
if ( |
|
144
|
|
|
typeof res == "string" && |
|
145
|
|
|
!empty(res) && |
|
146
|
|
|
/json|text\/plain/s.test(content_type) |
|
147
|
|
|
) { |
|
148
|
|
|
//begin decode json |
|
149
|
|
|
if (isJSON(res)) { |
|
150
|
|
|
res = JSON.parse(res); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
if ( |
|
154
|
|
|
typeof res == "object" && |
|
155
|
|
|
!settings.hasOwnProperty("silent") && |
|
156
|
|
|
typeof toastr != "undefined" && |
|
157
|
|
|
/json|javascript/s.test(content_type) |
|
158
|
|
|
) { |
|
159
|
|
|
var error = res.hasOwnProperty("error") && res.error ? true : false; |
|
160
|
|
|
var title = res.hasOwnProperty("title") ? res.title : "Unknown Title"; |
|
161
|
|
|
var msg = res.hasOwnProperty("message") ? res.message : "Unknown Error"; |
|
162
|
|
|
if (res.hasOwnProperty("error") && res.hasOwnProperty("message")) { |
|
163
|
|
|
if (error) { |
|
164
|
|
|
toastr.error(msg, title); |
|
165
|
|
|
} else { |
|
166
|
|
|
toastr.success(msg, title); |
|
167
|
|
|
} |
|
168
|
|
|
} else if (res.hasOwnProperty("message")) { |
|
169
|
|
|
toastr.info(msg, title); |
|
170
|
|
|
} |
|
171
|
|
|
if (res.hasOwnProperty("unauthorized")) { |
|
172
|
|
|
location.replace("/signin"); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
}); |
|
176
|
|
|
|
|
177
|
|
|
/* |
|
178
|
|
|
jQuery.ajaxPrefilter(function (options, originalOptions, jqXHR) { |
|
179
|
|
|
if (typeof options.data != 'undefined' && !jQuery.isEmptyObject(options.data)) { |
|
180
|
|
|
jqXHR.setRequestHeader('timeStamp', new Date().getTime().toString()); |
|
181
|
|
|
} |
|
182
|
|
|
}); |
|
183
|
|
|
*/ |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
function processAjaxForm(xhr: JQueryXHR, callback: string | Function) { |
|
187
|
|
|
//var content_type = typeof xhr.getResponseHeader == 'function' ? xhr.getResponseHeader('Content-Type') : null, res; |
|
188
|
|
|
console.log(getFuncName(), callback); |
|
189
|
|
|
var res: string | number | boolean; |
|
190
|
|
|
if (xhr.hasOwnProperty("responseJSON")) { |
|
191
|
|
|
res = xhr.responseJSON; |
|
192
|
|
|
} else if (xhr.hasOwnProperty("responseText")) { |
|
193
|
|
|
res = xhr.responseText; |
|
194
|
|
|
if (typeof res == "string" && !empty(res)) { |
|
195
|
|
|
//begin decode json |
|
196
|
|
|
if (isJSON(res)) { |
|
197
|
|
|
res = JSON.parse(res); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if (callback) { |
|
203
|
|
|
if (typeof callback == "function") { |
|
204
|
|
|
callback(res); |
|
205
|
|
|
} else if (typeof callback == "string") { |
|
206
|
|
|
call_user_func(callback, window, res); |
|
207
|
|
|
} else { |
|
208
|
|
|
console.error( |
|
209
|
|
|
"2nd parameters must be callback function, instead of " + |
|
210
|
|
|
typeof callback |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Custom ajax |
|
218
|
|
|
* @param settings ajax settings object |
|
219
|
|
|
*/ |
|
220
|
|
|
function ajx( |
|
221
|
|
|
settings: JQueryAjaxSettings, |
|
222
|
|
|
success: null | Function, |
|
223
|
|
|
failed: null | Function, |
|
224
|
|
|
complete: null | Function |
|
225
|
|
|
) { |
|
226
|
|
|
settings.headers = { |
|
227
|
|
|
"unique-id": getUID(), |
|
228
|
|
|
}; |
|
229
|
|
|
if (!settings.hasOwnProperty("indicator")) { |
|
230
|
|
|
settings.indicator = true; |
|
231
|
|
|
} |
|
232
|
|
|
if (!settings.hasOwnProperty("method")) { |
|
233
|
|
|
settings.method = "POST"; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $.ajax(settings) |
|
237
|
|
|
.done(function (data, textStatus, jqXHR) { |
|
238
|
|
|
processAjaxForm(jqXHR, success); |
|
239
|
|
|
}) |
|
240
|
|
|
.fail(function (jqXHR, textStatus, errorThrown) { |
|
241
|
|
|
processAjaxForm(jqXHR, failed); |
|
242
|
|
|
}) |
|
243
|
|
|
.always(function (jqXHR, textStatus, errorThrown) { |
|
244
|
|
|
processAjaxForm(jqXHR, complete); |
|
245
|
|
|
}); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Handling form with ajax |
|
250
|
|
|
* @requires data-success success function name |
|
251
|
|
|
* @requires data-error error function name |
|
252
|
|
|
* @requires data-complete complete function name |
|
253
|
|
|
*/ |
|
254
|
|
|
function AjaxForm() { |
|
255
|
|
|
$(document).on("submit", "form", function (e) { |
|
256
|
|
|
e.preventDefault(); |
|
257
|
|
|
var t = $(this); |
|
258
|
|
|
var sukses = t.data("success"); |
|
259
|
|
|
var err = t.data("error"); |
|
260
|
|
|
var complete = t.data("complete"); |
|
261
|
|
|
var targetURL = t.attr("action") || location.href; //fallback to current url |
|
262
|
|
|
//console.log(targetURL, sukses, err, complete); |
|
263
|
|
|
if (!targetURL) { |
|
264
|
|
|
console.error("Target url of this form not exists"); |
|
265
|
|
|
return; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
ajx( |
|
269
|
|
|
{ |
|
270
|
|
|
url: targetURL, |
|
271
|
|
|
method: t.attr("method") || "POST", |
|
272
|
|
|
data: t.serialize(), |
|
273
|
|
|
headers: { |
|
274
|
|
|
Accept: "application/json", |
|
275
|
|
|
guid: guid(), |
|
276
|
|
|
}, |
|
277
|
|
|
}, |
|
278
|
|
|
sukses, |
|
279
|
|
|
err, |
|
280
|
|
|
complete |
|
281
|
|
|
); |
|
282
|
|
|
}); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* process page asynchronously |
|
287
|
|
|
* @param source_cache url |
|
288
|
|
|
*/ |
|
289
|
|
|
function async_process(source_cache: string) { |
|
290
|
|
|
var xhr = new XMLHttpRequest(); |
|
291
|
|
|
$.ajax({ |
|
292
|
|
|
url: source_cache, |
|
293
|
|
|
method: "POST", |
|
294
|
|
|
silent: true, |
|
295
|
|
|
indicator: false, |
|
296
|
|
|
xhr: function () { |
|
297
|
|
|
return xhr; |
|
298
|
|
|
}, |
|
299
|
|
|
headers: { |
|
300
|
|
|
Pragma: "no-cache", |
|
301
|
|
|
"Cache-Control": "no-cache", |
|
302
|
|
|
"Refresh-Cache": "true", |
|
303
|
|
|
}, |
|
304
|
|
|
success: function (response) { |
|
305
|
|
|
$("html").html($("html", response).html()); |
|
306
|
|
|
console.log(xhr.responseURL); |
|
307
|
|
|
}, |
|
308
|
|
|
}); |
|
309
|
|
|
} |
|
310
|
|
|
|