diff options
Diffstat (limited to 'static/js/confirm.js')
| -rw-r--r-- | static/js/confirm.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/static/js/confirm.js b/static/js/confirm.js new file mode 100644 index 0000000..fcc6742 --- /dev/null +++ b/static/js/confirm.js @@ -0,0 +1,43 @@ +function initConfirmModals() { + var modal = document.getElementById("confirm-modal"); + if (!modal) return; + + var title = document.getElementById("confirm-title"); + var message = document.getElementById("confirm-message"); + var actionButton = document.getElementById("confirm-action"); + var backdrop = modal.querySelector("[data-confirm-backdrop]"); + var cancelButton = modal.querySelector("[data-confirm-cancel]"); + + document.querySelectorAll("[data-confirm-trigger]").forEach(function (trigger) { + if (trigger.dataset.confirmInitialized) return; + trigger.dataset.confirmInitialized = "true"; + + trigger.addEventListener("click", function () { + title.textContent = trigger.dataset.confirmTitle; + message.textContent = trigger.dataset.confirmMessage; + modal.classList.remove("hidden"); + + var cloned = actionButton.cloneNode(true); + actionButton.parentNode.replaceChild(cloned, actionButton); + actionButton = cloned; + + actionButton.addEventListener("click", function () { + modal.classList.add("hidden"); + htmx.ajax("DELETE", trigger.dataset.confirmAction, { + target: "#content", + swap: "innerHTML" + }); + }); + }); + }); + + function closeModal() { + modal.classList.add("hidden"); + } + + backdrop.addEventListener("click", closeModal); + cancelButton.addEventListener("click", closeModal); +} + +document.addEventListener("DOMContentLoaded", initConfirmModals); +document.body.addEventListener("htmx:afterSwap", initConfirmModals);
\ No newline at end of file |
