|
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) { |
3 | 21 | if (response.ok) {
|
4 |
| - a.href = url + "/" + path; |
5 |
| - } else { |
6 |
| - a.href = url + "/index.html"; |
| 22 | + return url; |
7 | 23 | }
|
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 | + } |
11 | 36 | }
|
12 | 37 |
|
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"); |
15 | 48 | dropdown.className = "md-flex__cell md-flex__cell--shrink dropdown";
|
16 |
| - var button = document.createElement("button"); |
| 49 | + |
| 50 | + const button = document.createElement("button"); |
17 | 51 | button.className = "dropdownbutton";
|
18 |
| - var content = document.createElement("div"); |
| 52 | + |
| 53 | + const content = document.createElement("div"); |
19 | 54 | content.className = "dropdown-content md-hero";
|
| 55 | + |
20 | 56 | dropdown.appendChild(button);
|
21 | 57 | 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]; |
25 | 70 | if (path) {
|
26 | 71 | path = path.split("/");
|
27 | 72 | path = path.slice(2, path.length);
|
28 | 73 | 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); |
37 | 84 | }
|
38 | 85 | }
|
39 |
| - }).done(function() { |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Callback invoked upon resolving a JSON resource. |
| 90 | + * |
| 91 | + * @private |
| 92 | + */ |
| 93 | + function onDone() { |
40 | 94 | 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() { |
42 | 103 | 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() { |
44 | 112 | $(".navheader").append(dropdown);
|
45 |
| - }); |
| 113 | + } |
46 | 114 | };
|
0 commit comments