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 | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 3 | import * as angular from 'angular'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 4 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | import 'angular-ui-router'; |
| 6 | import 'angular-resource'; |
| 7 | import 'angular-cookies'; |
| 8 | import routesConfig from './routes'; |
| 9 | |
| 10 | import {main} from './app/main'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 11 | |
| 12 | import './index.scss'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 13 | import {xosCore} from './app/core/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 14 | import {xosDataSources} from './app/datasources/index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | import {xosViews} from './app/views/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 16 | import { |
| 17 | interceptorConfig, userStatusInterceptor, CredentialsInterceptor, |
| 18 | NoHyperlinksInterceptor |
| 19 | } from './interceptors'; |
| 20 | import {IRuntimeStatesService} from './app/core/services/runtime-states'; |
| 21 | import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest'; |
| 22 | import {IXosCrudData} from './app/views/crud/crud'; |
| 23 | import * as _ from 'lodash'; |
| 24 | import {IXosNavigationService} from './app/core/services/navigation'; |
| 25 | |
| 26 | export interface IXosState extends angular.ui.IState { |
| 27 | data: IXosCrudData; |
| 28 | }; |
| 29 | |
| 30 | const modeldefToTableCfg = (fields: {name: string, type: string}[]): any[] => { |
| 31 | const excluded_fields = [ |
| 32 | 'created', |
| 33 | 'updated', |
| 34 | 'enacted', |
| 35 | 'policed', |
| 36 | 'backend_register', |
| 37 | 'deleted', |
| 38 | 'write_protect', |
| 39 | 'lazy_blocked', |
| 40 | 'no_sync', |
| 41 | 'no_policy', |
| 42 | 'omf_friendly', |
| 43 | 'enabled' |
| 44 | ]; |
| 45 | const cfg = _.map(fields, (f) => { |
| 46 | if (excluded_fields.indexOf(f.name) > -1) { |
| 47 | return; |
| 48 | } |
| 49 | return { |
Matteo Scandolo | 3b3b3b4 | 2016-12-15 17:31:53 -0800 | [diff] [blame^] | 50 | label: `${f.name}`, // TODO confert name to label |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 51 | prop: f.name |
| 52 | }; |
| 53 | }) |
| 54 | .filter(v => angular.isDefined(v)); |
| 55 | |
| 56 | return cfg; |
| 57 | }; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 59 | angular |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 60 | .module('app', [xosCore, xosDataSources, xosViews, 'ui.router', 'ngResource']) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 61 | .config(routesConfig) |
| 62 | .config(interceptorConfig) |
| 63 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 64 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 65 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 66 | .component('xos', main) |
| 67 | .run((ModelDefs: IModeldefsService, RuntimeStates: IRuntimeStatesService, NavigationService: IXosNavigationService) => { |
| 68 | // Dinamically add a state |
| 69 | RuntimeStates.addState('test', { |
| 70 | parent: 'xos', |
| 71 | url: 'test', |
| 72 | template: 'Test State' |
| 73 | }); |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 74 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 75 | ModelDefs.get() |
| 76 | .then((models: IModeldef[]) => { |
| 77 | _.forEach(models, (m: IModeldef) => { |
| 78 | const state: IXosState = { |
| 79 | parent: 'xos', |
Matteo Scandolo | 3b3b3b4 | 2016-12-15 17:31:53 -0800 | [diff] [blame^] | 80 | url: `${m.name.toLowerCase()}s`, // TODO use https://github.com/blakeembrey/pluralize |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 81 | component: 'xosCrud', |
| 82 | data: { |
| 83 | model: m.name, |
| 84 | xosTableCfg: { |
| 85 | columns: modeldefToTableCfg(m.fields) |
| 86 | } |
| 87 | } |
| 88 | }; |
| 89 | RuntimeStates.addState(`${m.name.toLowerCase()}s`, state); |
| 90 | NavigationService.add({label: `${m.name}s`, url: `${m.name.toLowerCase()}s`}); |
| 91 | }); |
| 92 | }); |
| 93 | }); |