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

 
(5 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(tabber, activation, changeUrl) {
+
function updateTabber(buttonContainer, textContainer, toggleButton, changeUrl) {
  var [buttonContainer, textContainer] = tabber.children;
+
   var targetButton = buttonContainer.querySelector(":target");
   var targetButton = buttonContainer.querySelector(':target');
+
   if (targetButton !== null && targetButton.classList.contains("button")) {
  buttonContainer.addEventListener('click', function(event) {
 
    globalChange(event.target, buttonContainer, textContainer, activation, changeUrl, true);
 
  });
 
   if (targetButton !== null) {
 
 
     globalChange(targetButton, buttonContainer, textContainer, false, false);
 
     globalChange(targetButton, buttonContainer, textContainer, false, false);
 
   }
 
   }
   window.addEventListener('popstate', function(e) {
+
  buttonContainer.addEventListener("click", function(event) {
    var newHash = e.target.location.hash;
+
    var target = event.target.closest(".button");
    if (newHash !== '') {
+
    if (target) {
      var targetButton = document.getElementById(newHash.slice(1));
+
      globalChange(target, buttonContainer, textContainer, toggleButton, changeUrl, true);
       if (targetButton !== null) {
+
    }
 +
  });
 +
}
 +
 
 +
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);
 
         globalChange(targetButton, buttonContainer, textContainer, false, false);
 
       }
 
       }
 
     }
 
     }
 +
  }
 
   });
 
   });
 
}
 
}
  
 
(function() {
 
(function() {
   var tabberContainer = document.querySelectorAll('div.tabber-container');
+
   var tabberContainer = document.querySelectorAll("div.tabber-container");
 
   tabberContainer.forEach(function(tabber) {
 
   tabberContainer.forEach(function(tabber) {
     updateTabber(tabber, !tabber.dataset.activation, tabber.dataset.url);
+
     var [buttonContainer, textContainer] = tabber.children;
 +
    var toggleButton = tabber.dataset.toggle === "1";
 +
    var changeUrl = tabber.dataset.url === "1";
 +
    updateTabber(buttonContainer, textContainer, toggleButton, changeUrl);
 
   });
 
   });
 +
  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();
})();