This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal.js
59 lines (51 loc) · 1.82 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
function escapeModal() {
const currentModalVisibility = document.getElementById("modal").classList;
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" && currentModalVisibility.contains("d-none") == false) {
toggleModal();
}
})
}
function attachModalListeners() {
document.getElementById("close_modal").addEventListener("click", toggleModal);
document.getElementById("overlay").addEventListener("click", toggleModal);
escapeModal();
}
function detachModalListeners() {
document.getElementById("close_modal").removeEventListener("click", toggleModal);
document.getElementById("overlay").removeEventListener("click", toggleModal);
}
function removeModalContent() {
const modalContent = document.getElementById("fullTextContentContainer");
modalContent.innerHTML = "";
}
function saveStartingScrollHeight() {
const startingScrollHeight = window.pageYOffset;
const main = document.getElementById("main");
main.dataset.scrollHeight = startingScrollHeight;
}
function revertToStartingScrollHeight() {
const main = document.getElementById("main");
const startingScrollHeight = main.dataset.scrollHeight;
window.scrollTo(0, startingScrollHeight);
delete main.dataset.scrollHeight;
}
function toggleModal() {
const currentModalVisibility = document.getElementById("modal").classList;
const mainLayerClasses = document.getElementById("main").classList;
if (currentModalVisibility.contains("d-none")) {
saveStartingScrollHeight();
currentModalVisibility.remove("d-none");
mainLayerClasses.add("position-fixed")
currentModalVisibility.add("open");
attachModalListeners();
removeModalContent();
}
else {
revertToStartingScrollHeight();
currentModalVisibility.add("d-none");
currentModalVisibility.remove("open");
mainLayerClasses.remove("position-fixed")
detachModalListeners();
}
}