documentation/extensions/odoo_theme/static/js/switchers.js
Antoine Vandevenne (anv) 54bbc778bc [FIX] odoo_theme: correctly test for fallback URLs when using switchers
closes odoo/documentation#2272

X-original-commit: 9e84b6f681
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
2022-06-23 10:51:56 +02:00

31 lines
1.2 KiB
JavaScript

(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 = () => {
document.querySelectorAll('a[class="dropdown-item"]').forEach(element => {
element.addEventListener('click', async event => {
if (element.hasAttribute('href')) {
const targetUrl = element.getAttribute('href');
if (!targetUrl.startsWith('/')) { // Don't test for valid URLs if in localhost.
event.preventDefault();
const fallbackUrls = await _generateFallbackUrls(targetUrl);
const fallbackUrl = await _getFirstValidUrl(fallbackUrls);
window.location.href = fallbackUrl;
}
}
});
});
};
})();