Commit 41428e4d authored by Clément's avatar Clément

bugfix: resource-id in url

parent ba30d1ab
Pipeline #2647 passed with stage
in 27 seconds
......@@ -39,7 +39,8 @@
<summary>use-hash</summary>
<h1>Musical Instruments</h1>
<sib-link next="ukulele">→ Go to ukulele</sib-link>
<sib-link next="brass" data-src="myID">→ Go to brass</sib-link>
<hr>
<sib-router default-route="keyboard" route-prefix="examples/sub-menu.html" use-hash>
<sib-route name="keyboard">Keyboard</sib-route>
<sib-route name="string">String</sib-route>
......
......@@ -28,28 +28,23 @@ export default class SIBRoute extends HTMLElement {
}
get resourceId() {
const id = this.router.currentURL.split('/')[1];
if(!id) return '';
if(!id || !id.startsWith('@')) return '';
return this.decodeId(id);
}
encodeId(id) {
if (id) {
return encodeURIComponent(id);
}
return;
if (!id) return '';
return '@' + encodeURIComponent(id);
}
decodeId(id) {
if(id) {
return decodeURIComponent(id);
}
return;
if(!id) return '';
return decodeURIComponent(id.replace(/^@/, ''));
}
getPath(prefix, name, id = '') {
const encodedId = this.encodeId(id);
return [prefix, name, encodedId].filter(s=>s).join('/');
return [prefix, name, encodedId].filter(s => s).join('/');
}
updateResource() {
if(this.resourceId) {
......
......@@ -2,7 +2,9 @@ export default class SIBRouter extends HTMLElement {
constructor() {
super();
window.addEventListener('popstate', () => this.display());
window.addEventListener('requestNavigation', ({detail}) => this.navigate(detail.route, detail.resource, detail.keepURL));
window.addEventListener('requestNavigation', ({detail}) =>
this.navigate(detail.route, detail.resource, detail.keepURL)
);
window.addEventListener('DOMContentLoaded', () => {
for (const route of this.routes) {
route.updateSubRouters();
......@@ -49,7 +51,6 @@ export default class SIBRouter extends HTMLElement {
}
}
navigate(routeName = '', resource, keepURL) {
if(keepURL) console.log(routeName);
let route
if(routeName) {
route = this.querySelector('sib-route[name="'+routeName+'"]');
......@@ -61,10 +62,12 @@ export default class SIBRouter extends HTMLElement {
if(!route) return; //this route is not for me!
if(this.prefix) {
const route = this.prefix.split('/').pop();
this.dispatchEvent(new CustomEvent('requestNavigation', {
detail: {route: route, keepURL: true},
bubbles: true,
}));
setTimeout(() => {
this.dispatchEvent(new CustomEvent('requestNavigation', {
detail: {route: route, keepURL: true},
bubbles: true,
}))
});
}
const id = resource ? resource['@id'] : null;
const path = route.getPath(this.prefix, routeName, id);
......
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