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";
8 selector: 'plugin-frame',
9 templateUrl: './plugin-frame.component.html',
10 styleUrls: ['plugin-frame.component.less']
13 export class PluginFrameComponent implements OnInit {
15 @Input() plugin: Plugin;
16 @Input() queryParams: Object;
17 @Output() onLoadingDone: EventEmitter<void> = new EventEmitter<void>();
19 private urlSearchParams: URLSearchParams;
20 private isClosed: boolean;
21 private isReady: boolean;
22 private isPluginCheckDone: boolean;
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;
33 this.pluginsService.isPluginOnline(this.plugin.pluginId).subscribe(isPluginOnline => {
34 this.plugin.isOnline = isPluginOnline;
35 this.isPluginCheckDone = true;
37 if (this.plugin.isOnline) {
40 this.onLoadingDone.emit();
45 private initPlugin() {
46 this.pluginUrl = this.plugin.pluginSourceUrl;
47 this.isClosed = false;
50 if (this.queryParams && !_.isEmpty(this.queryParams)) {
51 _.forOwn(this.queryParams, (value, key) => {
52 this.urlSearchParams.set(key, value);
55 this.pluginUrl += '?';
56 this.pluginUrl += this.urlSearchParams.toString();
59 let readyEvent = (eventData) => {
60 if (eventData.originId === this.plugin.pluginId) {
61 if (eventData.type == "READY") {
63 this.onLoadingDone.emit();
64 this.eventBusService.off(readyEvent)
69 this.eventBusService.on(readyEvent);
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)) {
76 this.onLoadingDone.emit();
77 this.eventBusService.off(readyEvent)
79 if (this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) {
81 event.preventDefault();
82 this.eventBusService.notify("WINDOW_OUT").subscribe(() => {
84 this.eventBusService.unregister(this.plugin.pluginId);
86 this.$state.go(toState.name, toParams);
90 this.eventBusService.unregister(this.plugin.pluginId);