-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathevent-header.js
37 lines (36 loc) · 995 Bytes
/
event-header.js
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
(function() {
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)
}
}
}
})
})()