Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit 65d10c7

Browse files
committed
⚡ Better modal windows handling
* nanoid-generated identifiers * list of modals are stored in an object rather than an array * clicking or touching a modal window will bring it to the top, emulating common window managers behavior
1 parent f317248 commit 65d10c7

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/assets/css/modal.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ div.modal_popup.error {border-width: 0.7vh; border-style: double;}
1717
div.modal_popup.warning {border-width: 0.5vh; border-style: solid;}
1818
div.modal_popup.info {border-width: 0.2vh; border-style: solid;}
1919

20+
div.modal_popup.focus {
21+
z-index: 2500 !important;
22+
}
23+
2024
div.modal_popup.blink {
2125
border: none;
2226
animation-name: blink;

src/classes/modal.class.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
window.modals = [];
1+
window.modals = {};
22

33
class Modal {
44
constructor(options, onclose) {
55
if (!options || !options.type) throw "Missing parameters";
66

77
this.type = options.type;
8-
this.id = window.modals.length;
8+
this.id = require("nanoid")();
9+
while (typeof window.modals[this.id] !== "undefined") {
10+
this.id = require("nanoid")();
11+
}
912
this.title = options.title || options.type || "Modal window";
1013
this.message = options.message || "Lorem ipsum dolor sit amet.";
1114
this.onclose = onclose;
12-
let classes = "modal_popup";
15+
this.classes = "modal_popup";
1316
let buttons = [];
1417
let zindex = 0;
1518

@@ -18,29 +21,29 @@ class Modal {
1821

1922
switch(this.type) {
2023
case "error":
21-
classes += " error";
24+
this.classes += " error";
2225
zindex = 1500;
23-
buttons.push({label:"PANIC", action:"window.modals["+this.id+"].close();"}, {label:"RELOAD", action:"window.location.reload(true);"});
26+
buttons.push({label:"PANIC", action:"window.modals['"+this.id+"'].close();"}, {label:"RELOAD", action:"window.location.reload(true);"});
2427
break;
2528
case "warning":
26-
classes += " warning";
29+
this.classes += " warning";
2730
zindex = 1000;
28-
buttons.push({label:"OK", action:"window.modals["+this.id+"].close();"});
31+
buttons.push({label:"OK", action:"window.modals['"+this.id+"'].close();"});
2932
break;
3033
case "custom":
31-
classes += " info custom";
34+
this.classes += " info custom";
3235
zindex = 500;
3336
buttons = options.buttons || [];
34-
buttons.push({label:"Close", action:"window.modals["+this.id+"].close();"});
37+
buttons.push({label:"Close", action:"window.modals['"+this.id+"'].close();"});
3538
break;
3639
default:
37-
classes += " info";
40+
this.classes += " info";
3841
zindex = 500;
39-
buttons.push({label:"OK", action:"window.modals["+this.id+"].close();"});
42+
buttons.push({label:"OK", action:"window.modals['"+this.id+"'].close();"});
4043
break;
4144
}
4245

43-
let DOMstring = `<div id="modal_${this.id}" class="${classes}" style="z-index:${zindex+this.id};">
46+
let DOMstring = `<div id="modal_${this.id}" class="${this.classes}" style="z-index:${zindex+Object.keys(window.modals).length};">
4447
<h1>${this.title}</h1>
4548
${this.type === "custom" ? options.html : "<h5>"+this.message+"</h5>"}
4649
<div>`;
@@ -56,17 +59,39 @@ class Modal {
5659
window.audioManager.denied.play();
5760
setTimeout(() => {
5861
modalElement.remove();
62+
delete window.modals[this.id];
5963
}, 100);
6064

6165
if (typeof this.onclose === "function") {
6266
this.onclose();
6367
}
6468
};
6569

70+
this.focus = () => {
71+
let modalElement = document.getElementById("modal_"+this.id);
72+
modalElement.setAttribute("class", this.classes+" focus");
73+
Object.keys(window.modals).forEach(id => {
74+
if (id === this.id) return;
75+
window.modals[id].unfocus();
76+
});
77+
};
78+
79+
this.unfocus = () => {
80+
let modalElement = document.getElementById("modal_"+this.id);
81+
modalElement.setAttribute("class", this.classes);
82+
};
83+
6684
let tmp = document.createElement("div");
6785
tmp.innerHTML = DOMstring;
6886
let element = tmp.firstChild;
6987

88+
element.addEventListener("mousedown", () => {
89+
this.focus();
90+
});
91+
element.addEventListener("touchstart", () => {
92+
this.focus();
93+
});
94+
7095
switch(this.type) {
7196
case "error":
7297
window.audioManager.error.play();

0 commit comments

Comments
 (0)