blob: bf2a1ccffda29a12de16d91c44ca8e77736e0a2e [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';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080023import {IXosCrudData} from './app/views/crud/crud';
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080024import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolo04964232017-01-07 12:53:46 -080025import {IXosAuthService} from './app/datasources/rest/auth.rest';
Matteo Scandolob4b74a82017-01-12 13:12:26 -080026import {IXosModelSetupService} from './app/core/services/helpers/model-setup.helpers';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080027import {IXosNavigationRoute} from './app/core/services/navigation';
Matteo Scandolo17bf8242017-01-23 17:30:39 -080028import XosLogDecorator from './decorators';
Matteo Scandolo4e870232017-01-30 13:43:05 -080029import {xosExtender} from './app/extender/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080030
31export interface IXosState extends angular.ui.IState {
32 data: IXosCrudData;
33};
34
Matteo Scandolo828d1e82017-01-17 14:49:38 -080035export interface IXosAppConfig {
36 apiEndpoint: string;
37 websocketClient: string;
38}
39
40export interface IXosStyleConfig {
41 projectName: string;
42 payoff: string;
43 favicon: string;
44 background: string;
45 logo: string;
46 routes: IXosNavigationRoute[];
47}
48
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080049angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080050 .module('app', [
51 xosCore,
52 xosDataSources,
53 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080054 xosExtender,
Matteo Scandolo266907e2016-12-20 13:41:42 -080055 'ui.router',
56 'ngResource',
57 xosTemplate // template module
58 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080059 .config(XosLogDecorator)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080060 .config(routesConfig)
61 .config(interceptorConfig)
62 .factory('UserStatusInterceptor', userStatusInterceptor)
63 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080064 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
65 .component('xos', main)
Matteo Scandolo17bf8242017-01-23 17:30:39 -080066 .run(function($log: ng.ILogService, $rootScope: ng.IRootScopeService, $transitions: any, StyleConfig: IXosStyleConfig) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080067 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080068 $transitions.onSuccess({ to: '**' }, (transtion) => {
69 if (transtion.$to().name === 'login') {
70 $rootScope['class'] = 'blank';
71 }
72 else {
73 $rootScope['class'] = '';
74 }
75 });
76 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080077 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080078 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080079 $transitions: any,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080080 $location: ng.ILocationService,
81 $state: ng.ui.IStateService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080082 ModelSetup: IXosModelSetupService,
Matteo Scandolo04964232017-01-07 12:53:46 -080083 AuthService: IXosAuthService,
Matteo Scandolobac22452017-01-03 16:35:32 -080084 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080085 PageTitle: IXosPageTitleService
86 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080087
Matteo Scandolo04964232017-01-07 12:53:46 -080088 // check the user login
89 $transitions.onSuccess({ to: '**' }, (transtion) => {
90 if (!AuthService.getUser()) {
91 $state.go('login');
92 }
93 });
94
Matteo Scandolo17bf8242017-01-23 17:30:39 -080095 // preserve debug=true query string parameter
96 $transitions.onStart({ to: '**' }, (transtion) => {
97 // save location.search so we can add it back after transition is done
98 this.locationSearch = $location.search();
99 });
100
101 $transitions.onSuccess({ to: '**' }, (transtion) => {
102 // restore all query string parameters back to $location.search
103 if (angular.isDefined(this.locationSearch.debug) && this.locationSearch.debug) {
104 $location.search({debug: 'true'});
105 }
106 });
107
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800108 // save the last visited state before reload
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800109 const lastRoute = $location.path();
110 const lastQueryString = $location.search();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800111
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800112 // if the user is authenticated
113 if (AuthService.getUser()) {
114 ModelSetup.setup()
115 .then(() => {
116 // after setting up dynamic routes, redirect to previous state
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800117 $location.path(lastRoute).search(lastQueryString);
Matteo Scandolo6ef59ea2017-01-26 08:07:05 -0800118 $rootScope.$emit('xos.core.modelSetup');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800119 });
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800120 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800121
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800122 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800123