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

Ligne 31 : Ligne 31 :
  
 
function updateTabberWithUrlChange() {
 
function updateTabberWithUrlChange() {
   window.addEventListener('popstate', function(e) {
+
   window.addEventListener('hashchange', function(e) {
 
   var newHash = e.target.location.hash;
 
   var newHash = e.target.location.hash;
 
   if (newHash !== '') {
 
   if (newHash !== '') {

Version du 24 octobre 2022 à 07:12

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) {
  if (button.classList.contains('button')) {
    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');
  buttonContainer.addEventListener('click', function(event) {
    globalChange(event.target, buttonContainer, textContainer, activation, changeUrl, true);
  });
  if (targetButton !== null) {
    globalChange(targetButton, buttonContainer, textContainer, false, false);
  }
}

function updateTabberWithUrlChange() {
  window.addEventListener('hashchange', function(e) {
  var newHash = e.target.location.hash;
  if (newHash !== '') {
    var targetButton = document.getElementById(newHash.slice(1));
    if (targetButton !== null) {
      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();
})();