MediaWiki:Script/Modal.js : Différence entre versions

 
(2 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
function createModalInteraction(content) {
+
function handleModal(button) {
    const boutonModal = content.querySelector(".button");
+
  const modalName = button.dataset.modal;
    const modalContent = content.querySelector(".modal");
+
  let modalContainer;
    const boutonClose = content.querySelector(".close");
 
  
    const openModal = () => {
+
  if (modalName) {
        boutonModal.classList.add("tabber-active");
+
    modalContainer = document.querySelector(
        modalContent.classList.add("gen-active");
+
      `.modal[data-modal="${modalName}"]`
        window.addEventListener("click", handleClickOutside);
+
    );
    };
+
  } else {
 +
    modalContainer = button.nextElementSibling;
 +
  }
  
    const closeModal = () => {
+
  const closeButton = modalContainer.querySelector(".close-button");
        boutonModal.classList.remove("tabber-active");
+
  const { autoCloseChange, autoCloseLink, addEvent } = modalContainer.dataset;
        modalContent.classList.remove("gen-active");
 
        window.removeEventListener("click", handleClickOutside);
 
    };
 
  
    const handleClickOutside = (event) => {
+
  button.addEventListener("click", openModal);
        if (event.target === modalContent) {
+
  closeButton.addEventListener("click", closeModal);
            closeModal();
 
        }
 
    };
 
  
     boutonModal.addEventListener("click", openModal);
+
  if (autoCloseChange === "1") {
     boutonClose.addEventListener("click", closeModal);
+
     modalContainer.addEventListener("change", closeModal);
 +
  }
 +
 
 +
  if (autoCloseLink === "1") {
 +
    const links = modalContainer.querySelectorAll("a");
 +
    links.forEach((link) => {
 +
      link.addEventListener("click", closeModal);
 +
    });
 +
  }
 +
 
 +
  function openModal() {
 +
    button.classList.add("tabber-active");
 +
     modalContainer.classList.add("show-modal");
 +
    window.addEventListener("click", handleClickOutside);
 +
    window.addEventListener("keydown", handleEscape);
 +
 
 +
    if (addEvent === "1") {
 +
      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();
 +
    }
 +
  }
 +
}
 +
 
 +
function mainModal() {
 +
  const modalButtons = document.querySelectorAll(".modal-trigger");
 +
  modalButtons.forEach(handleModal);
 
}
 
}
  
(() => {
+
mainModal();
    const modalContainers = document.querySelectorAll("div#mw-content-text div.modalContainer");
 
    modalContainers.forEach(createModalInteraction);
 
})();
 

Version actuelle datée du 23 janvier 2025 à 16:21

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 { autoCloseChange, autoCloseLink, addEvent } = modalContainer.dataset;

  button.addEventListener("click", openModal);
  closeButton.addEventListener("click", closeModal);

  if (autoCloseChange === "1") {
    modalContainer.addEventListener("change", closeModal);
  }

  if (autoCloseLink === "1") {
    const links = modalContainer.querySelectorAll("a");
    links.forEach((link) => {
      link.addEventListener("click", closeModal);
    });
  }

  function openModal() {
    button.classList.add("tabber-active");
    modalContainer.classList.add("show-modal");
    window.addEventListener("click", handleClickOutside);
    window.addEventListener("keydown", handleEscape);

    if (addEvent === "1") {
      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();
    }
  }
}

function mainModal() {
  const modalButtons = document.querySelectorAll(".modal-trigger");
  modalButtons.forEach(handleModal);
}

mainModal();