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