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 | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 93 | $state.go('login'); |
| 94 | } |
| 95 | }); |
| 96 | |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 97 | // 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 110 | // save the last visited state before reload |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 111 | const lastRoute = $location.path(); |
| 112 | const lastQueryString = $location.search(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 114 | // if the user is authenticated |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 115 | 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 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 | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 133 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 134 | // register keyboard shortcut |
| 135 | XosKeyboardShortcut.setup(); |
| 136 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 137 | 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 153 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 154 | |