2017-01-18 12:51:10 +01:00
|
|
|
|
import { Components, getComponent } from './components';
|
2016-12-12 11:12:41 +09:00
|
|
|
|
|
2017-03-23 16:27:59 +09:00
|
|
|
|
export const Routes = {}; // will be populated on startup (see vulcan:routing)
|
2017-01-18 15:11:31 +01:00
|
|
|
|
export const RoutesTable = {}; // storage for infos about routes themselves
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
A route is defined in the list like:
|
2017-01-18 15:11:31 +01:00
|
|
|
|
RoutesTable.foobar = {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
name: 'foobar',
|
2017-01-18 15:11:31 +01:00
|
|
|
|
path: '/xyz',
|
|
|
|
|
component: getComponent('FooBar')
|
|
|
|
|
componentName: 'FooBar' // optional
|
2017-01-18 12:51:10 +01:00
|
|
|
|
}
|
|
|
|
|
*/
|
2016-12-12 11:12:41 +09:00
|
|
|
|
export const addRoute = routeOrRouteArray => {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|
|
|
|
|
// be sure to have an array of routes to manipulate
|
2016-12-12 11:12:41 +09:00
|
|
|
|
const addedRoutes = Array.isArray(routeOrRouteArray) ? routeOrRouteArray : [routeOrRouteArray];
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|
2017-01-18 15:11:31 +01:00
|
|
|
|
// modify the routes table with the new routes
|
2017-01-19 14:57:56 +01:00
|
|
|
|
addedRoutes.map(({name, path, ...properties}) => {
|
|
|
|
|
|
|
|
|
|
// check if there is already a route registered to this path
|
|
|
|
|
// note: destructure in order to get the first item of the array, as _.filter returns an array
|
|
|
|
|
const [routeWithSamePath] = _.filter(RoutesTable, route => route.path === path);
|
|
|
|
|
|
|
|
|
|
if (routeWithSamePath) {
|
|
|
|
|
// delete the route registered with same path
|
|
|
|
|
delete RoutesTable[routeWithSamePath.name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// register the new route
|
2017-01-18 15:11:31 +01:00
|
|
|
|
RoutesTable[name] = {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
name,
|
2017-01-19 14:57:56 +01:00
|
|
|
|
path,
|
2017-01-18 12:51:10 +01:00
|
|
|
|
...properties
|
|
|
|
|
};
|
2017-01-19 14:57:56 +01:00
|
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getRoute = name => {
|
2017-01-18 15:11:31 +01:00
|
|
|
|
const routeDef = RoutesTable[name];
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|
2017-01-18 15:11:31 +01:00
|
|
|
|
// components should be loaded by now (populateComponentsApp function), we can grab the component in the lookup table and assign it to the route
|
|
|
|
|
if (!routeDef.component && routeDef.componentName) {
|
|
|
|
|
routeDef.component = getComponent(routeDef.componentName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return routeDef;
|
2017-01-18 12:51:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:11:31 +01:00
|
|
|
|
/**
|
|
|
|
|
* Populate the lookup table for routes to be callable
|
|
|
|
|
* ℹ️ Called once on app startup
|
|
|
|
|
**/
|
|
|
|
|
export const populateRoutesApp = () => {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
// loop over each component in the list
|
2017-01-18 15:11:31 +01:00
|
|
|
|
Object.keys(RoutesTable).map(name => {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|
|
|
|
|
// populate an entry in the lookup table
|
|
|
|
|
Routes[name] = getRoute(name);
|
|
|
|
|
|
|
|
|
|
// uncomment for debug
|
|
|
|
|
// console.log('init route:', name);
|
|
|
|
|
});
|
2017-01-03 16:42:53 +08:00
|
|
|
|
}
|
2017-01-18 12:51:10 +01:00
|
|
|
|
|