Skip to content

Commit 45603c8

Browse files
committed
refactor(confirm): use BS5 modal template
1 parent 21e9591 commit 45603c8

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

js/admin.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
/**
3535
* Confirmation alert using either bootbox or default alert.
3636
* @since 1.20
37-
* @param string message Message to display.
38-
* @param callable trueCallback Callback to use once confirmed.
39-
* @param callable falseCallback Callback to use once canceled.
37+
* @param string message Message to display.
38+
* @param callable confirmCallback Callback to use once confirmed.
39+
* @param callable cancelCallback Callback to use once canceled.
4040
* @return void
4141
*/
42-
confirm: function(message, trueCallback, falseCallback, elem) {
42+
confirm: function(message, confirmCallback, cancelCallback, elem) {
4343
if ((message.startsWith("lang:") || message.startsWith("i18n:")) && typeof csk.i18n !== "undefined") {
4444
message = eval("csk.i18n." + message.substring(5)) || message;
4545
}
@@ -51,8 +51,10 @@
5151
message = sprintf(message, name);
5252
}
5353
}
54+
55+
// Bootbox if available.
5456
if (typeof bootbox !== "undefined") {
55-
bootbox.confirm({
57+
return bootbox.confirm({
5658
message: message,
5759
buttons: {
5860
confirm: {
@@ -64,18 +66,62 @@
6466
},
6567
callback: function(result) {
6668
bootbox.hideAll();
67-
if (result === true && typeof trueCallback === "function") {
68-
trueCallback(true);
69-
} else if (typeof falseCallback === "function") {
70-
falseCallback(true);
69+
if (result && typeof confirmCallback === "function") {
70+
confirmCallback(true);
71+
} else if (!result && typeof cancelCallback === "function") {
72+
cancelCallback(true);
7173
}
7274
}
7375
});
74-
} else if (confirm(message) && typeof trueCallback === "function") {
75-
trueCallback(true);
76-
} else if (typeof falseCallback === "function") {
77-
falseCallback(true);
7876
}
77+
78+
// Bootstrap modal path
79+
if (typeof bootstrap !== "undefined") {
80+
const tpl = document.getElementById("csk-confirm-template");
81+
if (tpl) {
82+
const modalEl = tpl.content.firstElementChild.cloneNode(true);
83+
const msgEl = modalEl.querySelector(".modal-body");
84+
const btnConfirm = modalEl.querySelector('.btn-confirm');
85+
const btnCancel = modalEl.querySelector('.btn-cancel');
86+
87+
msgEl.innerHTML = message;
88+
document.body.appendChild(modalEl);
89+
90+
const modal = new bootstrap.Modal(modalEl, {
91+
backdrop: true,
92+
focus: true
93+
});
94+
95+
btnConfirm.addEventListener("click", function () {
96+
modal.hide();
97+
if (typeof confirmCallback === "function") {
98+
confirmCallback(true);
99+
}
100+
});
101+
102+
btnCancel.addEventListener("click", function () {
103+
modal.hide();
104+
if (typeof cancelCallback === "function") {
105+
cancelCallback(true);
106+
}
107+
});
108+
109+
modalEl.addEventListener('shown.bs.modal', function () {
110+
btnConfirm.focus();
111+
});
112+
113+
return modal.show();
114+
}
115+
}
116+
117+
// Native fallback
118+
const result = confirm(message);
119+
if (result && typeof confirmCallback === "function") {
120+
confirmCallback(true);
121+
} else if (!result && typeof cancelCallback === "function") {
122+
cancelCallback(true)
123+
}
124+
return result;
79125
},
80126

81127
/**

0 commit comments

Comments
 (0)