summaryrefslogtreecommitdiff
path: root/src/lib/persistent-tabs.js
blob: 8f7f7119267c6edcd9854d73d20058d57b437a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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(";");
      });
    }
  }
}