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 | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 19 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
| 20 | import {IXosOnboarder} from '../../extender/services/onboard.service'; |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 21 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 22 | class LoaderCtrl { |
| 23 | static $inject = [ |
| 24 | '$log', |
| 25 | '$rootScope', |
| 26 | '$location', |
| 27 | '$timeout', |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 28 | '$state', |
| 29 | 'AuthService', |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 30 | 'XosConfig', |
| 31 | 'XosModelDiscoverer', |
| 32 | `XosOnboarder` |
| 33 | ]; |
| 34 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 35 | constructor ( |
| 36 | private $log: ng.ILogService, |
| 37 | private $rootScope: ng.IScope, |
| 38 | private $location: ng.ILocationService, |
| 39 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 40 | private $state: ng.ui.IStateService, |
| 41 | private XosAuthService: IXosAuthService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 42 | 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 Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 54 | else if (!this.XosAuthService.isAuthenticated()) { |
| 55 | this.$state.go('xos.login'); |
| 56 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 57 | else { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 58 | // NOTE loading XOS Models |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 59 | this.XosModelDiscoverer.discover() |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 60 | .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 Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 67 | // NOTE loading GUI Extensions |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 68 | return this.XosOnboarder.onboard(); |
| 69 | }) |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 70 | .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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 75 | 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 | |
| 100 | export const xosLoader: angular.IComponentOptions = { |
| 101 | template: ` |
| 102 | <div class="loader"></div> |
| 103 | `, |
| 104 | controllerAs: 'vm', |
| 105 | controller: LoaderCtrl |
| 106 | }; |