BZModalDialog
This commit is contained in:
43
buildoz.js
43
buildoz.js
@@ -11,6 +11,48 @@
|
||||
* as long as the copyright notice and license are kept.
|
||||
*/
|
||||
|
||||
function BZModalDialog(title, message) {
|
||||
const getFields = (dlg) => {
|
||||
const form = dlg.querySelector('form')
|
||||
if (!form) return {}
|
||||
const fd = new FormData(form)
|
||||
const out = {}
|
||||
for (const [key, value] of fd.entries()) {
|
||||
if (Object.prototype.hasOwnProperty.call(out, key)) {
|
||||
out[key] = Array.isArray(out[key]) ? [...out[key], value] : [out[key], value]
|
||||
} else {
|
||||
out[key] = value
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
const dlg = document.createElement('dialog')
|
||||
dlg.classList.add('bz-modal-dialog')
|
||||
dlg.innerHTML = `
|
||||
<form method="dialog">
|
||||
<header>${title}</header>
|
||||
<section>${message}</section>
|
||||
<footer>
|
||||
<button value="cancel">Cancel</button>
|
||||
<button value="ok">OK</button>
|
||||
</footer>
|
||||
</form>
|
||||
`
|
||||
dlg.addEventListener('close', () => {
|
||||
const ok = dlg.returnValue.toLowerCase() === 'ok'
|
||||
if(ok) {
|
||||
resolve(getFields(dlg))
|
||||
} else {
|
||||
resolve(ok)
|
||||
}
|
||||
dlg.remove()
|
||||
})
|
||||
document.body.appendChild(dlg)
|
||||
dlg.showModal()
|
||||
})
|
||||
}
|
||||
|
||||
class Buildoz extends HTMLElement {
|
||||
|
||||
// static is evaluated when the class is defined, therefore while buildoz.js is executing.
|
||||
@@ -386,4 +428,3 @@ class BZslidePane extends Buildoz {
|
||||
}
|
||||
}
|
||||
Buildoz.define('slidepane', BZslidePane)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user