blob: 74b0c047b8f7a09b33c35010a864a9024f37263e [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 Scandolo5053cbe2017-01-31 17:37:56 -080030import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031
32export interface IXosState extends angular.ui.IState {
33 data: IXosCrudData;
34};
35
Matteo Scandolo828d1e82017-01-17 14:49:38 -080036export interface IXosAppConfig {
37 apiEndpoint: string;
38 websocketClient: string;
39}
40
41export interface IXosStyleConfig {
42 projectName: string;
43 payoff: string;
44 favicon: string;
45 background: string;
46 logo: string;
47 routes: IXosNavigationRoute[];
48}
49
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080050angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080051 .module('app', [
52 xosCore,
53 xosDataSources,
54 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080055 xosExtender,
Matteo Scandolo266907e2016-12-20 13:41:42 -080056 'ui.router',
57 'ngResource',
58 xosTemplate // template module
59 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080060 .config(XosLogDecorator)
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)
Matteo Scandolo17bf8242017-01-23 17:30:39 -080067 .run(function($log: ng.ILogService, $rootScope: ng.IRootScopeService, $transitions: any, StyleConfig: IXosStyleConfig) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080068 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080069 $transitions.onSuccess({ to: '**' }, (transtion) => {
70 if (transtion.$to().name === 'login') {
71 $rootScope['class'] = 'blank';
72 }
73 else {
74 $rootScope['class'] = '';
75 }
76 });
77 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080078 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080079 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080080 $transitions: any,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080081 $location: ng.ILocationService,
82 $state: ng.ui.IStateService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080083 ModelSetup: IXosModelSetupService,
Matteo Scandolo04964232017-01-07 12:53:46 -080084 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080085 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolobac22452017-01-03 16:35:32 -080086 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080087 PageTitle: IXosPageTitleService
88 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080089
Matteo Scandolo04964232017-01-07 12:53:46 -080090 // check the user login
91 $transitions.onSuccess({ to: '**' }, (transtion) => {
92 if (!AuthService.getUser()) {
93 $state.go('login');
94 }
95 });
96
Matteo Scandolo17bf8242017-01-23 17:30:39 -080097 // preserve debug=true query string parameter
98 $transitions.onStart({ to: '**' }, (transtion) => {
99 // save location.search so we can add it back after transition is done
100 this.locationSearch = $location.search();
101 });
102
103 $transitions.onSuccess({ to: '**' }, (transtion) => {
104 // restore all query string parameters back to $location.search
105 if (angular.isDefined(this.locationSearch.debug) && this.locationSearch.debug) {
106 $location.search({debug: 'true'});
107 }
108 });
109
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800110 // save the last visited state before reload
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800111 const lastRoute = $location.path();
112 const lastQueryString = $location.search();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800113
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800114 // if the user is authenticated
115 if (AuthService.getUser()) {
116 ModelSetup.setup()
117 .then(() => {
118 // after setting up dynamic routes, redirect to previous state
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800119 $location.path(lastRoute).search(lastQueryString);
Matteo Scandolo6ef59ea2017-01-26 08:07:05 -0800120 $rootScope.$emit('xos.core.modelSetup');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800121 });
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800122 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800123
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800124 // register keyboard shortcut
125 XosKeyboardShortcut.setup();
126
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800127 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800128