forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbox.js
More file actions
201 lines (177 loc) · 3.69 KB
/
box.js
File metadata and controls
201 lines (177 loc) · 3.69 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import "./style.scss";
import actionStack from "lib/actionStack";
import restoreTheme from "lib/restoreTheme";
/**
* Confirm dialog box
* @param {string} titleText Title text
* @param {string} html HTML string
* @param {string} [hideButtonText] Text for hide button
* @param {string} [cancelButtonText] Text for cancel button
* @returns {PromiseLike}
*/
function box(titleText, html, hideButtonText, cancelButtonText) {
let waitFor = 0,
strOK = hideButtonText || strings.ok,
_onclick = () => {},
_onhide = () => {},
_then = () => {},
_onOk = _hide,
_onCancel = () => {};
const promiseLike = {
hide,
wait,
onclick,
onhide,
then,
ok,
cancel,
};
let cancelBtn;
const hideButton =
typeof hideButtonText === "boolean" ? hideButtonText : false;
if (cancelButtonText) {
cancelBtn = tag("button", {
className: "disabled",
textContent: cancelButtonText,
onclick: () => {
_onCancel();
},
});
}
const okBtn = tag("button", {
className: "disabled",
textContent: strOK,
onclick: () => {
_onOk();
},
});
const body = tag("div", {
className: "message",
innerHTML: html,
onclick: __onclick,
});
const box = tag("div", {
className: "prompt box",
children: [
tag("strong", {
className: "title",
textContent: titleText,
}),
body,
],
});
const mask = tag("span", {
className: "mask",
onclick: _hide,
});
if (!hideButton) {
box.append(
tag("div", {
className: "button-container",
children: cancelBtn ? [cancelBtn, okBtn] : [okBtn],
}),
);
}
setTimeout(() => {
decTime();
actionStack.push({
id: "box",
action: hideBox,
});
document.body.append(box, mask);
__then();
restoreTheme(true);
}, 0);
function decTime() {
if (waitFor >= 1000) {
okBtn.textContent = `${strOK} (${Number.parseInt(waitFor / 1000)}sec)`;
waitFor -= 1000;
setTimeout(decTime, 1000);
} else {
okBtn.textContent = strOK;
okBtn.classList.remove("disabled");
cancelBtn?.classList.remove("disabled");
}
}
function hideBox() {
box.classList.add("hide");
restoreTheme();
setTimeout(() => {
document.body.removeChild(box);
document.body.removeChild(mask);
}, 300);
}
function hide() {
if (waitFor) return;
const imgs = box.getAll("img");
if (imgs) {
for (const img of imgs) {
URL.revokeObjectURL(img.src);
}
}
actionStack.remove("box");
hideBox();
}
function _hide() {
hide();
if (_onhide) _onhide.call(promiseLike);
}
function wait(time) {
time -= time % 1000;
waitFor = time;
return promiseLike;
}
function __onclick(e) {
if (_onclick) _onclick.call(this, e);
}
function __then() {
if (_then) _then(body.children);
}
/**
* Set callback function
* @param {function(HTMLCollection)} callback Callback function
* @returns {PromiseLike}
*/
function then(callback) {
_then = callback;
return promiseLike;
}
/**
* Set onclick callback function
* @param {function(this:HTMLElement, Event):void} onclick Callback function
* @returns {PromiseLike}
*/
function onclick(onclick) {
_onclick = onclick;
return promiseLike;
}
/**
* Set onhide callback function
* @param {function():void} onhide Callback function
* @returns {PromiseLike}
*/
function onhide(onhide) {
_onhide = onhide;
return promiseLike;
}
/**
* Set onOk callback function
* @param {function():void} onOk Callback function
* @returns {PromiseLike}
*/
function ok(onOk) {
_onOk = onOk;
return promiseLike;
}
/**
* Set onCancel callback function
* @param {function():void} onCancel Callback function
* @returns {PromiseLike}
*/
function cancel(onCancel) {
_onCancel = onCancel;
return promiseLike;
}
return promiseLike;
}
export default box;