2023-11-15 20:18:01 +00:00
|
|
|
export function dialog (header, body, onclose = async (result, form) => { }) {
|
2023-06-29 22:20:15 +00:00
|
|
|
const dialog = document.createElement("dialog");
|
2023-05-05 21:43:15 +00:00
|
|
|
dialog.innerHTML = `
|
|
|
|
<p class="w3-large" id="prompt" style="text-align: center;"></p>
|
2023-11-14 00:09:41 +00:00
|
|
|
<div id="body"></div>
|
2023-05-05 21:43:15 +00:00
|
|
|
<div class="w3-center w3-container">
|
2023-11-14 00:09:41 +00:00
|
|
|
<button id="cancel" value="cancel" form="form" class="w3-button w3-margin" style="background-color: var(--negative-color, #f00); color: var(--lightbg-text-color, black);" formnovalidate>CANCEL</button>
|
|
|
|
<button id="confirm" value="confirm" form="form" class="w3-button w3-margin" style="background-color: var(--positive-color, #0f0); color: var(--lightbg-text-color, black);">CONFIRM</button>
|
2023-05-05 21:43:15 +00:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
dialog.className = "w3-container w3-card w3-border-0";
|
|
|
|
dialog.querySelector("#prompt").innerText = header;
|
2023-11-14 00:09:41 +00:00
|
|
|
dialog.querySelector("#body").innerHTML = body;
|
2023-05-05 21:43:15 +00:00
|
|
|
dialog.addEventListener("close", async () => {
|
2023-11-14 00:09:41 +00:00
|
|
|
const formElem = dialog.querySelector("form");
|
2023-11-15 20:18:01 +00:00
|
|
|
const formData = formElem ? new FormData(formElem) : null;
|
2023-11-14 00:09:41 +00:00
|
|
|
await onclose(dialog.returnValue, formData);
|
2023-05-05 21:43:15 +00:00
|
|
|
dialog.parentElement.removeChild(dialog);
|
|
|
|
});
|
2023-11-15 20:18:01 +00:00
|
|
|
if (!dialog.querySelector("form")) {
|
|
|
|
dialog.querySelector("#confirm").addEventListener("click", async (e) => {
|
|
|
|
e.preventDefault();
|
2023-11-14 00:09:41 +00:00
|
|
|
dialog.close(e.target.value);
|
2023-11-15 20:18:01 +00:00
|
|
|
});
|
|
|
|
dialog.querySelector("#cancel").addEventListener("click", async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
dialog.close(e.target.value);
|
|
|
|
});
|
|
|
|
}
|
2023-07-06 04:53:47 +00:00
|
|
|
document.body.append(dialog);
|
|
|
|
dialog.showModal();
|
2023-05-05 21:43:15 +00:00
|
|
|
return dialog;
|
2023-02-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 22:20:15 +00:00
|
|
|
export function alert (message) {
|
|
|
|
const dialog = document.createElement("dialog");
|
2023-05-05 21:43:15 +00:00
|
|
|
dialog.innerHTML = `
|
|
|
|
<form method="dialog">
|
|
|
|
<p class="w3-center" style="margin-bottom: 0px;">${message}</p>
|
|
|
|
<div class="w3-center">
|
|
|
|
<button class="w3-button w3-margin" id="submit">OK</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
`;
|
|
|
|
dialog.className = "w3-container w3-card w3-border-0";
|
2023-04-18 20:08:59 +00:00
|
|
|
|
|
|
|
document.body.append(dialog);
|
|
|
|
dialog.showModal();
|
2023-04-28 23:53:38 +00:00
|
|
|
|
|
|
|
dialog.addEventListener("close", () => {
|
|
|
|
dialog.parentElement.removeChild(dialog);
|
2023-06-29 22:20:15 +00:00
|
|
|
});
|
2023-04-18 20:08:59 +00:00
|
|
|
|
2023-05-05 21:43:15 +00:00
|
|
|
return dialog;
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|