diff options
Diffstat (limited to 'static/js/ext/event-header.js')
-rw-r--r-- | static/js/ext/event-header.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/static/js/ext/event-header.js b/static/js/ext/event-header.js new file mode 100644 index 0000000..0ccb7de --- /dev/null +++ b/static/js/ext/event-header.js @@ -0,0 +1,41 @@ +(function(){ + if (htmx.version && !htmx.version.startsWith("1.")) { + console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version + + ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions") + } + function stringifyEvent(event) { + var obj = {}; + for (var key in event) { + obj[key] = event[key]; + } + return JSON.stringify(obj, function(key, value){ + if(value instanceof Node){ + var nodeRep = value.tagName; + if (nodeRep) { + nodeRep = nodeRep.toLowerCase(); + if(value.id){ + nodeRep += "#" + value.id; + } + if(value.classList && value.classList.length){ + nodeRep += "." + value.classList.toString().replace(" ", ".") + } + return nodeRep; + } else { + return "Node" + } + } + if (value instanceof Window) return 'Window'; + return value; + }); + } + + htmx.defineExtension('event-header', { + onEvent: function (name, evt) { + if (name === "htmx:configRequest") { + if (evt.detail.triggeringEvent) { + evt.detail.headers['Triggering-Event'] = stringifyEvent(evt.detail.triggeringEvent); + } + } + } + }); +})(); |