blob: 0760b268e75c3c603e968d0dde24a700a9966161 [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 Scandolobac22452017-01-03 16:35:32 -080031import {IXosResourceService} from './app/datasources/rest/model.rest';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080032
33export interface IXosState extends angular.ui.IState {
34 data: IXosCrudData;
35};
36
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080037angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080038 .module('app', [
39 xosCore,
40 xosDataSources,
41 xosViews,
42 'ui.router',
43 'ngResource',
44 xosTemplate // template module
45 ])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046 .config(routesConfig)
47 .config(interceptorConfig)
48 .factory('UserStatusInterceptor', userStatusInterceptor)
49 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
51 .component('xos', main)
Matteo Scandolo266907e2016-12-20 13:41:42 -080052 .run(function($rootScope: ng.IRootScopeService, $transitions: any) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080053 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080054 $transitions.onSuccess({ to: '**' }, (transtion) => {
55 if (transtion.$to().name === 'login') {
56 $rootScope['class'] = 'blank';
57 }
58 else {
59 $rootScope['class'] = '';
60 }
61 });
62 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080063 .run((
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080064 $location: ng.ILocationService,
65 $state: ng.ui.IStateService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080066 ModelDefs: IModeldefsService,
Matteo Scandolobac22452017-01-03 16:35:32 -080067 ModelRest: IXosResourceService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080068 RuntimeStates: IRuntimeStatesService,
69 NavigationService: IXosNavigationService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080070 ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080071 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080072 PageTitle: IXosPageTitleService
73 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080074
75 // save the last visited state before reload
76 const lastRoute = window.location.hash.replace('#', '');
77
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080078 // Dinamically add a core states
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080079 ModelDefs.get()
80 .then((models: IModeldef[]) => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080081 // TODO move in a separate service and test
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080082 _.forEach(models, (m: IModeldef) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080083 const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080084 const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080085 const state: IXosState = {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080086 parent: 'core',
87 url: stateUrl,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080088 component: 'xosCrud',
Matteo Scandoloee655a12016-12-19 15:38:43 -080089 params: {
90 id: null
91 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080092 data: {
93 model: m.name,
Matteo Scandoloee655a12016-12-19 15:38:43 -080094 related: m.relations,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080095 xosTableCfg: {
Matteo Scandolo231de262017-01-04 16:33:14 -080096 columns: ConfigHelpers.modeldefToColumnsCfg(m.fields, stateUrl),
Matteo Scandolod62ea792016-12-22 14:02:28 -080097 filter: 'fulltext',
98 order: {field: 'id', reverse: false}, // TODO understand dynamic interfaces
99 actions: [
100 {
101 label: 'delete',
102 icon: 'remove',
103 color: 'red',
104 cb: (item) => {
Matteo Scandolobac22452017-01-03 16:35:32 -0800105 let obj = angular.copy(item);
Matteo Scandolobac22452017-01-03 16:35:32 -0800106
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800107 item.$delete()
Matteo Scandolobac22452017-01-03 16:35:32 -0800108 .then((res) => {
109 if (res.status === 404) {
110 // TODO understand why it does not go directly in catch
111 throw new Error();
112 }
113 toastr.info(`${m.name} ${obj.name} succesfully deleted`);
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800114 })
Matteo Scandolobac22452017-01-03 16:35:32 -0800115 .catch(() => {
116 console.log(obj);
117 toastr.error(`Error while deleting ${obj.name}`);
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800118 });
Matteo Scandolod62ea792016-12-22 14:02:28 -0800119 }
120 }
121 ]
Matteo Scandoloee655a12016-12-19 15:38:43 -0800122 },
123 // TODO add form config
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800124 }
125 };
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800126
127 RuntimeStates.addState(stateName, state);
128 NavigationService.add({
129 label: ConfigHelpers.pluralize(m.name),
130 state: stateName,
131 parent: 'xos.core'
132 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800133 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800134
135 // after setting up dynamic routes, redirect to previous state
136 $location.path(lastRoute);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800137 });
138 });