|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + let contentRoot = new URL( |
| 3 | + document.documentElement.dataset.content_root ?? DOCUMENTATION_OPTIONS.URL_ROOT, |
| 4 | + window.location.href, |
| 5 | + ); |
| 6 | + |
| 7 | + function insertVersionSwitch(versions) { |
| 8 | + let versionElement = document.querySelector('.wy-side-nav-search > .version'); |
| 9 | + if (!versionElement) return; |
| 10 | + |
| 11 | + let root = document.createElement('div'); |
| 12 | + root.innerHTML = ` |
| 13 | + <div class="switch-menus"> |
| 14 | + <div class="version-switch"><select></select></div> |
| 15 | + </div> |
| 16 | + `; |
| 17 | + |
| 18 | + let switchMenus = root.firstElementChild; |
| 19 | + let versionSwitch = switchMenus.firstElementChild; |
| 20 | + let versionSwitchSelect = versionSwitch.firstElementChild; |
| 21 | + |
| 22 | + let versionElementStyles = getComputedStyle(versionElement); |
| 23 | + let cssStyleSheet = new CSSStyleSheet(); |
| 24 | + cssStyleSheet.replaceSync(String.raw` |
| 25 | + .wy-side-nav-search > div.switch-menus { |
| 26 | + margin-top: ${versionElementStyles.marginTop}; |
| 27 | + margin-bottom: ${versionElementStyles.marginBottom}; |
| 28 | + color: ${versionElementStyles.color}; |
| 29 | +
|
| 30 | + div.version-switch { |
| 31 | + select { |
| 32 | + display: inline-block; |
| 33 | + margin-right: -2rem; |
| 34 | + padding-right: 2rem; |
| 35 | + text-align-last: center; |
| 36 | + background: none; |
| 37 | + border: none; |
| 38 | + border-radius: 0em; |
| 39 | + box-shadow: none; |
| 40 | + font-family: inherit; |
| 41 | + font-size: 1em; |
| 42 | + font-weight: normal; |
| 43 | + color: inherit; |
| 44 | + cursor: pointer; |
| 45 | + appearance: none; |
| 46 | +
|
| 47 | + &:hover, &:active, &:focus { |
| 48 | + background: rgba(255, 255, 255, .1); |
| 49 | + color: rgba(255, 255, 255, .5); |
| 50 | + } |
| 51 | +
|
| 52 | + option { |
| 53 | + color: black; |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + &:has(> select):after { |
| 58 | + display: inline-block; |
| 59 | + width: 1.5em; |
| 60 | + height: 100%; |
| 61 | + padding: .1em; |
| 62 | + content: "\f0d7"; |
| 63 | + font-size: 1em; |
| 64 | + line-height: 1.2em; |
| 65 | + font-family: FontAwesome; |
| 66 | + text-align: center; |
| 67 | + pointer-events: none; |
| 68 | + box-sizing: border-box; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + `); |
| 73 | + document.adoptedStyleSheets.push(cssStyleSheet); |
| 74 | + |
| 75 | + let currentVersion = DOCUMENTATION_OPTIONS.VERSION; |
| 76 | + if (!versions.includes(currentVersion)) { |
| 77 | + versions = [ |
| 78 | + { name: currentVersion, root_url: contentRoot }, |
| 79 | + ...versions, |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + for (let { name, root_url: rootURL } of versions) { |
| 84 | + rootURL = new URL(rootURL, window.location.href); |
| 85 | + |
| 86 | + let versionOptionElement = document.createElement('option'); |
| 87 | + versionOptionElement.textContent = name; |
| 88 | + versionOptionElement.dataset.url = rootURL; |
| 89 | + |
| 90 | + if (name === currentVersion) { |
| 91 | + versionOptionElement.selected = true; |
| 92 | + } |
| 93 | + |
| 94 | + versionSwitchSelect.appendChild(versionOptionElement); |
| 95 | + } |
| 96 | + |
| 97 | + versionSwitchSelect.addEventListener('change', (event) => { |
| 98 | + let option = event.target.selectedIndex; |
| 99 | + let item = event.target.options[option]; |
| 100 | + window.location.href = item.dataset.url; |
| 101 | + }); |
| 102 | + |
| 103 | + versionElement.replaceWith(switchMenus); |
| 104 | + } |
| 105 | + |
| 106 | + fetch(new URL('../versions.json', contentRoot)).then(async (response) => { |
| 107 | + if (response.status !== 200) return; |
| 108 | + let versions = await response.json(); |
| 109 | + insertVersionSwitch(versions); |
| 110 | + }); |
| 111 | +}); |
0 commit comments