From fe65ddd4b7e68a683d4e027f136ac85c8d0b29f4 Mon Sep 17 00:00:00 2001 From: Idan Amit Date: Tue, 8 Jan 2019 16:41:23 +0200 Subject: minor fixes to sdc-pubsub Aligned sdc code to the fixes that were made in the sdc-pubsub library Change-Id: I54e48e55915dadd3fdb53c0290e013708161aa46 Issue-ID: SDC-2032 Signed-off-by: Idan Amit --- catalog-ui/package.json | 1 + catalog-ui/src/app/models/base-pubsub.ts | 127 --------------------- catalog-ui/src/app/models/plugin-pubsub.ts | 29 ----- .../components/ui/plugin/plugin-frame.component.ts | 9 +- .../src/app/ng2/services/event-bus.service.ts | 72 ++++++------ .../view-models/plugins/plugins-tab-view-model.ts | 17 ++- .../tabs/plugins/plugins-context-view-model.ts | 19 ++- 7 files changed, 56 insertions(+), 218 deletions(-) delete mode 100644 catalog-ui/src/app/models/base-pubsub.ts delete mode 100644 catalog-ui/src/app/models/plugin-pubsub.ts (limited to 'catalog-ui') diff --git a/catalog-ui/package.json b/catalog-ui/package.json index d32506ee3a..1bd49284c8 100644 --- a/catalog-ui/package.json +++ b/catalog-ui/package.json @@ -113,6 +113,7 @@ "restangular": "1.6.1", "rxjs": "5.4.2", "sdc-angular-dragdrop": "1.0.14", + "sdc-pubsub": "1.0.30", "typescript": "2.4.2", "typings": "2.1.0", "underscore": "1.8.3", diff --git a/catalog-ui/src/app/models/base-pubsub.ts b/catalog-ui/src/app/models/base-pubsub.ts deleted file mode 100644 index 41e8039da5..0000000000 --- a/catalog-ui/src/app/models/base-pubsub.ts +++ /dev/null @@ -1,127 +0,0 @@ -declare const window: Window; - -export class BasePubSub { - - subscribers: Map; - eventsCallbacks: Array; - clientId: string; - eventsToWait: Map>; - lastEventNotified: string; - - constructor(pluginId: string) { - this.subscribers = new Map(); - this.eventsCallbacks = []; - this.eventsToWait = new Map>(); - this.clientId = pluginId; - this.lastEventNotified = ""; - this.onMessage = this.onMessage.bind(this); - - window.addEventListener("message", this.onMessage); - } - - public register(subscriberId: string, subscriberWindow: Window, subscriberUrl: string) { - const subscriber = { - window: subscriberWindow, - locationUrl: subscriberUrl || subscriberWindow.location.href - } as ISubscriber; - - this.subscribers.set(subscriberId, subscriber); - } - - public unregister(subscriberId: string) { - this.subscribers.delete(subscriberId); - } - - public on(callback: Function) { - let functionExists = this.eventsCallbacks.find((func: Function) => { - return callback.toString() == func.toString() - }); - - if (!functionExists) { - this.eventsCallbacks.push(callback); - } - } - - public off(callback: Function) { - let index = this.eventsCallbacks.indexOf(callback); - this.eventsCallbacks.splice(index, 1) - } - - public notify(eventType:string, eventData?:any) { - let eventObj = { - type: eventType, - data: eventData, - originId: this.clientId - } as IPubSubEvent; - - this.subscribers.forEach( (subscriber: ISubscriber, subscriberId: string) => { - subscriber.window.postMessage(eventObj, subscriber.locationUrl); - }); - - this.lastEventNotified = eventType; - - return { - subscribe: function(callbackFn) { - - if(this.subscribers.size !== 0) { - let subscribersToNotify = Array.from(this.subscribers.keys()); - - const checkNotifyComplete = (subscriberId: string) => { - - let index = subscribersToNotify.indexOf(subscriberId); - subscribersToNotify.splice(index, 1); - - if (subscribersToNotify.length === 0) { - callbackFn(); - } - }; - - this.subscribers.forEach((subscriber: ISubscriber, subscriberId: string) => { - if (this.eventsToWait.has(subscriberId) && this.eventsToWait.get(subscriberId).indexOf(eventType) !== -1) { - - const actionCompletedFunction = (eventData, subId = subscriberId) => { - if (eventData.type == "ACTION_COMPLETED") { - checkNotifyComplete(subId); - } - this.off(actionCompletedFunction); - - }; - this.on(actionCompletedFunction); - } - else { - checkNotifyComplete(subscriberId); - } - }); - } - else { - callbackFn(); - } - }.bind(this) - } - } - - public isWaitingForEvent(eventName: string) : boolean { - return Array.from(this.eventsToWait.values()).some((eventsList: Array) => - eventsList.indexOf(eventName) !== -1 - ); - } - - protected onMessage(event: any) { - if (this.subscribers.has(event.data.originId)) { - this.eventsCallbacks.forEach((callback: Function) => { - callback(event.data, event); - }) - } - } -} - -export interface IPubSubEvent { - type: string; - originId: string; - data: any; -} - -export interface ISubscriber { - window: Window; - locationUrl: string; -} diff --git a/catalog-ui/src/app/models/plugin-pubsub.ts b/catalog-ui/src/app/models/plugin-pubsub.ts deleted file mode 100644 index 3a34de99cc..0000000000 --- a/catalog-ui/src/app/models/plugin-pubsub.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {BasePubSub} from "./base-pubsub"; - -declare const window: Window; - -export class PluginPubSub extends BasePubSub { - - constructor(pluginId: string, parentUrl: string, eventsToWait?: Array) { - super(pluginId); - this.register('sdc-hub', window.parent, parentUrl); - this.subscribe(eventsToWait); - } - - public subscribe(eventsToWait?: Array) { - const registerData = { - pluginId: this.clientId, - eventsToWait: eventsToWait || [] - }; - - this.notify('PLUGIN_REGISTER', registerData); - } - - public unsubscribe() { - const unregisterData = { - pluginId: this.clientId - }; - - this.notify('PLUGIN_UNREGISTER', unregisterData); - } -} diff --git a/catalog-ui/src/app/ng2/components/ui/plugin/plugin-frame.component.ts b/catalog-ui/src/app/ng2/components/ui/plugin/plugin-frame.component.ts index 067bb96d8e..d70c448984 100644 --- a/catalog-ui/src/app/ng2/components/ui/plugin/plugin-frame.component.ts +++ b/catalog-ui/src/app/ng2/components/ui/plugin/plugin-frame.component.ts @@ -1,4 +1,4 @@ -import {Component, Inject, Input, Output, OnInit, EventEmitter} from "@angular/core"; +import {Component, EventEmitter, Inject, Input, OnInit, Output} from "@angular/core"; import {URLSearchParams} from '@angular/http'; import {Plugin} from "app/models"; import {EventBusService} from "../../../services/event-bus.service"; @@ -35,6 +35,8 @@ export class PluginFrameComponent implements OnInit { if (this.plugin.isOnline) { this.initPlugin(); + } else { + this.onLoadingDone.emit(); } }) @@ -68,7 +70,7 @@ export class PluginFrameComponent implements OnInit { // before moving to a new state this.$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => { if ((fromState.name !== toState.name) || (fromState.name === toState.name) && (toParams.path !== fromParams.path)) { - if(this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) { + if (this.eventBusService.NoWindowOutEvents.indexOf(this.eventBusService.lastEventNotified) == -1) { if (!this.isClosed) { event.preventDefault(); @@ -79,8 +81,7 @@ export class PluginFrameComponent implements OnInit { this.$state.go(toState.name, toParams); }); } - } - else { + } else { this.eventBusService.unregister(this.plugin.pluginId); } } diff --git a/catalog-ui/src/app/ng2/services/event-bus.service.ts b/catalog-ui/src/app/ng2/services/event-bus.service.ts index 664ce35080..cc53d7978b 100644 --- a/catalog-ui/src/app/ng2/services/event-bus.service.ts +++ b/catalog-ui/src/app/ng2/services/event-bus.service.ts @@ -1,5 +1,5 @@ -import { Injectable } from '@angular/core'; -import {BasePubSub, IPubSubEvent} from "../../models/base-pubsub"; +import {Injectable} from '@angular/core'; +import {BasePubSub, IPubSubEvent} from 'sdc-pubsub'; @Injectable() export class EventBusService extends BasePubSub { @@ -11,26 +11,6 @@ export class EventBusService extends BasePubSub { this.NoWindowOutEvents = ["CHECK_IN", "SUBMIT_FOR_TESTING", "UNDO_CHECK_OUT"]; } - protected handlePluginRegistration(eventData: IPubSubEvent, event: any) { - if (eventData.type === 'PLUGIN_REGISTER') { - this.register(eventData.data.pluginId, event.source, event.origin); - - let newEventsList = []; - - if (this.eventsToWait.has(eventData.data.pluginId)) { - newEventsList = _.union(this.eventsToWait.get(eventData.data.pluginId), eventData.data.eventsToWait); - } - else { - newEventsList = eventData.data.eventsToWait; - } - - this.eventsToWait.set(eventData.data.pluginId, newEventsList); - - } else if (eventData.type === 'PLUGIN_UNREGISTER') { - this.unregister(eventData.data.pluginId); - } - } - public unregister(pluginId: string) { const unregisterData = { pluginId: pluginId @@ -41,18 +21,6 @@ export class EventBusService extends BasePubSub { }); } - protected onMessage(event: any) { - if (event.data.type === 'PLUGIN_REGISTER') { - this.handlePluginRegistration(event.data, event); - } - - super.onMessage(event); - - if (event.data.type === 'PLUGIN_UNREGISTER') { - this.handlePluginRegistration(event.data, event); - } - } - public disableNavigation(isDisable: boolean) { let iframes = document.getElementsByClassName("plugin-iframe"); @@ -70,8 +38,7 @@ export class EventBusService extends BasePubSub { "left: 0;"; disableDiv.setAttribute("class", "disable-navigation-div"); document.body.appendChild(disableDiv); - } - else { + } else { document.getElementsByClassName("disable-navigation-div")[0].remove(); _.forEach(iframes, (iframeElement: HTMLElement) => { @@ -80,7 +47,7 @@ export class EventBusService extends BasePubSub { } } - public notify(eventType:string, eventData?:any, disableOnWaiting:boolean=true) { + public notify(eventType: string, eventData?: any, disableOnWaiting: boolean = true) { let doDisable = false; if (disableOnWaiting) { @@ -105,4 +72,35 @@ export class EventBusService extends BasePubSub { }.bind(this) }; } + + protected handlePluginRegistration(eventData: IPubSubEvent, event: any) { + if (eventData.type === 'PLUGIN_REGISTER') { + this.register(eventData.data.pluginId, event.source, event.origin); + + let newEventsList = []; + + if (this.eventsToWait.has(eventData.data.pluginId)) { + newEventsList = _.union(this.eventsToWait.get(eventData.data.pluginId), eventData.data.eventsToWait); + } else { + newEventsList = eventData.data.eventsToWait; + } + + this.eventsToWait.set(eventData.data.pluginId, newEventsList); + + } else if (eventData.type === 'PLUGIN_UNREGISTER') { + this.unregister(eventData.data.pluginId); + } + } + + protected onMessage(event: any) { + if (event.data.type === 'PLUGIN_REGISTER') { + this.handlePluginRegistration(event.data, event); + } + + super.onMessage(event); + + if (event.data.type === 'PLUGIN_UNREGISTER') { + this.handlePluginRegistration(event.data, event); + } + } } diff --git a/catalog-ui/src/app/view-models/plugins/plugins-tab-view-model.ts b/catalog-ui/src/app/view-models/plugins/plugins-tab-view-model.ts index 47b2f78c10..d25cd19bff 100644 --- a/catalog-ui/src/app/view-models/plugins/plugins-tab-view-model.ts +++ b/catalog-ui/src/app/view-models/plugins/plugins-tab-view-model.ts @@ -28,7 +28,7 @@ */ -import {Plugin, IUserProperties} from "app/models"; +import {IUserProperties, Plugin} from "app/models"; import {CacheService} from "app/services"; import {PluginsService} from "../../ng2/services/plugins.service"; @@ -51,23 +51,20 @@ export class PluginsTabViewModel { 'PluginsService' ]; - constructor(private $scope:IPluginsTabViewModelScope, - private $stateParams:any, - private cacheService:CacheService, - private pluginsService:PluginsService) { + constructor(private $scope: IPluginsTabViewModelScope, + private $stateParams: any, + private cacheService: CacheService, + private pluginsService: PluginsService) { this.initScope(); } - private initScope = ():void => { + private initScope = (): void => { this.$scope.plugin = this.pluginsService.getPluginByStateUrl(this.$stateParams.path); this.$scope.version = this.cacheService.get('version'); this.$scope.user = this.cacheService.get('user'); - // Don't show the loader if the plugin isn't online - if (this.$scope.plugin.isOnline) { - this.$scope.isLoading = true; - } + this.$scope.isLoading = true; this.$scope.queryParams = { userId: this.$scope.user.userId, diff --git a/catalog-ui/src/app/view-models/workspace/tabs/plugins/plugins-context-view-model.ts b/catalog-ui/src/app/view-models/workspace/tabs/plugins/plugins-context-view-model.ts index db76f73ce6..d3ccbe3325 100644 --- a/catalog-ui/src/app/view-models/workspace/tabs/plugins/plugins-context-view-model.ts +++ b/catalog-ui/src/app/view-models/workspace/tabs/plugins/plugins-context-view-model.ts @@ -28,7 +28,7 @@ */ -import {Plugin, IUserProperties} from "app/models"; +import {IUserProperties, Plugin} from "app/models"; import {CacheService} from "app/services"; import {PluginsService} from "../../../../ng2/services/plugins.service"; import {IWorkspaceViewModelScope} from "../../workspace-view-model"; @@ -36,7 +36,7 @@ import {IWorkspaceViewModelScope} from "../../workspace-view-model"; interface IPluginsContextViewModelScope extends IWorkspaceViewModelScope { plugin: Plugin; - user:IUserProperties; + user: IUserProperties; queryParams: Object; isLoading: boolean; show: boolean; @@ -52,23 +52,20 @@ export class PluginsContextViewModel { 'PluginsService' ]; - constructor(private $scope:IPluginsContextViewModelScope, - private $stateParams:any, - private cacheService:CacheService, - private pluginsService:PluginsService) { + constructor(private $scope: IPluginsContextViewModelScope, + private $stateParams: any, + private cacheService: CacheService, + private pluginsService: PluginsService) { this.initScope(); } - private initScope = ():void => { + private initScope = (): void => { this.$scope.show = false; this.$scope.plugin = this.pluginsService.getPluginByStateUrl(this.$stateParams.path); this.$scope.user = this.cacheService.get('user'); - // Don't show loader if the plugin isn't online - if (this.$scope.plugin.isOnline) { - this.$scope.isLoading = true; - } + this.$scope.isLoading = true; this.$scope.queryParams = { userId: this.$scope.user.userId, -- cgit 1.2.3-korg