forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathalert.js
More file actions
74 lines (66 loc) · 1.46 KB
/
alert.js
File metadata and controls
74 lines (66 loc) · 1.46 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import DOMPurify from "dompurify";
import actionStack from "lib/actionStack";
import restoreTheme from "lib/restoreTheme";
/**
* Alert dialog
* @param {string} titleText Title text
* @param {string} message Alert message
* @param {function():void} [onhide] Callback function
*/
function alert(titleText, message, onhide) {
if (!message && titleText) {
message = titleText;
titleText = "";
}
const regex = /(https?:\/\/[^\s]+)/g;
if (regex.test(message)) {
message = message.replace(regex, (url) => `<a href='${url}'>${url}</a>`);
}
const titleSpan = tag("strong", {
className: "title",
textContent: titleText,
});
const messageSpan = tag("span", {
className: "message scroll",
innerHTML: DOMPurify.sanitize(message),
});
const okBtn = tag("button", {
textContent: strings.ok,
onclick: hide,
});
const alertDiv = tag("div", {
className: "prompt alert",
children: [
titleSpan,
messageSpan,
tag("div", {
className: "button-container",
child: okBtn,
}),
],
});
const mask = tag("span", {
className: "mask",
onclick: hide,
});
actionStack.push({
id: "alert",
action: hideAlert,
});
app.append(alertDiv, mask);
restoreTheme(true);
function hideAlert() {
alertDiv.classList.add("hide");
restoreTheme();
setTimeout(() => {
app.removeChild(alertDiv);
app.removeChild(mask);
}, 300);
}
function hide() {
if (onhide) onhide();
actionStack.remove("alert");
hideAlert();
}
}
export default alert;