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 | + | 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 | + | function updateTabber(tabber, activation, changeUrl) { |
− | + | var [buttonContainer, textContainer] = tabber.children; | |
− | var | + | var targetButton = buttonContainer.querySelector(':target'); |
− | var targetButton = | + | buttonContainer.addEventListener('click', function(event) { |
− | + | globalChange(event.target, buttonContainer, textContainer, activation, changeUrl, true); | |
− | |||
− | |||
}); | }); | ||
if (targetButton !== null) { | 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(){ | + | (function() { |
− | var | + | var tabberContainer = document.querySelectorAll('div.tabber-container'); |
− | + | tabberContainer.forEach(function(tabber) { | |
− | + | 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);
});
})();