-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlays.js
282 lines (271 loc) · 12.3 KB
/
overlays.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* WARNING:
* Before making modifications to this file, make absolutely sure that
* you've used the functions and their respective flags (if any) properly.
* These functions work for almost every webpage, so there are more chances
* you've used something incorrectly.
*
* When making modifications, you also need to test out if the modified code
* works for each and every webpage.
*/
import { $, getChildElement } from '/common/js/domfunc.js';
/**
* Contains global data for behavior of overlays viz menus and dialogs.
* The time taken for an overlay to animate out is Overlay.animation_duration ms.
* Value of instance_open needs to be set to true everytime an overlay opens and to false everytime an overlay closes.
* This has been made automatic when using dialog and menu functions.
*
* How low/high is too low/high?
* 0 ms and over 5000 milliseconds is too low/high.
*/
export const Overlay = {
/**
* @type {Boolean} If an overlay is already open, other overlays are postponed for Overlay.animation_duration ms.
*/
instance_open: false,
/**
* @type {Number} Can be modified to increase or decrease duration of overlay animations. Too low/high values may break the UI.
*/
animation_duration: 250,
/**
* @deprecated The value associated is automatically handled by dialog.hide() and menu.hide().
* Setter for scripts that import modules.js. Please do not use this function as the process has been made automatic.
* @param {Boolean} val Is set to true if an overlay is opened. Reverse is true.
*/
setInstanceOpen(val) {
this.instance_open = val;
},
/**
* Setter for scripts that import modules.js.
* @param {Number} val Duration of all overlay animations.
*/
setAnimDuration(val) {
this.animation_duration = val;
},
}
/**
* Represents a splashscreen.
* Needs the code for a splashscreen in the HTML document.
*/
export const SplashScreen = {
visible: false,
/**
* Display the splashscreen.
* NOTE: If SplashScreen is already visible, it'll throw an error. Make sure to check the console for the error when you feel like the SplashScreen isn't working as expected.
* @param {String} innerHTML Optional, default value = ''. Content of the SplashScreen.
*/
display(innerHTML = '') {
// delay when one overlay is already open
let timeout = 0;
if (Overlay.instance_open) timeout = Overlay.animation_duration;
if (!this.visible) setTimeout(() => {
$('#splashScreen').innerHTML = innerHTML;
$('#splashScreenRoot').style.animation = `fadeIn 0ms forwards`;
Overlay.setInstanceOpen(this.visible = true);
}, timeout);
else throw `Error: overlays.js: SplashScreen.display(): SplashScreen is already visible`;
},
/**
* Hide the SplashScreen.
* @param {Function} func Optional, function to run once splashscreen is closed.
* @throws {Error} If typeof func is not function.
*/
hide(func) {
let timeout = Overlay.animation_duration;
if (this.visible) {
$('#splashScreen').innerHTML = '';
$('#splashScreenRoot').style.animation = `fadeOut ${Overlay.animation_duration}ms forwards`;
} else timeout = 0;
setTimeout(() => {
Overlay.setInstanceOpen(this.visible = false);
// additional function
if (!func) return;
if (typeof func != 'function') {
throw `Error: overlays.js: SplashScreen.hide(): func is ${typeof func}, expected function`;
}
func.call();
}, timeout);
}
}
/* ----------------------------------------- TODO -------------------------------------------
* The alert and action dialogs need to be made into a single
* object named dialog.
*/
// alertDialog
const AlertDialog = {
visible: false,
// default button is Close
display(title = 'Alert!', message = '', button = 'Close', func) {
if (typeof button != 'string') {
throw `Error: overlays.js: AlertDialog.display(): button is ${typeof button}, expected String`;
}
if (func && typeof func != 'function') {
throw `Error: overlays.js: AlertDialog.display(): func is ${typeof func}, expected function`;
}
// delay when one overlay is already open
let timeout = 0;
if (Overlay.instance_open) timeout = Overlay.animation_duration;
if (!this.visible) setTimeout(() => {
getChildElement($('#alertDialog'), 'h2')[0].innerHTML = title.replace(/\n/g, '<br>');;
getChildElement($('#alertDialog'), 'div')[0].innerHTML = message.replace(/\n/g, '<br>');
$('#alertDialog_btn').innerHTML = button;
// once: true removes listener after it fires atmost once
if (func) $('#alertDialog_btn').addEventListener('click', func, { once: true });
$('#alertDialogRoot').style.animation = `fadeIn ${Overlay.animation_duration}ms forwards`;
$('#alertDialog').style.animation = `scaleIn ${Overlay.animation_duration}ms forwards`;
Overlay.setInstanceOpen(this.visible = true);
}, timeout);
else throw `Error: overlays.js: AlertDialog.display(): AlertDialog (a.k.a. 'alert') is already visible`;
},
hide(func) {
let timeout = Overlay.animation_duration;
if (this.visible) {
$('#alertDialogRoot').style.animation = `fadeOut ${Overlay.animation_duration}ms forwards`;
$('#alertDialog').style.animation = `scaleOut ${Overlay.animation_duration}ms forwards`;
} else timeout = 0;
setTimeout(() => {
Overlay.setInstanceOpen(this.visible = false);
// additional function
if (!func) return;
if (typeof func != 'function') {
throw `Error: overlays.js: AlertDialog.hide(): func is ${typeof func}, expected function`;
}
func.call();
}, timeout);
}
}
// actionDialog
const ActionDialog = {
visible: false,
onClickFunction: undefined,
// default button is Close
display(title = 'Alert!', message = '', button = 'OK', func) {
if (typeof button != 'string') {
throw `Error: overlays.js: ActionDialog.display(): button is ${typeof button}, expected String`;
}
if (!func || typeof func != 'function') {
throw `Error: overlays.js: ActionDialog.display(): func is ${typeof func}, expected function`;
}
// delay when one overlay is already open
let timeout = 0;
// remove previous button click listener if any
if (this.onClickFunction) $('#actionDialog_btnOk').removeEventListener('click', this.onClickFunction);
// the function to run on button click
this.onClickFunction = func;
if (Overlay.instance_open) timeout = Overlay.animation_duration;
if (!this.visible) setTimeout(() => {
getChildElement($('#actionDialog'), 'h2')[0].innerHTML = title.replace(/\n/g, '<br>');;
getChildElement($('#actionDialog'), '.content')[0].innerHTML = message.replace(/\n/g, '<br>');
$('#actionDialog_btnOk').innerHTML = button;
if (this.onClickFunction) $('#actionDialog_btnOk').addEventListener('click', this.onClickFunction);
$('#actionDialogRoot').style.animation = `fadeIn ${Overlay.animation_duration}ms forwards`;
$('#actionDialog').style.animation = `scaleIn ${Overlay.animation_duration}ms forwards`;
Overlay.setInstanceOpen(this.visible = true);
}, timeout);
else throw `Error: overlays.js: ActionDialog.display(): ActionDialog (a.k.a. 'action') is already visible`;
},
hide(func) {
let timeout = Overlay.animation_duration;
if (this.visible) {
$('#actionDialogRoot').style.animation = `fadeOut ${Overlay.animation_duration}ms forwards`;
$('#actionDialog').style.animation = `scaleOut ${Overlay.animation_duration}ms forwards`;
} else timeout = 0;
// remove button click listeners while hiding dialog, if any
if (this.onClickFunction) $('#actionDialog_btnOk').removeEventListener('click', this.onClickFunction);
setTimeout(() => {
Overlay.setInstanceOpen(this.visible = false);
// additional function
if (!func) return;
if (typeof func != 'function') {
throw `Error: overlays.js: ActionDialog.hide(): func is ${typeof func}, expected function`;
}
func.call();
}, timeout);
}
}
/*--------------------------------------- TODO START -------------------------------------------*/
/**
* Represents a dialog.
* Needs the code for a dialog in the HTML document.
*/
export const Dialog = {
/**
* Display the dialog.
* @param {String} category Either 'alert' or 'action'.
* @param {String} title Title of the dialog.
* @param {String} message Message to be displayed.
* @param {String} button Title of the default button.
* @param {Function} func Optional for 'alert' category, function to run if default is button clicked.
* @throws {Error} If category is invalid.
* @throws {Error} If no function is provided for 'action' category.
* NOTE: If menu is already visible, it'll throw an error. Make sure to check the console for the error when you feel like the Menu isn't working as expected.
*/
display(category, title, message, button, func) {
if (category == 'alert') {
AlertDialog.display(title, message, button, func);
} else if (category == 'action') {
ActionDialog.display(title, message, button, func);
} else throw `Error: overlays.js: Dialog.display(): category = ${category}, expected 'alert' or 'action'`;
},
/**
* Hide the dialog.
* @param {String} category Either 'alert' or 'action'.
* @param {Function} func Optional, function to run once dialog is closed.
* @throws {Error} If category is invalid.
* @throws {Error} If typeof func is not function.
*/
hide(category, func) {
if (category == 'alert') {
AlertDialog.hide(func);
} else if (category == 'action') {
ActionDialog.hide(func);
} else throw `Error: overlays.js: Dialog.hide(): category = ${category}, expected 'alert' or 'action'`;
}
}
/*--------------------------------------- TODO END --------------------------------------------*/
/**
* Represents a menu.
* Needs the code for a menu in the HTML document.
*/
export const Menu = {
visible: false,
/**
* Display the menu.
* NOTE: If Menu is already visible, it'll throw an error. Make sure to check the console for the error when you feel like the Menu isn't working as expected.
* @param {String} title Optional, default value = 'Menu'. Title of the menu dialog.
*/
display(title = 'Menu') {
// delay when one overlay is already open
let timeout = 0;
if (Overlay.instance_open) timeout = Overlay.animation_duration;
if (!this.visible) setTimeout(() => {
getChildElement($('#menu'), 'h2')[0].innerHTML = title.replace(/\n/g, '<br>');;
$('#menuRoot').style.animation = `fadeIn ${Overlay.animation_duration}ms forwards`;
$('#menu').style.animation = `scaleIn ${Overlay.animation_duration}ms forwards`;
Overlay.setInstanceOpen(this.visible = true);
}, timeout);
else throw `Error: overlays.js: Menu.display(): menu is already visible`;
},
/**
* Hide the menu.
* @param {Function} func Optional, function to run once menu is closed.
* @throws {Error} If typeof func is not function.
*/
hide(func) {
let timeout = Overlay.animation_duration;
if (this.visible) {
$('#menuRoot').style.animation = `fadeOut ${Overlay.animation_duration}ms forwards`;
$('#menu').style.animation = `scaleOut ${Overlay.animation_duration}ms forwards`;
} else timeout = 0;
setTimeout(() => {
Overlay.setInstanceOpen(this.visible = false);
// additional function
if (!func) return;
if (typeof func != 'function') {
throw `Error: overlays.js: Menu.hide(): func is ${typeof func}, expected function`;
}
func.call();
}, timeout);
}
}
console.log('module overlays.js loaded');