Skip to content

Commit 80f8f6b

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 419dd37 commit 80f8f6b

File tree

1 file changed

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

1 file changed

+112
-0
lines changed

extensions/odoo_theme/static/js/menu.js

Lines changed: 112 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,113 @@
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/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+
77189
})();

0 commit comments

Comments
 (0)