mirror of
https://github.com/bigskysoftware/htmx.git
synced 2026-02-14 20:16:44 +00:00
experimental hx-on- attribute support
This commit is contained in:
parent
36780c034f
commit
56d5f38fc3
33
src/htmx.js
33
src/htmx.js
@ -1922,12 +1922,45 @@ return (function () {
|
||||
var kebabedEvent = makeEvent(kebabName, event.detail);
|
||||
eventResult = eventResult && elt.dispatchEvent(kebabedEvent)
|
||||
}
|
||||
triggerInlineHandlers(elt, kebabName);
|
||||
withExtensions(elt, function (extension) {
|
||||
eventResult = eventResult && (extension.onEvent(eventName, event) !== false)
|
||||
});
|
||||
return eventResult;
|
||||
}
|
||||
|
||||
function triggerInlineHandlers(elt, kebabbedEvent, noBubble) {
|
||||
var strippedEventName = kebabbedEvent.replace('htmx:', '').replace(":", "\\:").replace(".", "\\.");
|
||||
var hxOnAttributeNameForEvent = "hx-on-" + strippedEventName;
|
||||
var selector = "[" + hxOnAttributeNameForEvent + "]";
|
||||
if (noBubble) {
|
||||
if (matches(elt, selector)) {
|
||||
evalAttrValue(elt, hxOnAttributeNameForEvent);
|
||||
}
|
||||
} else {
|
||||
var closestMatch = closest(elt, selector);
|
||||
while (closestMatch != null) {
|
||||
// terminate simulated bubbling if handler returns false
|
||||
if(evalAttrValue(closestMatch, hxOnAttributeNameForEvent) === false) {
|
||||
return;
|
||||
}
|
||||
var parentOfMatch = parentElt(closestMatch);
|
||||
closestMatch = closest(parentOfMatch, selector);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function evalAttrValue(elt, attributeName) {
|
||||
var attrValue = elt.getAttribute(attributeName);
|
||||
try {
|
||||
return eval(attrValue)
|
||||
} catch(e) {
|
||||
console.error("Error evaluating " + attributeName + " on", elt, ":", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// History Support
|
||||
//====================================================================
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user