2021-02-22 20:27:58 +00:00
|
|
|
//-
|
|
|
|
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
|
2021-02-22 20:27:58 +00:00
|
|
|
```
|
|
|
|
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);
|
2021-02-22 20:27:58 +00:00
|
|
|
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-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)}`;
|
2021-02-22 20:27:58 +00:00
|
|
|
script!=hublComponents
|