Skip to content

Commit 3b32fe2

Browse files
committed
Add comments
1 parent 6649c20 commit 3b32fe2

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

spec/_static/javascripts/version_dropdown.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @param {string} path - resource path
77
* @returns {Promise} promise which resolves a resource URL
88
*/
9-
async function href(url, path) {
9+
function href(url, path) {
1010
const defaultURL = url + "/index.html";
1111
url += "/" + path;
1212

1313
// If a versioned resource exists, return the resource's URL; otherwise, return a default URL:
14-
await fetch(url).then(onResponse).catch(onError);
14+
return fetch(url).then(onResponse).catch(onError);
1515

1616
/**
1717
* Callback invoked upon successfully resolving a resource.
@@ -68,25 +68,44 @@ async function add_version_dropdown(json_loc, target_loc, text) {
6868
* @returns {Promise} promise which resolves upon processing version data
6969
*/
7070
async function onDone(versions) {
71-
console.log(versions)
71+
console.log(versions);
72+
73+
// Resolve the current browser URL:
7274
const currentURL = window.location.href;
75+
76+
// Check whether the user is currently on a resource page (e.g., is viewing the specification for a particular function):
7377
let path = currentURL.split(/_site|array_api/)[1];
78+
79+
// Extract the resource subpath:
7480
if (path) {
7581
path = path.split("/");
7682
path = path.slice(2, path.length);
7783
path = path.join("/");
7884
} else {
7985
path = "";
8086
}
87+
// For each version, create an anchor element and attempt to resolve a given resource for that version...
88+
const promises = [];
89+
const el = [];
8190
for (let key in versions) {
8291
if (versions.hasOwnProperty(key)) {
8392
let a = document.createElement("a");
8493
a.innerHTML = key;
8594
a.title = key;
86-
a.href = await href(target_loc + versions[key], path);
87-
content.appendChild(a);
95+
el.push(a);
96+
promises.push(href(target_loc + versions[key], path));
8897
}
8998
}
99+
// Resolve all resource URLs:
100+
const urls = await Promise.all(promises);
101+
102+
// Append the version links to the dropdown menu:
103+
for (let i = 0; i < urls.length; i++) {
104+
let a = el[i];
105+
a.href = urls[i];
106+
content.appendChild(a);
107+
}
108+
// Set the button text:
90109
button.innerHTML = text;
91110
}
92111

0 commit comments

Comments
 (0)