Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 1 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
| 2 | import {IXosOnboarder} from '../../extender/services/onboard.service'; |
Matteo Scandolo | 3ca7f1b | 2017-05-08 17:53:25 -0700 | [diff] [blame^] | 3 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 4 | class LoaderCtrl { |
| 5 | static $inject = [ |
| 6 | '$log', |
| 7 | '$rootScope', |
| 8 | '$location', |
| 9 | '$timeout', |
Matteo Scandolo | 3ca7f1b | 2017-05-08 17:53:25 -0700 | [diff] [blame^] | 10 | '$state', |
| 11 | 'AuthService', |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 12 | 'XosConfig', |
| 13 | 'XosModelDiscoverer', |
| 14 | `XosOnboarder` |
| 15 | ]; |
| 16 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 17 | constructor ( |
| 18 | private $log: ng.ILogService, |
| 19 | private $rootScope: ng.IScope, |
| 20 | private $location: ng.ILocationService, |
| 21 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | 3ca7f1b | 2017-05-08 17:53:25 -0700 | [diff] [blame^] | 22 | private $state: ng.ui.IStateService, |
| 23 | private XosAuthService: IXosAuthService, |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 24 | private XosConfig: any, |
| 25 | private XosModelDiscoverer: IXosModelDiscovererService, |
| 26 | private XosOnboarder: IXosOnboarder |
| 27 | ) { |
| 28 | |
| 29 | this.run(); |
| 30 | } |
| 31 | |
| 32 | public run() { |
| 33 | if (this.XosModelDiscoverer.areModelsLoaded()) { |
| 34 | this.moveOnTo(this.XosConfig.lastVisitedUrl); |
| 35 | } |
Matteo Scandolo | 3ca7f1b | 2017-05-08 17:53:25 -0700 | [diff] [blame^] | 36 | else if (!this.XosAuthService.isAuthenticated()) { |
| 37 | this.$state.go('xos.login'); |
| 38 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 39 | else { |
| 40 | this.XosModelDiscoverer.discover() |
| 41 | // NOTE loading XOS Models |
| 42 | .then((res) => { |
| 43 | if (res) { |
| 44 | this.$log.info('[XosLoader] All models loaded'); |
| 45 | } |
| 46 | else { |
| 47 | this.$log.info('[XosLoader] Failed to load some models, moving on.'); |
| 48 | } |
| 49 | return this.XosOnboarder.onboard(); |
| 50 | }) |
| 51 | // NOTE loading GUI Extensions |
| 52 | .then(() => { |
| 53 | this.moveOnTo(this.XosConfig.lastVisitedUrl); |
| 54 | }) |
| 55 | .finally(() => { |
| 56 | // 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] | 57 | this.$timeout(() => { |
| 58 | this.$rootScope.$emit('xos.core.modelSetup'); |
| 59 | }, 500); |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public moveOnTo(url: string) { |
| 65 | this.$log.info(`[XosLoader] Redirecting to: ${url}`); |
| 66 | switch (url) { |
| 67 | case '': |
| 68 | case '/': |
| 69 | case '/loader': |
| 70 | case '/login': |
| 71 | this.$location.path('/dashboard'); |
| 72 | break; |
| 73 | default: |
| 74 | this.$timeout(() => { |
| 75 | this.$location.path(url); |
| 76 | }, 500); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export const xosLoader: angular.IComponentOptions = { |
| 83 | template: ` |
| 84 | <div class="loader"></div> |
| 85 | `, |
| 86 | controllerAs: 'vm', |
| 87 | controller: LoaderCtrl |
| 88 | }; |