blob: de9d9bf19c5d0fdb8ad76d375e5503d834b7f1ed [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 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 Scandolof2c3ed62016-12-15 14:32:50 -080030
Matteo Scandolo828d1e82017-01-17 14:49:38 -080031export interface IXosAppConfig {
32 apiEndpoint: string;
33 websocketClient: string;
34}
35
36export interface IXosStyleConfig {
37 projectName: string;
38 payoff: string;
39 favicon: string;
40 background: string;
41 logo: string;
42 routes: IXosNavigationRoute[];
43}
44
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080045angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080046 .module('app', [
47 xosCore,
48 xosDataSources,
49 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080050 xosExtender,
Matteo Scandolo266907e2016-12-20 13:41:42 -080051 'ui.router',
52 'ngResource',
53 xosTemplate // template module
54 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080055 .config(XosLogDecorator)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080056 .config(routesConfig)
57 .config(interceptorConfig)
58 .factory('UserStatusInterceptor', userStatusInterceptor)
59 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080060 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
61 .component('xos', main)
Matteo Scandolo17bf8242017-01-23 17:30:39 -080062 .run(function($log: ng.ILogService, $rootScope: ng.IRootScopeService, $transitions: any, StyleConfig: IXosStyleConfig) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080063 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080064 $transitions.onSuccess({ to: '**' }, (transtion) => {
65 if (transtion.$to().name === 'login') {
66 $rootScope['class'] = 'blank';
67 }
68 else {
69 $rootScope['class'] = '';
70 }
71 });
72 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080073 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080074 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080075 $transitions: any,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080076 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080077 $location: ng.ILocationService,
78 $state: ng.ui.IStateService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080079 XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo04964232017-01-07 12:53:46 -080080 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080081 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolobac22452017-01-03 16:35:32 -080082 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080083 PageTitle: IXosPageTitleService
84 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080085
Matteo Scandolo04964232017-01-07 12:53:46 -080086 // check the user login
87 $transitions.onSuccess({ to: '**' }, (transtion) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080088 if (!AuthService.isAuthenticated()) {
Matteo Scandolo04964232017-01-07 12:53:46 -080089 $state.go('login');
90 }
91 });
92
Matteo Scandolo17bf8242017-01-23 17:30:39 -080093 // preserve debug=true query string parameter
94 $transitions.onStart({ to: '**' }, (transtion) => {
95 // save location.search so we can add it back after transition is done
96 this.locationSearch = $location.search();
97 });
98
99 $transitions.onSuccess({ to: '**' }, (transtion) => {
100 // restore all query string parameters back to $location.search
101 if (angular.isDefined(this.locationSearch.debug) && this.locationSearch.debug) {
102 $location.search({debug: 'true'});
103 }
104 });
105
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800106 // save the last visited state before reload
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800107 const lastRoute = $location.path();
108 const lastQueryString = $location.search();
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800109
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800110 // if the user is authenticated
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800111 if (AuthService.isAuthenticated()) {
112 // ModelSetup.setup()
113 XosModelDiscoverer.discover()
114 .then((res) => {
115 if (res) {
116 $log.info('[XOS] All models loaded');
117 }
118 else {
119 $log.info('[XOS] Failed to load some models, moving on.');
120 }
121
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800122 // after setting up dynamic routes, redirect to previous state
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800123 $location.path(lastRoute).search(lastQueryString);
Matteo Scandolo6ef59ea2017-01-26 08:07:05 -0800124 $rootScope.$emit('xos.core.modelSetup');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800125 });
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800126 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800127
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800128 // register keyboard shortcut
129 XosKeyboardShortcut.setup();
130
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800131 XosKeyboardShortcut.registerKeyBinding({
132 key: 'd',
133 // modifiers: ['Command'],
134 cb: () => {
135 if (window.localStorage.getItem('debug') === 'true') {
136 $log.info(`[XosKeyboardShortcut] Disabling debug`);
137 window.localStorage.setItem('debug', 'false');
138 }
139 else {
140 window.localStorage.setItem('debug', 'true');
141 $log.info(`[XosKeyboardShortcut] Enabling debug`);
142 }
143 },
144 description: 'Toggle debug messages in browser console'
145 }, 'global');
146
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800147 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800148