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]( | + | button.classList[method]("tabber-active"); |
− | textContainer.children[button.dataset.position].classList[method]( | + | textContainer.children[button.dataset.position].classList[method]("tabber-active"); |
} | } | ||
− | function globalChange(button, allButton, allText, | + | 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, | + | function updateTabber(buttonContainer, textContainer, toggleButton, changeUrl) { |
− | var targetButton = buttonContainer.querySelector( | + | var targetButton = buttonContainer.querySelector(":target"); |
− | + | if (targetButton !== null && targetButton.classList.contains("button")) { | |
− | |||
− | |||
− | 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( | + | 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 | + | if (targetButton) { |
− | if (targetButton.classList.contains( | + | 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( | + | 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; | ||
− | + | 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();
})();