Commit 14c2a38b authored by Clément's avatar Clément

Merge branch 'fix/default-route' into 'fix/#26-nav-to-sub-route'

Fix/default route

See merge request !13
parents a26caba3 f04520f5
Pipeline #2642 passed with stage
in 32 seconds
......@@ -4,9 +4,6 @@ export default class SIBRoute extends HTMLElement {
}
connectedCallback() {
this.addEventListener('click', event => this.router.navigate(this.name));
// Resetting all routers within the selected view
document.addEventListener('DOMContentLoaded', () => this.updateSubRouters());
}
get name() {
......
......@@ -2,8 +2,13 @@ export default class SIBRouter extends HTMLElement {
constructor() {
super();
window.addEventListener('popstate', () => this.display());
window.addEventListener('requestNavigation', event => this.navigate(event.detail.route, event.detail.resource, event.detail.keepURL));
window.addEventListener('DOMContentLoaded', () => this.display());
window.addEventListener('requestNavigation', ({detail}) => this.navigate(detail.route, detail.resource, detail.keepURL));
window.addEventListener('DOMContentLoaded', () => {
for (const route of this.routes) {
route.updateSubRouters();
}
this.display();
});
}
get useHash() {
return this.hasAttribute('use-hash');
......@@ -23,10 +28,7 @@ export default class SIBRouter extends HTMLElement {
if(url.startsWith(this.prefix)) {
url = url.slice(this.prefix.length);
}
if(url.startsWith('/')) {
url = url.slice(1);
}
return url;
return stripSlash(url);
}
get currentRouteName() {
return this.currentURL.split('/')[0] || this.defaultRoute;
......@@ -74,5 +76,14 @@ export default class SIBRouter extends HTMLElement {
this.display(routeName, resource);
}
}
customElements.define('sib-router', SIBRouter);
function stripSlash(str) {
if(str.startsWith('/')) {
str = str.slice(1);
}
if(str.endsWith('/')) {
str = str.slice(0, str.length - 1);
}
return str;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment