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 | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 22 | import './loader.scss'; |
| 23 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 24 | class LoaderCtrl { |
| 25 | static $inject = [ |
| 26 | '$log', |
| 27 | '$rootScope', |
| 28 | '$location', |
| 29 | '$timeout', |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 30 | '$state', |
| 31 | 'AuthService', |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 32 | 'XosConfig', |
| 33 | 'XosModelDiscoverer', |
| 34 | `XosOnboarder` |
| 35 | ]; |
| 36 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 37 | public loader: boolean = true; |
| 38 | public error: string; |
| 39 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 40 | constructor ( |
| 41 | private $log: ng.ILogService, |
| 42 | private $rootScope: ng.IScope, |
| 43 | private $location: ng.ILocationService, |
| 44 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 45 | private $state: ng.ui.IStateService, |
| 46 | private XosAuthService: IXosAuthService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 47 | private XosConfig: any, |
| 48 | private XosModelDiscoverer: IXosModelDiscovererService, |
| 49 | private XosOnboarder: IXosOnboarder |
| 50 | ) { |
| 51 | |
| 52 | this.run(); |
| 53 | } |
| 54 | |
| 55 | public run() { |
| 56 | if (this.XosModelDiscoverer.areModelsLoaded()) { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 57 | this.$log.debug(`[XosLoader] Models are already loaded, moving to: ${this.XosConfig.lastVisitedUrl}`); |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 58 | this.moveOnTo(this.XosConfig.lastVisitedUrl); |
| 59 | } |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 60 | else if (!this.XosAuthService.isAuthenticated()) { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 61 | this.$log.debug(`[XosLoader] Not authenticated, send to login`); |
Matteo Scandolo | bd60dee | 2017-05-08 17:53:25 -0700 | [diff] [blame] | 62 | this.$state.go('xos.login'); |
| 63 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 64 | else { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 65 | // NOTE loading XOS Models |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 66 | this.XosModelDiscoverer.discover() |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 67 | .then((res) => { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 68 | this.$log.info('[XosLoader] res: ' + res, res, typeof res); |
| 69 | if (res === 'chameleon') { |
| 70 | this.loader = false; |
| 71 | this.error = 'chameleon'; |
| 72 | return 'chameleon'; |
| 73 | } |
| 74 | else if (res) { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 75 | this.$log.info('[XosLoader] All models loaded'); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 76 | // NOTE loading GUI Extensions |
| 77 | this.XosOnboarder.onboard(); |
| 78 | return true; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 79 | } |
| 80 | else { |
| 81 | this.$log.info('[XosLoader] Failed to load some models, moving on.'); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 82 | return true; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 83 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 84 | }) |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 85 | .then((res) => { |
| 86 | if (res === true) { |
| 87 | this.moveOnTo(this.XosConfig.lastVisitedUrl); |
| 88 | } |
| 89 | // NOTE otherwise stay here since we're printing some error messages |
| 90 | }) |
| 91 | .catch(() => { |
| 92 | // XosModelDiscoverer.discover reject only in case of authentication error |
| 93 | this.XosAuthService.clearUser(); |
| 94 | this.moveOnTo('/login'); |
| 95 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 96 | }) |
| 97 | .finally(() => { |
| 98 | // 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] | 99 | this.$timeout(() => { |
| 100 | this.$rootScope.$emit('xos.core.modelSetup'); |
| 101 | }, 500); |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public moveOnTo(url: string) { |
| 107 | this.$log.info(`[XosLoader] Redirecting to: ${url}`); |
| 108 | switch (url) { |
| 109 | case '': |
| 110 | case '/': |
| 111 | case '/loader': |
| 112 | case '/login': |
| 113 | this.$location.path('/dashboard'); |
| 114 | break; |
| 115 | default: |
| 116 | this.$timeout(() => { |
| 117 | this.$location.path(url); |
| 118 | }, 500); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | export const xosLoader: angular.IComponentOptions = { |
| 125 | template: ` |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 126 | <div ng-show="vm.loader" class="loader"></div> |
| 127 | <div class="row" ng-show="vm.error == 'chameleon'"> |
| 128 | <div class="col-sm-6 col-sm-offset-3"> |
| 129 | <div class="alert alert-danger"> |
| 130 | <div class="row"> |
| 131 | <div class="col-xs-2"> |
| 132 | <i class="fa fa-exclamation-triangle"></i> |
| 133 | </div> |
| 134 | <div class="col-xs-10"> |
| 135 | <strong>Cannot load models definition.</strong><br> |
| 136 | Please check that the Chameleon container is running |
| 137 | </div> |
| 138 | </div> |
| 139 | </div> |
| 140 | </div> |
| 141 | </div> |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 142 | `, |
| 143 | controllerAs: 'vm', |
| 144 | controller: LoaderCtrl |
| 145 | }; |