hubl/src/hubl-router.pug

80 lines
2.6 KiB
Plaintext
Raw Normal View History

//-
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)
2021-03-16 14:27:30 +00:00
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) => {
2021-03-16 14:27:30 +00:00
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;
}
2021-04-15 11:29:03 +00:00
if(component.extensions)
for extension of component.extensions
-
if(typeof extension.route === 'undefined') {
extension.route = extension.type;
}
if(extension.route) {
extension.uniq = Math.random().toString(16).slice(2);
let route = extension.route;
if (routes.has(extension.route)) {
route += "-" + extension.uniq;
}
routes.add(route);
extension.route = route;
}
2021-03-16 14:27:30 +00:00
-
const defaultComponent = components.filter(e=>e.defaultRoute != undefined);
let defaultRoute = "dashboard";
if(defaultComponent.length == 1) {
defaultRoute = defaultComponent[0].uniq;
}
2021-04-08 15:48:21 +00:00
- const hublComponents = `window.hubl={};window.hubl.components = ${JSON.stringify(components)};window.hubl.defaultRoute = "${defaultRoute}";window.hubl.client = ${JSON.stringify(client)}`;
script!=hublComponents