-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmodal.js
More file actions
96 lines (89 loc) · 2.85 KB
/
modal.js
File metadata and controls
96 lines (89 loc) · 2.85 KB
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
document.addEventListener("DOMContentLoaded", () => {
// Bind click event to all elements with class 'open-modal'
const openModalLinks = document.querySelectorAll("a.open-modal");
const modalContainer = document.getElementById("modal-container");
const getActiveModal = () => modalContainer.querySelector(".modal.is-active");
const hideScrollbar = () => {
document.documentElement.style.overflow = "hidden";
document.body.style.overflow = "hidden";
};
const showScrollbar = () => {
document.body.style.overflow = "";
document.documentElement.style.overflow = "";
};
// Bind click event on open-modal links
// Clone modal content to bottom of the page
// Fade in with CSS transition
openModalLinks.forEach((link) => {
link.addEventListener("click", (e) => {
const cell = link.closest(".cell");
if (cell) {
const modal = cell.querySelector(".modal");
if (modal) {
const clonedModal = modal.cloneNode(true);
clonedModal.classList.add("is-active", "fade-in");
modalContainer.appendChild(clonedModal);
setTimeout(() => {
getActiveModal().classList.add("visible");
hideScrollbar();
}, 10);
bindClose();
}
}
});
});
// Bind close button
const bindClose = () => {
getActiveModal()
.querySelectorAll(".close")
.forEach((close) => {
close.addEventListener("click", (e) => {
e.preventDefault();
const modal = getActiveModal();
modal.classList.remove("visible");
setTimeout(() => {
modal.remove();
showScrollbar();
history.pushState(
"",
document.title,
window.location.pathname + window.location.search,
);
}, 500);
});
});
};
// Handle ESC key press to close the modal
document.addEventListener("keydown", (e) => {
if (e.keyCode === 27) {
const activeModal = getActiveModal();
if (activeModal) {
const closeBtn = activeModal.querySelector(".close");
if (closeBtn) {
closeBtn.click();
}
}
}
});
// Open modal if related fragment is open
if (window.location.hash) {
const activeModal = document.querySelector(
`a.open-modal[href="${window.location.hash}"]`,
);
if (activeModal) {
activeModal.click();
}
}
});
document.addEventListener("DOMContentLoaded", function () {
if (typeof _paq === "undefined") {
return;
}
var pageName = window.location.pathname.split("/").filter(Boolean).pop() || "modal";
document.querySelectorAll("a.open-modal").forEach(function (link) {
link.addEventListener("click", function () {
var linkText = link.textContent.trim();
_paq.push(["trackEvent", pageName, "open-modal", linkText]);
});
});
});