Commit 260e22fb authored by Nicolas Mérigot's avatar Nicolas Mérigot

Merge branch 'fix/remove_id_prefix' into 'master'

fix: remove id prefix, use b64 url encoded iri id instead

Closes #23

See merge request !2
parents dc940e55 e3edc760
...@@ -27,23 +27,39 @@ export class SIBRoute extends HTMLElement { ...@@ -27,23 +27,39 @@ export class SIBRoute extends HTMLElement {
return this.closest('sib-router'); return this.closest('sib-router');
} }
get resourceId() { get resourceId() {
//get resource id from url if(this.router.currentURL.split('/')[1]) {
if(this.hasAttribute('id-prefix') && this.router.currentURL.split('/')[1]) return this.decodeId(this.router.currentURL.split('/')[1]);
return this.getAttribute('id-prefix') + this.router.currentURL.split('/')[1] + '/'; }
return '';
} }
encodeId(id) {
getPath(prefix, name, id) { if (id) {
if(id && this.hasAttribute('id-prefix')) return btoa(unescape(encodeURIComponent(id)));
id = id.replace(this.getAttribute('id-prefix'), ''); }
return [prefix, name, id].filter(s=>s).join('/'); return;
}
decodeId(id) {
if(id) {
return decodeURIComponent(escape(atob(id)));
}
return;
}
getPath(prefix, name, id = '') {
const encodedId = this.encodeId(id);
return [prefix, name, encodedId].filter(s=>s).join('/');
} }
updateResource() { updateResource() {
if(this.resourceId) { if(this.resourceId) {
if(this.view.hasAttribute('bind-resources')) if(this.view.hasAttribute('bind-resources')) {
this.view.setAttribute('data-src', this.resourceId); this.view.setAttribute('data-src', this.resourceId);
for(let element of this.view.querySelectorAll('[bind-resources]')) }
for(let element of this.view.querySelectorAll('[bind-resources]')) {
element.setAttribute('data-src', this.resourceId); element.setAttribute('data-src', this.resourceId);
}
} }
} }
updateSubrouter(router) { updateSubrouter(router) {
...@@ -116,10 +132,10 @@ export class SIBRouter extends HTMLElement { ...@@ -116,10 +132,10 @@ export class SIBRouter extends HTMLElement {
navigate(routeName = '', resource) { navigate(routeName = '', resource) {
const route = this.querySelector('sib-route[name='+routeName+']'); const route = this.querySelector('sib-route[name='+routeName+']');
if(!route) return; //this route is not for me! if(!route) return; //this route is not for me!
const id = resource ? resource['@id'] : null; const id = resource ? resource['@id'] : null;
const path = route.getPath(this.prefix, routeName, id) const path = route.getPath(this.prefix, routeName, id)
route.getPath(this.prefix, routeName, id);
//update current URL //update current URL
if(this.useHash) if(this.useHash)
window.location.hash = path; window.location.hash = path;
......
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