blob: f12b17bc5214d9a86aadfc00ffc59b334d0c9bf6 [file] [log] [blame]
Matteo Scandolo9b460042017-04-14 16:24:45 -07001import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
2import {IXosOnboarder} from '../../extender/services/onboard.service';
Matteo Scandolo3ca7f1b2017-05-08 17:53:25 -07003import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo9b460042017-04-14 16:24:45 -07004class LoaderCtrl {
5 static $inject = [
6 '$log',
7 '$rootScope',
8 '$location',
9 '$timeout',
Matteo Scandolo3ca7f1b2017-05-08 17:53:25 -070010 '$state',
11 'AuthService',
Matteo Scandolo9b460042017-04-14 16:24:45 -070012 'XosConfig',
13 'XosModelDiscoverer',
14 `XosOnboarder`
15 ];
16
Matteo Scandolo9b460042017-04-14 16:24:45 -070017 constructor (
18 private $log: ng.ILogService,
19 private $rootScope: ng.IScope,
20 private $location: ng.ILocationService,
21 private $timeout: ng.ITimeoutService,
Matteo Scandolo3ca7f1b2017-05-08 17:53:25 -070022 private $state: ng.ui.IStateService,
23 private XosAuthService: IXosAuthService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070024 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 Scandolo3ca7f1b2017-05-08 17:53:25 -070036 else if (!this.XosAuthService.isAuthenticated()) {
37 this.$state.go('xos.login');
38 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070039 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 Scandolo9b460042017-04-14 16:24:45 -070057 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
82export const xosLoader: angular.IComponentOptions = {
83 template: `
84 <div class="loader"></div>
85 `,
86 controllerAs: 'vm',
87 controller: LoaderCtrl
88};