blob: 81a0d43ad6b548aa7f7d8d1d97ebcda40a762853 [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../typings/index.d.ts" />
Matteo Scandolod819c922016-12-02 14:06:14 -08002
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08003import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -08004
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005import 'angular-ui-router';
6import 'angular-resource';
7import 'angular-cookies';
8import routesConfig from './routes';
9
10import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080011
12import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080013import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080014import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015import {xosViews} from './app/views/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080016import {
17 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
18 NoHyperlinksInterceptor
19} from './interceptors';
20import {IRuntimeStatesService} from './app/core/services/runtime-states';
21import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest';
22import {IXosCrudData} from './app/views/crud/crud';
23import * as _ from 'lodash';
24import {IXosNavigationService} from './app/core/services/navigation';
25
26export interface IXosState extends angular.ui.IState {
27 data: IXosCrudData;
28};
29
30const 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 Scandolo3b3b3b42016-12-15 17:31:53 -080050 label: `${f.name}`, // TODO confert name to label
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080051 prop: f.name
52 };
53 })
54 .filter(v => angular.isDefined(v));
55
56 return cfg;
57};
Matteo Scandolod819c922016-12-02 14:06:14 -080058
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080059angular
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080060 .module('app', [xosCore, xosDataSources, xosViews, 'ui.router', 'ngResource'])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080061 .config(routesConfig)
62 .config(interceptorConfig)
63 .factory('UserStatusInterceptor', userStatusInterceptor)
64 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080065 .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 Scandolod819c922016-12-02 14:06:14 -080074
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080075 ModelDefs.get()
76 .then((models: IModeldef[]) => {
77 _.forEach(models, (m: IModeldef) => {
78 const state: IXosState = {
79 parent: 'xos',
Matteo Scandolo3b3b3b42016-12-15 17:31:53 -080080 url: `${m.name.toLowerCase()}s`, // TODO use https://github.com/blakeembrey/pluralize
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080081 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 });