Modal events!

This commit is contained in:
oitsjustjose 2020-03-05 22:50:07 -05:00
commit a866692311
2 changed files with 104 additions and 10 deletions

View file

@ -1,37 +1,131 @@
const dispatchModalClosed = (modal) => {
let evt = new CustomEvent(
"modalClosed",
{
detail: {
message: modal
},
bubbles: true,
cancelable: true
}
);
document.dispatchEvent(evt);
};
const dispatchModalClosing = (modal) => {
let evt = new CustomEvent(
"modalClosing",
{
detail: {
message: modal
},
bubbles: true,
cancelable: true
}
);
document.dispatchEvent(evt);
};
const dispatchModalOpened = (modal) => {
let evt = new CustomEvent(
"modalOpened",
{
detail: {
message: modal
},
bubbles: true,
cancelable: true
}
);
document.dispatchEvent(evt);
};
const dispatchModalOpening = (modal) => {
let evt = new CustomEvent(
"modalOpening",
{
detail: {
message: modal
},
bubbles: true,
cancelable: true
}
);
document.dispatchEvent(evt);
};
window.addEventListener("DOMContentLoaded", (evt) => { window.addEventListener("DOMContentLoaded", (evt) => {
// Close via clicking outside of bounds
window.onclick = (event) => { window.onclick = (event) => {
if (event.target.classList.contains("modal")) { if (event.target.classList.contains("modal")) {
let modal = event.target; let modal = event.target;
dispatchModalClosing(modal);
modal.classList.remove("visible"); modal.classList.remove("visible");
modal.removeAttribute("active"); modal.removeAttribute("active");
dispatchModalClosed(modal);
document.body.classList.remove("modal-no-scroll"); document.body.classList.remove("modal-no-scroll");
} }
}; };
// Close via close btn
document.querySelectorAll(".modal .close").forEach((closeBtn) => { document.querySelectorAll(".modal .close").forEach((closeBtn) => {
closeBtn.addEventListener('click', (evt) => { closeBtn.addEventListener('click', (evt) => {
let modal = closeBtn.parentElement.parentElement; let modal = closeBtn.parentElement.parentElement;
dispatchModalClosing(modal);
modal.classList.remove("visible"); modal.classList.remove("visible");
modal.removeAttribute("active"); modal.removeAttribute("active");
dispatchModalClosed(modal);
document.body.classList.remove("modal-no-scroll"); document.body.classList.remove("modal-no-scroll");
}); });
}); });
// Close via escape-key
document.body.addEventListener('keydown', (evt) => {
if (evt.key == "Escape") {
let modal = document.querySelector(".modal[active]");
dispatchModalClosing(modal);
modal.classList.remove("visible");
modal.removeAttribute("active");
dispatchModalClosed(modal);
document.body.classList.remove("modal-no-scroll");
}
});
// Open via for-modal element
document.querySelectorAll("[for-modal]").forEach((modalTrigerEl) => { document.querySelectorAll("[for-modal]").forEach((modalTrigerEl) => {
modalTrigerEl.addEventListener('click', (evt) => { modalTrigerEl.addEventListener('click', (evt) => {
let modal = document.getElementById(modalTrigerEl.getAttribute("for-modal")); let modal = document.getElementById(modalTrigerEl.getAttribute("for-modal"));
dispatchModalOpening(modal);
modal.classList.add("visible"); modal.classList.add("visible");
modal.setAttribute("active", ""); modal.setAttribute("active", "");
dispatchModalOpened(modal);
document.body.classList.add("modal-no-scroll"); document.body.classList.add("modal-no-scroll");
}); });
}); });
document.body.addEventListener('keydown', (evt) => { // Destroy embeds on modal close
if (evt.key == "Escape") { document.addEventListener("modalClosed", (evt) => {
let modal = document.querySelector(".modal[active]"); evt.detail.message.querySelectorAll("iframe").forEach((iframe) => {
modal.classList.remove("visible"); const origSrc = iframe.getAttribute("src");
modal.removeAttribute("active"); iframe.removeAttribute("src");
document.body.classList.remove("modal-no-scroll"); iframe.setAttribute("data-src", origSrc);
});
});
// Re-init embeds on modal open
document.addEventListener("modalOpening", (evt) => {
evt.detail.message.querySelectorAll("iframe").forEach((iframe) => {
if (iframe.hasAttribute("data-src")) {
const origSrc = iframe.getAttribute("data-src");
iframe.removeAttribute("data-src");
iframe.setAttribute("src", origSrc);
} }
}); });
});
}); });

View file

@ -147,7 +147,7 @@
</p> </p>
</div> </div>
<div class="card" for-modal="JP-Pandas"> <div class="card">
<h3 class="header">Predicting Hospital Surges</h3> <h3 class="header">Predicting Hospital Surges</h3>
<p class="content"> <p class="content">
Using NOAA, OpenFEMA and hospitalization data to predict hospital surges based on natural disasters. Using NOAA, OpenFEMA and hospitalization data to predict hospital surges based on natural disasters.
@ -344,7 +344,7 @@
2018 USIgnite Smart Cities Connect Conference 2018 USIgnite Smart Cities Connect Conference
</h3> </h3>
<div class="centered embed"> <div class="centered embed">
<iframe src="https://www.youtube-nocookie.com/embed/6nkfsjqPBio?rel=0" frameborder="0" <iframe data-src="https://www.youtube-nocookie.com/embed/6nkfsjqPBio?rel=0" frameborder="0"
allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
</iframe> </iframe>
</div> </div>
@ -373,7 +373,7 @@
<h3 class="header"> 2017 USIgnite Smart Cities Connect Conference </h3> <h3 class="header"> 2017 USIgnite Smart Cities Connect Conference </h3>
<div class="centered embed"> <div class="centered embed">
<iframe src="https://www.youtube-nocookie.com/embed/oL7Xc_uvFyc?rel=0" frameborder="0" <iframe data-src="https://www.youtube-nocookie.com/embed/oL7Xc_uvFyc?rel=0" frameborder="0"
allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
</iframe> </iframe>
</div> </div>