wiki/assets/js/tabs.js
Pk11 6eb8220d6e Improve tabs
- They now work as tabs without JS
- Use an include for cleaner markdown
- A little bit nicer code overall, annoying that the split is needed, but otherwise pretty nice now
2021-03-12 19:45:57 -06:00

33 lines
834 B
JavaScript

const platforms = {
"Win32": "tab-windows",
"MacIntel": "tab-macos"
};
function setTab(tab) {
let url = new URL(window.location);
url.searchParams.set("tab", tab);
history.pushState({}, "", url);
}
// Open the tabs for the current OS or the one in the URL
for(let tabGroup of document.getElementsByClassName("tab-container")) {
let tab = null;
// Try get tab from URL
let urlTab = new URL(window.location).searchParams.get("tab");
if(urlTab)
tab = Array.from(tabGroup.children).filter(r => r.id == `tab-${urlTab}`)[0];
// Try get tab for OS
if(!tab)
tab = Array.from(tabGroup.children).filter(r => r.id == platforms[navigator.platform])[0];
// Fall back to "other" tab
if(!tab)
tab = Array.from(tabGroup.children).filter(r => r.id == "tab-other")[0];
// If a tab was found, open it
if(tab)
tab.click();
}