blob: 4ba1f3d7950bcf2f33c1dd3157c4698375dcc6ef [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 Scandolo266907e2016-12-20 13:41:42 -08003
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08004import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -08005
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08006import 'angular-ui-router';
7import 'angular-resource';
8import 'angular-cookies';
9import routesConfig from './routes';
10
11import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080012
13import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080014import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080015import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080016import {xosViews} from './app/views/index';
Matteo Scandolo266907e2016-12-20 13:41:42 -080017import {xosTemplate} from './app/template/index';
18
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019import {
20 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
21 NoHyperlinksInterceptor
22} from './interceptors';
23import {IRuntimeStatesService} from './app/core/services/runtime-states';
24import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest';
25import {IXosCrudData} from './app/views/crud/crud';
26import * as _ from 'lodash';
27import {IXosNavigationService} from './app/core/services/navigation';
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080028import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolod58d5042016-12-16 16:59:21 -080029import {IXosConfigHelpersService} from './app/core/services/helpers/config.helpers';
Matteo Scandolo99fface2016-12-21 15:37:23 -080030import {StyleConfig} from './app/config/style.config';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031
32export interface IXosState extends angular.ui.IState {
33 data: IXosCrudData;
34};
35
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080036angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080037 .module('app', [
38 xosCore,
39 xosDataSources,
40 xosViews,
41 'ui.router',
42 'ngResource',
43 xosTemplate // template module
44 ])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080045 .config(routesConfig)
46 .config(interceptorConfig)
47 .factory('UserStatusInterceptor', userStatusInterceptor)
48 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080049 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
50 .component('xos', main)
Matteo Scandolo266907e2016-12-20 13:41:42 -080051 .run(function($rootScope: ng.IRootScopeService, $transitions: any) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080052 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080053 $transitions.onSuccess({ to: '**' }, (transtion) => {
54 if (transtion.$to().name === 'login') {
55 $rootScope['class'] = 'blank';
56 }
57 else {
58 $rootScope['class'] = '';
59 }
60 });
61 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080062 .run((
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080063 $location: ng.ILocationService,
64 $state: ng.ui.IStateService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080065 ModelDefs: IModeldefsService,
66 RuntimeStates: IRuntimeStatesService,
67 NavigationService: IXosNavigationService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080068 ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080069 PageTitle: IXosPageTitleService
70 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080071
72 // save the last visited state before reload
73 const lastRoute = window.location.hash.replace('#', '');
74
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080075 // Dinamically add a core states
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080076 ModelDefs.get()
77 .then((models: IModeldef[]) => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080078 // TODO move in a separate service and test
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080079 _.forEach(models, (m: IModeldef) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080080 const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080081 const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080082 const state: IXosState = {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080083 parent: 'core',
84 url: stateUrl,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080085 component: 'xosCrud',
Matteo Scandoloee655a12016-12-19 15:38:43 -080086 params: {
87 id: null
88 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080089 data: {
90 model: m.name,
Matteo Scandoloee655a12016-12-19 15:38:43 -080091 related: m.relations,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080092 xosTableCfg: {
Matteo Scandolod62ea792016-12-22 14:02:28 -080093 columns: ConfigHelpers.modeldefToTableCfg(m.fields, stateUrl),
94 filter: 'fulltext',
95 order: {field: 'id', reverse: false}, // TODO understand dynamic interfaces
96 actions: [
97 {
98 label: 'delete',
99 icon: 'remove',
100 color: 'red',
101 cb: (item) => {
102 console.log(item);
103 item.$delete();
104 }
105 }
106 ]
Matteo Scandoloee655a12016-12-19 15:38:43 -0800107 },
108 // TODO add form config
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800109 }
110 };
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800111
112 RuntimeStates.addState(stateName, state);
113 NavigationService.add({
114 label: ConfigHelpers.pluralize(m.name),
115 state: stateName,
116 parent: 'xos.core'
117 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800118 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800119
120 // after setting up dynamic routes, redirect to previous state
121 $location.path(lastRoute);
122 // $state.get().forEach(s => {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800123 // console.log(s.name, $state.href(s.name), s);
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800124 // });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800125 });
126 });