Skip to content

Commit 2e0c414

Browse files
authored
Merge pull request #650 from kgryte/fix-version-menu-bug2
2 parents 833a5f2 + 4448b37 commit 2e0c414

File tree

1 file changed

+121
-27
lines changed

1 file changed

+121
-27
lines changed
Lines changed: 121 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,140 @@
1-
function assign_href(a, url, path) {
2-
fetch(url + "/" + path).then(response => {
1+
/**
2+
* Returns a promise for resolving a URL corresponding to a versioned resource (if one exists).
3+
*
4+
* @private
5+
* @param {string} url - base URL
6+
* @param {string} path - resource path
7+
* @returns {Promise} promise which resolves a resource URL
8+
*/
9+
function href(url, path) {
10+
const defaultURL = url + "/index.html";
11+
url += "/" + path;
12+
13+
// If a versioned resource exists, return the resource's URL; otherwise, return a default URL:
14+
const opts = {
15+
'method': 'HEAD'
16+
};
17+
return fetch(url, opts).then(onResponse).catch(onError);
18+
19+
/**
20+
* Callback invoked upon successfully resolving a resource.
21+
*
22+
* @private
23+
* @param {Object} response - response object
24+
*/
25+
function onResponse(response) {
326
if (response.ok) {
4-
a.href = url + "/" + path;
5-
} else {
6-
a.href = url + "/index.html";
27+
return url;
728
}
8-
}).catch(error => {
9-
a.href = url + "/index.html";
10-
});
29+
return defaultURL;
30+
}
31+
32+
/**
33+
* Callback invoked upon failing to resolve a resource.
34+
*
35+
* @private
36+
* @param {Error} error - error object
37+
*/
38+
function onError(error) {
39+
return defaultURL;
40+
}
1141
}
1242

13-
function add_version_dropdown(json_loc, target_loc, text) {
14-
var dropdown = document.createElement("div");
43+
/**
44+
* Adds a version dropdown menu with custom URL paths depending on the current page.
45+
*
46+
* @param {string} json_loc - JSON URL
47+
* @param {string} target_loc - target URL
48+
* @param {string} text - text
49+
* @returns {Promise} promise which resolves upon adding a version menu
50+
*/
51+
async function add_version_dropdown(json_loc, target_loc, text) {
52+
const dropdown = document.createElement("div");
1553
dropdown.className = "md-flex__cell md-flex__cell--shrink dropdown";
16-
var button = document.createElement("button");
54+
55+
const button = document.createElement("button");
1756
button.className = "dropdownbutton";
18-
var content = document.createElement("div");
57+
58+
const content = document.createElement("div");
1959
content.className = "dropdown-content md-hero";
60+
2061
dropdown.appendChild(button);
2162
dropdown.appendChild(content);
22-
$.getJSON(json_loc, function(versions) {
23-
var currentURL = window.location.href;
24-
var path = currentURL.split(/_site|array_api/)[1];
63+
64+
const opts = {
65+
'method': 'GET'
66+
};
67+
await fetch(json_loc, opts).then(onResponse).then(onVersions).catch(onError);
68+
69+
/**
70+
* Callback invoked upon resolving a resource.
71+
*
72+
* @private
73+
* @param {Object} response - response object
74+
*/
75+
function onResponse(response) {
76+
return response.json();
77+
}
78+
79+
/**
80+
* Callback invoked upon resolving a JSON resource.
81+
*
82+
* @private
83+
* @param {Object} versions - versions object
84+
* @returns {Promise} promise which resolves upon processing version data
85+
*/
86+
async function onVersions(versions) {
87+
// Resolve the current browser URL:
88+
const currentURL = window.location.href;
89+
90+
// Check whether the user is currently on a resource page (e.g., is viewing the specification for a particular function):
91+
let path = currentURL.split(/_site|array\-api/)[1];
92+
93+
// Extract the resource subpath:
2594
if (path) {
2695
path = path.split("/");
2796
path = path.slice(2, path.length);
2897
path = path.join("/");
29-
for (var key in versions) {
30-
if (versions.hasOwnProperty(key)) {
31-
var a = document.createElement("a");
32-
a.innerHTML = key;
33-
a.title = key;
34-
assign_href(a, target_loc + versions[key], path);
35-
content.appendChild(a);
36-
}
98+
} else {
99+
path = "";
100+
}
101+
// For each version, create an anchor element and attempt to resolve a given resource for that version...
102+
const promises = [];
103+
const el = [];
104+
for (let key in versions) {
105+
if (versions.hasOwnProperty(key)) {
106+
let a = document.createElement("a");
107+
a.innerHTML = key;
108+
a.title = key;
109+
el.push(a);
110+
promises.push(href(target_loc + versions[key], path));
37111
}
38112
}
39-
}).done(function() {
113+
// Resolve all resource URLs:
114+
const urls = await Promise.all(promises);
115+
116+
// Append the version links to the dropdown menu:
117+
for (let i = 0; i < urls.length; i++) {
118+
let a = el[i];
119+
a.href = urls[i];
120+
content.appendChild(a);
121+
}
122+
// Set the button text:
40123
button.innerHTML = text;
41-
}).fail(function() {
124+
125+
// Append dropdown:
126+
$(".navheader").append(dropdown);
127+
}
128+
129+
/**
130+
* Callback invoked upon failing to resolve a resource.
131+
*
132+
* @private
133+
*/
134+
function onError() {
42135
button.innerHTML = "Other Versions Not Found";
43-
}).always(function() {
136+
137+
// Append dropdown:
44138
$(".navheader").append(dropdown);
45-
});
139+
}
46140
};

0 commit comments

Comments
 (0)