forked from cadence/breezewiki
Tabber code size and quality
This commit is contained in:
parent
47d92d3a37
commit
755efe3cd6
2 changed files with 30 additions and 64 deletions
|
@ -1,74 +1,40 @@
|
|||
"use strict";
|
||||
|
||||
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);
|
||||
const tabFromHash = location.hash.length > 1 ? location.hash.substring(1) : null
|
||||
|
||||
for (let i in tabs) {
|
||||
let tab = tabs[i];
|
||||
let content = contents[i];
|
||||
for (const tabber of document.body.querySelectorAll(".wds-tabber")) {
|
||||
for (const [tab, content] of getTabberTabs(tabber)) {
|
||||
// set up click listener on every tab
|
||||
tab.addEventListener("click", e => {
|
||||
setCurrentTab(tabber, tab, content)
|
||||
e.preventDefault()
|
||||
})
|
||||
|
||||
tab.addEventListener("click", function(e) {
|
||||
setCurrentTab(tabber, tab, content);
|
||||
e.preventDefault();
|
||||
});
|
||||
if (tab.dataset.hash === tabToFind) {
|
||||
setCurrentTab(tabber, tab, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
document.body.classList.remove("bw-tabs-nojs");
|
||||
|
||||
|
||||
|
||||
function getTabs(tabber) {
|
||||
let tabs = [];
|
||||
let contents = [];
|
||||
|
||||
for (let i of tabber.querySelector(".wds-tabs__wrapper").querySelectorAll(".wds-tabs__tab")) {
|
||||
tabs.push(i);
|
||||
}
|
||||
for (let i of tabber.children) {
|
||||
if (!i.matches(".wds-tab__content")) {
|
||||
continue;
|
||||
}
|
||||
contents.push(i);
|
||||
}
|
||||
|
||||
return [tabs, contents];
|
||||
// re-open a specific tab on page load based on the URL hash
|
||||
if (tab.dataset.hash === tabFromHash) {
|
||||
setCurrentTab(tabber, tab, content)
|
||||
tab.scrollIntoView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getCurrentTab(tabber) {
|
||||
let tab = null;
|
||||
let content = null;
|
||||
|
||||
tab = tabber.querySelector(".wds-tabs__wrapper").querySelector(".wds-tabs__tab.wds-is-current");
|
||||
for (let i of tabber.children) {
|
||||
if (!i.matches(".wds-tab__content.wds-is-current")) {
|
||||
continue;
|
||||
}
|
||||
content = i;
|
||||
break;
|
||||
}
|
||||
|
||||
return [tab, content];
|
||||
function getTabberTabs(tabber) {
|
||||
// need to scope the selector to handle nested tabs. see /unturned/wiki/Crate for an example
|
||||
const tabs = [...tabber.querySelectorAll(":scope > .wds-tabs__wrapper .wds-tabs__tab")]
|
||||
const contents = [...tabber.querySelectorAll(":scope > .wds-tab__content")]
|
||||
return tabs.map((_, index) => [tabs[index], contents[index]]) // transpose arrays into [[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");
|
||||
}
|
||||
// clear currently selected tab
|
||||
getTabberTabs(tabber).flat().forEach(e => e.classList.remove("wds-is-current"))
|
||||
|
||||
tab.classList.add("wds-is-current");
|
||||
content.classList.add("wds-is-current");
|
||||
if (tab.dataset.hash) {
|
||||
let fragment = "#" + tab.dataset.hash;
|
||||
if (location.hash !== fragment) {
|
||||
history.pushState(null, "", fragment);
|
||||
}
|
||||
}
|
||||
// select new tab
|
||||
tab.classList.add("wds-is-current")
|
||||
content.classList.add("wds-is-current")
|
||||
if (tab.dataset.hash) {
|
||||
history.replaceState(null, "", `#${tab.dataset.hash}`)
|
||||
}
|
||||
}
|
||||
|
||||
document.body.classList.remove("bw-tabs-nojs")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue