Catalog alignment
[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 isPluginCheckDone: boolean;
22
23     constructor(private eventBusService: EventBusService,
24                 private pluginsService: PluginsService,
25                 @Inject('$scope') private $scope: ng.IScope,
26                 @Inject('$state') private $state: ng.ui.IStateService) {
27         this.urlSearchParams = new URLSearchParams();
28         this.isPluginCheckDone = false;
29     }
30
31     ngOnInit(): void {
32         this.pluginsService.isPluginOnline(this.plugin.pluginId).subscribe(isPluginOnline => {
33             this.plugin.isOnline = isPluginOnline;
34             this.isPluginCheckDone = true;
35
36             if (this.plugin.isOnline) {
37                 this.initPlugin();
38             } else {
39                 this.onLoadingDone.emit();
40             }
41         });
42     }
43
44     private initPlugin() {
45         this.pluginUrl = this.plugin.pluginSourceUrl;
46         this.isClosed = false;
47
48         if (this.queryParams && !_.isEmpty(this.queryParams)) {
49             _.forOwn(this.queryParams, (value, key) => {
50                 this.urlSearchParams.set(key, value);
51             });
52
53             this.pluginUrl += '?';
54             this.pluginUrl += this.urlSearchParams.toString();
55         }
56
57         let readyEvent = (eventData) => {
58             if (eventData.originId === this.plugin.pluginId) {
59                 if (eventData.type == "READY") {
60                     this.onLoadingDone.emit();
61                     this.eventBusService.off(readyEvent)
62                 }
63             }
64         };
65
66         this.eventBusService.on(readyEvent);
67
68         // Listening to the stateChangeStart event in order to notify the plugin about it being closed
69         // before moving to a new state
70         this.$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
71             if ((fromState.name !== toState.name) || (fromState.name === toState.name) && (toParams.path !== fromParams.path)) {
72                 if (this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) {
73                     if (!this.isClosed) {
74                         event.preventDefault();
75
76                         this.eventBusService.notify("WINDOW_OUT").subscribe(() => {
77                             this.isClosed = true;
78                             this.eventBusService.unregister(this.plugin.pluginId);
79
80                             this.$state.go(toState.name, toParams);
81                         });
82                     }
83                 } else {
84                     this.eventBusService.unregister(this.plugin.pluginId);
85                 }
86             }
87         });
88     }
89 }