aboutsummaryrefslogtreecommitdiff
path: root/static/js/confirm.js
blob: 6990b95277a6bb14c145892d2a91a8922e9e016b (plain)
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
41
42
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: "none"
                });
            });
        });
    });

    function closeModal() {
        modal.classList.add("hidden");
    }

    backdrop.addEventListener("click", closeModal);
    cancelButton.addEventListener("click", closeModal);
}

document.addEventListener("DOMContentLoaded", initConfirmModals);
document.body.addEventListener("htmx:afterSwap", initConfirmModals);