blob: 68920b09c36032aa09bb514c4152b823d9c39d67 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolof6acdbe2016-12-13 10:29:37 -080019/// <reference path="../typings/index.d.ts" />
Matteo Scandolod819c922016-12-02 14:06:14 -080020
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080021import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -080022
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080023import 'angular-ui-router';
24import 'angular-resource';
25import 'angular-cookies';
Matteo Scandolo042ea632017-03-01 19:02:34 -080026import '../node_modules/ngprogress/build/ngProgress';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080027import routesConfig from './routes';
28
29import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080030
31import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080032import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080033import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080034import {xosViews} from './app/views/index';
Matteo Scandolo266907e2016-12-20 13:41:42 -080035import {xosTemplate} from './app/template/index';
36
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080037import {
38 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
39 NoHyperlinksInterceptor
40} from './interceptors';
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080041import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolo04964232017-01-07 12:53:46 -080042import {IXosAuthService} from './app/datasources/rest/auth.rest';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080043import {IXosNavigationRoute} from './app/core/services/navigation';
Matteo Scandolo17bf8242017-01-23 17:30:39 -080044import XosLogDecorator from './decorators';
Matteo Scandolo4e870232017-01-30 13:43:05 -080045import {xosExtender} from './app/extender/index';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080046import {IXosKeyboardShortcutService} from './app/core/services/keyboard-shortcut';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080047import {IXosModelDiscovererService} from './app/datasources/helpers/model-discoverer.service';
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080048import {xosServiceGraph} from './app/service-graph/index';
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070049import {IXosDebugService} from './app/core/debug/debug.service';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050
Matteo Scandolo828d1e82017-01-17 14:49:38 -080051export interface IXosAppConfig {
52 apiEndpoint: string;
53 websocketClient: string;
54}
55
56export interface IXosStyleConfig {
57 projectName: string;
58 payoff: string;
59 favicon: string;
60 background: string;
61 logo: string;
62 routes: IXosNavigationRoute[];
63}
64
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080065angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080066 .module('app', [
67 xosCore,
68 xosDataSources,
69 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080070 xosExtender,
Matteo Scandolo042ea632017-03-01 19:02:34 -080071 xosTemplate, // template module
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080072 xosServiceGraph,
Matteo Scandolo266907e2016-12-20 13:41:42 -080073 'ui.router',
74 'ngResource',
Matteo Scandolo042ea632017-03-01 19:02:34 -080075 'ngProgress'
Matteo Scandolo266907e2016-12-20 13:41:42 -080076 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080077 .config(XosLogDecorator)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080078 .config(routesConfig)
79 .config(interceptorConfig)
80 .factory('UserStatusInterceptor', userStatusInterceptor)
81 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080082 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
83 .component('xos', main)
Matteo Scandolo9b460042017-04-14 16:24:45 -070084 .provider('XosConfig', function(){
85 // save the last visited state before reload
Matteo Scandoloafd05d72017-10-11 17:09:27 -070086 let lastVisitedUrl = window.location.hash.replace('#', '');
Matteo Scandolo9b460042017-04-14 16:24:45 -070087 this.$get = [() => {
Matteo Scandoloafd05d72017-10-11 17:09:27 -070088 if (lastVisitedUrl === '/login' || lastVisitedUrl === '/loader') {
89 lastVisitedUrl = '/dashboard';
90 }
91 return {lastVisitedUrl};
Matteo Scandolo9b460042017-04-14 16:24:45 -070092 }] ;
93 return this;
Matteo Scandolo266907e2016-12-20 13:41:42 -080094 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080095 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080096 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080097 $transitions: any,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080098 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080099 $location: ng.ILocationService,
100 $state: ng.ui.IStateService,
Matteo Scandolo9b460042017-04-14 16:24:45 -0700101 StyleConfig: IXosStyleConfig,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800102 XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo04964232017-01-07 12:53:46 -0800103 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800104 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandoloc3804aa2017-08-09 16:00:43 -0700105 PageTitle: IXosPageTitleService, // NOTE this service is not used, but needs to be loaded somewhere
106 XosDebug: IXosDebugService
Matteo Scandolo5655bdc2016-12-16 08:32:15 -0800107 ) => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700108 // handle style configs
109 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
110 if ($state.current.data && $state.current.data.specialClass) {
111 $rootScope['class'] = $state.current.data.specialClass;
112 }
113 $transitions.onSuccess({ to: '**' }, (transtion) => {
114 if ($state.current.data && $state.current.data.specialClass) {
115 $rootScope['class'] = transtion.$to().data.specialClass;
116 }
117 else {
118 $rootScope['class'] = '';
119 }
120 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800121
Matteo Scandolo9b460042017-04-14 16:24:45 -0700122 // check the user login (on route change)
Matteo Scandolo04964232017-01-07 12:53:46 -0800123 $transitions.onSuccess({ to: '**' }, (transtion) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800124 if (!AuthService.isAuthenticated()) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800125 AuthService.clearUser();
Matteo Scandolo04964232017-01-07 12:53:46 -0800126 $state.go('login');
127 }
128 });
129
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800130 // if the user is authenticated
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800131 $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800132 if (AuthService.isAuthenticated()) {
Matteo Scandoloafd05d72017-10-11 17:09:27 -0700133 $log.info(`[XOS] Redirect to "loader"`);
Matteo Scandolo9b460042017-04-14 16:24:45 -0700134 $state.go('loader');
Matteo Scandoloafd05d72017-10-11 17:09:27 -0700135 $rootScope.$apply();
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800136 }
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800137 else {
138 AuthService.clearUser();
Matteo Scandoloafd05d72017-10-11 17:09:27 -0700139 $log.info(`[XOS] Redirect to "login"`);
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800140 $state.go('login');
Matteo Scandoloafd05d72017-10-11 17:09:27 -0700141 $rootScope.$apply();
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800142 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800143
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800144 // register keyboard shortcut
145 XosKeyboardShortcut.setup();
Matteo Scandoloc3804aa2017-08-09 16:00:43 -0700146 XosDebug.setupShortcuts();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800147 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800148