-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
124 lines (98 loc) · 3.02 KB
/
popup.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
(function() {
window.popup = { open, close, register };
var active = [];
function register(className, options) {
var popup = {
triggers:
(options && options.triggers) ||
document.querySelectorAll(".trigger." + className),
element:
(options && options.element) ||
document.querySelector(".popup." + className)
};
if (!popup.element) {
console.warn(`No popup element for ${className}`);
return;
}
Object.assign(popup, options);
popup.triggers.forEach(trigger => {
trigger.addEventListener("click", event => {
event.preventDefault();
event.stopPropagation();
window.popup.open(popup, event);
});
});
popup.element.querySelector(".content").addEventListener("click", event => {
event.stopPropagation();
});
var x = popup.element.querySelector(".close");
if (x) {
x.addEventListener("click", event => {
window.popup.close(popup, event);
});
}
if (!popup.preventCloseOnOutsideClick) {
window.addEventListener("click", event => {
window.popup.close(popup, event);
});
}
return popup;
}
function open(popup, event) {
if (popup.beforeOpen) {
if (popup.beforeOpen(event) === false) {
return;
}
}
active.forEach(popup => {
if (popup.beforeClose) {
popup.beforeClose(event);
}
popup.element.querySelector(".content").removeAttribute("style");
popup.element.removeAttribute("style");
active = [];
if (popup.afterClose) {
popup.afterClose(event);
}
});
active.push(popup);
document.documentElement.style.overflow = "hidden";
document.body.style.overflow = "hidden";
document.querySelector("body").classList.add("interstitial");
popup.element.style.display = "flex";
if (popup.center) {
popup.element.style.alignItems = "center";
} else {
popup.element.style.alignItems = "flex-start";
}
popup.element.style.justifyContent = "center";
popup.element.style.opacity = 1;
popup.element.style.zIndex = 999999;
popup.element.scrollTop = 0;
setTimeout(function() {
popup.element.querySelector(".content").style.transform = "scale(1)";
if (popup.afterOpen) {
popup.afterOpen(event);
}
}, 30);
}
function close(popup, event) {
if (!active.includes(popup)) return;
active.splice(active.indexOf(popup), 1);
if (popup.beforeClose) {
popup.beforeClose(event);
}
popup.element.querySelector(".content").style.transform = "scale(0.85)";
popup.element.style.opacity = 0;
document.documentElement.style.overflow = "";
document.body.style.overflow = "";
setTimeout(function() {
popup.element.removeAttribute("style");
popup.element.querySelector(".content").removeAttribute("style");
document.querySelector("body").classList.remove("interstitial");
if (popup.afterClose) {
popup.afterClose(event);
}
}, 300);
}
})();