|
34 | 34 | /** |
35 | 35 | * Confirmation alert using either bootbox or default alert. |
36 | 36 | * @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. |
40 | 40 | * @return void |
41 | 41 | */ |
42 | | - confirm: function(message, trueCallback, falseCallback, elem) { |
| 42 | + confirm: function(message, confirmCallback, cancelCallback, elem) { |
43 | 43 | if ((message.startsWith("lang:") || message.startsWith("i18n:")) && typeof csk.i18n !== "undefined") { |
44 | 44 | message = eval("csk.i18n." + message.substring(5)) || message; |
45 | 45 | } |
|
51 | 51 | message = sprintf(message, name); |
52 | 52 | } |
53 | 53 | } |
| 54 | + |
| 55 | + // Bootbox if available. |
54 | 56 | if (typeof bootbox !== "undefined") { |
55 | | - bootbox.confirm({ |
| 57 | + return bootbox.confirm({ |
56 | 58 | message: message, |
57 | 59 | buttons: { |
58 | 60 | confirm: { |
|
64 | 66 | }, |
65 | 67 | callback: function(result) { |
66 | 68 | 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); |
71 | 73 | } |
72 | 74 | } |
73 | 75 | }); |
74 | | - } else if (confirm(message) && typeof trueCallback === "function") { |
75 | | - trueCallback(true); |
76 | | - } else if (typeof falseCallback === "function") { |
77 | | - falseCallback(true); |
78 | 76 | } |
| 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; |
79 | 125 | }, |
80 | 126 |
|
81 | 127 | /** |
|
0 commit comments