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

Ligne 36 : Ligne 36 :
 
     var targetButton = document.getElementById(newHash.slice(1));
 
     var targetButton = document.getElementById(newHash.slice(1));
 
     if (targetButton !== null) {
 
     if (targetButton !== null) {
       if (targetButton.classList.contains('.button')) {
+
       if (targetButton.classList.contains('button')) {
 
         var [buttonContainer, textContainer] = targetButton.parentElement.parentElement.children;
 
         var [buttonContainer, textContainer] = targetButton.parentElement.parentElement.children;
 
         globalChange(targetButton, buttonContainer, textContainer, false, false);
 
         globalChange(targetButton, buttonContainer, textContainer, false, false);

Version du 26 octobre 2022 à 20:25

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