Fix loader for unready plugins
[sdc.git] / catalog-ui / src / app / ng2 / components / ui / plugin / plugin-frame.component.ts
1 import {Component, EventEmitter, Inject, Input, OnInit, Output} from "@angular/core";
2 import {URLSearchParams} from '@angular/http';
3 import {Plugin} from "app/models";
4 import {EventBusService} from "../../../services/event-bus.service";
5 import {PluginsService} from "../../../services/plugins.service";
6
7 @Component({
8     selector: 'plugin-frame',
9     templateUrl: './plugin-frame.component.html',
10     styleUrls: ['plugin-frame.component.less']
11 })
12
13 export class PluginFrameComponent implements OnInit {
14
15     @Input() plugin: Plugin;
16     @Input() queryParams: Object;
17     @Output() onLoadingDone: EventEmitter<void> = new EventEmitter<void>();
18     pluginUrl: string;
19     private urlSearchParams: URLSearchParams;
20     private isClosed: boolean;
21     private isReady: boolean;
22     private isPluginCheckDone: boolean;
23
24     constructor(private eventBusService: EventBusService,
25                 private pluginsService: PluginsService,
26                 @Inject('$scope') private $scope: ng.IScope,
27                 @Inject('$state') private $state: ng.ui.IStateService) {
28         this.urlSearchParams = new URLSearchParams();
29         this.isPluginCheckDone = false;
30     }
31
32     ngOnInit(): void {
33         this.pluginsService.isPluginOnline(this.plugin.pluginId).subscribe(isPluginOnline => {
34             this.plugin.isOnline = isPluginOnline;
35             this.isPluginCheckDone = true;
36
37             if (this.plugin.isOnline) {
38                 this.initPlugin();
39             } else {
40                 this.onLoadingDone.emit();
41             }
42         });
43     }
44
45     private initPlugin() {
46         this.pluginUrl = this.plugin.pluginSourceUrl;
47         this.isClosed = false;
48         this.isReady = false;
49
50         if (this.queryParams && !_.isEmpty(this.queryParams)) {
51             _.forOwn(this.queryParams, (value, key) => {
52                 this.urlSearchParams.set(key, value);
53             });
54
55             this.pluginUrl += '?';
56             this.pluginUrl += this.urlSearchParams.toString();
57         }
58
59         let readyEvent = (eventData) => {
60             if (eventData.originId === this.plugin.pluginId) {
61                 if (eventData.type == "READY") {
62                     this.isReady = true;
63                     this.onLoadingDone.emit();
64                     this.eventBusService.off(readyEvent)
65                 }
66             }
67         };
68
69         this.eventBusService.on(readyEvent);
70
71         // Listening to the stateChangeStart event in order to notify the plugin about it being closed
72         // before moving to a new state
73         this.$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
74             if ((fromState.name !== toState.name) || (fromState.name === toState.name) && (toParams.path !== fromParams.path)) {
75                 if (!this.isReady) {
76                     this.onLoadingDone.emit();
77                     this.eventBusService.off(readyEvent)
78                 }
79                 if (this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) {
80                     if (!this.isClosed) {
81                         event.preventDefault();
82                         this.eventBusService.notify("WINDOW_OUT").subscribe(() => {
83                             this.isClosed = true;
84                             this.eventBusService.unregister(this.plugin.pluginId);
85
86                             this.$state.go(toState.name, toParams);
87                         });
88                     }
89                 } else {
90                     this.eventBusService.unregister(this.plugin.pluginId);
91                 }
92             }
93         });
94     }
95 }