forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirm.js
More file actions
97 lines (90 loc) · 2.22 KB
/
Copy pathconfirm.js
File metadata and controls
97 lines (90 loc) · 2.22 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Checkbox from "components/checkbox";
import DOMPurify from "dompurify";
import actionStack from "lib/actionStack";
import restoreTheme from "lib/restoreTheme";
/**
* Confirm dialog box
* @param {string} titleText Title text
* @param {string} [message] Alert message
* @param {boolean} [isHTML] Whether the message is HTML
* @param {{checkboxText?: string, returnState?: boolean}} [options]
* @returns {Promise<boolean | {confirmed: boolean, checked: boolean}>}
*/
function confirm(titleText, message, isHTML, options = {}) {
return new Promise((resolve) => {
if (!message && titleText) {
message = titleText;
titleText = "";
}
const titleSpan = tag("strong", {
className: "title",
textContent: titleText,
});
const messageSpan = tag("span", {
className: "message scroll",
innerHTML: isHTML ? DOMPurify.sanitize(message) : undefined,
textContent: isHTML ? undefined : message,
});
const checkbox = options.checkboxText
? Checkbox(options.checkboxText, false)
: null;
if (checkbox) {
checkbox.classList.add("confirm-checkbox");
}
const getResponse = (confirmed) => {
if (!options.returnState) return confirmed;
return {
confirmed,
checked: Boolean(checkbox?.checked),
};
};
const okBtn = tag("button", {
textContent: strings.ok,
onclick: function () {
hide();
resolve(getResponse(true));
},
});
const cancelBtn = tag("button", {
textContent: strings.cancel,
onclick: function () {
hide();
resolve(getResponse(false));
},
});
const confirmDiv = tag("div", {
className: "prompt confirm",
children: [
titleSpan,
messageSpan,
checkbox,
tag("div", {
className: "button-container",
children: [cancelBtn, okBtn],
}),
].filter(Boolean),
});
const mask = tag("span", {
className: "mask",
});
actionStack.push({
id: "confirm",
action: hideAlert,
});
app.append(confirmDiv, mask);
restoreTheme(true);
function hideAlert() {
confirmDiv.classList.add("hide");
restoreTheme();
setTimeout(() => {
app.removeChild(confirmDiv);
app.removeChild(mask);
}, 300);
}
function hide() {
actionStack.remove("confirm");
hideAlert();
}
});
}
export default confirm;