|
6 | 6 | <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> |
7 | 7 |
|
8 | 8 | <script> |
9 | | - w3.includeHTML(); |
| 9 | + w3.includeHTML(function() { |
| 10 | + const basePath = "{{ site.BaseURL | relURL }}"; |
10 | 11 |
|
11 | | - const basePath = "{{ site.BaseURL | relURL }}"; |
| 12 | + function stripBase(path) { |
| 13 | + if (path.startsWith(basePath)) return path.substring(basePath.length); |
| 14 | + if (basePath === "/" && path.startsWith("/")) return path.substring(1); |
| 15 | + return path.replace(/^\//, ''); |
| 16 | + } |
12 | 17 |
|
13 | | - function stripBase(path) { |
14 | | - if (path.startsWith(basePath)) return path.substring(basePath.length); |
15 | | - if (basePath === "/" && path.startsWith("/")) return path.substring(1); |
16 | | - return path.replace(/^\//, ''); |
17 | | - } |
| 18 | + document.getElementById('version-dropdown-menu').addEventListener('click', function(e) { |
| 19 | + const link = e.target.closest('a.dropdown-item'); |
| 20 | + if (!link) return; |
| 21 | + e.preventDefault(); |
18 | 22 |
|
19 | | - document.getElementById('version-dropdown-menu').addEventListener('click', function(e) { |
20 | | - const link = e.target.closest('a.dropdown-item'); |
21 | | - if (!link) return; |
22 | | - e.preventDefault(); |
| 23 | + let targetPath = link.pathname; |
| 24 | + if (!targetPath.endsWith('/')) targetPath += '/'; |
23 | 25 |
|
24 | | - let targetPath = link.pathname; |
25 | | - if (!targetPath.endsWith('/')) targetPath += '/'; |
| 26 | + let cleanCurrentPath = stripBase(window.location.pathname); |
26 | 27 |
|
27 | | - let cleanCurrentPath = stripBase(window.location.pathname); |
| 28 | + const allLinks = document.querySelectorAll('#version-dropdown-menu a.dropdown-item'); |
| 29 | + let currentVersionPrefix = ""; |
28 | 30 |
|
29 | | - const allLinks = document.querySelectorAll('#version-dropdown-menu a.dropdown-item'); |
30 | | - let currentVersionPrefix = ""; |
| 31 | + allLinks.forEach(a => { |
| 32 | + let cleanVPath = stripBase(a.pathname); |
| 33 | + if (!cleanVPath.endsWith('/')) cleanVPath += '/'; |
31 | 34 |
|
32 | | - allLinks.forEach(a => { |
33 | | - let cleanVPath = stripBase(a.pathname); |
34 | | - if (!cleanVPath.endsWith('/')) cleanVPath += '/'; |
| 35 | + if (cleanVPath !== "" && cleanVPath !== "/" && cleanCurrentPath.startsWith(cleanVPath)) { |
| 36 | + if (cleanVPath.length > currentVersionPrefix.length) { |
| 37 | + currentVersionPrefix = cleanVPath; |
| 38 | + } |
| 39 | + } |
| 40 | + }); |
35 | 41 |
|
36 | | - if (cleanVPath !== "" && cleanVPath !== "/" && cleanCurrentPath.startsWith(cleanVPath)) { |
37 | | - if (cleanVPath.length > currentVersionPrefix.length) { |
38 | | - currentVersionPrefix = cleanVPath; |
39 | | - } |
40 | | - } |
41 | | - }); |
| 42 | + let deepPath = cleanCurrentPath; |
| 43 | + if (currentVersionPrefix !== "") { |
| 44 | + deepPath = cleanCurrentPath.substring(currentVersionPrefix.length); |
| 45 | + } |
| 46 | + deepPath = deepPath.replace(/^\//, ''); |
42 | 47 |
|
43 | | - let deepPath = cleanCurrentPath; |
44 | | - if (currentVersionPrefix !== "") { |
45 | | - deepPath = cleanCurrentPath.substring(currentVersionPrefix.length); |
46 | | - } |
47 | | - deepPath = deepPath.replace(/^\//, ''); |
48 | | - |
49 | | - // Execute the deep route |
50 | | - window.location.href = targetPath + deepPath; |
| 48 | + // Execute the deep route |
| 49 | + window.location.href = targetPath + deepPath; |
| 50 | + }); |
51 | 51 | }); |
52 | 52 | </script> |
53 | 53 | </div> |
|
0 commit comments