|
13 | 13 |
|
14 | 14 | // Show hidden menu when the css classes have been properly specified |
15 | 15 | this.navigationMenu.removeAttribute('hidden'); |
| 16 | + |
| 17 | + // add onclick listeners when clicking on a version or language |
| 18 | + _prepareOnSwitcherClick(); |
16 | 19 | }); |
17 | 20 |
|
18 | 21 | /** |
|
74 | 77 | } |
75 | 78 | }; |
76 | 79 |
|
| 80 | + const _prepareOnSwitcherClick = () => { |
| 81 | + [...document.getElementsByClassName('dropdown-item')]. |
| 82 | + filter(i => (i.tagName === "A" && i.getAttribute("href"))). |
| 83 | + forEach(dropdownItem => { |
| 84 | + dropdownItem.addEventListener("click", e => { |
| 85 | + _findClosestValidUrl(dropdownItem.getAttribute("href")); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + /** |
| 91 | + * Test url and find best alternative if not available |
| 92 | + * Test order: |
| 93 | + * 1. /documentation/15.0/fr/administration/install/install.html |
| 94 | + * 2. /documentation/15.0/administration/install/install.html |
| 95 | + * 3. /documentation/15.0/fr/administration/install.html |
| 96 | + * 4. /documentation/15.0/administration/install.html |
| 97 | + * 5. /documentation/15.0/fr/administration.html |
| 98 | + * 6. /documentation/15.0/administration.html |
| 99 | + * 7. /documentation/15.0/fr/ |
| 100 | + * 8. /documentation/15.0/ |
| 101 | + * 9. /documentation/ |
| 102 | + */ |
| 103 | + const _findClosestValidUrl = (url) => { |
| 104 | + const fragments = url.split("/"); |
| 105 | + let originalPath = ""; |
| 106 | + let version = ""; |
| 107 | + let language = ""; |
| 108 | + for (let f of fragments.reverse()){ |
| 109 | + if (f.match(/((^\d{2}\.\d$)|(^saas\-\d{2}.\d$)|(^master$))/)) { |
| 110 | + version = f; |
| 111 | + break; |
| 112 | + } else if (f.match(/((^[a-z]{2}_[A-Z]{2})$|^([a-z]{2})$)/)) { |
| 113 | + language = f; |
| 114 | + } else { |
| 115 | + originalPath = (originalPath ? f + "/" + originalPath : f); |
| 116 | + } |
| 117 | + } |
| 118 | + const urls = []; |
| 119 | + const paths = originalPath.split("/"); |
| 120 | + |
| 121 | + while (paths.length) { |
| 122 | + if (!paths[paths.length-1].endsWith(".html")) |
| 123 | + paths[paths.length-1] += ".html"; |
| 124 | + |
| 125 | + if (version && language) |
| 126 | + // -> 15.0/fr/administration.html |
| 127 | + // -> 15.0/administration.html |
| 128 | + urls.push( |
| 129 | + url.replace(version + "/" + language + "/" + originalPath, |
| 130 | + version + "/" + language + "/" + paths.join("/")), |
| 131 | + url.replace(version + "/" + language + "/" + originalPath, |
| 132 | + version + "/" + paths.join("/"))); |
| 133 | + else if (version && !language) |
| 134 | + // -> 15.0/administration.html |
| 135 | + urls.push( |
| 136 | + url.replace(version + "/" + originalPath, |
| 137 | + version + "/" + paths.join("/"))); |
| 138 | + else if (!version && !language) |
| 139 | + // -> administration.html |
| 140 | + urls.push( |
| 141 | + url.replace(originalPath, |
| 142 | + paths.join("/"))); |
| 143 | + |
| 144 | + paths.pop(); |
| 145 | + } |
| 146 | + |
| 147 | + if (version && language) |
| 148 | + // -> 15.0/index.html |
| 149 | + // -> index.html |
| 150 | + urls.push( |
| 151 | + url.replace(version + "/" + language + "/" + originalPath, |
| 152 | + version + "/index.html"), |
| 153 | + url.replace(version + "/" + language + "/" + originalPath, |
| 154 | + "index.html")); |
| 155 | + else if (version && !language) |
| 156 | + // -> index.html |
| 157 | + urls.push( |
| 158 | + url.replace(version + "/" + originalPath, |
| 159 | + "index.html")); |
| 160 | + else if (!version && !language) |
| 161 | + // -> index.html |
| 162 | + urls.push( |
| 163 | + url.replace(originalPath, |
| 164 | + "index.html")); |
| 165 | + |
| 166 | + _testFetchUrl(urls); |
| 167 | + }; |
| 168 | + |
| 169 | + const _testFetchUrl = (urls) => { |
| 170 | + const url = urls.shift(); |
| 171 | + if (urls.length == 0 || url.startsWith("file:///")) { |
| 172 | + window.location.href = url; |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + fetch(url).then(resp => { |
| 177 | + if (resp.ok) { |
| 178 | + // success, navigate |
| 179 | + window.location.href = url; |
| 180 | + } else { |
| 181 | + // 404, try next one |
| 182 | + _testFetchUrl(urls); |
| 183 | + } |
| 184 | + }); |
| 185 | + }; |
| 186 | + |
| 187 | + |
| 188 | + |
77 | 189 | })(); |
0 commit comments