-
Notifications
You must be signed in to change notification settings - Fork 2
/
modal.js
76 lines (54 loc) · 2.07 KB
/
modal.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
export class modalManager {
constructor() {
this.modalObject = document.getElementById("modal");
this.overlay = document.getElementById("overlay");
this.modalTitle = document.getElementById("modal-title");
this.modalContent = document.getElementById("modal-content");
this.modalNumInput = document.getElementById("modal-num-input");
this.modalClose = document.getElementById("modal-close");
this.modalOK = document.getElementById("modal-ok");
this.receivedInput = undefined;
}
timeout = async ms => new Promise(res => setTimeout(res, ms));
hide() {
this.modalObject.style.display = "none";
this.overlay.style.display = "none";
}
//styles are:
// 0 = classic ok and cancel
// can return true for ok, false for cancel
// 1 = num input field, ok and cancel
// returns false for cancel, input field value for ok
async show(title, contents, style) {
if(style === undefined){
style = 0;
}
this.modalTitle.innerHTML = title;
this.modalContent.innerHTML = contents;
this.modalObject.style.display = "flex";
this.overlay.style.display = "block";
// if no input field
if(style == 0){
this.modalNumInput.style.display = "none";
}
else if (style == 1){
this.modalNumInput.style.display = "block";
this.modalNumInput.value = 1;
}
this.modalOK.focus();
let response = await this.waitForUserSelection();
if(response && style == 1){
response = this.modalNumInput.value;
}
return response;
}
async waitForUserSelection() {
//wait for a buttonpress in another function to ste received input to something other than -1
while (this.receivedInput === undefined) await this.timeout(50);
//grab value
let userValue = this.receivedInput;
//reset for the next thing
this.receivedInput = undefined;
return userValue;
}
}