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) => {
// Close via clicking outside of bounds
window.onclick = (event) => {
if (event.target.classList.contains("modal")) {
let modal = event.target;
dispatchModalClosing(modal);
modal.classList.remove("visible");
modal.removeAttribute("active");
dispatchModalClosed(modal);
document.body.classList.remove("modal-no-scroll");
}
};
// Close via close btn
document.querySelectorAll(".modal .close").forEach((closeBtn) => {
closeBtn.addEventListener('click', (evt) => {
let modal = closeBtn.parentElement.parentElement;
dispatchModalClosing(modal);
modal.classList.remove("visible");
modal.removeAttribute("active");
dispatchModalClosed(modal);
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) => {
modalTrigerEl.addEventListener('click', (evt) => {
let modal = document.getElementById(modalTrigerEl.getAttribute("for-modal"));
dispatchModalOpening(modal);
modal.classList.add("visible");
modal.setAttribute("active", "");
dispatchModalOpened(modal);
document.body.classList.add("modal-no-scroll");
});
});
document.body.addEventListener('keydown', (evt) => {
if (evt.key == "Escape") {
let modal = document.querySelector(".modal[active]");
modal.classList.remove("visible");
modal.removeAttribute("active");
document.body.classList.remove("modal-no-scroll");
}
// Destroy embeds on modal close
document.addEventListener("modalClosed", (evt) => {
evt.detail.message.querySelectorAll("iframe").forEach((iframe) => {
const origSrc = iframe.getAttribute("src");
iframe.removeAttribute("src");
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>
</div>
<div class="card" for-modal="JP-Pandas">
<div class="card">
<h3 class="header">Predicting Hospital Surges</h3>
<p class="content">
Using NOAA, OpenFEMA and hospitalization data to predict hospital surges based on natural disasters.
@ -344,7 +344,7 @@
2018 USIgnite Smart Cities Connect Conference
</h3>
<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>
</iframe>
</div>
@ -373,7 +373,7 @@
<h3 class="header"> 2017 USIgnite Smart Cities Connect Conference </h3>
<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>
</iframe>
</div>