-
Notifications
You must be signed in to change notification settings - Fork 0
/
customPopups.js
87 lines (69 loc) · 2.03 KB
/
customPopups.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
class customPopup extends HTMLElement{
constructor() {
// Always call super first in constructor
super();
this.attachShadow({mode: 'open'});
let mainDiv = document.createElement("div");
mainDiv.className = "popupMain";
mainDiv.style.display = "none";
let contentDiv = document.createElement("div");
contentDiv.className = "popupContent";
let closeButton = document.createElement("button");
closeButton.className = "closeButton";
closeButton.innerText = "Ok"
closeButton.onclick = () => {
this.close();
}
let variableDiv = document.createElement("div");
variableDiv.className = "variableDiv";
contentDiv.appendChild(variableDiv);
contentDiv.appendChild(closeButton);
mainDiv.appendChild(contentDiv);
let style = document.createElement("link");
style.href = "./popupStyle.css";
style.rel = "stylesheet";
style.type = "text/css";
this.shadowRoot.append(mainDiv, style);
this.close = function(){
try{
sounds.close.play().catch(e => {
console.log("closed");
});
} catch(e) {
console.log("closed");
}
mainDiv.className = "popupMain";
mainDiv.style.display = "none";
if (this.onClose){
this.onClose();
}
}
this.open = function(){
this.focus();
try{
sounds.open.play().catch(e => {
console.log("opened");
});
} catch(e) {
console.log("opened");
}
mainDiv.className = "popupMain open";
mainDiv.style.display = "block";
}
this.content = variableDiv;
}
}
customElements.define('custom-popup', customPopup);
function popup(html, onClose){
let toPop = new customPopup();
if (onClose){
toPop.onClose = onClose;
}
if (typeof html === 'string' || html instanceof String){
toPop.content.innerHTML = html;
} else {
toPop.content.appendChild(html);
}
document.body.appendChild(toPop);
return toPop;
}