MediaWiki:Script/Tabber.js

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac)
  • Google Chrome : appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac)
  • Internet Explorer : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5
  • Opera : allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d’exploration → Images et fichiers en cache.
function changeDisplay(button, textContainer, method) {
  button.classList[method]("tabber-active");
  textContainer.children[button.dataset.position].classList[method]("tabber-active");
}

function globalChange(button, allButton, allText, activation, changeUrl, bool = false) {
  var activeButton = allButton.querySelector(".tabber-active");
  if (activation || 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, activation, 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, activation, 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;
    updateTabber(buttonContainer, textContainer, !tabber.dataset.activation, tabber.dataset.url);
  });
  updateTabberWithUrlChange();
})();