|
15 | 15 | margin: 0; |
16 | 16 | padding: 0; |
17 | 17 | } |
| 18 | + /* |
| 19 | + * ReadTheDocs injects its version selector which is confusing on |
| 20 | + * this page since it doesn't affect the API version. So we hide it. |
| 21 | + */ |
| 22 | + readthedocs-flyout { |
| 23 | + display: none; |
| 24 | + } |
| 25 | + /* Our own version selector. */ |
| 26 | + #versionSelect { |
| 27 | + position: fixed; |
| 28 | + /* |
| 29 | + * Like the ReadTheDocs selector we put it in the bottom-right corner. |
| 30 | + * When the browser is narrow ReDoc puts its menu button in the same corner, |
| 31 | + * the position here is chosen not to overlap with that. |
| 32 | + */ |
| 33 | + bottom: 14px; |
| 34 | + right: 14px; |
| 35 | + z-index: 99; |
| 36 | + } |
18 | 37 | </style> |
19 | 38 | </head> |
20 | 39 | <body> |
21 | 40 | <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script> |
| 41 | + <select id="versionSelect" aria-label="Version"> |
| 42 | + <option>latest</option> |
| 43 | + </select> |
22 | 44 | <div id="redoc-container"></div> |
23 | | - <script> |
| 45 | + <script type="module"> |
| 46 | + const versionSelect = document.getElementById("versionSelect"); |
| 47 | + const resp = await fetch("versions.json"); |
| 48 | + const versions = await resp.json(); |
| 49 | + for (const version of versions) { |
| 50 | + const opt = document.createElement("option"); |
| 51 | + opt.textContent = "v" + version; |
| 52 | + versionSelect.append(opt); |
| 53 | + } |
24 | 54 | // get version from query (default to latest) |
25 | 55 | var queryString = window.location.search; |
26 | 56 | var query = new URLSearchParams(queryString); |
27 | 57 | var version = "latest"; |
28 | 58 | if (query.has("version")) { |
29 | | - version = query.get("version"); |
| 59 | + versionSelect.value = query.get("version"); |
30 | 60 | } |
31 | 61 |
|
32 | | - Redoc.init("https://storage.googleapis.com/libpod-master-releases/swagger-" + version + ".yaml", { |
33 | | - sortPropsAlphabetically: true, |
34 | | - sortOperationsAlphabetically: true, |
35 | | - }, document.getElementById("redoc-container")); |
| 62 | + function load() { |
| 63 | + // Note: We replace the whole container element because otherwise Redoc.init calls |
| 64 | + // after the initial Redoc.init call take a second rather than just a few ms. |
| 65 | + let redocContainer = document.createElement('div'); |
| 66 | + redocContainer.id = 'redoc-container'; |
| 67 | + let oldContainer = document.getElementById("redoc-container"); |
| 68 | + oldContainer.parentNode.replaceChild(redocContainer, oldContainer); |
| 69 | + |
| 70 | + Redoc.init("https://storage.googleapis.com/libpod-master-releases/swagger-" + versionSelect.value + ".yaml", { |
| 71 | + sortPropsAlphabetically: true, |
| 72 | + sortOperationsAlphabetically: true, |
| 73 | + }, redocContainer); |
| 74 | + history.pushState(null, '', '?version=' + versionSelect.value); |
| 75 | + document.title = 'Reference ' + versionSelect.value; |
| 76 | + } |
| 77 | + load(); |
| 78 | + versionSelect.addEventListener('change', load); |
36 | 79 | </script> |
37 | 80 | </body> |
38 | 81 | </html> |
0 commit comments