-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversions.js
86 lines (79 loc) · 2.61 KB
/
versions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function createVersionLink(version) {
var tag = document.createElement("li");
var link = document.createElement("a");
link.setAttribute("class", "dropdown-item");
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");
link.appendChild(span);
span.innerHTML = version.name;
return tag;
}
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;
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", "/");
// Alter the version dropdown to show the displayed version
var url = window.location.pathname;
var urlParts = url.split("/");
var versionPath = "";
if (urlParts.length > 3 && urlParts[1] == "versioned") {
versionPath = urlParts[2];
}
for (var i = 0; i < json.versions.length; i++) {
var version = json.versions[i];
if (version.link == versionPath) {
var dropdownElement = document.getElementById("nav-menu-version");
dropdownElement.innerHTML = version.name;
break;
}
}
}
fetch('/versions.json')
.then((response) => response.json())
.then((json) => setVersionLinks(json));