|
| 1 | +(async function () { |
| 2 | + function sleep(ms) { |
| 3 | + return new Promise((r) => setTimeout(r, ms)); |
| 4 | + } |
| 5 | + |
| 6 | + // Wait for RTD sidebar to exist (theme may build it after our script loads) |
| 7 | + async function waitForSidebar(maxTries = 50, delayMs = 100) { |
| 8 | + for (let i = 0; i < maxTries; i++) { |
| 9 | + const search = document.querySelector(".wy-side-nav-search"); |
| 10 | + const scroll = document.querySelector(".wy-side-scroll"); |
| 11 | + if (search || scroll) return { search, scroll }; |
| 12 | + await sleep(delayMs); |
| 13 | + } |
| 14 | + return { search: null, scroll: null }; |
| 15 | + } |
| 16 | + |
| 17 | + const { search, scroll } = await waitForSidebar(); |
| 18 | + if (!search && !scroll) { |
| 19 | + console.warn("[version-switcher] RTD sidebar not found."); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + // Avoid duplicates (important if theme re-renders and script runs again) |
| 24 | + if (document.querySelector(".version-switcher")) return; |
| 25 | + |
| 26 | + // --- UI --- |
| 27 | + const container = document.createElement("div"); |
| 28 | + container.className = "version-switcher"; |
| 29 | + container.style.padding = "0.5rem 1rem"; |
| 30 | + container.style.display = "flex"; |
| 31 | + container.style.gap = "0.5rem"; |
| 32 | + container.style.alignItems = "center"; |
| 33 | + container.style.justifyContent = "center"; |
| 34 | + |
| 35 | + const label = document.createElement("span"); |
| 36 | + label.textContent = "Version:"; |
| 37 | + label.style.fontSize = "0.9em"; |
| 38 | + label.className = "vs-label"; |
| 39 | + |
| 40 | + const select = document.createElement("select"); |
| 41 | + select.setAttribute("aria-label", "Select documentation version"); |
| 42 | + select.style.width = "50%"; |
| 43 | + select.style.borderRadius = "20px"; |
| 44 | + |
| 45 | + container.appendChild(label); |
| 46 | + container.appendChild(select); |
| 47 | + |
| 48 | + // Insert under the search box if possible |
| 49 | + if (search && search.parentNode) { |
| 50 | + search.insertAdjacentElement("afterend", container); |
| 51 | + } else { |
| 52 | + scroll.prepend(container); |
| 53 | + } |
| 54 | + |
| 55 | + // --- Load versions.json --- |
| 56 | + select.disabled = true; |
| 57 | + select.innerHTML = `<option>Loading…</option>`; |
| 58 | + |
| 59 | + async function fetchJson(url) { |
| 60 | + const resp = await fetch(url, { cache: "no-store" }); |
| 61 | + if (!resp.ok) throw new Error(`${resp.status} ${resp.statusText}`); |
| 62 | + return resp.json(); |
| 63 | + } |
| 64 | + |
| 65 | + // For local http://localhost:8000/ this will work. |
| 66 | + // For GitHub project pages it will also work if versions.json is at the root of the published site. |
| 67 | + let versions; |
| 68 | + try { |
| 69 | + versions = await fetchJson(`${window.location.origin}/versions.json`); |
| 70 | + } catch (e) { |
| 71 | + // GitHub project page fallback: /QCElemental/versions.json |
| 72 | + const parts = window.location.pathname.split("/").filter(Boolean); |
| 73 | + if (parts.length) { |
| 74 | + try { |
| 75 | + versions = await fetchJson(`${window.location.origin}/${parts[0]}/versions.json`); |
| 76 | + } catch (e2) { |
| 77 | + console.warn("[version-switcher] Could not load versions.json", e, e2); |
| 78 | + select.innerHTML = `<option>No versions.json</option>`; |
| 79 | + return; |
| 80 | + } |
| 81 | + } else { |
| 82 | + console.warn("[version-switcher] Could not load versions.json", e); |
| 83 | + select.innerHTML = `<option>No versions.json</option>`; |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Populate |
| 89 | + select.innerHTML = ""; |
| 90 | + for (const v of versions) { |
| 91 | + const opt = document.createElement("option"); |
| 92 | + opt.value = v.path; // "dev/" etc. |
| 93 | + opt.textContent = v.label; // "dev" |
| 94 | + select.appendChild(opt); |
| 95 | + } |
| 96 | + |
| 97 | + // Determine current "version folder" (dev / v0.30.1 / etc.) |
| 98 | + // If you're at /dev/index.html => currentVersion = "dev" |
| 99 | + // If you're at /index.html => currentVersion = "" |
| 100 | + const pathParts = window.location.pathname.split("/").filter(Boolean); |
| 101 | + const currentVersion = pathParts.length >= 2 ? pathParts[1] : pathParts[0] || ""; |
| 102 | + |
| 103 | + for (const opt of select.options) { |
| 104 | + if (opt.value.replace(/\/+$/, "") === currentVersion) { |
| 105 | + opt.selected = true; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + select.disabled = false; |
| 111 | + |
| 112 | + // Navigate on change; for local dev it’s simplest to go to version root |
| 113 | + select.addEventListener("change", () => { |
| 114 | + const target = select.value; // e.g. "dev/" |
| 115 | + // If on GitHub project site, keep the project prefix automatically. |
| 116 | + const parts = window.location.pathname.split("/").filter(Boolean); |
| 117 | + const prefix = parts.length ? `/${parts[0]}/` : "/"; |
| 118 | + window.location.href = `${window.location.origin}${prefix}${target}`; |
| 119 | + }); |
| 120 | +})(); |
| 121 | + |
0 commit comments