hubl/src/hubl-router.pug
2021-04-08 17:55:47 +02:00

64 lines
2.1 KiB
Plaintext

//-
Hubl router declaration for latter generation
Create a window.hubl.components, accessible by all components with the route declaration.
Components can also get benefits from the `getRoute` function
Eg.
```
window.hubl.getRoute('chat', true)
window.hubl.getRoute('uxnzsa') // Where uxnzsa is the uniq of the component
```
will return the route of the first chat component, if exists, or triggers an error.
-
let routes = new Set();
const getRoute = (type, returnFirst = false) => {
let availables = components.filter(c=>c.type==type||c.uniq==type);
if(availables.length > 1) {
if(returnFirst) {
return availables[0].route;
} else {
console.error(`Too much components availables for route ${type}`);
}
} else if(availables.length < 1) {
console.error(`No component found for route ${type}`);
} else {
return availables[0].route;
}
}
const getComponent = (type, returnFirst = false) => {
let availables = components.filter(c=>c.type==type);
if(availables.length > 1) {
if(returnFirst) {
return availables[0];
} else {
console.error(`Too much components availables for type ${type}`);
}
} else if(availables.length < 1) {
console.error(`No component found for type ${type}`);
} else {
return availables[0];
}
}
for component of components
-
if(typeof component.route === 'undefined') {
component.route = component.type;
}
if(component.route) {
component.uniq = Math.random().toString(16).slice(2);
let route = component.route;
if (routes.has(component.route)) {
route += "-" + component.uniq;
}
routes.add(route);
component.route = route;
}
-
const defaultComponent = components.filter(e=>e.defaultRoute != undefined);
let defaultRoute = "dashboard";
if(defaultComponent.length == 1) {
defaultRoute = defaultComponent[0].uniq;
}
- const hublComponents = `window.hubl={};window.hubl.components = ${JSON.stringify(components)};window.hubl.defaultRoute = "${defaultRoute}";window.hubl.client = ${JSON.stringify(client)}`;
script!=hublComponents