Commit d6771be1 authored by Matthieu Fesselier's avatar Matthieu Fesselier

update: display default route for /

parent 5fc4fc0e
Pipeline #4679 passed with stage
in 27 seconds
......@@ -55,9 +55,9 @@ export default class SIBRoute extends HTMLElement {
.replace(/^@/, '');
}
getPath(prefix, name, id = '') {
getPath(prefix, name, id = '', isDefaultRoute = false) {
const encodedId = this.encodeId(id);
return [prefix, name, encodedId].filter(s => s).join('/');
return [prefix, isDefaultRoute ? '' : name, encodedId].filter(s => s).join('/');
}
updateResource(id = null) {
const resourceId = id || this.resourceId;
......@@ -76,6 +76,7 @@ export default class SIBRoute extends HTMLElement {
this.router.prefix,
this.name,
id || this.resourceId,
this.router.defaultRoute == this.name // is current route default route?
);
router.setAttribute('route-prefix', routePrefix);
router.display();
......
......@@ -32,7 +32,9 @@ export default class SIBRouter extends HTMLElement {
return stripSlashes(url);
}
get currentRouteName() {
return this.currentURL.split('/')[0] || this.defaultRoute;
const currentURL = this.currentURL.split('/')[0];
const isInRouter = Array.from(this.routes).some(route => route.name == currentURL) // is currentURL in current routes?
return !isInRouter || !currentURL ? this.defaultRoute : currentURL; // if url not in current routes, the url belongs to a subrouter -> return default route
}
get routes() {
return this.querySelectorAll('sib-route');
......@@ -77,14 +79,17 @@ export default class SIBRouter extends HTMLElement {
});
}
const id = resource ? resource['@id'] : null;
const path = route.getPath(this.prefix, routeName, route.hasAttribute('use-id') ? id : '');
if (!keepURL) {
//update current URL
const prefix = this.useHash ? '#' : '/';
history.pushState({}, routeName, prefix + path);
this.updateURL(route)
}
this.display(routeName, id, keepURL);
}
updateURL(route) {
const path = route.getPath(this.prefix, route.name, route.hasAttribute('use-id') ? id : '', route.name == this.defaultRoute);
const prefix = this.useHash ? '#' : '/';
history.pushState({}, route.name, prefix + path); // update current URL
}
}
if (!customElements.get('sib-router')) {
......
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