Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 46 additions & 39 deletions conda_package/docs/shared/version-switcher.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
(async function () {
const container = document.getElementById("version-switcher");
if (!container) return;

const metaVersion = document.querySelector('meta[name="doc-version"]');
const currentVersion = metaVersion ? metaVersion.content : "unknown";
console.log("Stored current version:", currentVersion);

async function fetchVersions() {
try {
const res = await fetch("/shared/versions.json");
if (!res.ok) throw new Error();
return await res.json();
} catch {
const res = await fetch("../shared/versions.json");
return await res.json();
}
const container = document.getElementById("version-switcher");
if (!container) return;

const metaVersion = document.querySelector('meta[name="doc-version"]');
const currentVersion = metaVersion ? metaVersion.content : "unknown";
console.log("Detected current version:", currentVersion);

async function fetchVersions() {
try {
const scriptUrl = document.currentScript.src;
const basePath = scriptUrl.substring(0, scriptUrl.lastIndexOf('/') + 1);
const versionsUrl = basePath + "versions.json";

const res = await fetch(versionsUrl);
if (!res.ok) throw new Error(`Failed to load ${versionsUrl}`);
return await res.json();
} catch (err) {
console.error("Could not load versions.json:", err);
return [];
}
}

const versions = await fetchVersions();
if (!versions.length) return;

const select = document.createElement("select");
select.style.marginLeft = "1em";
select.onchange = () => {
window.location.href = select.value;
};

versions.forEach(({ version, url }) => {
const option = document.createElement("option");
option.value = url;
option.textContent = version;
if (version === currentVersion) {
option.selected = true;
}
select.appendChild(option);
});

const label = document.createElement("label");
label.textContent = "Version: ";
label.style.color = "white";
label.appendChild(select);
container.appendChild(label);
})();

const versions = await fetchVersions();

const select = document.createElement("select");
select.style.marginLeft = "1em";
select.onchange = () => {
window.location.href = select.value;
};

versions.forEach(({ version, url }) => {
const option = document.createElement("option");
option.value = url;
option.textContent = version;
if (url.includes(`/${currentVersion}/`)) {
option.selected = true;
}
select.appendChild(option);
});

const label = document.createElement("label");
label.textContent = "Version: ";
label.appendChild(select);
container.appendChild(label);
})();