MediaWiki:Script/Modal.js : Différence entre versions
Ligne 1 : | Ligne 1 : | ||
− | + | mainModal(); | |
− | modalButtons.forEach((button) | + | function mainModal() { |
+ | const modalButtons = document.querySelectorAll(".modal-trigger"); | ||
+ | modalButtons.forEach(handleModal); | ||
+ | } | ||
+ | |||
+ | function handleModal(button) { | ||
const modalName = button.dataset.modal; | const modalName = button.dataset.modal; | ||
let modalContainer; | let modalContainer; | ||
Ligne 15 : | Ligne 20 : | ||
const closeButton = modalContainer.querySelector(".close-button"); | const closeButton = modalContainer.querySelector(".close-button"); | ||
const autoClose = modalContainer.dataset.autoClose === "1"; | const autoClose = modalContainer.dataset.autoClose === "1"; | ||
+ | const addEvent = modalContainer.dataset.addEvent === "1"; | ||
button.addEventListener("click", openModal); | button.addEventListener("click", openModal); | ||
Ligne 28 : | Ligne 34 : | ||
window.addEventListener("click", handleClickOutside); | window.addEventListener("click", handleClickOutside); | ||
window.addEventListener("keydown", handleEscape); | window.addEventListener("keydown", handleEscape); | ||
+ | |||
+ | if (addEvent) { | ||
+ | const modalOpenEvent = new CustomEvent('modalOpen', { | ||
+ | bubbles: true, | ||
+ | detail: { modal: modalContainer, name: modalName } | ||
+ | }); | ||
+ | button.dispatchEvent(modalOpenEvent); | ||
+ | } | ||
} | } | ||
Ligne 48 : | Ligne 62 : | ||
} | } | ||
} | } | ||
− | } | + | } |
Version du 6 décembre 2024 à 08:11
mainModal();
function mainModal() {
const modalButtons = document.querySelectorAll(".modal-trigger");
modalButtons.forEach(handleModal);
}
function handleModal(button) {
const modalName = button.dataset.modal;
let modalContainer;
if (modalName) {
modalContainer = document.querySelector(
`.modal[data-modal="${modalName}"]`
);
} else {
modalContainer = button.nextElementSibling;
}
const closeButton = modalContainer.querySelector(".close-button");
const autoClose = modalContainer.dataset.autoClose === "1";
const addEvent = modalContainer.dataset.addEvent === "1";
button.addEventListener("click", openModal);
closeButton.addEventListener("click", closeModal);
if (autoClose) {
modalContainer.addEventListener("change", closeModal);
}
function openModal() {
button.classList.add("tabber-active");
modalContainer.classList.add("show-modal");
window.addEventListener("click", handleClickOutside);
window.addEventListener("keydown", handleEscape);
if (addEvent) {
const modalOpenEvent = new CustomEvent('modalOpen', {
bubbles: true,
detail: { modal: modalContainer, name: modalName }
});
button.dispatchEvent(modalOpenEvent);
}
}
function closeModal() {
button.classList.remove("tabber-active");
modalContainer.classList.remove("show-modal");
window.removeEventListener("click", handleClickOutside);
window.removeEventListener("keydown", handleEscape);
}
function handleClickOutside(event) {
if (event.target === modalContainer) {
closeModal();
}
}
function handleEscape(event) {
if (event.key === "Escape") {
closeModal();
}
}
}