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 | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | |
| 33 | export interface IXosState extends angular.ui.IState { |
| 34 | data: IXosCrudData; |
| 35 | }; |
| 36 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 37 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 38 | .module('app', [ |
| 39 | xosCore, |
| 40 | xosDataSources, |
| 41 | xosViews, |
| 42 | 'ui.router', |
| 43 | 'ngResource', |
| 44 | xosTemplate // template module |
| 45 | ]) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 46 | .config(routesConfig) |
| 47 | .config(interceptorConfig) |
| 48 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 49 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 51 | .component('xos', main) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 52 | .run(function($rootScope: ng.IRootScopeService, $transitions: any) { |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 53 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 54 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 55 | if (transtion.$to().name === 'login') { |
| 56 | $rootScope['class'] = 'blank'; |
| 57 | } |
| 58 | else { |
| 59 | $rootScope['class'] = ''; |
| 60 | } |
| 61 | }); |
| 62 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 63 | .run(( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 64 | $location: ng.ILocationService, |
| 65 | $state: ng.ui.IStateService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 66 | ModelDefs: IModeldefsService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 67 | ModelRest: IXosResourceService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 68 | RuntimeStates: IRuntimeStatesService, |
| 69 | NavigationService: IXosNavigationService, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 70 | ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 71 | toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 72 | PageTitle: IXosPageTitleService |
| 73 | ) => { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 74 | |
| 75 | // save the last visited state before reload |
| 76 | const lastRoute = window.location.hash.replace('#', ''); |
| 77 | |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 78 | // Dinamically add a core states |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 79 | ModelDefs.get() |
| 80 | .then((models: IModeldef[]) => { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 81 | // TODO move in a separate service and test (StateConfig service in Core) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 82 | _.forEach(models, (m: IModeldef) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 83 | const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 84 | const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 85 | const state: IXosState = { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 86 | parent: 'core', |
| 87 | url: stateUrl, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 88 | component: 'xosCrud', |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 89 | params: { |
| 90 | id: null |
| 91 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 92 | data: { |
| 93 | model: m.name, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 94 | related: m.relations, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 95 | xosTableCfg: ConfigHelpers.modelToTableCfg(m, stateUrl), |
| 96 | xosFormCfg: ConfigHelpers.modelToFormCfg(m) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 97 | } |
| 98 | }; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 99 | |
| 100 | RuntimeStates.addState(stateName, state); |
| 101 | NavigationService.add({ |
| 102 | label: ConfigHelpers.pluralize(m.name), |
| 103 | state: stateName, |
| 104 | parent: 'xos.core' |
| 105 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 106 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 107 | |
| 108 | // after setting up dynamic routes, redirect to previous state |
| 109 | $location.path(lastRoute); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 110 | }); |
| 111 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 112 | |