Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve page versioning #82

Merged
merged 2 commits into from
Mar 14, 2024
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
57 changes: 51 additions & 6 deletions versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ function createVersionLink(version) {
var tag = document.createElement("li");
var link = document.createElement("a");
link.setAttribute("class", "dropdown-item");
link.setAttribute("href", "/versioned/" + version.link + "/");
// link.setAttribute("href", "/versioned/" + version.link + "/");
link.setAttribute("rel", "");
link.setAttribute("target", "");
link.addEventListener("click", function() { versionClick(version.link) });
tag.appendChild(link);
var span = document.createElement("span");
span.setAttribute("class", "dropdown-text");
Expand All @@ -17,9 +18,10 @@ function createLatestLink() {
var tag = document.createElement("li");
var link = document.createElement("a");
link.setAttribute("class", "dropdown-item");
link.setAttribute("href", "/");
// link.setAttribute("href", "/");
link.setAttribute("rel", "");
link.setAttribute("target", "");
link.addEventListener("click", function() { versionClick("") });
tag.appendChild(link);
var span = document.createElement("span");
span.setAttribute("class", "dropdown-text");
Expand All @@ -28,16 +30,59 @@ function createLatestLink() {
return tag;
}

function addVersionLinks(json) {
var element = document.getElementById("nav-menu-version");
var list = element.nextElementSibling;
function versionClick(version) {
// console.log("versionClick " + version);
var url = window.location.pathname;
var urlParts = url.split("/");
console.log(urlParts);
if (urlParts.length > 2 && urlParts[1] == "versioned") {
// console.log("versioned " + url);
urlParts.splice(1, 2);
} else {
// console.log("not versioned " + url);
}

var newUrl;
var fallbackUrl;
if (version == "") {
newUrl = urlParts.join("/");
fallbackUrl = "/";
} else {
newUrl = "/versioned/" + version + urlParts.join("/");
fallbackUrl = "/versioned/" + version + "/";
}
// console.log("newUrl " + newUrl);
// console.log("fallbackUrl " + fallbackUrl);

// Prefetch the new URL and redirect if it exists
// Otherwise, redirect to the fallback URL
fetch (newUrl)
.then((response) => {
// console.log("response");
// console.log(response);
if (response.ok) {
window.location.href = newUrl;
} else {
window.location.href = fallbackUrl;
}
});
}

function setVersionLinks(json) {
// Populate the version drowdown
var dropdownElement = document.getElementById("nav-menu-version");
var list = dropdownElement.nextElementSibling;
list.appendChild( createLatestLink() );
for (var i = 0; i < json.versions.length; i++) {
var version = json.versions[i];
list.appendChild( createVersionLink(version) );
}

// Alter the navbar logo link to point to the root
var navbarElements = document.getElementsByClassName("navbar-brand");
navbarElements[0].setAttribute("href", "/");
}

fetch('/versions.json')
.then((response) => response.json())
.then((json) => addVersionLinks(json));
.then((json) => setVersionLinks(json));
Loading