summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/event-bus.service.ts
blob: 7730a77f41e64a11b2ddb3bbf4db490305e89af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Injectable } from '@angular/core';
import {BasePubSub, IPubSubEvent} from "../../models/base-pubsub";

@Injectable()
export class EventBusService extends BasePubSub {

    constructor() {
        super("sdc-hub");
    }

    protected handlePluginRegistration(eventData: IPubSubEvent, event: any) {
        if (eventData.type === 'PLUGIN_REGISTER') {
            this.register(eventData.data.pluginId, event.source, event.origin);
        } else if (eventData.type === 'PLUGIN_UNREGISTER') {
            this.unregister(eventData.data.pluginId);
        }
    }

    public unregister(pluginId: string) {
        const unregisterData = {
            pluginId: pluginId
        };

        this.notify('PLUGIN_CLOSE', unregisterData);
        super.unregister(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);
        }
    }
}