-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathnavbar-version-selector.html
More file actions
67 lines (58 loc) · 2.59 KB
/
navbar-version-selector.html
File metadata and controls
67 lines (58 loc) · 2.59 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
{{ if .Site.Params.versions -}}
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
{{ .Site.Params.version_menu }}
</a>
<div class="dropdown-menu" id="version-dropdown-menu" aria-labelledby="navbarDropdown">
<div w3-include-html="{{ .Site.Params.releases_url }}" w3-include-html-default='<a class="dropdown-item" href="{{ .Site.Params.global_logo_url }}dev/">Dev</a>'></div>
<script>
w3.includeHTML(function() {
const basePath = "{{ site.BaseURL | relURL }}";
function stripBase(path) {
if (path.startsWith(basePath)) return path.substring(basePath.length);
if (basePath === "/" && path.startsWith("/")) return path.substring(1);
return path.replace(/^\//, '');
}
document.getElementById('version-dropdown-menu').addEventListener('click', function(e) {
const link = e.target.closest('a.dropdown-item');
if (!link) return;
e.preventDefault();
let targetPath = link.pathname;
if (!targetPath.endsWith('/')) targetPath += '/';
let cleanCurrentPath = stripBase(window.location.pathname);
const allLinks = document.querySelectorAll('#version-dropdown-menu a.dropdown-item');
let currentVersionPrefix = "";
allLinks.forEach(a => {
let cleanVPath = stripBase(a.pathname);
if (!cleanVPath.endsWith('/')) cleanVPath += '/';
if (cleanVPath !== "" && cleanVPath !== "/" && cleanCurrentPath.startsWith(cleanVPath)) {
if (cleanVPath.length > currentVersionPrefix.length) {
currentVersionPrefix = cleanVPath;
}
}
});
let deepPath = cleanCurrentPath;
if (currentVersionPrefix !== "") {
deepPath = cleanCurrentPath.substring(currentVersionPrefix.length);
}
deepPath = deepPath.replace(/^\//, '');
const fullTargetPath = targetPath + deepPath;
// Perform a HEAD request to check if the deep route exists
fetch(fullTargetPath, { method: 'HEAD' })
.then(response => {
if (response.ok) {
// Page exists! Redirect to deep path
window.location.href = fullTargetPath;
} else {
// 404 or other error: Fallback to the version root
window.location.href = targetPath;
}
})
.catch(() => {
// If the fetch fails entirely, fallback to root
window.location.href = targetPath;
});
});
});
</script>
</div>
{{ end -}}