blob: b93aebf562c341fc7a56ec20bd860c359960a893 [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 Scandolo9b460042017-04-14 16:24:45 -070019import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
20import {IXosOnboarder} from '../../extender/services/onboard.service';
Matteo Scandolobd60dee2017-05-08 17:53:25 -070021import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo9b460042017-04-14 16:24:45 -070022class LoaderCtrl {
23 static $inject = [
24 '$log',
25 '$rootScope',
26 '$location',
27 '$timeout',
Matteo Scandolobd60dee2017-05-08 17:53:25 -070028 '$state',
29 'AuthService',
Matteo Scandolo9b460042017-04-14 16:24:45 -070030 'XosConfig',
31 'XosModelDiscoverer',
32 `XosOnboarder`
33 ];
34
Matteo Scandolo9b460042017-04-14 16:24:45 -070035 constructor (
36 private $log: ng.ILogService,
37 private $rootScope: ng.IScope,
38 private $location: ng.ILocationService,
39 private $timeout: ng.ITimeoutService,
Matteo Scandolobd60dee2017-05-08 17:53:25 -070040 private $state: ng.ui.IStateService,
41 private XosAuthService: IXosAuthService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070042 private XosConfig: any,
43 private XosModelDiscoverer: IXosModelDiscovererService,
44 private XosOnboarder: IXosOnboarder
45 ) {
46
47 this.run();
48 }
49
50 public run() {
51 if (this.XosModelDiscoverer.areModelsLoaded()) {
52 this.moveOnTo(this.XosConfig.lastVisitedUrl);
53 }
Matteo Scandolobd60dee2017-05-08 17:53:25 -070054 else if (!this.XosAuthService.isAuthenticated()) {
55 this.$state.go('xos.login');
56 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070057 else {
Matteo Scandolo9b460042017-04-14 16:24:45 -070058 // NOTE loading XOS Models
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070059 this.XosModelDiscoverer.discover()
Matteo Scandolo9b460042017-04-14 16:24:45 -070060 .then((res) => {
61 if (res) {
62 this.$log.info('[XosLoader] All models loaded');
63 }
64 else {
65 this.$log.info('[XosLoader] Failed to load some models, moving on.');
66 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070067 // NOTE loading GUI Extensions
Matteo Scandolo9b460042017-04-14 16:24:45 -070068 return this.XosOnboarder.onboard();
69 })
Matteo Scandolo9b460042017-04-14 16:24:45 -070070 .then(() => {
71 this.moveOnTo(this.XosConfig.lastVisitedUrl);
72 })
73 .finally(() => {
74 // NOTE it is in a timeout as the searchService is loaded after that
Matteo Scandolo9b460042017-04-14 16:24:45 -070075 this.$timeout(() => {
76 this.$rootScope.$emit('xos.core.modelSetup');
77 }, 500);
78 });
79 }
80 }
81
82 public moveOnTo(url: string) {
83 this.$log.info(`[XosLoader] Redirecting to: ${url}`);
84 switch (url) {
85 case '':
86 case '/':
87 case '/loader':
88 case '/login':
89 this.$location.path('/dashboard');
90 break;
91 default:
92 this.$timeout(() => {
93 this.$location.path(url);
94 }, 500);
95 break;
96 }
97 }
98}
99
100export const xosLoader: angular.IComponentOptions = {
101 template: `
102 <div class="loader"></div>
103 `,
104 controllerAs: 'vm',
105 controller: LoaderCtrl
106};