blob: 458f0a7fbea7fcc1c0dcc3bbcbf2b8f17df8933c [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 Scandolof2c3ed62016-12-15 14:32:50 -080030
31export interface IXosState extends angular.ui.IState {
32 data: IXosCrudData;
33};
34
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080036 .module('app', [
37 xosCore,
38 xosDataSources,
39 xosViews,
40 'ui.router',
41 'ngResource',
42 xosTemplate // template module
43 ])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044 .config(routesConfig)
45 .config(interceptorConfig)
46 .factory('UserStatusInterceptor', userStatusInterceptor)
47 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080048 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
49 .component('xos', main)
Matteo Scandolo266907e2016-12-20 13:41:42 -080050 .run(function($rootScope: ng.IRootScopeService, $transitions: any) {
51 $transitions.onSuccess({ to: '**' }, (transtion) => {
52 if (transtion.$to().name === 'login') {
53 $rootScope['class'] = 'blank';
54 }
55 else {
56 $rootScope['class'] = '';
57 }
58 });
59 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080060 .run((
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080061 $location: ng.ILocationService,
62 $state: ng.ui.IStateService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080063 ModelDefs: IModeldefsService,
64 RuntimeStates: IRuntimeStatesService,
65 NavigationService: IXosNavigationService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080066 ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080067 PageTitle: IXosPageTitleService
68 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080069
70 // save the last visited state before reload
71 const lastRoute = window.location.hash.replace('#', '');
72
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080073 // Dinamically add a core states
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080074 ModelDefs.get()
75 .then((models: IModeldef[]) => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080076 // TODO move in a separate service and test
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080077 _.forEach(models, (m: IModeldef) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080078 const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080079 const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080080 const state: IXosState = {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080081 parent: 'core',
82 url: stateUrl,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080083 component: 'xosCrud',
Matteo Scandoloee655a12016-12-19 15:38:43 -080084 params: {
85 id: null
86 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080087 data: {
88 model: m.name,
Matteo Scandoloee655a12016-12-19 15:38:43 -080089 related: m.relations,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080090 xosTableCfg: {
Matteo Scandoloee655a12016-12-19 15:38:43 -080091 columns: ConfigHelpers.modeldefToTableCfg(m.fields, stateUrl)
92 },
93 // TODO add form config
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080094 }
95 };
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080096
97 RuntimeStates.addState(stateName, state);
98 NavigationService.add({
99 label: ConfigHelpers.pluralize(m.name),
100 state: stateName,
101 parent: 'xos.core'
102 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800103 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800104
105 // after setting up dynamic routes, redirect to previous state
106 $location.path(lastRoute);
107 // $state.get().forEach(s => {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800108 // console.log(s.name, $state.href(s.name), s);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800109 // });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800110 });
111 });