Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | /// <reference path="../typings/index.d.ts" /> |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 20 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 21 | import * as angular from 'angular'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 23 | import 'angular-ui-router'; |
| 24 | import 'angular-resource'; |
| 25 | import 'angular-cookies'; |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 26 | import '../node_modules/ngprogress/build/ngProgress'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 27 | import routesConfig from './routes'; |
| 28 | |
| 29 | import {main} from './app/main'; |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 30 | |
| 31 | import './index.scss'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 32 | import {xosCore} from './app/core/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 33 | import {xosDataSources} from './app/datasources/index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 34 | import {xosViews} from './app/views/index'; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 35 | import {xosTemplate} from './app/template/index'; |
| 36 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 37 | import { |
| 38 | interceptorConfig, userStatusInterceptor, CredentialsInterceptor, |
| 39 | NoHyperlinksInterceptor |
| 40 | } from './interceptors'; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 41 | import {IXosPageTitleService} from './app/core/services/page-title'; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 42 | import {IXosAuthService} from './app/datasources/rest/auth.rest'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 43 | import {IXosNavigationRoute} from './app/core/services/navigation'; |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 44 | import XosLogDecorator from './decorators'; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 45 | import {xosExtender} from './app/extender/index'; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 46 | import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 47 | import {IXosModelDiscovererService} from './app/datasources/helpers/model-discoverer.service'; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 48 | import {xosServiceGraph} from './app/service-graph/index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 49 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 50 | export interface IXosAppConfig { |
| 51 | apiEndpoint: string; |
| 52 | websocketClient: string; |
| 53 | } |
| 54 | |
| 55 | export interface IXosStyleConfig { |
| 56 | projectName: string; |
| 57 | payoff: string; |
| 58 | favicon: string; |
| 59 | background: string; |
| 60 | logo: string; |
| 61 | routes: IXosNavigationRoute[]; |
| 62 | } |
| 63 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 64 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 65 | .module('app', [ |
| 66 | xosCore, |
| 67 | xosDataSources, |
| 68 | xosViews, |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 69 | xosExtender, |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 70 | xosTemplate, // template module |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 71 | xosServiceGraph, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 72 | 'ui.router', |
| 73 | 'ngResource', |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 74 | 'ngProgress' |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 75 | ]) |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 76 | .config(XosLogDecorator) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 77 | .config(routesConfig) |
| 78 | .config(interceptorConfig) |
| 79 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 80 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 81 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 82 | .component('xos', main) |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 83 | .provider('XosConfig', function(){ |
| 84 | // save the last visited state before reload |
| 85 | const lastVisitedUrl = window.location.hash.replace('#', ''); |
| 86 | this.$get = [() => { |
| 87 | return { |
| 88 | lastVisitedUrl |
| 89 | }; |
| 90 | }] ; |
| 91 | return this; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 92 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 93 | .run(( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 94 | $rootScope: ng.IRootScopeService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 95 | $transitions: any, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 96 | $log: ng.ILogService, |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 97 | $location: ng.ILocationService, |
| 98 | $state: ng.ui.IStateService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 99 | StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 100 | XosModelDiscoverer: IXosModelDiscovererService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 101 | AuthService: IXosAuthService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 102 | XosKeyboardShortcut: IXosKeyboardShortcutService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 103 | PageTitle: IXosPageTitleService // NOTE this service is not used, but needs to be loaded somewhere |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 104 | ) => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 105 | // handle style configs |
| 106 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
| 107 | if ($state.current.data && $state.current.data.specialClass) { |
| 108 | $rootScope['class'] = $state.current.data.specialClass; |
| 109 | } |
| 110 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 111 | if ($state.current.data && $state.current.data.specialClass) { |
| 112 | $rootScope['class'] = transtion.$to().data.specialClass; |
| 113 | } |
| 114 | else { |
| 115 | $rootScope['class'] = ''; |
| 116 | } |
| 117 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 118 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 119 | // check the user login (on route change) |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 120 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 121 | if (!AuthService.isAuthenticated()) { |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 122 | AuthService.clearUser(); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 123 | $state.go('login'); |
| 124 | } |
| 125 | }); |
| 126 | |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 127 | // if the user is authenticated |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 128 | $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 129 | if (AuthService.isAuthenticated()) { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 130 | $state.go('loader'); |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 131 | } |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 132 | else { |
| 133 | AuthService.clearUser(); |
| 134 | $state.go('login'); |
| 135 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 136 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 137 | // register keyboard shortcut |
| 138 | XosKeyboardShortcut.setup(); |
| 139 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 140 | XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 141 | key: 'D', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 142 | cb: () => { |
| 143 | if (window.localStorage.getItem('debug') === 'true') { |
| 144 | $log.info(`[XosKeyboardShortcut] Disabling debug`); |
| 145 | window.localStorage.setItem('debug', 'false'); |
| 146 | } |
| 147 | else { |
| 148 | window.localStorage.setItem('debug', 'true'); |
| 149 | $log.info(`[XosKeyboardShortcut] Enabling debug`); |
| 150 | } |
| 151 | }, |
| 152 | description: 'Toggle debug messages in browser console' |
| 153 | }, 'global'); |
| 154 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 155 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 156 | |