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