blob: 2a287d56660843457792a5fbad959664d3477289 [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
86 const lastVisitedUrl = window.location.hash.replace('#', '');
87 this.$get = [() => {
88 return {
89 lastVisitedUrl
90 };
91 }] ;
92 return this;
Matteo Scandolo266907e2016-12-20 13:41:42 -080093 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080094 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080095 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080096 $transitions: any,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080097 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080098 $location: ng.ILocationService,
99 $state: ng.ui.IStateService,
Matteo Scandolo9b460042017-04-14 16:24:45 -0700100 StyleConfig: IXosStyleConfig,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800101 XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo04964232017-01-07 12:53:46 -0800102 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800103 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandoloc3804aa2017-08-09 16:00:43 -0700104 PageTitle: IXosPageTitleService, // NOTE this service is not used, but needs to be loaded somewhere
105 XosDebug: IXosDebugService
Matteo Scandolo5655bdc2016-12-16 08:32:15 -0800106 ) => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700107 // 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 Scandoloe0d71ea2016-12-19 11:56:12 -0800120
Matteo Scandolo9b460042017-04-14 16:24:45 -0700121 // check the user login (on route change)
Matteo Scandolo04964232017-01-07 12:53:46 -0800122 $transitions.onSuccess({ to: '**' }, (transtion) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800123 if (!AuthService.isAuthenticated()) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800124 AuthService.clearUser();
Matteo Scandolo04964232017-01-07 12:53:46 -0800125 $state.go('login');
126 }
127 });
128
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800129 // if the user is authenticated
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800130 $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800131 if (AuthService.isAuthenticated()) {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700132 $state.go('loader');
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800133 }
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800134 else {
135 AuthService.clearUser();
136 $state.go('login');
137 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800138
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800139 // register keyboard shortcut
140 XosKeyboardShortcut.setup();
Matteo Scandoloc3804aa2017-08-09 16:00:43 -0700141 XosDebug.setupShortcuts();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800142 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800143