(function () {
"use strict";
var basePath = "/torc/";
function getCurrentVersion() {
var path = window.location.pathname;
var match = path.match(/^\/torc\/(v[^/]+|latest)\//);
return match ? match[1] : "latest";
}
function getRelativePath() {
var path = window.location.pathname;
var match = path.match(/^\/torc\/(?:v[^/]+|latest)\/(.*)/);
return match ? match[1] : "";
}
function createSelector(versions, currentVersion) {
var container = document.createElement("div");
container.className = "version-selector";
var select = document.createElement("select");
select.id = "version-select";
select.setAttribute("aria-label", "Select documentation version");
versions.forEach(function (v) {
var option = document.createElement("option");
option.value = v.path;
option.textContent = v.label;
if (v.version === currentVersion) {
option.selected = true;
}
select.appendChild(option);
});
select.addEventListener("change", function () {
var relativePath = getRelativePath();
var targetUrl = select.value + relativePath;
fetch(targetUrl, { method: "HEAD" })
.then(function (response) {
window.location.href = response.ok ? targetUrl : select.value;
})
.catch(function () {
window.location.href = select.value;
});
});
container.appendChild(select);
return container;
}
function init() {
fetch(basePath + "versions.json")
.then(function (response) {
if (!response.ok) throw new Error("versions.json not found");
return response.json();
})
.then(function (data) {
var currentVersion = getCurrentVersion();
var selector = createSelector(data.versions, currentVersion);
var rightButtons = document.querySelector(".right-buttons");
if (rightButtons) {
rightButtons.insertBefore(selector, rightButtons.firstChild);
}
})
.catch(function () {
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();