forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmx.js
More file actions
72 lines (72 loc) · 2.26 KB
/
Copy pathhtmx.js
File metadata and controls
72 lines (72 loc) · 2.26 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
(function () {
window.$x = window.$x || {
/**
* A global object to manage custom events and callbacks.
*
* @property {Function} ready - Registers a callback function to be executed
* once when the DOM is fully loaded or when an `htmx:load` event occurs.
*
* @function ready
* @param {Function} callback - The callback function to be executed.
* @param {String} selector - The selector to be used to check if the callback
* should be executed.
*/
ready: function (callback, selector) {
const f = function (evt) {
if (selector) {
if (document.querySelector(selector)) {
callback(evt);
}
} else {
callback(evt);
}
};
let boosted = false;
document.addEventListener("DOMContentLoaded", function (evt) {
f(evt);
});
document.addEventListener("htmx:load", function (evt) {
if (boosted) {
f(evt);
boosted = false;
}
});
document.addEventListener("htmx:beforeOnLoad", function (evt) {
// trigger ready function again when a boosted request is done
boosted = evt.detail.boosted;
});
},
/**
* The fetch function is a wrapper of native fetch with Hx-Trigger support
* like it in htmx requests.
*
* @function fetch
* @async
* @param {String|Request} input - The URL to be requested or the Request
* object.
* @param {Object} init - The options to be used for the request. See the
* {@link
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch|fetch}
* API.
* @returns {Promise<Response>} - The response of the request.
*/
fetch: async (...args) => {
const response = await fetch(...args);
if (!response.ok) {
const hx = response.headers.get("Hx-Trigger");
if (hx) {
try{
const d = JSON.parse(hx);
const keys = Object.keys(d);
for (const key of keys) {
window.dispatchEvent(new CustomEvent(key, { detail: d[key] }));
}
}catch(e){
// prevent invalid JSON from breaking the application
}
}
}
return response;
},
};
})();