-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathajax.js
More file actions
156 lines (139 loc) · 4.56 KB
/
Copy pathajax.js
File metadata and controls
156 lines (139 loc) · 4.56 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Patterns ajax - AJAX injection for forms and anchors
*
* Copyright 2012-2013 Florian Friesdorf
* Copyright 2012-2013 Marko Durkovic
*/
import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
import $ from "jquery";
import logging from "../../core/logging";
import Parser from "../../core/parser";
import registry from "../../core/registry";
const log = logging.getLogger("pat.ajax");
export const parser = new Parser("ajax");
parser.addArgument("accept", "text/html");
parser.addArgument("url", function ($el) {
const el = $el[0];
const value =
el.tagName === "A"
? el.getAttribute("href")
: el.tagName === "FORM"
? el.getAttribute("action")
: "";
return (value || "").split("#")[0];
});
parser.addArgument("browser-cache", "no-cache", ["cache", "no-cache"]); // Cache ajax requests
const xhrCount = {};
xhrCount.get = function (a) {
return this[a] !== undefined ? this[a] : 0;
};
xhrCount.inc = function (a) {
this[a] = this.get(a) + 1;
return this.get(a);
};
const _ = {
name: "ajax",
trigger: ".pat-ajax",
parser: parser,
init($el) {
$el.off(".pat-ajax");
$el.filter("a").on("click.pat-ajax", _.onTriggerEvents);
$el.filter("form")
.on("submit.pat-ajax", _.onTriggerEvents)
.on("click.pat-ajax", "[type=submit]", _.onClickSubmit);
$el.filter(":not(form,a)").each(function () {
log.warn("Unsupported element:", this);
});
return $el;
},
destroy($el) {
$el.off(".pat-ajax");
},
onClickSubmit(event) {
const el = event.submitter || event.target;
const form = el.form;
const data = {};
if (el.name) {
data[el.name] = el.value;
}
$(form).data("pat-ajax.clicked-data", data);
},
onTriggerEvents(event) {
if (event) {
event.preventDefault();
}
_.request($(this));
},
request($el, opts) {
return $el.each(function () {
_._request($(this), opts);
});
},
_request($el, opts) {
const cfg = _.parser.parse($el, opts);
const onError = function (jqxhr, status, error) {
// error can also stem from a javascript
// exception, not only errors described in the
// jqxhr.
log.error("load error for " + cfg.url + ":", error, jqxhr);
$el.trigger({
type: "pat-ajax-error",
jqxhr: jqxhr,
});
};
const seqNumber = xhrCount.inc(cfg.url);
let native_xhr;
const onSuccess = function (data, status, jqxhr) {
log.debug("success: jqxhr:", jqxhr);
if (seqNumber === xhrCount.get(cfg.url)) {
// if this url is requested multiple time, only return the last result
$el.trigger({
type: "pat-ajax-success",
jqxhr: {"url": cfg.url, ...jqxhr, "responseURL": native_xhr?.responseURL || cfg.url},
});
} else {
// ignore
}
};
const temp = $el.data("pat-ajax.clicked-data");
const clickedData = temp ? $.param(temp) : "";
const args = {
context: $el,
data: [$el.serialize(), clickedData].filter(Boolean).join("&"),
headers: {},
url: cfg.url,
method: $el.attr("method") ? $el.attr("method") : "GET",
cache: cfg.browserCache === "cache" ? true : false,
xhr: function () {
native_xhr = $.ajaxSettings.xhr();
return native_xhr;
},
};
if (cfg.accept) {
args.headers.Accept = cfg.accept;
}
if (
$el.is("form") &&
$el.attr("method") &&
$el.attr("method").toUpperCase() == "POST"
) {
const formdata = new FormData($el[0]);
for (const key in temp) {
formdata.append(key, temp[key]);
}
args["method"] = "POST";
args["data"] = formdata;
args["cache"] = false;
args["contentType"] = false;
args["processData"] = false;
args["type"] = "POST";
}
$el.removeData("pat-ajax.clicked-data");
log.debug("request:", args, $el[0]);
// Make it happen
const ajax_deferred = $.ajax(args);
if (ajax_deferred) ajax_deferred.done(onSuccess).fail(onError);
},
};
registry.register(_);
export default _;