|
6 | 6 | * @param {string} path - resource path |
7 | 7 | * @returns {Promise} promise which resolves a resource URL |
8 | 8 | */ |
9 | | -async function href(url, path) { |
| 9 | +function href(url, path) { |
10 | 10 | const defaultURL = url + "/index.html"; |
11 | 11 | url += "/" + path; |
12 | 12 |
|
13 | 13 | // 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); |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * Callback invoked upon successfully resolving a resource. |
@@ -68,25 +68,44 @@ async function add_version_dropdown(json_loc, target_loc, text) { |
68 | 68 | * @returns {Promise} promise which resolves upon processing version data |
69 | 69 | */ |
70 | 70 | async function onDone(versions) { |
71 | | - console.log(versions) |
| 71 | + console.log(versions); |
| 72 | + |
| 73 | + // Resolve the current browser URL: |
72 | 74 | 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): |
73 | 77 | let path = currentURL.split(/_site|array_api/)[1]; |
| 78 | + |
| 79 | + // Extract the resource subpath: |
74 | 80 | if (path) { |
75 | 81 | path = path.split("/"); |
76 | 82 | path = path.slice(2, path.length); |
77 | 83 | path = path.join("/"); |
78 | 84 | } else { |
79 | 85 | path = ""; |
80 | 86 | } |
| 87 | + // For each version, create an anchor element and attempt to resolve a given resource for that version... |
| 88 | + const promises = []; |
| 89 | + const el = []; |
81 | 90 | for (let key in versions) { |
82 | 91 | if (versions.hasOwnProperty(key)) { |
83 | 92 | let a = document.createElement("a"); |
84 | 93 | a.innerHTML = key; |
85 | 94 | 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)); |
88 | 97 | } |
89 | 98 | } |
| 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: |
90 | 109 | button.innerHTML = text; |
91 | 110 | } |
92 | 111 |
|
|
0 commit comments