-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
173 lines (141 loc) · 4.4 KB
/
index.js
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
const Prompts = (function () {
// Common styles
const dialogStyle = {
border: "none",
borderRadius: "6px",
padding: "20px",
minWidth: "300px",
maxWidth: "80%",
boxSizing: "border-box",
fontFamily: "sans-serif",
boxShadow: "0 2px 10px rgba(0,0,0,0.2)",
background: "#fff",
};
const messageStyle = {
marginBottom: "20px",
fontSize: "16px",
color: "#333",
whiteSpace: "pre-wrap",
wordWrap: "break-word",
};
const buttonRowStyle = {
textAlign: "right",
marginTop: "20px",
};
const buttonStyle = {
backgroundColor: "#007bff",
color: "#fff",
border: "none",
borderRadius: "4px",
padding: "8px 12px",
fontSize: "14px",
cursor: "pointer",
marginLeft: "8px",
};
const cancelButtonStyle = {
backgroundColor: "#6c757d",
};
const inputStyle = {
width: "100%",
boxSizing: "border-box",
padding: "8px",
fontSize: "16px",
marginBottom: "20px",
borderRadius: "4px",
border: "1px solid #ccc",
};
function applyStyles(element, styles) {
Object.assign(element.style, styles);
}
function createDialog(message) {
const dialog = document.createElement("dialog");
applyStyles(dialog, dialogStyle);
dialog.setAttribute("role", "dialog");
dialog.setAttribute("aria-modal", "true");
const form = document.createElement("form");
form.method = "dialog"; // Allows form to close the dialog on submission.
const msg = document.createElement("div");
applyStyles(msg, messageStyle);
msg.textContent = message;
form.appendChild(msg);
dialog.appendChild(form);
return { dialog, form };
}
function createButton(label, value, customStyles = {}, type = "submit") {
const btn = document.createElement("button");
applyStyles(btn, buttonStyle);
applyStyles(btn, customStyles);
btn.type = type;
btn.value = value; // form submission will set dialog.returnValue to this
btn.textContent = label;
return btn;
}
async function alert(message) {
return new Promise((resolve) => {
const { dialog, form } = createDialog(message);
const buttonRow = document.createElement("div");
applyStyles(buttonRow, buttonRowStyle);
const okBtn = createButton("OK", "ok");
buttonRow.appendChild(okBtn);
form.appendChild(buttonRow);
dialog.addEventListener("close", () => {
resolve();
dialog.remove();
});
document.body.appendChild(dialog);
dialog.showModal();
okBtn.focus();
});
}
async function confirm(message) {
return new Promise((resolve) => {
const { dialog, form } = createDialog(message);
const buttonRow = document.createElement("div");
applyStyles(buttonRow, buttonRowStyle);
const cancelBtn = createButton("Cancel", "cancel", cancelButtonStyle);
const okBtn = createButton("OK", "ok");
buttonRow.appendChild(cancelBtn);
buttonRow.appendChild(okBtn);
form.appendChild(buttonRow);
dialog.addEventListener("close", () => {
// dialog.returnValue will be "ok", "cancel", or "" (if ESC pressed)
const val = dialog.returnValue;
resolve(val === "ok");
dialog.remove();
});
document.body.appendChild(dialog);
dialog.showModal();
// Set focus to the OK button so pressing Enter will confirm
okBtn.focus();
});
}
async function prompt(message) {
return new Promise((resolve) => {
const { dialog, form } = createDialog(message);
const input = document.createElement("input");
applyStyles(input, inputStyle);
input.type = "text";
input.name = "promptInput";
form.appendChild(input);
const buttonRow = document.createElement("div");
applyStyles(buttonRow, buttonRowStyle);
const cancelBtn = createButton("Cancel", "cancel", cancelButtonStyle, "button");
const okBtn = createButton("OK", "ok");
buttonRow.appendChild(cancelBtn);
buttonRow.appendChild(okBtn);
form.appendChild(buttonRow);
cancelBtn.addEventListener("click", () => {
dialog.close(null);
});
dialog.addEventListener("close", () => {
const val = dialog.returnValue === "ok" ? input.value : null;
resolve(val);
dialog.remove();
});
document.body.appendChild(dialog);
dialog.showModal();
input.focus();
});
}
return { alert, confirm, prompt };
})();