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

 
Ligne 4 : Ligne 4 :
 
}
 
}
  
function globalChange(button, allButton, allText, activation, changeUrl, bool = false) {
+
function globalChange(button, allButton, allText, toggleButton, changeUrl, bool = false) {
 
   var activeButton = allButton.querySelector(".tabber-active");
 
   var activeButton = allButton.querySelector(".tabber-active");
   if (activation || button !== activeButton) {
+
   if (toggleButton || button !== activeButton) {
 
     if (bool && changeUrl) {
 
     if (bool && changeUrl) {
 
       var newHash = "#"+button.id;
 
       var newHash = "#"+button.id;
Ligne 18 : Ligne 18 :
 
}
 
}
  
function updateTabber(buttonContainer, textContainer, activation, changeUrl) {
+
function updateTabber(buttonContainer, textContainer, toggleButton, changeUrl) {
 
   var targetButton = buttonContainer.querySelector(":target");
 
   var targetButton = buttonContainer.querySelector(":target");
 
   if (targetButton !== null && targetButton.classList.contains("button")) {
 
   if (targetButton !== null && targetButton.classList.contains("button")) {
Ligne 26 : Ligne 26 :
 
     var target = event.target.closest(".button");
 
     var target = event.target.closest(".button");
 
     if (target) {
 
     if (target) {
       globalChange(target, buttonContainer, textContainer, activation, changeUrl, true);
+
       globalChange(target, buttonContainer, textContainer, toggleButton, changeUrl, true);
 
     }
 
     }
 
   });
 
   });
Ligne 50 : Ligne 50 :
 
   tabberContainer.forEach(function(tabber) {
 
   tabberContainer.forEach(function(tabber) {
 
     var [buttonContainer, textContainer] = tabber.children;
 
     var [buttonContainer, textContainer] = tabber.children;
     updateTabber(buttonContainer, textContainer, !tabber.dataset.activation, tabber.dataset.url);
+
     var toggleButton = tabber.dataset.toggle === "1";
 +
    var changeUrl = tabber.dataset.url === "1";
 +
    updateTabber(buttonContainer, textContainer, toggleButton, changeUrl);
 
   });
 
   });
 
   updateTabberWithUrlChange();
 
   updateTabberWithUrlChange();
 
})();
 
})();

Version actuelle datée du 25 mai 2024 à 19:22

function changeDisplay(button, textContainer, method) {
  button.classList[method]("tabber-active");
  textContainer.children[button.dataset.position].classList[method]("tabber-active");
}

function globalChange(button, allButton, allText, toggleButton, changeUrl, bool = false) {
  var activeButton = allButton.querySelector(".tabber-active");
  if (toggleButton || button !== activeButton) {
    if (bool && changeUrl) {
      var newHash = "#"+button.id;
      history.pushState({}, "", newHash);
    }
    changeDisplay(button, allText, "toggle");
    if (button !== activeButton && activeButton !== null) {
      changeDisplay(activeButton, allText, "remove");
    }
  }
}

function updateTabber(buttonContainer, textContainer, toggleButton, changeUrl) {
  var targetButton = buttonContainer.querySelector(":target");
  if (targetButton !== null && targetButton.classList.contains("button")) {
    globalChange(targetButton, buttonContainer, textContainer, false, false);
  }
  buttonContainer.addEventListener("click", function(event) {
    var target = event.target.closest(".button");
    if (target) {
      globalChange(target, buttonContainer, textContainer, toggleButton, changeUrl, true);
    }
  });
}

function updateTabberWithUrlChange() {
  window.addEventListener("hashchange", function(e) {
  var newHash = e.target.location.hash;
  if (newHash !== "") {
    var targetButton = document.getElementById(newHash.slice(1));
    if (targetButton) {
      if (targetButton.classList.contains("button")) {
        var [buttonContainer, textContainer] = targetButton.parentElement.parentElement.children;
        globalChange(targetButton, buttonContainer, textContainer, false, false);
      }
    }
  }
  });
}

(function() {
  var tabberContainer = document.querySelectorAll("div.tabber-container");
  tabberContainer.forEach(function(tabber) {
    var [buttonContainer, textContainer] = tabber.children;
    var toggleButton = tabber.dataset.toggle === "1";
    var changeUrl = tabber.dataset.url === "1";
    updateTabber(buttonContainer, textContainer, toggleButton, changeUrl);
  });
  updateTabberWithUrlChange();
})();