-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog-tag-confirm.js
40 lines (34 loc) · 1.19 KB
/
dialog-tag-confirm.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
38
39
40
htmx.defineExtension('dialog-tag-confirm', {
onEvent: function(name, evt) {
if (name !== 'htmx:confirm') return
if (!evt.detail?.question) return
evt.preventDefault()
/**
* @type {HTMLElement | null}
*/
const sourceElement = evt.detail.elt
const templateId = sourceElement.dataset.confirmTemplate ?? '#dialog-tag-confirm'
/**
* @type {HTMLTemplateElement | null}
*/
const template = document.querySelector(templateId)
if (!template) throw `Template '${templateId}' not found!`
const clone = document.importNode(template.content, true)
/**
* @type {HTMLElement | null}
*/
let content = clone.querySelector('[slot=question]')
content.innerHTML = evt.detail.question
/**
* @type {HTMLDialogElement | null}
*/
let dialog = clone.querySelector('dialog')
document.body.appendChild(clone)
dialog.showModal()
dialog.addEventListener('close', event => {
dialog.remove()
if (dialog.returnValue !== 'confirmed') return
evt.detail.issueRequest(true)
})
}
})