Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../typings/index.d.ts" /> |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 2 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 3 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 4 | import * as angular from 'angular'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 5 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 6 | import 'angular-ui-router'; |
| 7 | import 'angular-resource'; |
| 8 | import 'angular-cookies'; |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 9 | import '../node_modules/ngprogress/build/ngProgress'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | import routesConfig from './routes'; |
| 11 | |
| 12 | import {main} from './app/main'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 13 | |
| 14 | import './index.scss'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | import {xosCore} from './app/core/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 16 | import {xosDataSources} from './app/datasources/index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 17 | import {xosViews} from './app/views/index'; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 18 | import {xosTemplate} from './app/template/index'; |
| 19 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 20 | import { |
| 21 | interceptorConfig, userStatusInterceptor, CredentialsInterceptor, |
| 22 | NoHyperlinksInterceptor |
| 23 | } from './interceptors'; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 24 | import {IXosPageTitleService} from './app/core/services/page-title'; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 25 | import {IXosAuthService} from './app/datasources/rest/auth.rest'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 26 | import {IXosNavigationRoute} from './app/core/services/navigation'; |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 27 | import XosLogDecorator from './decorators'; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 28 | import {xosExtender} from './app/extender/index'; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 29 | import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 30 | import {IXosModelDiscovererService} from './app/datasources/helpers/model-discoverer.service'; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 31 | import {xosServiceGraph} from './app/service-graph/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 33 | export interface IXosAppConfig { |
| 34 | apiEndpoint: string; |
| 35 | websocketClient: string; |
| 36 | } |
| 37 | |
| 38 | export interface IXosStyleConfig { |
| 39 | projectName: string; |
| 40 | payoff: string; |
| 41 | favicon: string; |
| 42 | background: string; |
| 43 | logo: string; |
| 44 | routes: IXosNavigationRoute[]; |
| 45 | } |
| 46 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 47 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 48 | .module('app', [ |
| 49 | xosCore, |
| 50 | xosDataSources, |
| 51 | xosViews, |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 52 | xosExtender, |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 53 | xosTemplate, // template module |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 54 | xosServiceGraph, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 55 | 'ui.router', |
| 56 | 'ngResource', |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 57 | 'ngProgress' |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 58 | ]) |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 59 | .config(XosLogDecorator) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 60 | .config(routesConfig) |
| 61 | .config(interceptorConfig) |
| 62 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 63 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 64 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 65 | .component('xos', main) |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 66 | .run(function($log: ng.ILogService, $rootScope: ng.IRootScopeService, $transitions: any, StyleConfig: IXosStyleConfig) { |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 67 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 68 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 69 | if (transtion.$to().name === 'login') { |
| 70 | $rootScope['class'] = 'blank'; |
| 71 | } |
| 72 | else { |
| 73 | $rootScope['class'] = ''; |
| 74 | } |
| 75 | }); |
| 76 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 77 | .run(( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 78 | $rootScope: ng.IRootScopeService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 79 | $transitions: any, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 80 | $log: ng.ILogService, |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 81 | $location: ng.ILocationService, |
| 82 | $state: ng.ui.IStateService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 83 | XosModelDiscoverer: IXosModelDiscovererService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 84 | AuthService: IXosAuthService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 85 | XosKeyboardShortcut: IXosKeyboardShortcutService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 86 | toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 87 | PageTitle: IXosPageTitleService |
| 88 | ) => { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 89 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 90 | // check the user login |
| 91 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 92 | if (!AuthService.isAuthenticated()) { |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 93 | AuthService.clearUser(); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 94 | $state.go('login'); |
| 95 | } |
| 96 | }); |
| 97 | |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 98 | // 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 111 | // save the last visited state before reload |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 112 | const lastRoute = $location.path(); |
| 113 | const lastQueryString = $location.search(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 114 | |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 115 | // if the user is authenticated |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 116 | $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 117 | if (AuthService.isAuthenticated()) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 118 | 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 Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 126 | // after setting up dynamic routes, redirect to previous state |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 127 | $location.path(lastRoute).search(lastQueryString); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 128 | }) |
| 129 | .finally(() => { |
Matteo Scandolo | 6ef59ea | 2017-01-26 08:07:05 -0800 | [diff] [blame] | 130 | $rootScope.$emit('xos.core.modelSetup'); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 131 | }); |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 132 | } |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 133 | else { |
| 134 | AuthService.clearUser(); |
| 135 | $state.go('login'); |
| 136 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 137 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 138 | // register keyboard shortcut |
| 139 | XosKeyboardShortcut.setup(); |
| 140 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 141 | 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 157 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 158 | |