blob: 611ae212d2554e233457095446a4c0f918199b2d [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 Scandolo04964232017-01-07 12:53:46 -080093 $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
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800115 if (AuthService.isAuthenticated()) {
116 // ModelSetup.setup()
117 XosModelDiscoverer.discover()
118 .then((res) => {
119 if (res) {
120 $log.info('[XOS] All models loaded');
121 }
122 else {
123 $log.info('[XOS] Failed to load some models, moving on.');
124 }
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 Scandoloe0d71ea2016-12-19 11:56:12 -0800133
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800134 // register keyboard shortcut
135 XosKeyboardShortcut.setup();
136
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800137 XosKeyboardShortcut.registerKeyBinding({
138 key: 'd',
139 // modifiers: ['Command'],
140 cb: () => {
141 if (window.localStorage.getItem('debug') === 'true') {
142 $log.info(`[XosKeyboardShortcut] Disabling debug`);
143 window.localStorage.setItem('debug', 'false');
144 }
145 else {
146 window.localStorage.setItem('debug', 'true');
147 $log.info(`[XosKeyboardShortcut] Enabling debug`);
148 }
149 },
150 description: 'Toggle debug messages in browser console'
151 }, 'global');
152
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800153 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800154