Add the ability to specify/open the last open tab in the URL
This commit is contained in:
parent
3f94452de6
commit
458cce1c5c
1 changed files with 23 additions and 15 deletions
|
@ -1,29 +1,22 @@
|
||||||
"use strict";
|
"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);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue