Developed disable navigation mechanism
[sdc.git] / catalog-ui / src / app / ng2 / components / ui / plugin / plugin-frame.component.ts
1 import {Component, Inject, Input, Output, OnInit, EventEmitter} 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         if (this.plugin.isOnline) {
29             this.initPlugin();
30         }
31     }
32
33     private initPlugin() {
34         this.pluginUrl = this.plugin.pluginSourceUrl;
35         this.isClosed = false;
36
37         if (this.queryParams && !_.isEmpty(this.queryParams)) {
38             _.forOwn(this.queryParams, (value, key) => {
39                 this.urlSearchParams.set(key, value);
40             });
41
42             this.pluginUrl += '?';
43             this.pluginUrl += this.urlSearchParams.toString();
44         }
45
46         let readyEvent = (eventData) => {
47             if (eventData.originId === this.plugin.pluginId) {
48                 if (eventData.type == "READY") {
49                     this.onLoadingDone.emit();
50                     this.eventBusService.off(readyEvent)
51                 }
52             }
53         };
54
55         this.eventBusService.on(readyEvent);
56
57         // Listening to the stateChangeStart event in order to notify the plugin about it being closed
58         // before moving to a new state
59         this.$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
60             if ((fromState.name !== toState.name) || (fromState.name === toState.name) && (toParams.path !== fromParams.path)) {
61                 if(this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) {
62                     if (!this.isClosed) {
63                         event.preventDefault();
64
65                         this.eventBusService.notify("WINDOW_OUT").subscribe(() => {
66                             this.isClosed = true;
67                             this.eventBusService.unregister(this.plugin.pluginId);
68
69                             this.$state.go(toState.name, toParams);
70                         });
71                     }
72                 }
73                 else {
74                     this.eventBusService.unregister(this.plugin.pluginId);
75                 }
76             }
77         });
78     }
79 }