decentralized-video-chat/public/js/modal.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-31 02:16:08 +08:00
(function () {
"use strict";
const modals = document.getElementsByClassName("modal");
const modalTriggers = document.getElementsByClassName("modal-trigger");
2020-03-24 11:20:28 +08:00
2020-03-31 02:16:08 +08:00
function openModal(el) {
2020-03-24 11:20:28 +08:00
if (el) {
2020-03-31 02:16:08 +08:00
document.body.classList.add("modal-is-active");
el.classList.add("is-active");
2020-03-24 11:20:28 +08:00
}
}
2020-03-31 02:16:08 +08:00
function closeModals() {
document.body.classList.remove("modal-is-active");
2020-03-24 11:20:28 +08:00
for (let i = 0; i < modals.length; i++) {
2020-03-31 02:16:08 +08:00
modals[i].classList.remove("is-active");
2020-03-24 11:20:28 +08:00
}
}
if (modals.length > 0 && modalTriggers.length > 0) {
for (let i = 0; i < modalTriggers.length; i++) {
2020-03-31 02:16:08 +08:00
let modalTrigger = modalTriggers[i];
let modal = document.getElementById(
modalTrigger.getAttribute("aria-controls")
);
2020-03-24 11:20:28 +08:00
if (modal) {
// Modal video
2020-03-31 02:16:08 +08:00
if (modalTrigger.hasAttribute("data-video")) {
if (modal.querySelector("iframe") !== null) {
modal
.querySelector("iframe")
.setAttribute("src", modalTrigger.getAttribute("data-video"));
} else if (modal.querySelector("video") !== null) {
modal
.querySelector("video")
.setAttribute("src", modalTrigger.getAttribute("data-video"));
2020-03-24 11:20:28 +08:00
}
}
2020-03-31 02:16:08 +08:00
modalTrigger.addEventListener("click", function (e) {
e.preventDefault();
if (modalTrigger.hasAttribute("aria-controls")) {
openModal(modal);
2020-03-24 11:20:28 +08:00
}
2020-03-31 02:16:08 +08:00
});
2020-03-24 11:20:28 +08:00
}
}
}
2020-03-31 02:16:08 +08:00
document.addEventListener("click", function (e) {
if (
e.target.classList.contains("modal") ||
e.target.classList.contains("modal-close-trigger")
) {
e.preventDefault();
closeModals();
2020-03-24 11:20:28 +08:00
}
2020-03-31 02:16:08 +08:00
});
2020-03-24 11:20:28 +08:00
2020-03-31 02:16:08 +08:00
document.addEventListener("keydown", function (event) {
var e = event || window.event;
2020-03-24 11:20:28 +08:00
if (e.keyCode === 27) {
2020-03-31 02:16:08 +08:00
closeModals();
2020-03-24 11:20:28 +08:00
}
2020-03-31 02:16:08 +08:00
});
})();