blob: fa8c2ea55e7a4b775369b9c52642f00e78af1239 [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';
Matteo Scandolo042ea632017-03-01 19:02:34 -08009import '../node_modules/ngprogress/build/ngProgress';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010import routesConfig from './routes';
11
12import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080013
14import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080016import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080017import {xosViews} from './app/views/index';
Matteo Scandolo266907e2016-12-20 13:41:42 -080018import {xosTemplate} from './app/template/index';
19
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080020import {
21 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
22 NoHyperlinksInterceptor
23} from './interceptors';
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 Scandolo828d1e82017-01-17 14:49:38 -080026import {IXosNavigationRoute} from './app/core/services/navigation';
Matteo Scandolo17bf8242017-01-23 17:30:39 -080027import XosLogDecorator from './decorators';
Matteo Scandolo4e870232017-01-30 13:43:05 -080028import {xosExtender} from './app/extender/index';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080029import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080030import {IXosModelDiscovererService} from './app/datasources/helpers/model-discoverer.service';
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080031import {xosServiceGraph} from './app/service-graph/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080032
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033export interface IXosAppConfig {
34 apiEndpoint: string;
35 websocketClient: string;
36}
37
38export interface IXosStyleConfig {
39 projectName: string;
40 payoff: string;
41 favicon: string;
42 background: string;
43 logo: string;
44 routes: IXosNavigationRoute[];
45}
46
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080047angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080048 .module('app', [
49 xosCore,
50 xosDataSources,
51 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080052 xosExtender,
Matteo Scandolo042ea632017-03-01 19:02:34 -080053 xosTemplate, // template module
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080054 xosServiceGraph,
Matteo Scandolo266907e2016-12-20 13:41:42 -080055 'ui.router',
56 'ngResource',
Matteo Scandolo042ea632017-03-01 19:02:34 -080057 'ngProgress'
Matteo Scandolo266907e2016-12-20 13:41:42 -080058 ])
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 Scandolo1aee1982017-02-17 08:33:23 -080080 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080081 $location: ng.ILocationService,
82 $state: ng.ui.IStateService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080083 XosModelDiscoverer: IXosModelDiscovererService,
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) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080092 if (!AuthService.isAuthenticated()) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -080093 AuthService.clearUser();
Matteo Scandolo04964232017-01-07 12:53:46 -080094 $state.go('login');
95 }
96 });
97
Matteo Scandolo17bf8242017-01-23 17:30:39 -080098 // preserve debug=true query string parameter
99 $transitions.onStart({ to: '**' }, (transtion) => {
100 // save location.search so we can add it back after transition is done
101 this.locationSearch = $location.search();
102 });
103
104 $transitions.onSuccess({ to: '**' }, (transtion) => {
105 // restore all query string parameters back to $location.search
106 if (angular.isDefined(this.locationSearch.debug) && this.locationSearch.debug) {
107 $location.search({debug: 'true'});
108 }
109 });
110
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800111 // save the last visited state before reload
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800112 const lastRoute = $location.path();
113 const lastQueryString = $location.search();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800114
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800115 // if the user is authenticated
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800116 $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800117 if (AuthService.isAuthenticated()) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800118 XosModelDiscoverer.discover()
119 .then((res) => {
120 if (res) {
121 $log.info('[XOS] All models loaded');
122 }
123 else {
124 $log.info('[XOS] Failed to load some models, moving on.');
125 }
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800126 // after setting up dynamic routes, redirect to previous state
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800127 $location.path(lastRoute).search(lastQueryString);
Matteo Scandolo042ea632017-03-01 19:02:34 -0800128 })
129 .finally(() => {
Matteo Scandolo6ef59ea2017-01-26 08:07:05 -0800130 $rootScope.$emit('xos.core.modelSetup');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800131 });
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800132 }
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800133 else {
134 AuthService.clearUser();
135 $state.go('login');
136 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800137
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800138 // register keyboard shortcut
139 XosKeyboardShortcut.setup();
140
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800141 XosKeyboardShortcut.registerKeyBinding({
142 key: 'd',
143 // modifiers: ['Command'],
144 cb: () => {
145 if (window.localStorage.getItem('debug') === 'true') {
146 $log.info(`[XosKeyboardShortcut] Disabling debug`);
147 window.localStorage.setItem('debug', 'false');
148 }
149 else {
150 window.localStorage.setItem('debug', 'true');
151 $log.info(`[XosKeyboardShortcut] Enabling debug`);
152 }
153 },
154 description: 'Toggle debug messages in browser console'
155 }, 'global');
156
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800157 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800158