minor fixes to sdc-pubsub 75/75475/3
authorIdan Amit <idan.amit@intl.att.com>
Tue, 8 Jan 2019 14:41:23 +0000 (16:41 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Wed, 16 Jan 2019 09:13:38 +0000 (09:13 +0000)
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 <idan.amit@intl.att.com>
catalog-ui/package.json
catalog-ui/src/app/models/base-pubsub.ts [deleted file]
catalog-ui/src/app/models/plugin-pubsub.ts [deleted file]
catalog-ui/src/app/ng2/components/ui/plugin/plugin-frame.component.ts
catalog-ui/src/app/ng2/services/event-bus.service.ts
catalog-ui/src/app/view-models/plugins/plugins-tab-view-model.ts
catalog-ui/src/app/view-models/workspace/tabs/plugins/plugins-context-view-model.ts

index d32506e..1bd4928 100644 (file)
     "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 (file)
index 41e8039..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-declare const window: Window;
-
-export class BasePubSub {
-
-    subscribers: Map<string, ISubscriber>;
-    eventsCallbacks: Array<Function>;
-    clientId: string;
-    eventsToWait: Map<string, Array<string>>;
-    lastEventNotified: string;
-
-    constructor(pluginId: string) {
-        this.subscribers = new Map<string, ISubscriber>();
-        this.eventsCallbacks = [];
-        this.eventsToWait = new Map<string, Array<string>>();
-        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<string>) =>
-            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 (file)
index 3a34de9..0000000
+++ /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<string>) {
-        super(pluginId);
-        this.register('sdc-hub', window.parent, parentUrl);
-        this.subscribe(eventsToWait);
-    }
-
-    public subscribe(eventsToWait?: Array<string>) {
-        const registerData = {
-            pluginId: this.clientId,
-            eventsToWait: eventsToWait || []
-        };
-
-        this.notify('PLUGIN_REGISTER', registerData);
-    }
-
-    public unsubscribe() {
-        const unregisterData = {
-            pluginId: this.clientId
-        };
-
-        this.notify('PLUGIN_UNREGISTER', unregisterData);
-    }
-}
index 067bb96..d70c448 100644 (file)
@@ -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);
                 }
             }
index 664ce35..cc53d79 100644 (file)
@@ -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);
+        }
+    }
 }
index 47b2f78..d25cd19 100644 (file)
@@ -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,
index db76f73..d3ccbe3 100644 (file)
@@ -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,