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

(Page créée avec « function onTabClick(){ var parent = this.parentElement; var buttonList = parent.children; var contentList = parent.nextElementSibling.children; var position = thi... »)
 
Ligne 1 : Ligne 1 :
function onTabClick(){
+
function changeDisplay(button, textContainer, method) {
 +
  button.classList[method]('tabber-active');
 +
  textContainer.children[button.dataset.position].classList[method]('tabber-active');
 +
}
  
  var parent = this.parentElement;
+
function globalChange(button, allButton, allText, activation, changeUrl, bool = false) {
   var buttonList = parent.children;
+
   if (button.classList.contains('button')) {
  var contentList = parent.nextElementSibling.children;
+
    var activeButton = allButton.querySelector('.tabber-active');
  var position = this.dataset.position;
+
    if (activation || button !== activeButton) {
  var activeButton = parent.querySelector(".tabber-active");
+
      if (bool && changeUrl) {
 
+
        var newHash = '#'+button.id;
  if (activeButton == null) {
+
        history.pushState({}, '', newHash);
    this.classList.add("tabber-active");
+
      }
    contentList[position].classList.add("tabber-active");
+
      changeDisplay(button, allText, 'toggle');
  } else {
+
      if (button !== activeButton && activeButton !== null) {
    var activePosition = activeButton.dataset.position;
+
         changeDisplay(activeButton, allText, 'remove');
    if (activePosition == position) {
 
      if (event.currentTarget==this) {
 
         this.classList.remove("tabber-active");
 
        contentList[position].classList.remove("tabber-active");
 
 
       }
 
       }
    } else {
 
      this.classList.add("tabber-active");
 
      contentList[position].classList.add("tabber-active");
 
      activeButton.classList.remove("tabber-active");
 
      contentList[activePosition].classList.remove("tabber-active");
 
 
     }
 
     }
 
   }
 
   }
 
}
 
}
  
function buttonTabber(context){
+
function updateTabber(tabber, activation, changeUrl) {
 
+
   var [buttonContainer, textContainer] = tabber.children;
   var buttonList = context.querySelectorAll(".button");
+
   var targetButton = buttonContainer.querySelector(':target');
   var targetButton = context.querySelector(".button:target");
+
   buttonContainer.addEventListener('click', function(event) {
   
+
     globalChange(event.target, buttonContainer, textContainer, activation, changeUrl, true);
   buttonList.forEach(function(button) {
 
     button.addEventListener("click", onTabClick, false);
 
 
   });
 
   });
 
   if (targetButton !== null) {
 
   if (targetButton !== null) {
     onTabClick.call(targetButton);
+
     globalChange(targetButton, buttonContainer, textContainer, false, false);
 
   }
 
   }
 +
  window.addEventListener('popstate', function(e) {
 +
    var newHash = e.target.location.hash;
 +
    if (newHash !== '') {
 +
      var targetButton = buttonContainer.querySelector(e.target.location.hash);
 +
      if (targetButton !== null) {
 +
        globalChange(targetButton, buttonContainer, textContainer, false, false);
 +
      }
 +
    }
 +
  });
 
}
 
}
  
(function(){
+
(function() {
   var tabberContainers = document.querySelectorAll("div#mw-content-text div.tabber-container");
+
   var tabberContainer = document.querySelectorAll('div.tabber-container');
   tabberContainers.forEach(function(tabberContainer){
+
   tabberContainer.forEach(function(tabber) {
     buttonTabber(tabberContainer);
+
     updateTabber(tabber, !tabber.dataset.activation, tabber.dataset.url);
 
   });
 
   });
 
})();
 
})();

Version du 2 octobre 2022 à 08:29

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(tabber, activation, changeUrl) {
  var [buttonContainer, textContainer] = tabber.children;
  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);
  }
  window.addEventListener('popstate', function(e) {
    var newHash = e.target.location.hash;
    if (newHash !== '') {
      var targetButton = buttonContainer.querySelector(e.target.location.hash);
      if (targetButton !== null) {
        globalChange(targetButton, buttonContainer, textContainer, false, false);
      }
    }
  });
}

(function() {
  var tabberContainer = document.querySelectorAll('div.tabber-container');
  tabberContainer.forEach(function(tabber) {
    updateTabber(tabber, !tabber.dataset.activation, tabber.dataset.url);
  });
})();