chore (frontend): move url in frontend

This commit is contained in:
MickaelK
2023-11-27 20:58:54 +11:00
parent def45d5ad5
commit dd6e91c493
151 changed files with 63 additions and 36 deletions

View File

@ -0,0 +1,39 @@
const triggerPageChange = () => window.dispatchEvent(new window.Event("pagechange"));
export async function init($root) {
window.addEventListener("DOMContentLoaded", triggerPageChange);
window.addEventListener("popstate", triggerPageChange);
$root.addEventListener("click", (e) => {
const href = _getHref(e.target, $root);
return !href ? null : e.preventDefault() || navigate(href);
});
}
export function navigate(href) {
window.history.pushState({
...window.history,
previous: window.location.pathname,
}, "", href);
triggerPageChange();
}
const trimPrefix = (value, prefix) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
export function currentRoute(r, notFoundRoute) {
const currentRoute = "/" + trimPrefix(
window.location.pathname,
window.document.head.querySelector("base")?.getAttribute("href") || "/"
);
for (const routeKey in r) {
if (new RegExp("^" + routeKey + "$").test(currentRoute)) {
return r[routeKey];
}
}
return r[notFoundRoute] || null;
}
function _getHref($node, $root) {
if ($node.matches("[data-link]")) return $node.getAttribute("href");
if (!$node.parentElement || $node.isSameNode($root)) return null;
return _getHref($node.parentElement, $root);
}