54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
(function () {
|
|
class Modal {
|
|
constructor(element) {
|
|
this.element = element;
|
|
this.onBackdropClick = this.onBackdropClick.bind(this);
|
|
this.onKeydown = this.onKeydown.bind(this);
|
|
}
|
|
|
|
show() {
|
|
this.element.classList.add("show");
|
|
this.element.setAttribute("aria-hidden", "false");
|
|
document.body.style.overflow = "hidden";
|
|
this.element.addEventListener("click", this.onBackdropClick);
|
|
document.addEventListener("keydown", this.onKeydown);
|
|
}
|
|
|
|
hide() {
|
|
this.element.classList.remove("show");
|
|
this.element.setAttribute("aria-hidden", "true");
|
|
document.body.style.overflow = "";
|
|
this.element.removeEventListener("click", this.onBackdropClick);
|
|
document.removeEventListener("keydown", this.onKeydown);
|
|
}
|
|
|
|
onBackdropClick(event) {
|
|
if (event.target === this.element || event.target.closest("[data-bs-dismiss='modal']")) {
|
|
this.hide();
|
|
}
|
|
}
|
|
|
|
onKeydown(event) {
|
|
if (event.key === "Escape") {
|
|
this.hide();
|
|
}
|
|
}
|
|
}
|
|
|
|
function installAlertDismiss() {
|
|
document.addEventListener("click", (event) => {
|
|
const button = event.target.closest("[data-bs-dismiss='alert']");
|
|
if (!button) {
|
|
return;
|
|
}
|
|
const alert = button.closest(".alert");
|
|
if (alert) {
|
|
alert.remove();
|
|
}
|
|
});
|
|
}
|
|
|
|
installAlertDismiss();
|
|
window.bootstrap = { Modal };
|
|
})();
|