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