Add the ability to specify/open the last open tab in the URL

This commit is contained in:
blankie 2023-05-30 13:41:49 +07:00
parent 3f94452de6
commit 01546a1a51
Signed by: blankie
GPG key ID: CC15FC822C7F61F5

View file

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