Skip to content

Commit 70fe30d

Browse files
committed
[IMP] odoo_theme: better management of non-existant pages
In case a page does not exists, try an alternative. When switching between versions and languages, the same documentation page may not exist in the target version-language. This is inspired by the behaviour of docs.python.org version and language switcher.
1 parent a64e95d commit 70fe30d

File tree

1 file changed

+82
-0
lines changed
  • extensions/odoo_theme/static/js

1 file changed

+82
-0
lines changed

extensions/odoo_theme/static/js/menu.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
// Show hidden menu when the css classes have been properly specified
1515
this.navigationMenu.removeAttribute('hidden');
16+
17+
// add onclick listeners when clicking on a version or language
18+
_prepareOnSwitcherClick();
1619
});
1720

1821
/**
@@ -74,4 +77,83 @@
7477
}
7578
};
7679

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/
96+
* 4. /documentation/15.0/
97+
* 5. /documentation/
98+
*/
99+
const _findClosestValidUrl = (url) => {
100+
const fragments = url.split("/");
101+
let path = "";
102+
let version = "";
103+
let language = "";
104+
for (let f of fragments.reverse()){
105+
if (f.match(/((^\d{2}\.\d$)|(^saas\-\d{2}.\d$)|(^master$))/)) {
106+
version = f;
107+
break;
108+
} else if (f.match(/((^[a-z]{2}_[A-Z]{2})$|^([a-z]{2})$)/)) {
109+
language = f;
110+
} else {
111+
path = (path ? f + "/" + path : f);
112+
}
113+
}
114+
const urls = [url]
115+
if (version && language)
116+
urls.push(
117+
url.replace(version + "/" + language + "/" + path,
118+
version + "/" + path),
119+
url.replace(version + "/" + language + "/" + path,
120+
version + "/" + language + "/index.html"),
121+
url.replace(version + "/" + language + "/" + path,
122+
version + "/index.html"),
123+
url.replace(version + "/" + language + "/" + path,
124+
"index.html"));
125+
else if (version && !language)
126+
urls.push(
127+
url.replace(version + "/" + path,
128+
version + "/index.html"),
129+
url.replace(version + "/" + path,
130+
"index.html"));
131+
else if (!version && !language)
132+
urls.push(
133+
url.replace(path,
134+
"index.html"));
135+
136+
_testFetchUrl(urls);
137+
};
138+
139+
const _testFetchUrl = (urls) => {
140+
const url = urls.shift();
141+
if (urls.length == 0 || url.startsWith("file:///")) {
142+
window.location.href = url;
143+
return;
144+
}
145+
146+
fetch(url).then(resp => {
147+
if (resp.ok) {
148+
// success, navigate
149+
window.location.href = url;
150+
} else {
151+
// 404, try next one
152+
_testFetchUrl(urls);
153+
}
154+
});
155+
};
156+
157+
158+
77159
})();

0 commit comments

Comments
 (0)