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 isPluginCheckDone: boolean;
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;
32 this.pluginsService.isPluginOnline(this.plugin.pluginId).subscribe(isPluginOnline => {
33 this.plugin.isOnline = isPluginOnline;
34 this.isPluginCheckDone = true;
36 if (this.plugin.isOnline) {
39 this.onLoadingDone.emit();
44 private initPlugin() {
45 this.pluginUrl = this.plugin.pluginSourceUrl;
46 this.isClosed = false;
48 if (this.queryParams && !_.isEmpty(this.queryParams)) {
49 _.forOwn(this.queryParams, (value, key) => {
50 this.urlSearchParams.set(key, value);
53 this.pluginUrl += '?';
54 this.pluginUrl += this.urlSearchParams.toString();
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)
66 this.eventBusService.on(readyEvent);
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) {
74 event.preventDefault();
76 this.eventBusService.notify("WINDOW_OUT").subscribe(() => {
78 this.eventBusService.unregister(this.plugin.pluginId);
80 this.$state.go(toState.name, toParams);
84 this.eventBusService.unregister(this.plugin.pluginId);