Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../typings/index.d.ts" /> |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 2 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 3 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 4 | import * as angular from 'angular'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 5 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 6 | import 'angular-ui-router'; |
| 7 | import 'angular-resource'; |
| 8 | import 'angular-cookies'; |
| 9 | import routesConfig from './routes'; |
| 10 | |
| 11 | import {main} from './app/main'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 12 | |
| 13 | import './index.scss'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 14 | import {xosCore} from './app/core/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 15 | import {xosDataSources} from './app/datasources/index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 16 | import {xosViews} from './app/views/index'; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 17 | import {xosTemplate} from './app/template/index'; |
| 18 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | import { |
| 20 | interceptorConfig, userStatusInterceptor, CredentialsInterceptor, |
| 21 | NoHyperlinksInterceptor |
| 22 | } from './interceptors'; |
| 23 | import {IRuntimeStatesService} from './app/core/services/runtime-states'; |
| 24 | import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest'; |
| 25 | import {IXosCrudData} from './app/views/crud/crud'; |
| 26 | import * as _ from 'lodash'; |
| 27 | import {IXosNavigationService} from './app/core/services/navigation'; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 28 | import {IXosPageTitleService} from './app/core/services/page-title'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 29 | import {IXosConfigHelpersService} from './app/core/services/helpers/config.helpers'; |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 30 | import {StyleConfig} from './app/config/style.config'; |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 31 | import {IXosResourceService} from './app/datasources/rest/model.rest'; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 32 | import {IXosAuthService} from './app/datasources/rest/auth.rest'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 33 | |
| 34 | export interface IXosState extends angular.ui.IState { |
| 35 | data: IXosCrudData; |
| 36 | }; |
| 37 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 38 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 39 | .module('app', [ |
| 40 | xosCore, |
| 41 | xosDataSources, |
| 42 | xosViews, |
| 43 | 'ui.router', |
| 44 | 'ngResource', |
| 45 | xosTemplate // template module |
| 46 | ]) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 47 | .config(routesConfig) |
| 48 | .config(interceptorConfig) |
| 49 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 50 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 51 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 52 | .component('xos', main) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 53 | .run(function($rootScope: ng.IRootScopeService, $transitions: any) { |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 54 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 55 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 56 | if (transtion.$to().name === 'login') { |
| 57 | $rootScope['class'] = 'blank'; |
| 58 | } |
| 59 | else { |
| 60 | $rootScope['class'] = ''; |
| 61 | } |
| 62 | }); |
| 63 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 64 | .run(( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 65 | $location: ng.ILocationService, |
| 66 | $state: ng.ui.IStateService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 67 | ModelDefs: IModeldefsService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 68 | ModelRest: IXosResourceService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 69 | RuntimeStates: IRuntimeStatesService, |
| 70 | NavigationService: IXosNavigationService, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 71 | ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 72 | AuthService: IXosAuthService, |
| 73 | $transitions: any, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 74 | toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 75 | PageTitle: IXosPageTitleService |
| 76 | ) => { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 77 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 78 | // check the user login |
| 79 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 80 | if (!AuthService.getUser()) { |
| 81 | $state.go('login'); |
| 82 | } |
| 83 | }); |
| 84 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 85 | // save the last visited state before reload |
| 86 | const lastRoute = window.location.hash.replace('#', ''); |
| 87 | |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 88 | // Dinamically add a core states |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 89 | ModelDefs.get() |
| 90 | .then((models: IModeldef[]) => { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 91 | // TODO move in a separate service and test (StateConfig service in Core) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 92 | _.forEach(models, (m: IModeldef) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 93 | const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 94 | const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 95 | const state: IXosState = { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 96 | parent: 'core', |
| 97 | url: stateUrl, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 98 | component: 'xosCrud', |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 99 | params: { |
| 100 | id: null |
| 101 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 102 | data: { |
| 103 | model: m.name, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 104 | related: m.relations, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 105 | xosTableCfg: ConfigHelpers.modelToTableCfg(m, stateUrl), |
| 106 | xosFormCfg: ConfigHelpers.modelToFormCfg(m) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 107 | } |
| 108 | }; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 109 | |
| 110 | RuntimeStates.addState(stateName, state); |
| 111 | NavigationService.add({ |
| 112 | label: ConfigHelpers.pluralize(m.name), |
| 113 | state: stateName, |
| 114 | parent: 'xos.core' |
| 115 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 116 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 117 | |
| 118 | // after setting up dynamic routes, redirect to previous state |
| 119 | $location.path(lastRoute); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 120 | }); |
| 121 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 122 | |