htmx.org 1.9.10 → 2.0.0-alpha2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -25
- package/dist/htmx.amd.js +3919 -0
- package/dist/htmx.cjs.js +3917 -0
- package/dist/htmx.esm.js +3917 -0
- package/dist/htmx.js +3799 -3788
- package/dist/htmx.min.js +1 -1
- package/dist/htmx.min.js.gz +0 -0
- package/package.json +42 -12
- package/CHANGELOG.md +0 -474
- package/dist/ext/ajax-header.js +0 -7
- package/dist/ext/alpine-morph.js +0 -16
- package/dist/ext/class-tools.js +0 -92
- package/dist/ext/client-side-templates.js +0 -96
- package/dist/ext/debug.js +0 -11
- package/dist/ext/disable-element.js +0 -18
- package/dist/ext/event-header.js +0 -37
- package/dist/ext/head-support.js +0 -141
- package/dist/ext/include-vals.js +0 -24
- package/dist/ext/json-enc.js +0 -12
- package/dist/ext/loading-states.js +0 -183
- package/dist/ext/method-override.js +0 -11
- package/dist/ext/morphdom-swap.js +0 -17
- package/dist/ext/multi-swap.js +0 -45
- package/dist/ext/path-deps.js +0 -60
- package/dist/ext/path-params.js +0 -11
- package/dist/ext/preload.js +0 -147
- package/dist/ext/rails-method.js +0 -10
- package/dist/ext/remove-me.js +0 -27
- package/dist/ext/response-targets.js +0 -130
- package/dist/ext/restored.js +0 -15
- package/dist/ext/sse.js +0 -355
- package/dist/ext/ws.js +0 -477
- package/dist/htmx.d.ts +0 -450
package/dist/ext/preload.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
// This adds the "preload" extension to htmx. By default, this will
|
|
2
|
-
// preload the targets of any tags with `href` or `hx-get` attributes
|
|
3
|
-
// if they also have a `preload` attribute as well. See documentation
|
|
4
|
-
// for more details
|
|
5
|
-
htmx.defineExtension("preload", {
|
|
6
|
-
|
|
7
|
-
onEvent: function(name, event) {
|
|
8
|
-
|
|
9
|
-
// Only take actions on "htmx:afterProcessNode"
|
|
10
|
-
if (name !== "htmx:afterProcessNode") {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// SOME HELPER FUNCTIONS WE'LL NEED ALONG THE WAY
|
|
15
|
-
|
|
16
|
-
// attr gets the closest non-empty value from the attribute.
|
|
17
|
-
var attr = function(node, property) {
|
|
18
|
-
if (node == undefined) {return undefined;}
|
|
19
|
-
return node.getAttribute(property) || node.getAttribute("data-" + property) || attr(node.parentElement, property)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// load handles the actual HTTP fetch, and uses htmx.ajax in cases where we're
|
|
23
|
-
// preloading an htmx resource (this sends the same HTTP headers as a regular htmx request)
|
|
24
|
-
var load = function(node) {
|
|
25
|
-
|
|
26
|
-
// Called after a successful AJAX request, to mark the
|
|
27
|
-
// content as loaded (and prevent additional AJAX calls.)
|
|
28
|
-
var done = function(html) {
|
|
29
|
-
if (!node.preloadAlways) {
|
|
30
|
-
node.preloadState = "DONE"
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (attr(node, "preload-images") == "true") {
|
|
34
|
-
document.createElement("div").innerHTML = html // create and populate a node to load linked resources, too.
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return function() {
|
|
39
|
-
|
|
40
|
-
// If this value has already been loaded, then do not try again.
|
|
41
|
-
if (node.preloadState !== "READY") {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Special handling for HX-GET - use built-in htmx.ajax function
|
|
46
|
-
// so that headers match other htmx requests, then set
|
|
47
|
-
// node.preloadState = TRUE so that requests are not duplicated
|
|
48
|
-
// in the future
|
|
49
|
-
var hxGet = node.getAttribute("hx-get") || node.getAttribute("data-hx-get")
|
|
50
|
-
if (hxGet) {
|
|
51
|
-
htmx.ajax("GET", hxGet, {
|
|
52
|
-
source: node,
|
|
53
|
-
handler:function(elt, info) {
|
|
54
|
-
done(info.xhr.responseText);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Otherwise, perform a standard xhr request, then set
|
|
61
|
-
// node.preloadState = TRUE so that requests are not duplicated
|
|
62
|
-
// in the future.
|
|
63
|
-
if (node.getAttribute("href")) {
|
|
64
|
-
var r = new XMLHttpRequest();
|
|
65
|
-
r.open("GET", node.getAttribute("href"));
|
|
66
|
-
r.onload = function() {done(r.responseText);};
|
|
67
|
-
r.send();
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// This function processes a specific node and sets up event handlers.
|
|
74
|
-
// We'll search for nodes and use it below.
|
|
75
|
-
var init = function(node) {
|
|
76
|
-
|
|
77
|
-
// If this node DOES NOT include a "GET" transaction, then there's nothing to do here.
|
|
78
|
-
if (node.getAttribute("href") + node.getAttribute("hx-get") + node.getAttribute("data-hx-get") == "") {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Guarantee that we only initialize each node once.
|
|
83
|
-
if (node.preloadState !== undefined) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Get event name from config.
|
|
88
|
-
var on = attr(node, "preload") || "mousedown"
|
|
89
|
-
const always = on.indexOf("always") !== -1
|
|
90
|
-
if (always) {
|
|
91
|
-
on = on.replace('always', '').trim()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// FALL THROUGH to here means we need to add an EventListener
|
|
95
|
-
|
|
96
|
-
// Apply the listener to the node
|
|
97
|
-
node.addEventListener(on, function(evt) {
|
|
98
|
-
if (node.preloadState === "PAUSE") { // Only add one event listener
|
|
99
|
-
node.preloadState = "READY"; // Required for the `load` function to trigger
|
|
100
|
-
|
|
101
|
-
// Special handling for "mouseover" events. Wait 100ms before triggering load.
|
|
102
|
-
if (on === "mouseover") {
|
|
103
|
-
window.setTimeout(load(node), 100);
|
|
104
|
-
} else {
|
|
105
|
-
load(node)() // all other events trigger immediately.
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
// Special handling for certain built-in event handlers
|
|
111
|
-
switch (on) {
|
|
112
|
-
|
|
113
|
-
case "mouseover":
|
|
114
|
-
// Mirror `touchstart` events (fires immediately)
|
|
115
|
-
node.addEventListener("touchstart", load(node));
|
|
116
|
-
|
|
117
|
-
// WHhen the mouse leaves, immediately disable the preload
|
|
118
|
-
node.addEventListener("mouseout", function(evt) {
|
|
119
|
-
if ((evt.target === node) && (node.preloadState === "READY")) {
|
|
120
|
-
node.preloadState = "PAUSE";
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
break;
|
|
124
|
-
|
|
125
|
-
case "mousedown":
|
|
126
|
-
// Mirror `touchstart` events (fires immediately)
|
|
127
|
-
node.addEventListener("touchstart", load(node));
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Mark the node as ready to run.
|
|
132
|
-
node.preloadState = "PAUSE";
|
|
133
|
-
node.preloadAlways = always;
|
|
134
|
-
htmx.trigger(node, "preload:init") // This event can be used to load content immediately.
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Search for all child nodes that have a "preload" attribute
|
|
138
|
-
event.target.querySelectorAll("[preload]").forEach(function(node) {
|
|
139
|
-
|
|
140
|
-
// Initialize the node with the "preload" attribute
|
|
141
|
-
init(node)
|
|
142
|
-
|
|
143
|
-
// Initialize all child elements that are anchors or have `hx-get` (use with care)
|
|
144
|
-
node.querySelectorAll("a,[hx-get],[data-hx-get]").forEach(init)
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
})
|
package/dist/ext/rails-method.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
htmx.defineExtension('rails-method', {
|
|
2
|
-
onEvent: function (name, evt) {
|
|
3
|
-
if (name === "configRequest.htmx") {
|
|
4
|
-
var methodOverride = evt.detail.headers['X-HTTP-Method-Override'];
|
|
5
|
-
if (methodOverride) {
|
|
6
|
-
evt.detail.parameters['_method'] = methodOverride;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
});
|
package/dist/ext/remove-me.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
(function(){
|
|
2
|
-
function maybeRemoveMe(elt) {
|
|
3
|
-
var timing = elt.getAttribute("remove-me") || elt.getAttribute("data-remove-me");
|
|
4
|
-
if (timing) {
|
|
5
|
-
setTimeout(function () {
|
|
6
|
-
elt.parentElement.removeChild(elt);
|
|
7
|
-
}, htmx.parseInterval(timing));
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
htmx.defineExtension('remove-me', {
|
|
12
|
-
onEvent: function (name, evt) {
|
|
13
|
-
if (name === "htmx:afterProcessNode") {
|
|
14
|
-
var elt = evt.detail.elt;
|
|
15
|
-
if (elt.getAttribute) {
|
|
16
|
-
maybeRemoveMe(elt);
|
|
17
|
-
if (elt.querySelectorAll) {
|
|
18
|
-
var children = elt.querySelectorAll("[remove-me], [data-remove-me]");
|
|
19
|
-
for (var i = 0; i < children.length; i++) {
|
|
20
|
-
maybeRemoveMe(children[i]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
})();
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
(function(){
|
|
2
|
-
|
|
3
|
-
/** @type {import("../htmx").HtmxInternalApi} */
|
|
4
|
-
var api;
|
|
5
|
-
|
|
6
|
-
var attrPrefix = 'hx-target-';
|
|
7
|
-
|
|
8
|
-
// IE11 doesn't support string.startsWith
|
|
9
|
-
function startsWith(str, prefix) {
|
|
10
|
-
return str.substring(0, prefix.length) === prefix
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @param {HTMLElement} elt
|
|
15
|
-
* @param {number} respCode
|
|
16
|
-
* @returns {HTMLElement | null}
|
|
17
|
-
*/
|
|
18
|
-
function getRespCodeTarget(elt, respCodeNumber) {
|
|
19
|
-
if (!elt || !respCodeNumber) return null;
|
|
20
|
-
|
|
21
|
-
var respCode = respCodeNumber.toString();
|
|
22
|
-
|
|
23
|
-
// '*' is the original syntax, as the obvious character for a wildcard.
|
|
24
|
-
// The 'x' alternative was added for maximum compatibility with HTML
|
|
25
|
-
// templating engines, due to ambiguity around which characters are
|
|
26
|
-
// supported in HTML attributes.
|
|
27
|
-
//
|
|
28
|
-
// Start with the most specific possible attribute and generalize from
|
|
29
|
-
// there.
|
|
30
|
-
var attrPossibilities = [
|
|
31
|
-
respCode,
|
|
32
|
-
|
|
33
|
-
respCode.substr(0, 2) + '*',
|
|
34
|
-
respCode.substr(0, 2) + 'x',
|
|
35
|
-
|
|
36
|
-
respCode.substr(0, 1) + '*',
|
|
37
|
-
respCode.substr(0, 1) + 'x',
|
|
38
|
-
respCode.substr(0, 1) + '**',
|
|
39
|
-
respCode.substr(0, 1) + 'xx',
|
|
40
|
-
|
|
41
|
-
'*',
|
|
42
|
-
'x',
|
|
43
|
-
'***',
|
|
44
|
-
'xxx',
|
|
45
|
-
];
|
|
46
|
-
if (startsWith(respCode, '4') || startsWith(respCode, '5')) {
|
|
47
|
-
attrPossibilities.push('error');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (var i = 0; i < attrPossibilities.length; i++) {
|
|
51
|
-
var attr = attrPrefix + attrPossibilities[i];
|
|
52
|
-
var attrValue = api.getClosestAttributeValue(elt, attr);
|
|
53
|
-
if (attrValue) {
|
|
54
|
-
if (attrValue === "this") {
|
|
55
|
-
return api.findThisElement(elt, attr);
|
|
56
|
-
} else {
|
|
57
|
-
return api.querySelectorExt(elt, attrValue);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** @param {Event} evt */
|
|
66
|
-
function handleErrorFlag(evt) {
|
|
67
|
-
if (evt.detail.isError) {
|
|
68
|
-
if (htmx.config.responseTargetUnsetsError) {
|
|
69
|
-
evt.detail.isError = false;
|
|
70
|
-
}
|
|
71
|
-
} else if (htmx.config.responseTargetSetsError) {
|
|
72
|
-
evt.detail.isError = true;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
htmx.defineExtension('response-targets', {
|
|
77
|
-
|
|
78
|
-
/** @param {import("../htmx").HtmxInternalApi} apiRef */
|
|
79
|
-
init: function (apiRef) {
|
|
80
|
-
api = apiRef;
|
|
81
|
-
|
|
82
|
-
if (htmx.config.responseTargetUnsetsError === undefined) {
|
|
83
|
-
htmx.config.responseTargetUnsetsError = true;
|
|
84
|
-
}
|
|
85
|
-
if (htmx.config.responseTargetSetsError === undefined) {
|
|
86
|
-
htmx.config.responseTargetSetsError = false;
|
|
87
|
-
}
|
|
88
|
-
if (htmx.config.responseTargetPrefersExisting === undefined) {
|
|
89
|
-
htmx.config.responseTargetPrefersExisting = false;
|
|
90
|
-
}
|
|
91
|
-
if (htmx.config.responseTargetPrefersRetargetHeader === undefined) {
|
|
92
|
-
htmx.config.responseTargetPrefersRetargetHeader = true;
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @param {string} name
|
|
98
|
-
* @param {Event} evt
|
|
99
|
-
*/
|
|
100
|
-
onEvent: function (name, evt) {
|
|
101
|
-
if (name === "htmx:beforeSwap" &&
|
|
102
|
-
evt.detail.xhr &&
|
|
103
|
-
evt.detail.xhr.status !== 200) {
|
|
104
|
-
if (evt.detail.target) {
|
|
105
|
-
if (htmx.config.responseTargetPrefersExisting) {
|
|
106
|
-
evt.detail.shouldSwap = true;
|
|
107
|
-
handleErrorFlag(evt);
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
if (htmx.config.responseTargetPrefersRetargetHeader &&
|
|
111
|
-
evt.detail.xhr.getAllResponseHeaders().match(/HX-Retarget:/i)) {
|
|
112
|
-
evt.detail.shouldSwap = true;
|
|
113
|
-
handleErrorFlag(evt);
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (!evt.detail.requestConfig) {
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
var target = getRespCodeTarget(evt.detail.requestConfig.elt, evt.detail.xhr.status);
|
|
121
|
-
if (target) {
|
|
122
|
-
handleErrorFlag(evt);
|
|
123
|
-
evt.detail.shouldSwap = true;
|
|
124
|
-
evt.detail.target = target;
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
})();
|
package/dist/ext/restored.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
htmx.defineExtension('restored', {
|
|
2
|
-
onEvent : function(name, evt) {
|
|
3
|
-
if (name === 'htmx:restored'){
|
|
4
|
-
var restoredElts = evt.detail.document.querySelectorAll(
|
|
5
|
-
"[hx-trigger='restored'],[data-hx-trigger='restored']"
|
|
6
|
-
);
|
|
7
|
-
// need a better way to do this, would prefer to just trigger from evt.detail.elt
|
|
8
|
-
var foundElt = Array.from(restoredElts).find(
|
|
9
|
-
(x) => (x.outerHTML === evt.detail.elt.outerHTML)
|
|
10
|
-
);
|
|
11
|
-
var restoredEvent = evt.detail.triggerEvent(foundElt, 'restored');
|
|
12
|
-
}
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
})
|
package/dist/ext/sse.js
DELETED
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Server Sent Events Extension
|
|
3
|
-
============================
|
|
4
|
-
This extension adds support for Server Sent Events to htmx. See /www/extensions/sse.md for usage instructions.
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
(function() {
|
|
9
|
-
|
|
10
|
-
/** @type {import("../htmx").HtmxInternalApi} */
|
|
11
|
-
var api;
|
|
12
|
-
|
|
13
|
-
htmx.defineExtension("sse", {
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Init saves the provided reference to the internal HTMX API.
|
|
17
|
-
*
|
|
18
|
-
* @param {import("../htmx").HtmxInternalApi} api
|
|
19
|
-
* @returns void
|
|
20
|
-
*/
|
|
21
|
-
init: function(apiRef) {
|
|
22
|
-
// store a reference to the internal API.
|
|
23
|
-
api = apiRef;
|
|
24
|
-
|
|
25
|
-
// set a function in the public API for creating new EventSource objects
|
|
26
|
-
if (htmx.createEventSource == undefined) {
|
|
27
|
-
htmx.createEventSource = createEventSource;
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* onEvent handles all events passed to this extension.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} name
|
|
35
|
-
* @param {Event} evt
|
|
36
|
-
* @returns void
|
|
37
|
-
*/
|
|
38
|
-
onEvent: function(name, evt) {
|
|
39
|
-
|
|
40
|
-
switch (name) {
|
|
41
|
-
|
|
42
|
-
case "htmx:beforeCleanupElement":
|
|
43
|
-
var internalData = api.getInternalData(evt.target)
|
|
44
|
-
// Try to remove remove an EventSource when elements are removed
|
|
45
|
-
if (internalData.sseEventSource) {
|
|
46
|
-
internalData.sseEventSource.close();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return;
|
|
50
|
-
|
|
51
|
-
// Try to create EventSources when elements are processed
|
|
52
|
-
case "htmx:afterProcessNode":
|
|
53
|
-
ensureEventSourceOnElement(evt.target);
|
|
54
|
-
registerSSE(evt.target);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
///////////////////////////////////////////////
|
|
60
|
-
// HELPER FUNCTIONS
|
|
61
|
-
///////////////////////////////////////////////
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* createEventSource is the default method for creating new EventSource objects.
|
|
66
|
-
* it is hoisted into htmx.config.createEventSource to be overridden by the user, if needed.
|
|
67
|
-
*
|
|
68
|
-
* @param {string} url
|
|
69
|
-
* @returns EventSource
|
|
70
|
-
*/
|
|
71
|
-
function createEventSource(url) {
|
|
72
|
-
return new EventSource(url, { withCredentials: true });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function splitOnWhitespace(trigger) {
|
|
76
|
-
return trigger.trim().split(/\s+/);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function getLegacySSEURL(elt) {
|
|
80
|
-
var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
|
|
81
|
-
if (legacySSEValue) {
|
|
82
|
-
var values = splitOnWhitespace(legacySSEValue);
|
|
83
|
-
for (var i = 0; i < values.length; i++) {
|
|
84
|
-
var value = values[i].split(/:(.+)/);
|
|
85
|
-
if (value[0] === "connect") {
|
|
86
|
-
return value[1];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function getLegacySSESwaps(elt) {
|
|
93
|
-
var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
|
|
94
|
-
var returnArr = [];
|
|
95
|
-
if (legacySSEValue != null) {
|
|
96
|
-
var values = splitOnWhitespace(legacySSEValue);
|
|
97
|
-
for (var i = 0; i < values.length; i++) {
|
|
98
|
-
var value = values[i].split(/:(.+)/);
|
|
99
|
-
if (value[0] === "swap") {
|
|
100
|
-
returnArr.push(value[1]);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return returnArr;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* registerSSE looks for attributes that can contain sse events, right
|
|
109
|
-
* now hx-trigger and sse-swap and adds listeners based on these attributes too
|
|
110
|
-
* the closest event source
|
|
111
|
-
*
|
|
112
|
-
* @param {HTMLElement} elt
|
|
113
|
-
*/
|
|
114
|
-
function registerSSE(elt) {
|
|
115
|
-
// Find closest existing event source
|
|
116
|
-
var sourceElement = api.getClosestMatch(elt, hasEventSource);
|
|
117
|
-
if (sourceElement == null) {
|
|
118
|
-
// api.triggerErrorEvent(elt, "htmx:noSSESourceError")
|
|
119
|
-
return null; // no eventsource in parentage, orphaned element
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Set internalData and source
|
|
123
|
-
var internalData = api.getInternalData(sourceElement);
|
|
124
|
-
var source = internalData.sseEventSource;
|
|
125
|
-
|
|
126
|
-
// Add message handlers for every `sse-swap` attribute
|
|
127
|
-
queryAttributeOnThisOrChildren(elt, "sse-swap").forEach(function(child) {
|
|
128
|
-
|
|
129
|
-
var sseSwapAttr = api.getAttributeValue(child, "sse-swap");
|
|
130
|
-
if (sseSwapAttr) {
|
|
131
|
-
var sseEventNames = sseSwapAttr.split(",");
|
|
132
|
-
} else {
|
|
133
|
-
var sseEventNames = getLegacySSESwaps(child);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
for (var i = 0; i < sseEventNames.length; i++) {
|
|
137
|
-
var sseEventName = sseEventNames[i].trim();
|
|
138
|
-
var listener = function(event) {
|
|
139
|
-
|
|
140
|
-
// If the source is missing then close SSE
|
|
141
|
-
if (maybeCloseSSESource(sourceElement)) {
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// If the body no longer contains the element, remove the listener
|
|
146
|
-
if (!api.bodyContains(child)) {
|
|
147
|
-
source.removeEventListener(sseEventName, listener);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// swap the response into the DOM and trigger a notification
|
|
151
|
-
swap(child, event.data);
|
|
152
|
-
api.triggerEvent(elt, "htmx:sseMessage", event);
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
// Register the new listener
|
|
156
|
-
api.getInternalData(child).sseEventListener = listener;
|
|
157
|
-
source.addEventListener(sseEventName, listener);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// Add message handlers for every `hx-trigger="sse:*"` attribute
|
|
162
|
-
queryAttributeOnThisOrChildren(elt, "hx-trigger").forEach(function(child) {
|
|
163
|
-
|
|
164
|
-
var sseEventName = api.getAttributeValue(child, "hx-trigger");
|
|
165
|
-
if (sseEventName == null) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Only process hx-triggers for events with the "sse:" prefix
|
|
170
|
-
if (sseEventName.slice(0, 4) != "sse:") {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// remove the sse: prefix from here on out
|
|
175
|
-
sseEventName = sseEventName.substr(4);
|
|
176
|
-
|
|
177
|
-
var listener = function() {
|
|
178
|
-
if (maybeCloseSSESource(sourceElement)) {
|
|
179
|
-
return
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (!api.bodyContains(child)) {
|
|
183
|
-
source.removeEventListener(sseEventName, listener);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* ensureEventSourceOnElement creates a new EventSource connection on the provided element.
|
|
191
|
-
* If a usable EventSource already exists, then it is returned. If not, then a new EventSource
|
|
192
|
-
* is created and stored in the element's internalData.
|
|
193
|
-
* @param {HTMLElement} elt
|
|
194
|
-
* @param {number} retryCount
|
|
195
|
-
* @returns {EventSource | null}
|
|
196
|
-
*/
|
|
197
|
-
function ensureEventSourceOnElement(elt, retryCount) {
|
|
198
|
-
|
|
199
|
-
if (elt == null) {
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// handle extension source creation attribute
|
|
204
|
-
queryAttributeOnThisOrChildren(elt, "sse-connect").forEach(function(child) {
|
|
205
|
-
var sseURL = api.getAttributeValue(child, "sse-connect");
|
|
206
|
-
if (sseURL == null) {
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
ensureEventSource(child, sseURL, retryCount);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
// handle legacy sse, remove for HTMX2
|
|
214
|
-
queryAttributeOnThisOrChildren(elt, "hx-sse").forEach(function(child) {
|
|
215
|
-
var sseURL = getLegacySSEURL(child);
|
|
216
|
-
if (sseURL == null) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
ensureEventSource(child, sseURL, retryCount);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function ensureEventSource(elt, url, retryCount) {
|
|
226
|
-
var source = htmx.createEventSource(url);
|
|
227
|
-
|
|
228
|
-
source.onerror = function(err) {
|
|
229
|
-
|
|
230
|
-
// Log an error event
|
|
231
|
-
api.triggerErrorEvent(elt, "htmx:sseError", { error: err, source: source });
|
|
232
|
-
|
|
233
|
-
// If parent no longer exists in the document, then clean up this EventSource
|
|
234
|
-
if (maybeCloseSSESource(elt)) {
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Otherwise, try to reconnect the EventSource
|
|
239
|
-
if (source.readyState === EventSource.CLOSED) {
|
|
240
|
-
retryCount = retryCount || 0;
|
|
241
|
-
var timeout = Math.random() * (2 ^ retryCount) * 500;
|
|
242
|
-
window.setTimeout(function() {
|
|
243
|
-
ensureEventSourceOnElement(elt, Math.min(7, retryCount + 1));
|
|
244
|
-
}, timeout);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
source.onopen = function(evt) {
|
|
249
|
-
api.triggerEvent(elt, "htmx:sseOpen", { source: source });
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
api.getInternalData(elt).sseEventSource = source;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* maybeCloseSSESource confirms that the parent element still exists.
|
|
257
|
-
* If not, then any associated SSE source is closed and the function returns true.
|
|
258
|
-
*
|
|
259
|
-
* @param {HTMLElement} elt
|
|
260
|
-
* @returns boolean
|
|
261
|
-
*/
|
|
262
|
-
function maybeCloseSSESource(elt) {
|
|
263
|
-
if (!api.bodyContains(elt)) {
|
|
264
|
-
var source = api.getInternalData(elt).sseEventSource;
|
|
265
|
-
if (source != undefined) {
|
|
266
|
-
source.close();
|
|
267
|
-
// source = null
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* queryAttributeOnThisOrChildren returns all nodes that contain the requested attributeName, INCLUDING THE PROVIDED ROOT ELEMENT.
|
|
276
|
-
*
|
|
277
|
-
* @param {HTMLElement} elt
|
|
278
|
-
* @param {string} attributeName
|
|
279
|
-
*/
|
|
280
|
-
function queryAttributeOnThisOrChildren(elt, attributeName) {
|
|
281
|
-
|
|
282
|
-
var result = [];
|
|
283
|
-
|
|
284
|
-
// If the parent element also contains the requested attribute, then add it to the results too.
|
|
285
|
-
if (api.hasAttribute(elt, attributeName)) {
|
|
286
|
-
result.push(elt);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Search all child nodes that match the requested attribute
|
|
290
|
-
elt.querySelectorAll("[" + attributeName + "], [data-" + attributeName + "]").forEach(function(node) {
|
|
291
|
-
result.push(node);
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
return result;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* @param {HTMLElement} elt
|
|
299
|
-
* @param {string} content
|
|
300
|
-
*/
|
|
301
|
-
function swap(elt, content) {
|
|
302
|
-
|
|
303
|
-
api.withExtensions(elt, function(extension) {
|
|
304
|
-
content = extension.transformResponse(content, null, elt);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
var swapSpec = api.getSwapSpecification(elt);
|
|
308
|
-
var target = api.getTarget(elt);
|
|
309
|
-
var settleInfo = api.makeSettleInfo(elt);
|
|
310
|
-
|
|
311
|
-
api.selectAndSwap(swapSpec.swapStyle, target, elt, content, settleInfo);
|
|
312
|
-
|
|
313
|
-
settleInfo.elts.forEach(function(elt) {
|
|
314
|
-
if (elt.classList) {
|
|
315
|
-
elt.classList.add(htmx.config.settlingClass);
|
|
316
|
-
}
|
|
317
|
-
api.triggerEvent(elt, 'htmx:beforeSettle');
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
// Handle settle tasks (with delay if requested)
|
|
321
|
-
if (swapSpec.settleDelay > 0) {
|
|
322
|
-
setTimeout(doSettle(settleInfo), swapSpec.settleDelay);
|
|
323
|
-
} else {
|
|
324
|
-
doSettle(settleInfo)();
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* doSettle mirrors much of the functionality in htmx that
|
|
330
|
-
* settles elements after their content has been swapped.
|
|
331
|
-
* TODO: this should be published by htmx, and not duplicated here
|
|
332
|
-
* @param {import("../htmx").HtmxSettleInfo} settleInfo
|
|
333
|
-
* @returns () => void
|
|
334
|
-
*/
|
|
335
|
-
function doSettle(settleInfo) {
|
|
336
|
-
|
|
337
|
-
return function() {
|
|
338
|
-
settleInfo.tasks.forEach(function(task) {
|
|
339
|
-
task.call();
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
settleInfo.elts.forEach(function(elt) {
|
|
343
|
-
if (elt.classList) {
|
|
344
|
-
elt.classList.remove(htmx.config.settlingClass);
|
|
345
|
-
}
|
|
346
|
-
api.triggerEvent(elt, 'htmx:afterSettle');
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function hasEventSource(node) {
|
|
352
|
-
return api.getInternalData(node).sseEventSource != null;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
})();
|