blob: ca6ca34cced73b3463de3481dd86332b6a7c3a04 [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../typings/index.d.ts" />
Matteo Scandolod819c922016-12-02 14:06:14 -08002
Matteo Scandolo266907e2016-12-20 13:41:42 -08003
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08004import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -08005
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08006import 'angular-ui-router';
7import 'angular-resource';
8import 'angular-cookies';
9import routesConfig from './routes';
10
11import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080012
13import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080014import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080015import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080016import {xosViews} from './app/views/index';
Matteo Scandolo266907e2016-12-20 13:41:42 -080017import {xosTemplate} from './app/template/index';
18
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019import {
20 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
21 NoHyperlinksInterceptor
22} from './interceptors';
23import {IRuntimeStatesService} from './app/core/services/runtime-states';
24import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest';
25import {IXosCrudData} from './app/views/crud/crud';
26import * as _ from 'lodash';
27import {IXosNavigationService} from './app/core/services/navigation';
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080028import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolod58d5042016-12-16 16:59:21 -080029import {IXosConfigHelpersService} from './app/core/services/helpers/config.helpers';
Matteo Scandolo99fface2016-12-21 15:37:23 -080030import {StyleConfig} from './app/config/style.config';
Matteo Scandolobac22452017-01-03 16:35:32 -080031import {IXosResourceService} from './app/datasources/rest/model.rest';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080032
33export interface IXosState extends angular.ui.IState {
34 data: IXosCrudData;
35};
36
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080037angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080038 .module('app', [
39 xosCore,
40 xosDataSources,
41 xosViews,
42 'ui.router',
43 'ngResource',
44 xosTemplate // template module
45 ])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046 .config(routesConfig)
47 .config(interceptorConfig)
48 .factory('UserStatusInterceptor', userStatusInterceptor)
49 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
51 .component('xos', main)
Matteo Scandolo266907e2016-12-20 13:41:42 -080052 .run(function($rootScope: ng.IRootScopeService, $transitions: any) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080053 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080054 $transitions.onSuccess({ to: '**' }, (transtion) => {
55 if (transtion.$to().name === 'login') {
56 $rootScope['class'] = 'blank';
57 }
58 else {
59 $rootScope['class'] = '';
60 }
61 });
62 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080063 .run((
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080064 $location: ng.ILocationService,
65 $state: ng.ui.IStateService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080066 ModelDefs: IModeldefsService,
Matteo Scandolobac22452017-01-03 16:35:32 -080067 ModelRest: IXosResourceService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080068 RuntimeStates: IRuntimeStatesService,
69 NavigationService: IXosNavigationService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080070 ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080071 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080072 PageTitle: IXosPageTitleService
73 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080074
75 // save the last visited state before reload
76 const lastRoute = window.location.hash.replace('#', '');
77
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080078 // Dinamically add a core states
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080079 ModelDefs.get()
80 .then((models: IModeldef[]) => {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080081 // TODO move in a separate service and test (StateConfig service in Core)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080082 _.forEach(models, (m: IModeldef) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080083 const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080084 const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080085 const state: IXosState = {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080086 parent: 'core',
87 url: stateUrl,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080088 component: 'xosCrud',
Matteo Scandoloee655a12016-12-19 15:38:43 -080089 params: {
90 id: null
91 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080092 data: {
93 model: m.name,
Matteo Scandoloee655a12016-12-19 15:38:43 -080094 related: m.relations,
Matteo Scandolo80c3a652017-01-06 10:48:31 -080095 xosTableCfg: ConfigHelpers.modelToTableCfg(m, stateUrl),
96 xosFormCfg: ConfigHelpers.modelToFormCfg(m)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080097 }
98 };
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080099
100 RuntimeStates.addState(stateName, state);
101 NavigationService.add({
102 label: ConfigHelpers.pluralize(m.name),
103 state: stateName,
104 parent: 'xos.core'
105 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800106 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800107
108 // after setting up dynamic routes, redirect to previous state
109 $location.path(lastRoute);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800110 });
111 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800112