Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 9x 9x 9x 9x 9x 9x 18x 18x 18x 18x 18x 9x 9x 9x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 23x 46x 11x 23x 12x 24x 12x 23x 19x 38x 38x 38x 38x 38x 17x 17x 17x 17x 17x 17x 17x 9x 4x 4x 4x 4x 8x 4x 4x 4x 4x | import { HistoryState } from "./historyState"; interface Tab { name: string; tabEl: HTMLElement; selected: boolean; index: number; contentEl: HTMLElement | null; } class TableOfContents { el: HTMLElement; instanceName: string; tabs: { [key: string]: Tab }; historyState: HistoryState; constructor(el: HTMLElement, historyState: HistoryState) { this.el = el; this.instanceName = el.dataset.js as string; this.tabs = {}; this.historyState = historyState; const tabLinks = this.el.querySelectorAll("[role='tab']"); tabLinks.forEach((tabLink, i) => { const name = tabLink.getAttribute("aria-controls") as string; let isSelected = tabLink.hasAttribute("aria-selected"); const controls = document.querySelector(`#${name}`) as HTMLElement | null; Iif ( this.historyState.path.length === 2 && this.historyState.path[0] === this.instanceName && this.historyState.path[1] === name ) { isSelected = true; } this.tabs[name] = { name, tabEl: tabLink as HTMLElement, selected: isSelected, index: i, contentEl: controls, }; }); this.updateUI(); this.initEvents(); this.historyState.addPopListener(this.popHandler.bind(this)); } focus() { const selectedTab = this.getSelected(); Eif (selectedTab) { this.historyState.updatePath(0, [this.instanceName, selectedTab.name]); } } popHandler(state: Array<string>) { Eif (state) { Eif (state.length === 2 && this.instanceName === state[0]) { Object.keys(this.tabs).forEach((tab) => { if (tab === state[1]) { this.tabs[tab].selected = true; } else { this.tabs[tab].selected = false; } }); this.updateUI(); } } } getSelected(): Tab | undefined { let selected: Tab | undefined; Object.keys(this.tabs).forEach((tab) => { if (this.tabs[tab].selected) { selected = this.tabs[tab]; } }); if (!selected) { Object.keys(this.tabs).forEach((tab) => { if (this.tabs[tab].index === 0) { selected = this.tabs[tab]; } }); } return selected; } resetTabUI() { Object.keys(this.tabs).forEach((tab) => { const tabData = this.tabs[tab]; Eif (tabData.tabEl) { tabData.tabEl.removeAttribute("aria-selected"); tabData.tabEl.classList.remove("is-active"); } Iif (tabData.contentEl) { tabData.contentEl.classList.add("u-hide"); } }); } updateUI() { this.resetTabUI(); const selectedTab = this.getSelected(); Eif (selectedTab) { Eif (selectedTab.tabEl) { selectedTab.tabEl.setAttribute("aria-selected", "true"); selectedTab.tabEl.classList.add("is-active"); } Iif (selectedTab.contentEl) { selectedTab.contentEl.classList.remove("u-hide"); } } } initEvents() { this.el.addEventListener("click", (e) => { e.preventDefault(); const clickedTab = (e.target as HTMLElement).getAttribute( "aria-controls" ); Eif (clickedTab) { Object.keys(this.tabs).forEach((tab) => { if (tab === clickedTab) { this.tabs[tab].selected = true; } else { this.tabs[tab].selected = false; } }); } this.historyState.updatePath(1, clickedTab); this.updateUI(); }); } } export { TableOfContents }; |