diff options
Diffstat (limited to 'src/lib/persistent-tabs.js')
-rw-r--r-- | src/lib/persistent-tabs.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/lib/persistent-tabs.js b/src/lib/persistent-tabs.js new file mode 100644 index 0000000..8f7f711 --- /dev/null +++ b/src/lib/persistent-tabs.js @@ -0,0 +1,21 @@ +if (typeof flextabs === "function") { + + for (let tabs of document.querySelectorAll(".flextabs")) { + flextabs(tabs).init(); + let {id} = tabs; + if (!id) continue; + let rx = new RegExp(`(?:^|[#;])tab-${id}=(\\d+)(?:;|$)`); + let current = location.hash.match(rx); + console.log(`persisted %o`, current); + let toggles = tabs.querySelectorAll(".flextabs__toggle"); + let currentToggle = toggles[current && parseInt(current[1]) || 0]; + if (currentToggle) currentToggle.click(); + for (let toggle of toggles) { + toggle.addEventListener("click", e => { + let currentIdx = Array.indexOf(toggles, toggle); + location.hash = location.hash.split(";").filter(p => !rx.test(p)) + .concat(`tab-${id}=${currentIdx}`).join(";"); + }); + } + } +} |