(function () { function normalizePrefix(prefix) { if (!prefix) { return "/"; } var normalized = prefix; if (!normalized.startsWith("/")) { normalized = "/" + normalized; } if (!normalized.endsWith("/")) { normalized += "/"; } return normalized.replace(/\/{2,}/g, "/"); } function joinPath(prefix, suffix) { var cleanSuffix = (suffix || "").replace(/^\/+/, ""); return normalizePrefix(prefix) + cleanSuffix; } function samePageTarget(currentPrefix, targetPrefix) { var currentPath = window.location.pathname; var normalizedCurrent = normalizePrefix(currentPrefix); var normalizedTarget = normalizePrefix(targetPrefix); if (currentPath.indexOf(normalizedCurrent) === 0) { return joinPath(normalizedTarget, currentPath.slice(normalizedCurrent.length)); } return normalizedTarget; } function setStatus(node, message, isError) { if (!node) { return; } node.textContent = message; node.classList.toggle("is-error", Boolean(isError)); } document.addEventListener("DOMContentLoaded", function () { var container = document.querySelector(".doc-version-selector"); if (!container) { return; } var select = container.querySelector(".js-version-select"); var status = container.querySelector(".js-version-status"); var currentVersion = container.dataset.currentVersion || "latest"; var versionsUrl = container.dataset.versionsUrl; if (!select || !versionsUrl) { return; } fetch(versionsUrl, { credentials: "same-origin" }) .then(function (response) { if (!response.ok) { throw new Error("Unable to load versions"); } return response.json(); }) .then(function (manifest) { var versions = Array.isArray(manifest.versions) ? manifest.versions : []; if (!versions.length) { throw new Error("No versions defined"); } var activeVersion = manifest.current || currentVersion; var activeEntry = versions.find(function (item) { return item.slug === activeVersion; }) || versions.find(function (item) { return item.slug === currentVersion; }) || versions[0]; select.innerHTML = ""; versions.forEach(function (item) { var option = document.createElement("option"); option.value = item.slug; option.textContent = item.label || item.slug; option.dataset.prefix = item.prefix || "/" + item.slug + "/"; if (item.slug === activeEntry.slug) { option.selected = true; } select.appendChild(option); }); setStatus(status, manifest.description || "Switch versions without leaving the current page."); select.addEventListener("change", function (event) { var chosen = versions.find(function (item) { return item.slug === event.target.value; }); if (!chosen) { return; } var targetPath = samePageTarget(activeEntry.prefix, chosen.prefix); var nextUrl = targetPath + window.location.search + window.location.hash; window.location.assign(nextUrl); }); }) .catch(function () { setStatus(status, "versions.json could not be loaded.", true); }); }); })();