/** * Spawn modal dialog from template node. Assumes the following structure: * * Where prompt is the modal dialog's prompt or header, * body contains an optional form or other information, * and controls contains a series of buttons which controls the form */ export function dialog (template, onclose = async (result, form) => { }) { const dialog = template.content.querySelector("dialog").cloneNode(true); document.body.append(dialog); dialog.addEventListener("close", async () => { const formElem = dialog.querySelector("form"); const formData = formElem ? new FormData(formElem) : null; await onclose(dialog.returnValue, formData); formElem.reset(); dialog.close(); dialog.parentElement.removeChild(dialog); }); if (!dialog.querySelector("form")) { for (const control of dialog.querySelector("#controls").childNodes) { control.addEventListener("click", async (e) => { e.preventDefault(); dialog.close(e.target.value); }); } } document.body.append(dialog); dialog.showModal(); return dialog; } export function alert (message) { const dialog = document.createElement("dialog"); dialog.innerHTML = `

${message}

`; dialog.className = "w3-container w3-card w3-border-0"; document.body.append(dialog); dialog.showModal(); dialog.addEventListener("close", () => { dialog.parentElement.removeChild(dialog); }); return dialog; }