Skip to content

Commit 5c124b8

Browse files
committed
Refactor to use async/await and update URL manipulation logic
1 parent 833a5f2 commit 5c124b8

File tree

1 file changed

+95
-27
lines changed

1 file changed

+95
-27
lines changed
Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,114 @@
1-
function assign_href(a, url, path) {
2-
fetch(url + "/" + path).then(response => {
1+
/**
2+
* Returns 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+
async function href(url, path) {
10+
const defaultURL = url + "/index.html";
11+
url += "/" + path;
12+
await fetch(url).then(onResponse).catch(onError);
13+
14+
/**
15+
* Success handler.
16+
*
17+
* @private
18+
* @param {Object} response - response object
19+
*/
20+
function onResponse(response) {
321
if (response.ok) {
4-
a.href = url + "/" + path;
5-
} else {
6-
a.href = url + "/index.html";
22+
return url;
723
}
8-
}).catch(error => {
9-
a.href = url + "/index.html";
10-
});
24+
return defaultURL;
25+
}
26+
27+
/**
28+
* Error handler.
29+
*
30+
* @private
31+
* @param {Error} error - error object
32+
*/
33+
function onError(error) {
34+
return defaultURL;
35+
}
1136
}
1237

13-
function add_version_dropdown(json_loc, target_loc, text) {
14-
var dropdown = document.createElement("div");
38+
/**
39+
* Adds a version dropdown menu with custom URL paths depending on the current page.
40+
*
41+
* @param {string} json_loc - JSON URL
42+
* @param {string} target_loc - target URL
43+
* @param {string} text - text
44+
* @returns {Promise} promise which resolves upon adding a version menu
45+
*/
46+
async function add_version_dropdown(json_loc, target_loc, text) {
47+
const dropdown = document.createElement("div");
1548
dropdown.className = "md-flex__cell md-flex__cell--shrink dropdown";
16-
var button = document.createElement("button");
49+
50+
const button = document.createElement("button");
1751
button.className = "dropdownbutton";
18-
var content = document.createElement("div");
52+
53+
const content = document.createElement("div");
1954
content.className = "dropdown-content md-hero";
55+
2056
dropdown.appendChild(button);
2157
dropdown.appendChild(content);
22-
$.getJSON(json_loc, function(versions) {
23-
var currentURL = window.location.href;
24-
var path = currentURL.split(/_site|array_api/)[1];
58+
59+
await $.getJSON(json_loc, onVersions).done(onDone).fail(onFail).always(onAlways);
60+
61+
/**
62+
* Callback invoked upon successfully resolving a list of versions.
63+
*
64+
* @private
65+
* @param {Object} versions - versions object
66+
*/
67+
async function onVersions(versions) {
68+
const currentURL = window.location.href;
69+
let path = currentURL.split(/_site|array_api/)[1];
2570
if (path) {
2671
path = path.split("/");
2772
path = path.slice(2, path.length);
2873
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-
}
74+
} else {
75+
path = "";
76+
}
77+
for (let key in versions) {
78+
if (versions.hasOwnProperty(key)) {
79+
let a = document.createElement("a");
80+
a.innerHTML = key;
81+
a.title = key;
82+
a.href = await href(target_loc + versions[key], path);
83+
content.appendChild(a);
3784
}
3885
}
39-
}).done(function() {
86+
}
87+
88+
/**
89+
* Callback invoked upon resolving a JSON resource.
90+
*
91+
* @private
92+
*/
93+
function onDone() {
4094
button.innerHTML = text;
41-
}).fail(function() {
95+
}
96+
97+
/**
98+
* Callback invoked upon failing to resolve a JSON resource.
99+
*
100+
* @private
101+
*/
102+
function onFail() {
42103
button.innerHTML = "Other Versions Not Found";
43-
}).always(function() {
104+
}
105+
106+
/**
107+
* Callback which is always invoked upon attempting to resolve a JSON resource.
108+
*
109+
* @private
110+
*/
111+
function onAlways() {
44112
$(".navheader").append(dropdown);
45-
});
113+
}
46114
};

0 commit comments

Comments
 (0)