| 1 |
window.addEventListener('load', function() { |
| 2 |
let tabIds = []; |
| 3 |
|
| 4 |
function toggle(elem, from = 0, to = 0) { |
| 5 |
elem[from].classList.remove("active"); |
| 6 |
elem[to].classList.add("active"); |
| 7 |
return to; |
| 8 |
} |
| 9 |
|
| 10 |
function initialize(tabs) { |
| 11 |
const active = tabs.findIndex(x => x.classList.contains("active")); |
| 12 |
tabs.forEach(function(x) { |
| 13 |
tabIds.push(x.id); |
| 14 |
x.dataset.id = x.id; |
| 15 |
x.removeAttribute("id"); |
| 16 |
x.classList.add("tab"); |
| 17 |
x.classList.remove("active"); |
| 18 |
}); |
| 19 |
return toggle(tabs, 0, active * (active > 0)); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
Array.from(document.querySelectorAll(".section.tabset")).forEach(section => { |
| 24 |
const tabs = Array.from(section.querySelectorAll(":scope>.section")); |
| 25 |
let active = initialize(tabs); |
| 26 |
|
| 27 |
const ul = section.insertBefore(document.createElement("ul"), tabs[0]); |
| 28 |
ul.classList.add("tabmenu"); |
| 29 |
tabs.forEach((tab, current) => { |
| 30 |
const button = document.createElement("button"); |
| 31 |
ul.appendChild(document.createElement("li")).appendChild(button); |
| 32 |
button.id = tab.dataset.id; |
| 33 |
button.innerHTML = tab.children[0].innerHTML; |
| 34 |
button.addEventListener("click", function() { |
| 35 |
history.pushState(null, null, "#" + button.id); |
| 36 |
toggle(tabs, active, current); |
| 37 |
active = toggle(ul.children, active, current); |
| 38 |
}); |
| 39 |
}); |
| 40 |
toggle(ul.children, 0, active); |
| 41 |
}); |
| 42 |
|
| 43 |
|
| 44 |
function showHashTab() { |
| 45 |
const buttonId = tabIds.find(x => ('#' + x) === location.hash); |
| 46 |
if (!buttonId) return; |
| 47 |
const button = document.getElementById(buttonId); |
| 48 |
let el = button.parentElement.parentElement.parentElement; |
| 49 |
while (el) { |
| 50 |
if ( |
| 51 |
el.tagName === "SECTION" |
| 52 |
&& el.classList.contains("tab") |
| 53 |
&& !el.classList.contains("active") |
| 54 |
) { |
| 55 |
document.getElementById(el.dataset.id).click(); |
| 56 |
} |
| 57 |
el = el.parentElement; |
| 58 |
} |
| 59 |
button.click(); |
| 60 |
button.scrollIntoView(); |
| 61 |
} |
| 62 |
showHashTab(); |
| 63 |
window.addEventListener("hashchange", showHashTab); |
| 64 |
}); |
| 65 |
|