Add events hub notify calls
[sdc.git] / catalog-ui / src / app / ng2 / components / ui / plugin / plugin-frame.component.ts
1 import {Component, Inject, Input, Output, OnInit, EventEmitter, ViewChild, ElementRef} from "@angular/core";
2 import {URLSearchParams} from '@angular/http';
3 import {Plugin} from "app/models";
4 import {EventBusService} from "../../../services/event-bus.service";
5
6 @Component({
7     selector: 'plugin-frame',
8     templateUrl: './plugin-frame.component.html',
9     styleUrls: ['plugin-frame.component.less']
10 })
11
12 export class PluginFrameComponent implements OnInit {
13
14     @Input() plugin: Plugin;
15     @Input() queryParams: Object;
16     @Output() onLoadingDone: EventEmitter<void> = new EventEmitter<void>();
17     pluginUrl: string;
18     private urlSearchParams: URLSearchParams;
19     private isClosed: boolean;
20
21     constructor(private eventBusService: EventBusService,
22                 @Inject('$scope') private $scope: ng.IScope,
23                 @Inject('$state') private $state: ng.ui.IStateService) {
24         this.urlSearchParams = new URLSearchParams();
25     }
26
27     ngOnInit(): void {
28         this.pluginUrl = this.plugin.pluginSourceUrl;
29         this.isClosed = false;
30
31         if (this.queryParams && !_.isEmpty(this.queryParams)) {
32             _.forOwn(this.queryParams, (value, key) => {
33                 this.urlSearchParams.set(key, value);
34             });
35
36             this.pluginUrl += '?';
37             this.pluginUrl += this.urlSearchParams.toString();
38         }
39
40         this.eventBusService.on((eventData) => {
41             if (eventData.originId === this.plugin.pluginId) {
42                 if (eventData.type == "READY") {
43                     this.onLoadingDone.emit();
44                 }
45             }
46         });
47
48         // Listening to the stateChangeStart event in order to notify the plugin about it being closed
49         // before moving to a new state
50         this.$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
51             if (fromState.name !== toState.name) {
52                 if (!this.isClosed) {
53                     event.preventDefault();
54
55                     this.eventBusService.notify("WINDOW_OUT");
56
57                     this.isClosed = true;
58
59                     setTimeout(() => {
60                         this.$state.go(toState.name, toParams);
61                     });
62                 }
63             }
64         });
65     }
66 }