blob: 937b482a2d0f453a80f516e037d700d9b9c4b4ab [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 Scandolof2c3ed62016-12-15 14:32:50 -080049
Matteo Scandolo828d1e82017-01-17 14:49:38 -080050export interface IXosAppConfig {
51 apiEndpoint: string;
52 websocketClient: string;
53}
54
55export interface IXosStyleConfig {
56 projectName: string;
57 payoff: string;
58 favicon: string;
59 background: string;
60 logo: string;
61 routes: IXosNavigationRoute[];
62}
63
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080064angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080065 .module('app', [
66 xosCore,
67 xosDataSources,
68 xosViews,
Matteo Scandolo4e870232017-01-30 13:43:05 -080069 xosExtender,
Matteo Scandolo042ea632017-03-01 19:02:34 -080070 xosTemplate, // template module
Matteo Scandoloa62adbc2017-03-02 15:37:34 -080071 xosServiceGraph,
Matteo Scandolo266907e2016-12-20 13:41:42 -080072 'ui.router',
73 'ngResource',
Matteo Scandolo042ea632017-03-01 19:02:34 -080074 'ngProgress'
Matteo Scandolo266907e2016-12-20 13:41:42 -080075 ])
Matteo Scandolo17bf8242017-01-23 17:30:39 -080076 .config(XosLogDecorator)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080077 .config(routesConfig)
78 .config(interceptorConfig)
79 .factory('UserStatusInterceptor', userStatusInterceptor)
80 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080081 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
82 .component('xos', main)
Matteo Scandolo9b460042017-04-14 16:24:45 -070083 .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 Scandolo266907e2016-12-20 13:41:42 -080092 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080093 .run((
Matteo Scandolo17bf8242017-01-23 17:30:39 -080094 $rootScope: ng.IRootScopeService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080095 $transitions: any,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080096 $log: ng.ILogService,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080097 $location: ng.ILocationService,
98 $state: ng.ui.IStateService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070099 StyleConfig: IXosStyleConfig,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800100 XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo04964232017-01-07 12:53:46 -0800101 AuthService: IXosAuthService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800102 XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolo9b460042017-04-14 16:24:45 -0700103 PageTitle: IXosPageTitleService // NOTE this service is not used, but needs to be loaded somewhere
Matteo Scandolo5655bdc2016-12-16 08:32:15 -0800104 ) => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700105 // 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 Scandoloe0d71ea2016-12-19 11:56:12 -0800118
Matteo Scandolo9b460042017-04-14 16:24:45 -0700119 // check the user login (on route change)
Matteo Scandolo04964232017-01-07 12:53:46 -0800120 $transitions.onSuccess({ to: '**' }, (transtion) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800121 if (!AuthService.isAuthenticated()) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800122 AuthService.clearUser();
Matteo Scandolo04964232017-01-07 12:53:46 -0800123 $state.go('login');
124 }
125 });
126
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800127 // if the user is authenticated
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800128 $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800129 if (AuthService.isAuthenticated()) {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700130 $state.go('loader');
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800131 }
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800132 else {
133 AuthService.clearUser();
134 $state.go('login');
135 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800136
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800137 // register keyboard shortcut
138 XosKeyboardShortcut.setup();
139
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800140 XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700141 key: 'D',
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800142 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 Scandolof2c3ed62016-12-15 14:32:50 -0800155 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800156