blob: 042f1bd79ea0961fc055d56999668b91dd8b13ab [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 Scandolof6acdbe2016-12-13 10:29:37 -08003import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -08004
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005import 'angular-ui-router';
6import 'angular-resource';
7import 'angular-cookies';
Matteo Scandolo042ea632017-03-01 19:02:34 -08008import '../node_modules/ngprogress/build/ngProgress';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08009import 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 Scandolo5655bdc2016-12-16 08:32:15 -080023import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolo04964232017-01-07 12:53:46 -080024import {IXosAuthService} from './app/datasources/rest/auth.rest';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080025import {IXosNavigationRoute} from './app/core/services/navigation';
Matteo Scandolo17bf8242017-01-23 17:30:39 -080026import XosLogDecorator from './decorators';
Matteo Scandolo4e870232017-01-30 13:43:05 -080027import {xosExtender} from './app/extender/index';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080028import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080029import {IXosModelDiscovererService} from './app/datasources/helpers/model-discoverer.service';
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080030import {xosServiceGraph} from './app/service-graph/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031
Matteo Scandolo828d1e82017-01-17 14:49:38 -080032export interface IXosAppConfig {
33 apiEndpoint: string;
34 websocketClient: string;
35}
36
37export interface IXosStyleConfig {
38 projectName: string;
39 payoff: string;
40 favicon: string;
41 background: string;
42 logo: string;
43 routes: IXosNavigationRoute[];
44}
45
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080047 .module('app', [
48 xosCore,
49 xosDataSources,
50 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080051 xosExtender,
Matteo Scandolo042ea632017-03-01 19:02:34 -080052 xosTemplate, // template module
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080053 xosServiceGraph,
Matteo Scandolo266907e2016-12-20 13:41:42 -080054 'ui.router',
55 'ngResource',
Matteo Scandolo042ea632017-03-01 19:02:34 -080056 'ngProgress'
Matteo Scandolo266907e2016-12-20 13:41:42 -080057 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080058 .config(XosLogDecorator)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080059 .config(routesConfig)
60 .config(interceptorConfig)
61 .factory('UserStatusInterceptor', userStatusInterceptor)
62 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080063 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
64 .component('xos', main)
Matteo Scandolo9b460042017-04-14 16:24:45 -070065 .provider('XosConfig', function(){
66 // save the last visited state before reload
67 const lastVisitedUrl = window.location.hash.replace('#', '');
68 this.$get = [() => {
69 return {
70 lastVisitedUrl
71 };
72 }] ;
73 return this;
Matteo Scandolo266907e2016-12-20 13:41:42 -080074 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080075 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080076 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080077 $transitions: any,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080078 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080079 $location: ng.ILocationService,
80 $state: ng.ui.IStateService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070081 StyleConfig: IXosStyleConfig,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080082 XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo04964232017-01-07 12:53:46 -080083 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080084 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070085 PageTitle: IXosPageTitleService // NOTE this service is not used, but needs to be loaded somewhere
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080086 ) => {
Matteo Scandolo9b460042017-04-14 16:24:45 -070087 // handle style configs
88 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
89 if ($state.current.data && $state.current.data.specialClass) {
90 $rootScope['class'] = $state.current.data.specialClass;
91 }
92 $transitions.onSuccess({ to: '**' }, (transtion) => {
93 if ($state.current.data && $state.current.data.specialClass) {
94 $rootScope['class'] = transtion.$to().data.specialClass;
95 }
96 else {
97 $rootScope['class'] = '';
98 }
99 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800100
Matteo Scandolo9b460042017-04-14 16:24:45 -0700101 // check the user login (on route change)
Matteo Scandolo04964232017-01-07 12:53:46 -0800102 $transitions.onSuccess({ to: '**' }, (transtion) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800103 if (!AuthService.isAuthenticated()) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800104 AuthService.clearUser();
Matteo Scandolo04964232017-01-07 12:53:46 -0800105 $state.go('login');
106 }
107 });
108
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800109 // if the user is authenticated
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800110 $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800111 if (AuthService.isAuthenticated()) {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700112 $state.go('loader');
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800113 }
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800114 else {
115 AuthService.clearUser();
116 $state.go('login');
117 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800118
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800119 // register keyboard shortcut
120 XosKeyboardShortcut.setup();
121
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800122 XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700123 key: 'D',
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800124 cb: () => {
125 if (window.localStorage.getItem('debug') === 'true') {
126 $log.info(`[XosKeyboardShortcut] Disabling debug`);
127 window.localStorage.setItem('debug', 'false');
128 }
129 else {
130 window.localStorage.setItem('debug', 'true');
131 $log.info(`[XosKeyboardShortcut] Enabling debug`);
132 }
133 },
134 description: 'Toggle debug messages in browser console'
135 }, 'global');
136
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800137 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800138