2022-02-14 13:07:29 +00:00
|
|
|
(function ($) {
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
// Enable fallback URLs for broken redirects from the version or language switchers.
|
|
|
|
|
_prepareSwitchersFallbacks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add event listeners on links in the version and language switchers.
|
|
|
|
|
*
|
|
|
|
|
* If a link is clicked, the user is redirected to the closest fallback URL (including the
|
|
|
|
|
* original target URL) that is available.
|
|
|
|
|
*/
|
|
|
|
|
const _prepareSwitchersFallbacks = () => {
|
2023-01-04 16:37:23 +00:00
|
|
|
document.querySelectorAll('a.o_switcher_item').forEach(element => {
|
2022-02-14 13:07:29 +00:00
|
|
|
element.addEventListener('click', async event => {
|
|
|
|
|
if (element.hasAttribute('href')) {
|
2022-06-15 09:20:45 +02:00
|
|
|
const targetUrl = element.getAttribute('href');
|
2022-06-22 14:27:12 +00:00
|
|
|
if (!targetUrl.startsWith('/')) { // Don't test for valid URLs if in localhost.
|
2022-06-15 09:20:45 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
const fallbackUrls = await _generateFallbackUrls(targetUrl);
|
2023-07-06 14:12:39 +00:00
|
|
|
const fallbackUrl = await _getFirstValidUrl(fallbackUrls) ?? targetUrl;
|
2022-06-15 09:20:45 +02:00
|
|
|
window.location.href = fallbackUrl;
|
|
|
|
|
}
|
2022-02-14 13:07:29 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|