This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.js
More file actions
241 lines (202 loc) · 7.01 KB
/
dialogs.js
File metadata and controls
241 lines (202 loc) · 7.01 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(function () {
var dialog = {
styles: {
//Container (Holds both background and dialog box)
containerClassName: "windowDialogContainer",
//Background
backgroundClassName: "windowDialogBackground",
//Dialog
dialogClassName: "windowDialogBox",
//Button
buttonClassName: "windowDialogButton",
//Header
headerClassName: "windowDialogHeader",
//Input (Where you type text)
inputClassName: "windowDialogInput",
},
texts: {
ok: "OK",
cancel: "Cancel",
},
_createDialogBase() {
var background = document.createElement("div");
background.style.position = "fixed";
background.style.top = "0";
background.style.left = "0";
background.style.width = "100vw";
background.style.height = "100vh";
background.style.opacity = "0.5";
background.className = this.styles.backgroundClassName;
var dialogBox = document.createElement("div");
dialogBox.style.position = "fixed";
dialogBox.style.top = "50%";
dialogBox.style.left = "50%";
dialogBox.style.transform = "translate(-50%, -50%)";
dialogBox.style.width = "fit-content";
dialogBox.style.height = "fit-content";
dialogBox.style.padding = "20px";
dialogBox.style.maxWidth = "500px";
dialogBox.style.maxHeight = "300px";
dialogBox.style.minWidth = "100px";
dialogBox.style.minHeight = "100px";
dialogBox.style.overflow = "auto";
dialogBox.className = this.styles.dialogClassName;
var dialogContainer = document.createElement("div");
dialogContainer.style.zIndex = "9999999";
dialogContainer.className = this.styles.containerClassName;
dialogContainer.append(background);
dialogContainer.append(dialogBox);
return { background, dialogBox, dialogContainer };
},
_createButtonBase() {
var button = document.createElement("div");
button.className = this.styles.buttonClassName;
button.style.width = "fit-content";
button.style.height = "fit-content";
button.style.minWidth = "30px";
button.style.minHeight = "20px";
button.style.padding = "3px";
button.style.cursor = "pointer";
button.style.display = "inline-block";
return button;
},
_createHeaderBase() {
var span = document.createElement("span");
span.className = this.styles.headerClassName;
return span;
},
_createColorInputBase() {
var input = document.createElement("input");
input.type = "color";
return input;
},
_createBreakBase() {
var br = document.createElement("br");
return br;
},
_createTextInputBase() {
var input = document.createElement("input");
input.type = "text";
input.className = this.styles.inputClassName;
return input;
},
_appendHeaders(message, dialogBox) {
var m = message.toString();
for (var m of m.split("\n")) {
var header = this._createHeaderBase();
header.textContent = m;
dialogBox.append(header);
dialogBox.append(this._createBreakBase());
}
},
displayButtonChooser: function (message, buttonTexts) {
var { dialogBox, background, dialogContainer } = this._createDialogBase();
dialogBox.focus();
this._appendHeaders(message, dialogBox);
document.body.append(dialogContainer);
return new Promise((accept) => {
buttonTexts.forEach((buttonText, index) => {
var button = this._createButtonBase();
button.textContent = buttonText;
button.onclick = function () {
dialogContainer.remove();
accept(index);
};
dialogBox.append(button);
});
});
},
alert: function (message) {
var { dialogBox, background, dialogContainer } = this._createDialogBase();
dialogBox.focus();
document.body.append(dialogContainer);
this._appendHeaders(message, dialogBox);
var acceptButton = this._createButtonBase();
acceptButton.textContent = this.texts.ok;
dialogBox.append(acceptButton);
return new Promise((accept) => {
acceptButton.onclick = function () {
accept();
dialogContainer.remove();
};
});
},
prompt: function (message) {
var { dialogBox, background, dialogContainer } = this._createDialogBase();
dialogBox.focus();
document.body.append(dialogContainer);
this._appendHeaders(message, dialogBox);
var input = this._createTextInputBase();
dialogBox.append(input);
dialogBox.append(this._createBreakBase());
var acceptButton = this._createButtonBase();
acceptButton.textContent = this.texts.ok;
dialogBox.append(acceptButton);
var cancelButton = this._createButtonBase();
cancelButton.textContent = this.texts.cancel;
dialogBox.append(cancelButton);
return new Promise((accept) => {
acceptButton.onclick = function () {
if (input.value.length < 1) {
accept(undefined);
} else {
accept(input.value);
}
dialogContainer.remove();
};
cancelButton.onclick = function () {
accept();
dialogContainer.remove();
};
});
},
confirm: function (message) {
var { dialogBox, background, dialogContainer } = this._createDialogBase();
dialogBox.focus();
document.body.append(dialogContainer);
this._appendHeaders(message, dialogBox);
var acceptButton = this._createButtonBase();
acceptButton.textContent = this.texts.ok;
dialogBox.append(acceptButton);
var cancelButton = this._createButtonBase();
cancelButton.textContent = this.texts.cancel;
dialogBox.append(cancelButton);
return new Promise((accept) => {
acceptButton.onclick = function () {
accept(true);
dialogContainer.remove();
};
cancelButton.onclick = function () {
accept(false);
dialogContainer.remove();
};
});
},
colorPrompt: function (message) {
var { dialogBox, background, dialogContainer } = this._createDialogBase();
dialogBox.focus();
document.body.append(dialogContainer);
this._appendHeaders(message, dialogBox);
var colorInput = this._createColorInputBase();
dialogBox.append(colorInput);
dialogBox.append(this._createBreakBase());
var acceptButton = this._createButtonBase();
acceptButton.textContent = this.texts.ok;
dialogBox.append(acceptButton);
var cancelButton = this._createButtonBase();
cancelButton.textContent = this.texts.cancel;
dialogBox.append(cancelButton);
return new Promise((accept) => {
acceptButton.onclick = function () {
accept(colorInput.value);
dialogContainer.remove();
};
cancelButton.onclick = function () {
accept();
dialogContainer.remove();
};
});
},
};
window.dialog = dialog;
})();