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