Skip to content

Commit f87c8b9

Browse files
docs: introduce custom version selector in api.html
Fixes: containers#27277 Signed-off-by: Martin Fischer <[email protected]>
1 parent fa5d6cc commit f87c8b9

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

docs/source/_static/api.html

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,67 @@
1515
margin: 0;
1616
padding: 0;
1717
}
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+
}
1837
</style>
1938
</head>
2039
<body>
2140
<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>
2244
<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+
}
2454
// get version from query (default to latest)
2555
var queryString = window.location.search;
2656
var query = new URLSearchParams(queryString);
2757
var version = "latest";
2858
if (query.has("version")) {
29-
version = query.get("version");
59+
versionSelect.value = query.get("version");
3060
}
3161

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);
3679
</script>
3780
</body>
3881
</html>

0 commit comments

Comments
 (0)