|
| 1 | +try { |
| 2 | + var session = window.sessionStorage || {}; |
| 3 | +} catch (e) { |
| 4 | + var session = {}; |
| 5 | +} |
| 6 | + |
| 7 | +window.addEventListener("DOMContentLoaded", () => { |
| 8 | + const allTabs = document.querySelectorAll('.sphinx-tabs-tab'); |
| 9 | + const tabLists = document.querySelectorAll('[role="tablist"]'); |
| 10 | + |
| 11 | + allTabs.forEach(tab => { |
| 12 | + tab.addEventListener("click", changeTabs); |
| 13 | + }); |
| 14 | + |
| 15 | + tabLists.forEach(tabList => { |
| 16 | + tabList.addEventListener("keydown", keyTabs); |
| 17 | + }); |
| 18 | + |
| 19 | + // Restore group tab selection from session |
| 20 | + const lastSelected = session.getItem('sphinx-tabs-last-selected'); |
| 21 | + if (lastSelected != null) selectNamedTabs(lastSelected); |
| 22 | +}); |
| 23 | + |
| 24 | +/** |
| 25 | + * Key focus left and right between sibling elements using arrows |
| 26 | + * @param {Node} e the element in focus when key was pressed |
| 27 | + */ |
| 28 | +function keyTabs(e) { |
| 29 | + const tab = e.target; |
| 30 | + let nextTab = null; |
| 31 | + if (e.keyCode === 39 || e.keyCode === 37) { |
| 32 | + tab.setAttribute("tabindex", -1); |
| 33 | + // Move right |
| 34 | + if (e.keyCode === 39) { |
| 35 | + nextTab = tab.nextElementSibling; |
| 36 | + if (nextTab === null) { |
| 37 | + nextTab = tab.parentNode.firstElementChild; |
| 38 | + } |
| 39 | + // Move left |
| 40 | + } else if (e.keyCode === 37) { |
| 41 | + nextTab = tab.previousElementSibling; |
| 42 | + if (nextTab === null) { |
| 43 | + nextTab = tab.parentNode.lastElementChild; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (nextTab !== null) { |
| 49 | + nextTab.setAttribute("tabindex", 0); |
| 50 | + nextTab.focus(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Select or deselect clicked tab. If a group tab |
| 56 | + * is selected, also select tab in other tabLists. |
| 57 | + * @param {Node} e the element that was clicked |
| 58 | + */ |
| 59 | +function changeTabs(e) { |
| 60 | + // Use this instead of the element that was clicked, in case it's a child |
| 61 | + const notSelected = this.getAttribute("aria-selected") === "false"; |
| 62 | + const positionBefore = this.parentNode.getBoundingClientRect().top; |
| 63 | + const notClosable = !this.parentNode.classList.contains("closeable"); |
| 64 | + |
| 65 | + deselectTabList(this); |
| 66 | + |
| 67 | + if (notSelected || notClosable) { |
| 68 | + selectTab(this); |
| 69 | + const name = this.getAttribute("name"); |
| 70 | + selectNamedTabs(name, this.id); |
| 71 | + |
| 72 | + if (this.classList.contains("group-tab")) { |
| 73 | + // Persist during session |
| 74 | + session.setItem('sphinx-tabs-last-selected', name); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const positionAfter = this.parentNode.getBoundingClientRect().top; |
| 79 | + const positionDelta = positionAfter - positionBefore; |
| 80 | + // Scroll to offset content resizing |
| 81 | + window.scrollTo(0, window.scrollY + positionDelta); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Select tab and show associated panel. |
| 86 | + * @param {Node} tab tab to select |
| 87 | + */ |
| 88 | +function selectTab(tab) { |
| 89 | + tab.setAttribute("aria-selected", true); |
| 90 | + |
| 91 | + // Show the associated panel |
| 92 | + document |
| 93 | + .getElementById(tab.getAttribute("aria-controls")) |
| 94 | + .removeAttribute("hidden"); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Hide the panels associated with all tabs within the |
| 99 | + * tablist containing this tab. |
| 100 | + * @param {Node} tab a tab within the tablist to deselect |
| 101 | + */ |
| 102 | +function deselectTabList(tab) { |
| 103 | + const parent = tab.parentNode; |
| 104 | + const grandparent = parent.parentNode; |
| 105 | + |
| 106 | + Array.from(parent.children) |
| 107 | + .forEach(t => t.setAttribute("aria-selected", false)); |
| 108 | + |
| 109 | + Array.from(grandparent.children) |
| 110 | + .slice(1) // Skip tablist |
| 111 | + .forEach(panel => panel.setAttribute("hidden", true)); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Select grouped tabs with the same name, but no the tab |
| 116 | + * with the given id. |
| 117 | + * @param {Node} name name of grouped tab to be selected |
| 118 | + * @param {Node} clickedId id of clicked tab |
| 119 | + */ |
| 120 | +function selectNamedTabs(name, clickedId=null) { |
| 121 | + const groupedTabs = document.querySelectorAll(`.sphinx-tabs-tab[name="${name}"]`); |
| 122 | + const tabLists = Array.from(groupedTabs).map(tab => tab.parentNode); |
| 123 | + |
| 124 | + tabLists |
| 125 | + .forEach(tabList => { |
| 126 | + // Don't want to change the tabList containing the clicked tab |
| 127 | + const clickedTab = tabList.querySelector(`[id="${clickedId}"]`); |
| 128 | + if (clickedTab === null ) { |
| 129 | + // Select first tab with matching name |
| 130 | + const tab = tabList.querySelector(`.sphinx-tabs-tab[name="${name}"]`); |
| 131 | + deselectTabList(tab); |
| 132 | + selectTab(tab); |
| 133 | + } |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +// TODO(christophfroehlich) this has to be uncommented for jQuery of our code to work on the same page |
| 138 | + |
| 139 | +// if (typeof exports === 'undefined') { |
| 140 | +// exports = {}; |
| 141 | +// } |
| 142 | + |
| 143 | +// exports.keyTabs = keyTabs; |
| 144 | +// exports.changeTabs = changeTabs; |
| 145 | +// exports.selectTab = selectTab; |
| 146 | +// exports.deselectTabList = deselectTabList; |
| 147 | +// exports.selectNamedTabs = selectNamedTabs; |
0 commit comments