documentation/extensions/odoo_theme/static/js/switchers.js
Martin Trigaux 64d2ac15c4 [IMP] odoo_theme: introduce fallback URLs for the page switchers
When a user clicks on the link of an alternate page in the version or
language switcher, we now check if the page referenced by the target URL
exists or not. If not, we generate a series of fallback URLs from the
target URL and check whether the targeted resource exists or not, until
we read the root of the documentation. As soon as we find a valid URL,
we redirect the user to it.

This is inspired by the behaviour of docs.python.org's version and
language switchers.

task-2534669

closes odoo/documentation#2042

X-original-commit: 25e863a64c
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
Co-authored-by: Antoine Vandevenne (anv) <anv@odoo.com>
2022-05-19 22:30:38 +02:00

28 lines
1.0 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')) {
event.preventDefault();
const fallbackUrls = await _generateFallbackUrls(element.getAttribute('href'));
const fallbackUrl = await _getFirstValidUrl(fallbackUrls);
window.location.href = fallbackUrl;
}
});
});
};
})();