-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-selector.js
More file actions
69 lines (61 loc) · 2.42 KB
/
Copy pathversion-selector.js
File metadata and controls
69 lines (61 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Version selector for NIOS Swagger UI
(function() {
// Use DOMContentLoaded instead of window.onload to avoid conflict with Swagger UI
document.addEventListener('DOMContentLoaded', function() {
// Check if Swagger UI is loaded every 500ms
const checkInterval = setInterval(function() {
if (document.querySelector('.swagger-ui')) {
clearInterval(checkInterval);
setTimeout(addVersionSelector, 500); // Wait additional time for complete render
}
}, 500);
});
function addVersionSelector() {
// Available versions - update this when adding new versions
const versions = [
{ name: 'json', label: 'v2.13.6 (Latest)' },
{ name: 'v2.13.6', label: 'v2.13.6' },
{ name: 'v2.12.3', label: 'v2.12.3' }
];
// Get the current version from URL
const urlParams = new URLSearchParams(window.location.search);
const currentVersion = urlParams.get('version') || 'json';
// Create the floating version selector div
const versionDiv = document.createElement('div');
versionDiv.className = 'version-floating-container';
document.body.appendChild(versionDiv);
// Create the version selector elements
const label = document.createElement('span');
label.textContent = 'API Version: ';
versionDiv.appendChild(label);
const select = document.createElement('select');
versionDiv.appendChild(select);
// Add versions to select
versions.forEach(version => {
const option = document.createElement('option');
option.value = version.name;
option.text = version.label;
option.selected = version.name === currentVersion;
select.appendChild(option);
});
// Change handler
select.onchange = function() {
const newVersion = this.value;
const url = new URL(window.location.href);
url.searchParams.set('version', newVersion);
window.location.href = url.toString();
};
// Try to add version badge to the API title
try {
const title = document.querySelector('.swagger-ui .title');
if (title) {
const versionBadge = document.createElement('span');
versionBadge.className = 'version-badge';
versionBadge.textContent = versions.find(v => v.name === currentVersion)?.label || currentVersion;
title.appendChild(versionBadge);
}
} catch (e) {
console.log('Could not add version badge:', e);
}
}
})();