2017-04-07 20:50:41 +08: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
|
|
|
|
|
|
|
|
|
/*
|
2017-04-07 13:40:14 +08:00
|
|
|
|
A route is defined in the list like:
|
|
|
|
|
RoutesTable.foobar = {
|
2017-01-18 12:51:10 +01:00
|
|
|
|
name: 'foobar',
|
2017-04-07 13:40:14 +08:00
|
|
|
|
path: '/xyz',
|
2017-01-18 15:11:31 +01:00
|
|
|
|
component: getComponent('FooBar')
|
|
|
|
|
componentName: 'FooBar' // optional
|
2017-04-07 13:40:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if there there is value for parentRouteName it will look for the route and add the new route as a child of it
|
|
|
|
|
*/
|
|
|
|
|
export const addRoute = (routeOrRouteArray, parentRouteName) => {
|
|
|
|
|
|
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-04-07 13:40:14 +08:00
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
// if there is a value for parentRouteName you are adding this route as new child
|
2017-04-07 13:40:14 +08:00
|
|
|
|
if (parentRouteName) {
|
2017-04-07 18:41:33 +09:00
|
|
|
|
|
2017-04-07 13:40:14 +08:00
|
|
|
|
addAsChildRoute(parentRouteName, addedRoutes);
|
2017-04-07 20:50:41 +08:00
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
} else {
|
|
|
|
|
|
2017-04-07 13:40:14 +08:00
|
|
|
|
// modify the routes table with the new routes
|
|
|
|
|
addedRoutes.map(({name, path, ...properties}) => {
|
|
|
|
|
|
|
|
|
|
// check if there is already a route registered to this path
|
2017-08-30 18:50:14 +09:00
|
|
|
|
const routeWithSamePath = _.findWhere(RoutesTable, { path });
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
|
|
|
|
if (routeWithSamePath) {
|
|
|
|
|
// delete the route registered with same path
|
|
|
|
|
delete RoutesTable[routeWithSamePath.name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// register the new route
|
|
|
|
|
RoutesTable[name] = {
|
|
|
|
|
name,
|
|
|
|
|
path,
|
|
|
|
|
...properties
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-30 18:50:14 +09:00
|
|
|
|
export const extendRoute = (routeName, routeProps) => {
|
|
|
|
|
|
|
|
|
|
const route = _.findWhere(RoutesTable, {name: routeName});
|
|
|
|
|
|
2017-10-05 09:14:42 +09:00
|
|
|
|
if (route) {
|
|
|
|
|
RoutesTable[route.name] = {
|
|
|
|
|
...route,
|
|
|
|
|
...routeProps
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-08-30 18:50:14 +09:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
A route is defined in the list like: (same as above)
|
|
|
|
|
RoutesTable.foobar = {
|
|
|
|
|
name: 'foobar',
|
|
|
|
|
path: '/xyz',
|
|
|
|
|
component: getComponent('FooBar')
|
|
|
|
|
componentName: 'FooBar' // optional
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NOTE: This is implemented on single level deep ONLY for now
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
2017-04-16 21:39:16 +09:00
|
|
|
|
export const addAsChildRoute = (parentRouteName, addedRoutes) => {
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
// if the parentRouteName does not exist, error
|
2017-04-07 20:50:41 +08:00
|
|
|
|
if (!RoutesTable[parentRouteName]) {
|
2017-04-07 18:41:33 +09:00
|
|
|
|
throw new Error(`Route ${parentRouteName} doesn't exist`)
|
|
|
|
|
}
|
2017-04-07 13:40:14 +08: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}) => {
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
// get the current child routes for this Route
|
2017-04-07 13:40:14 +08:00
|
|
|
|
const childRoutes = RoutesTable[parentRouteName]['childRoutes'] || [];
|
|
|
|
|
|
2017-01-19 14:57:56 +01:00
|
|
|
|
// check if there is already a route registered to this path
|
2017-04-07 13:40:14 +08:00
|
|
|
|
const [routeWithSamePath] = _.filter(childRoutes, route => route.path === path);
|
|
|
|
|
|
|
|
|
|
if (routeWithSamePath) {
|
2017-01-19 14:57:56 +01:00
|
|
|
|
// delete the route registered with same path
|
2017-04-07 13:40:14 +08:00
|
|
|
|
delete childRoutes[routeWithSamePath.name];
|
2017-01-19 14:57:56 +01:00
|
|
|
|
}
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
// append to the child routes the new route
|
2017-04-07 13:40:14 +08:00
|
|
|
|
childRoutes.push({
|
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-04-07 13:40:14 +08:00
|
|
|
|
});
|
|
|
|
|
|
2017-04-07 18:41:33 +09:00
|
|
|
|
// register the new child route (overwriting the current which is fine)
|
2017-04-07 13:40:14 +08:00
|
|
|
|
RoutesTable[parentRouteName]['childRoutes'] = childRoutes;
|
|
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
|
});
|
2017-04-07 13:40:14 +08: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-04-07 13:40:14 +08: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);
|
|
|
|
|
}
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-01-18 15:11:31 +01:00
|
|
|
|
return routeDef;
|
2017-01-18 12:51:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 22:59:25 +01:00
|
|
|
|
export const getChildRoute = (name, index) => {
|
|
|
|
|
const routeDef = RoutesTable[name]['childRoutes'][index];
|
|
|
|
|
|
|
|
|
|
// 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 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 => {
|
2018-01-11 22:59:25 +01:00
|
|
|
|
// loop over child routes if available
|
|
|
|
|
if(typeof RoutesTable[name]['childRoutes'] !== typeof undefined){
|
|
|
|
|
RoutesTable[name]['childRoutes'].map((item, index) => {
|
|
|
|
|
RoutesTable[name]['childRoutes'][index] = getChildRoute(name, index);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
|
// populate an entry in the lookup table
|
|
|
|
|
Routes[name] = getRoute(name);
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|
2017-01-18 12:51:10 +01:00
|
|
|
|
// uncomment for debug
|
|
|
|
|
// console.log('init route:', name);
|
|
|
|
|
});
|
2017-01-03 16:42:53 +08:00
|
|
|
|
}
|
2017-04-07 13:40:14 +08:00
|
|
|
|
|