summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/event-bus.service.ts
blob: 10990e2f2c05a94b5dc7de209c0e271e7deab7f4 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Injectable } from '@angular/core';
import {BasePubSub, IPubSubEvent} from "../../models/base-pubsub";

@Injectable()
export class EventBusService extends BasePubSub {

    NoWindowOutEvents: Array<string>;

    constructor() {
        super("sdc-hub");
        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
        };

        this.notify('PLUGIN_CLOSE', unregisterData).subscribe(() => {
            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);
        }
    }

    public disableNavigation(isDisable: boolean) {
        if (isDisable) {
            let disableDiv = document.createElement('div');
            disableDiv.style.cssText = "position: fixed;\n" +
                "z-index: 1029;\n" +
                "background: rgba(0,0,0,0.5);\n" +
                "width: 100%;\n" +
                "height: 100%;\n" +
                "top: 0;\n" +
                "left: 0;";
            disableDiv.setAttribute("class", "disable-navigation-div");
            document.body.appendChild(disableDiv);
        }
        else {
            document.getElementsByClassName("disable-navigation-div")[0].remove();
        }
    }

    public notify(eventType:string, eventData?:any, disableOnWaiting:boolean=true) {
        let doDisable = false;

        if (disableOnWaiting) {
            doDisable = this.isWaitingForEvent(eventType);

            if (doDisable) {
                this.disableNavigation(true);
            }
        }

        const origSubscribe = super.notify(eventType, eventData).subscribe;

        return {
            subscribe: function (callbackFn) {
                origSubscribe(() => {
                    if (doDisable) {
                        this.disableNavigation(false);
                    }

                    callbackFn();
                });
            }.bind(this)
        };
    }
}