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

 
(2 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
 
function changeDisplay(button, textContainer, method) {
 
function changeDisplay(button, textContainer, method) {
   button.classList[method]('tabber-active');
+
   button.classList[method]("tabber-active");
   textContainer.children[button.dataset.position].classList[method]('tabber-active');
+
   textContainer.children[button.dataset.position].classList[method]("tabber-active");
 
}
 
}
  
function globalChange(button, allButton, allText, activation, changeUrl, bool = false) {
+
function globalChange(button, allButton, allText, toggleButton, changeUrl, bool = false) {
   if (button.classList.contains('button')) {
+
   var activeButton = allButton.querySelector(".tabber-active");
    var activeButton = allButton.querySelector('.tabber-active');
+
  if (toggleButton || button !== activeButton) {
    if (activation || button !== activeButton) {
+
    if (bool && changeUrl) {
      if (bool && changeUrl) {
+
      var newHash = "#"+button.id;
        var newHash = '#'+button.id;
+
      history.pushState({}, "", newHash);
        history.pushState({}, '', newHash);
+
    }
      }
+
    changeDisplay(button, allText, "toggle");
      changeDisplay(button, allText, 'toggle');
+
    if (button !== activeButton && activeButton !== null) {
      if (button !== activeButton && activeButton !== null) {
+
      changeDisplay(activeButton, allText, "remove");
        changeDisplay(activeButton, allText, 'remove');
 
      }
 
 
     }
 
     }
 
   }
 
   }
 
}
 
}
  
function updateTabber(buttonContainer, textContainer, activation, changeUrl) {
+
function updateTabber(buttonContainer, textContainer, toggleButton, changeUrl) {
   var targetButton = buttonContainer.querySelector(':target');
+
   var targetButton = buttonContainer.querySelector(":target");
  buttonContainer.addEventListener('click', function(event) {
+
   if (targetButton !== null && targetButton.classList.contains("button")) {
    globalChange(event.target, buttonContainer, textContainer, activation, changeUrl, true);
 
  });
 
   if (targetButton !== null) {
 
 
     globalChange(targetButton, buttonContainer, textContainer, false, false);
 
     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() {
 
function updateTabberWithUrlChange() {
   window.addEventListener('hashchange', function(e) {
+
   window.addEventListener("hashchange", function(e) {
 
   var newHash = e.target.location.hash;
 
   var newHash = e.target.location.hash;
   if (newHash !== '') {
+
   if (newHash !== "") {
 
     var targetButton = document.getElementById(newHash.slice(1));
 
     var targetButton = document.getElementById(newHash.slice(1));
     if (targetButton !== null) {
+
     if (targetButton) {
       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);
Ligne 46 : Ligne 47 :
  
 
(function() {
 
(function() {
   var tabberContainer = document.querySelectorAll('div.tabber-container');
+
   var tabberContainer = document.querySelectorAll("div.tabber-container");
 
   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();
})();