Add better support for tabs #15

Manually merged
cadence merged 5 commits from blankie/breezewiki:tabs into main 2024-06-05 11:09:06 +00:00
Showing only changes of commit ead6896818 - Show all commits

View file

@ -1,29 +1,22 @@
"use strict";
function handleTabber(tabber) {
let tabToFind = location.hash.length > 1 ? location.hash.substring(1) : null;
for (let tabber of document.body.querySelectorAll(".wds-tabber")) {
let [tabs, contents] = getTabs(tabber);
for (let i in tabs) {
let tab = tabs[i];
let content = contents[i];
tab.addEventListener("click", function(e) {
let [currentTab, currentContent] = getCurrentTab(tabber);
if (currentTab) {
currentTab.classList.remove("wds-is-current");
}
if (currentContent) {
currentContent.classList.remove("wds-is-current");
}
tab.classList.add("wds-is-current");
content.classList.add("wds-is-current");
tab.addEventListener("click", function(e) {
setCurrentTab(tabber, tab, content);
e.preventDefault();
});
if (tab.dataset.hash === tabToFind) {
setCurrentTab(tabber, tab, content);
tabToFind = null;
}
}
for (let tabber of document.body.querySelectorAll(".wds-tabber")) {
handleTabber(tabber);
}
document.body.classList.remove("bw-tabs-nojs");
@ -61,3 +54,18 @@ function getCurrentTab(tabber) {
return [tab, content];
}
function setCurrentTab(tabber, tab, content) {
let [currentTab, currentContent] = getCurrentTab(tabber);
if (currentTab) {
currentTab.classList.remove("wds-is-current");
}
if (currentContent) {
currentContent.classList.remove("wds-is-current");
}
tab.classList.add("wds-is-current");
content.classList.add("wds-is-current");
location.hash = "#" + tab.dataset.hash;
history.pushState(null, "", "#" + tab.dataset.hash);
}