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 | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 30 | import {StyleConfig} from './app/config/style.config'; |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 31 | import {IXosResourceService} from './app/datasources/rest/model.rest'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | |
| 33 | export interface IXosState extends angular.ui.IState { |
| 34 | data: IXosCrudData; |
| 35 | }; |
| 36 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 37 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 38 | .module('app', [ |
| 39 | xosCore, |
| 40 | xosDataSources, |
| 41 | xosViews, |
| 42 | 'ui.router', |
| 43 | 'ngResource', |
| 44 | xosTemplate // template module |
| 45 | ]) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 46 | .config(routesConfig) |
| 47 | .config(interceptorConfig) |
| 48 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 49 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 51 | .component('xos', main) |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 52 | .run(function($rootScope: ng.IRootScopeService, $transitions: any) { |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 53 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 54 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 55 | if (transtion.$to().name === 'login') { |
| 56 | $rootScope['class'] = 'blank'; |
| 57 | } |
| 58 | else { |
| 59 | $rootScope['class'] = ''; |
| 60 | } |
| 61 | }); |
| 62 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 63 | .run(( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 64 | $location: ng.ILocationService, |
| 65 | $state: ng.ui.IStateService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 66 | ModelDefs: IModeldefsService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 67 | ModelRest: IXosResourceService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 68 | RuntimeStates: IRuntimeStatesService, |
| 69 | NavigationService: IXosNavigationService, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 70 | ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 71 | toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 72 | PageTitle: IXosPageTitleService |
| 73 | ) => { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 74 | |
| 75 | // save the last visited state before reload |
| 76 | const lastRoute = window.location.hash.replace('#', ''); |
| 77 | |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 78 | // Dinamically add a core states |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 79 | ModelDefs.get() |
| 80 | .then((models: IModeldef[]) => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 81 | // TODO move in a separate service and test |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 82 | _.forEach(models, (m: IModeldef) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 83 | const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 84 | const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 85 | const state: IXosState = { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 86 | parent: 'core', |
| 87 | url: stateUrl, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 88 | component: 'xosCrud', |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 89 | params: { |
| 90 | id: null |
| 91 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 92 | data: { |
| 93 | model: m.name, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 94 | related: m.relations, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 95 | xosTableCfg: { |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame^] | 96 | columns: ConfigHelpers.modeldefToColumnsCfg(m.fields, stateUrl), |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 97 | 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 Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 105 | let obj = angular.copy(item); |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 106 | |
Matteo Scandolo | 99ac9d9 | 2017-01-03 13:58:19 -0800 | [diff] [blame] | 107 | item.$delete() |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 108 | .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 Scandolo | 99ac9d9 | 2017-01-03 13:58:19 -0800 | [diff] [blame] | 114 | }) |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 115 | .catch(() => { |
| 116 | console.log(obj); |
| 117 | toastr.error(`Error while deleting ${obj.name}`); |
Matteo Scandolo | 99ac9d9 | 2017-01-03 13:58:19 -0800 | [diff] [blame] | 118 | }); |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | ] |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 122 | }, |
| 123 | // TODO add form config |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 124 | } |
| 125 | }; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 126 | |
| 127 | RuntimeStates.addState(stateName, state); |
| 128 | NavigationService.add({ |
| 129 | label: ConfigHelpers.pluralize(m.name), |
| 130 | state: stateName, |
| 131 | parent: 'xos.core' |
| 132 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 133 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 134 | |
| 135 | // after setting up dynamic routes, redirect to previous state |
| 136 | $location.path(lastRoute); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 137 | }); |
| 138 | }); |