-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
63 lines (56 loc) · 2.36 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(function (actions, $) {
function callAction(operation, inputs, onSuccess, onError) {
actionInputs = null;
if (typeof inputs === 'string' || inputs instanceof String)
actionInputs = inputs;
else if (typeof inputs === 'object' && inputs !== null)
actionInputs = JSON.stringify(inputs);
url = `/_api/mwo_powerpagesactions?$filter=createdon ge ${new Date().toISOString()} and mwo_operation eq '${operation}'`
if (actionInputs)
url += ` and mwo_inputs eq '${actionInputs}'`
safeAjax({
type: "GET",
url: url,
success: (data) => successFunction(data, onSuccess, onError),
error: (jqXHR) => errorFunction(jqXHR, onError)
});
}
actions.callAction = callAction;
function successFunction(data, onSuccess, onError) {
console.log(data);
var record = data.value[0];
var outputs = JSON.parse(record.mwo_outputs);
if (record.statuscode == 407770001)
onSuccess(outputs);
else
onError(outputs);
}
function errorFunction(jqXHR, onError) {
console.log(jqXHR);
if (jqXHR.status = 404)
onError({ message: "Actions endpoint not found, please check whether you have configured the Site Settings 'Webapi/mwo_powerpagesaction/enabled: True' and 'Webapi/mwo_powerpagesaction/fields: *'"})
else
onError(jqXHR.responseJSON);
}
function safeAjax(ajaxOptions) {
var deferredAjax = $.Deferred();
shell.getTokenDeferred().done(function (token) {
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
"__RequestVerificationToken": token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(function (data, textStatus, jqXHR) {
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
}).fail(deferredAjax.reject); //AJAX
}).fail(function () {
deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
});
return deferredAjax.promise();
}
})(window.actions = window.actions || {}, jQuery)