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

 
(3 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
const SPAWN_LAYER_COLOR = "spawnLayerColor";
+
function handleModal(button) {
 +
  const modalName = button.dataset.modal;
 +
  let modalContainer;
  
function createModalInteraction(content) {
+
  if (modalName) {
     const boutonModal = content.querySelector(".button");
+
     modalContainer = document.querySelector(
     const modalContent = content.querySelector(".modal");
+
      `.modal[data-modal="${modalName}"]`
     const boutonClose = content.querySelector(".close");
+
     );
 +
  } else {
 +
     modalContainer = button.nextElementSibling;
 +
  }
  
    const openModal = () => {
+
  const closeButton = modalContainer.querySelector(".close-button");
        boutonModal.classList.add("tabber-active");
+
  const { autoCloseChange, autoCloseLink, addEvent } = modalContainer.dataset;
        modalContent.classList.add("gen-active");
 
        window.addEventListener("click", handleClickOutside);
 
    };
 
  
    const closeModal = () => {
+
  button.addEventListener("click", openModal);
        boutonModal.classList.remove("tabber-active");
+
  closeButton.addEventListener("click", closeModal);
        modalContent.classList.remove("gen-active");
 
        window.removeEventListener("click", handleClickOutside);
 
    };
 
  
    const handleClickOutside = (event) => {
+
  if (autoCloseChange === "1") {
        if (event.target === modalContent) {
+
    modalContainer.addEventListener("change", closeModal);
            closeModal();
+
  }
        }
 
    };
 
  
     boutonModal.addEventListener("click", openModal);
+
  if (autoCloseLink === "1") {
     boutonClose.addEventListener("click", closeModal);
+
     const links = modalContainer.querySelectorAll("a");
}
+
     links.forEach((link) => {
 
+
      link.addEventListener("click", closeModal);
function changeSpawnLayerColor(value, selects, spawnLayers, optionMapping) {
+
    });
    if (!optionMapping.hasOwnProperty(value)) {
+
  }
        return;
 
    }
 
  
     let filter;
+
  function openModal() {
 +
     button.classList.add("tabber-active");
 +
    modalContainer.classList.add("show-modal");
 +
    window.addEventListener("click", handleClickOutside);
 +
    window.addEventListener("keydown", handleEscape);
  
     if (value === "black") {
+
     if (addEvent === "1") {
        filter = "invert(1) brightness(0) contrast(100%)";
+
      const modalOpenEvent = new CustomEvent('modalOpen', {
    } else if (value === "white") {
+
        bubbles: true,
        filter = "invert(1) sepia(1) saturate(100%) brightness(3)";
+
        detail: { modal: modalContainer, name: modalName }
    } else {
+
      });
        filter = `hue-rotate(${value}deg)`;
+
      button.dispatchEvent(modalOpenEvent);
 
     }
 
     }
 +
  }
  
     selects.forEach(select => select.value = value);
+
  function closeModal() {
     spawnLayers.forEach(spawnLayer => spawnLayer.style.filter = filter);
+
     button.classList.remove("tabber-active");
 +
    modalContainer.classList.remove("show-modal");
 +
     window.removeEventListener("click", handleClickOutside);
 +
    window.removeEventListener("keydown", handleEscape);
 +
  }
  
    localStorage.setItem(SPAWN_LAYER_COLOR, value);
+
  function handleClickOutside(event) {
}
+
     if (event.target === modalContainer) {
 
+
      closeModal();
function createSelectColor(optionMapping, styleMapping) {
 
     const select = document.createElement("select");
 
    Object.entries(optionMapping).forEach(([key, value]) => {
 
        const option = document.createElement("option");
 
        option.value = key;
 
        option.textContent = value;
 
        select.appendChild(option);
 
    });
 
    Object.entries(styleMapping).forEach(([property, value]) => {
 
        select.style[property] = value;
 
    });
 
 
 
    return select;
 
}
 
 
 
function initializeSpawnLayers(spawnLayerContainers) {
 
    const optionMapping = {
 
        0: "🟥 Rouge",
 
        50: "🟫 Marron",
 
        140: "🟩 Vert",
 
        230: "🟦 Bleu",
 
        290: "🟪 Violet",
 
        black: "⬛ Noir",
 
        white: "⬜ Blanc"
 
 
     }
 
     }
    const styleMapping = {
+
  }
        position: "absolute",
 
        left: "0",
 
        margin: "0",
 
        background: "#434242b3",
 
        color: "white",
 
        borderRadius: "5px",
 
    }
 
 
 
    const selects = [];
 
    const spawnLayers = [];
 
    const spawnLayerColor = localStorage.getItem(SPAWN_LAYER_COLOR);
 
 
 
    spawnLayerContainers.forEach((spawnLayerContainer) => {
 
        const spawnLayer = spawnLayerContainer.querySelector("img");
 
       
 
        if (spawnLayer) {
 
            const selectColor = createSelectColor(optionMapping, styleMapping);
 
            spawnLayerContainer.appendChild(selectColor);
 
            selects.push(selectColor);
 
            spawnLayers.push(spawnLayer);
 
        }
 
    });
 
 
 
    const handleChange = (event) => {
 
        changeSpawnLayerColor(event.target.value, selects, spawnLayers, optionMapping);
 
    };
 
 
 
    selects.forEach(select => {
 
        select.addEventListener("change", handleChange);
 
    });
 
  
     if (spawnLayerColor) {
+
  function handleEscape(event) {
        changeSpawnLayerColor(spawnLayerColor, selects, spawnLayers, optionMapping);
+
     if (event.key === "Escape") {
 +
      closeModal();
 
     }
 
     }
 +
  }
 
}
 
}
  
(() => {
+
function mainModal() {
    const modalContainers = document.querySelectorAll("div#mw-content-text div.modalContainer");
+
  const modalButtons = document.querySelectorAll(".modal-trigger");
    const spawnLayerContainers = content.querySelectorAll("div.modalContainer .spawn-layer");
+
  modalButtons.forEach(handleModal);
 +
}
  
    modalContainers.forEach(createModalInteraction);
+
mainModal();
 
 
    if (spawnLayerContainers.length) {
 
        initializeSpawnLayers(spawnLayerContainers);
 
    }
 
})();
 

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