* Alert dialog * @param {string} titleText Title text * @param {string} message Alert message * @param {function():void} [onhide] Callback function
(titleText, message, onhide)
| 9 | * @param {function():void} [onhide] Callback function |
| 10 | */ |
| 11 | function alert(titleText, message, onhide) { |
| 12 | if (!message && titleText) { |
| 13 | message = titleText; |
| 14 | titleText = ""; |
| 15 | } |
| 16 | |
| 17 | const regex = /(https?:\/\/[^\s]+)/g; |
| 18 | if (regex.test(message)) { |
| 19 | message = message.replace(regex, function (url) { |
| 20 | return `<a href='${url}'>${url}</a>`; |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | const titleSpan = tag("strong", { |
| 25 | className: "title", |
| 26 | textContent: titleText, |
| 27 | }); |
| 28 | const messageSpan = tag("span", { |
| 29 | className: "message scroll", |
| 30 | innerHTML: DOMPurify.sanitize(message), |
| 31 | }); |
| 32 | const okBtn = tag("button", { |
| 33 | textContent: strings.ok, |
| 34 | onclick: hide, |
| 35 | }); |
| 36 | const alertDiv = tag("div", { |
| 37 | className: "prompt alert", |
| 38 | children: [ |
| 39 | titleSpan, |
| 40 | messageSpan, |
| 41 | tag("div", { |
| 42 | className: "button-container", |
| 43 | child: okBtn, |
| 44 | }), |
| 45 | ], |
| 46 | }); |
| 47 | const mask = tag("span", { |
| 48 | className: "mask", |
| 49 | onclick: hide, |
| 50 | }); |
| 51 | |
| 52 | actionStack.push({ |
| 53 | id: "alert", |
| 54 | action: hideAlert, |
| 55 | }); |
| 56 | |
| 57 | app.append(alertDiv, mask); |
| 58 | restoreTheme(true); |
| 59 | |
| 60 | function hideAlert() { |
| 61 | alertDiv.classList.add("hide"); |
| 62 | restoreTheme(); |
| 63 | setTimeout(() => { |
| 64 | app.removeChild(alertDiv); |
| 65 | app.removeChild(mask); |
| 66 | }, 300); |
| 67 | } |
| 68 |
no test coverage detected