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 | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 49 | import {IXosDebugService} from './app/core/debug/debug.service'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 51 | export interface IXosAppConfig { |
| 52 | apiEndpoint: string; |
| 53 | websocketClient: string; |
| 54 | } |
| 55 | |
| 56 | export interface IXosStyleConfig { |
| 57 | projectName: string; |
| 58 | payoff: string; |
| 59 | favicon: string; |
| 60 | background: string; |
| 61 | logo: string; |
| 62 | routes: IXosNavigationRoute[]; |
| 63 | } |
| 64 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 65 | angular |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 66 | .module('app', [ |
| 67 | xosCore, |
| 68 | xosDataSources, |
| 69 | xosViews, |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 70 | xosExtender, |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 71 | xosTemplate, // template module |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 72 | xosServiceGraph, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 73 | 'ui.router', |
| 74 | 'ngResource', |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 75 | 'ngProgress' |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 76 | ]) |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 77 | .config(XosLogDecorator) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 78 | .config(routesConfig) |
| 79 | .config(interceptorConfig) |
| 80 | .factory('UserStatusInterceptor', userStatusInterceptor) |
| 81 | .factory('CredentialsInterceptor', CredentialsInterceptor) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 82 | .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor) |
| 83 | .component('xos', main) |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 84 | .provider('XosConfig', function(){ |
| 85 | // save the last visited state before reload |
| 86 | const lastVisitedUrl = window.location.hash.replace('#', ''); |
| 87 | this.$get = [() => { |
| 88 | return { |
| 89 | lastVisitedUrl |
| 90 | }; |
| 91 | }] ; |
| 92 | return this; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 93 | }) |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 94 | .run(( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 95 | $rootScope: ng.IRootScopeService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 96 | $transitions: any, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 97 | $log: ng.ILogService, |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 98 | $location: ng.ILocationService, |
| 99 | $state: ng.ui.IStateService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 100 | StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 101 | XosModelDiscoverer: IXosModelDiscovererService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 102 | AuthService: IXosAuthService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 103 | XosKeyboardShortcut: IXosKeyboardShortcutService, |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 104 | PageTitle: IXosPageTitleService, // NOTE this service is not used, but needs to be loaded somewhere |
| 105 | XosDebug: IXosDebugService |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 106 | ) => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 107 | // handle style configs |
| 108 | $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`; |
| 109 | if ($state.current.data && $state.current.data.specialClass) { |
| 110 | $rootScope['class'] = $state.current.data.specialClass; |
| 111 | } |
| 112 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
| 113 | if ($state.current.data && $state.current.data.specialClass) { |
| 114 | $rootScope['class'] = transtion.$to().data.specialClass; |
| 115 | } |
| 116 | else { |
| 117 | $rootScope['class'] = ''; |
| 118 | } |
| 119 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 120 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 121 | // check the user login (on route change) |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 122 | $transitions.onSuccess({ to: '**' }, (transtion) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 123 | if (!AuthService.isAuthenticated()) { |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 124 | AuthService.clearUser(); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 125 | $state.go('login'); |
| 126 | } |
| 127 | }); |
| 128 | |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 129 | // if the user is authenticated |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 130 | $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 131 | if (AuthService.isAuthenticated()) { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 132 | $state.go('loader'); |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 133 | } |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 134 | else { |
| 135 | AuthService.clearUser(); |
| 136 | $state.go('login'); |
| 137 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 138 | |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 139 | // register keyboard shortcut |
| 140 | XosKeyboardShortcut.setup(); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 141 | XosDebug.setupShortcuts(); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 142 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 143 | |