salix/front/salix/routes.js

76 lines
2.2 KiB
JavaScript

import ngModule from './module';
import getMainRoute from 'core/lib/get-main-route';
config.$inject = ['$stateProvider', '$urlRouterProvider'];
function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('login', {
url: '/login?continue',
description: 'Login',
views: {
login: {template: '<vn-login></vn-login>'}
}
})
.state('home', {
url: '/',
description: 'Home',
template: '<vn-home></vn-home>'
});
for (let file in window.routes) {
let routeFile = window.routes[file];
let fileRoutes = routeFile.routes;
let moduleName = routeFile.module;
let validations = routeFile.validations || false;
let mainModule = getMainRoute(fileRoutes);
if (!mainModule) continue;
for (let route of fileRoutes) {
let configRoute = {
url: route.url,
template: `<${route.component} ${getParams(route)}></${route.component}>`,
description: route.description,
reloadOnSearch: false,
resolve: {
loader: loader(moduleName, validations)
},
data: {
moduleIndex: file,
acl: route.acl
}
};
if (route.abstract)
configRoute.abstract = true;
if (route.routeParams)
configRoute.params = route.routeParams;
$stateProvider.state(route.state, configRoute);
}
}
function getParams(route) {
let params = '';
let temporalParams = [];
if (!route.params)
return params;
Object.keys(route.params).forEach(key => {
temporalParams.push(`${key} = "${route.params[key]}"`);
});
return temporalParams.join(' ');
}
function loader(moduleName) {
load.$inject = ['vnModuleLoader'];
function load(moduleLoader) {
return moduleLoader.load(moduleName);
}
return load;
}
}
ngModule.config(config);