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

(Page créée avec « function createModalInteraction(content){ var boutonModal = content.querySelector(".button"); var modalContent = content.querySelector(".modal"); var boutonClose... »)
 
 
(5 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
function createModalInteraction(content){
+
function handleModal(button) {
    
+
   const modalName = button.dataset.modal;
   var boutonModal = content.querySelector(".button");
+
  let modalContainer;
   var modalContent = content.querySelector(".modal");
+
 
   var boutonClose = content.querySelector(".close");
+
   if (modalName) {
    
+
    modalContainer = document.querySelector(
   boutonModal.addEventListener("click", function(){
+
      `.modal[data-modal="${modalName}"]`
    boutonModal.className += " tabber-active";
+
    );
    modalContent.style.display = "block";
+
   } else {
   
+
    modalContainer = button.nextElementSibling;
     boutonClose.addEventListener("click", function(){
+
   }
      modalContent.style.display = "none";
+
 
      boutonModal.className = boutonModal.className.remove("tabber-active");
+
  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);
 
     });
 
     });
      
+
  }
     window.addEventListener("click", function(e){
+
 
      if (e.target == modalContent) {
+
  function openModal() {
         modalContent.style.display = "none";
+
     button.classList.add("tabber-active");
        boutonModal.className = boutonModal.className.remove("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);
 
}
 
}
  
(function(){
+
mainModal();
  var modalContainers = document.querySelectorAll("div#mw-content-text div.modalContainer");
 
  modalContainers.forEach(function(modalContainer){
 
    createModalInteraction(modalContainer);
 
  });
 
})();
 

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();