Developed disable navigation mechanism
[sdc.git] / catalog-ui / src / app / ng2 / services / event-bus.service.ts
1 import { Injectable } from '@angular/core';
2 import {BasePubSub, IPubSubEvent} from "../../models/base-pubsub";
3
4 @Injectable()
5 export class EventBusService extends BasePubSub {
6
7     NoWindowOutEvents: Array<string>;
8
9     constructor() {
10         super("sdc-hub");
11         this.NoWindowOutEvents = ["CHECK_IN", "SUBMIT_FOR_TESTING", "UNDO_CHECK_OUT"];
12     }
13
14     protected handlePluginRegistration(eventData: IPubSubEvent, event: any) {
15         if (eventData.type === 'PLUGIN_REGISTER') {
16             this.register(eventData.data.pluginId, event.source, event.origin);
17
18             let newEventsList = [];
19
20             if (this.eventsToWait.has(eventData.data.pluginId)) {
21                 newEventsList = _.union(this.eventsToWait.get(eventData.data.pluginId), eventData.data.eventsToWait);
22             }
23             else {
24                 newEventsList = eventData.data.eventsToWait;
25             }
26
27             this.eventsToWait.set(eventData.data.pluginId, newEventsList);
28
29         } else if (eventData.type === 'PLUGIN_UNREGISTER') {
30             this.unregister(eventData.data.pluginId);
31         }
32     }
33
34     public unregister(pluginId: string) {
35         const unregisterData = {
36             pluginId: pluginId
37         };
38
39         this.notify('PLUGIN_CLOSE', unregisterData).subscribe(() => {
40             super.unregister(pluginId);
41         });
42     }
43
44     protected onMessage(event: any) {
45         if (event.data.type === 'PLUGIN_REGISTER') {
46             this.handlePluginRegistration(event.data, event);
47         }
48
49         super.onMessage(event);
50
51         if (event.data.type === 'PLUGIN_UNREGISTER') {
52             this.handlePluginRegistration(event.data, event);
53         }
54     }
55
56     public disableNavigation(isDisable: boolean) {
57         if (isDisable) {
58             let disableDiv = document.createElement('div');
59             disableDiv.style.cssText = "position: fixed;\n" +
60                 "z-index: 1029;\n" +
61                 "background: rgba(0,0,0,0.5);\n" +
62                 "width: 100%;\n" +
63                 "height: 100%;\n" +
64                 "top: 0;\n" +
65                 "left: 0;";
66             disableDiv.setAttribute("class", "disable-navigation-div");
67             document.body.appendChild(disableDiv);
68         }
69         else {
70             document.getElementsByClassName("disable-navigation-div")[0].remove();
71         }
72     }
73
74     public notify(eventType:string, eventData?:any, disableOnWaiting:boolean=true) {
75         let doDisable = false;
76
77         if (disableOnWaiting) {
78             doDisable = this.isWaitingForEvent(eventType);
79
80             if (doDisable) {
81                 this.disableNavigation(true);
82             }
83         }
84
85         const origSubscribe = super.notify(eventType, eventData).subscribe;
86
87         return {
88             subscribe: function (callbackFn) {
89                 origSubscribe(() => {
90                     if (doDisable) {
91                         this.disableNavigation(false);
92                     }
93
94                     callbackFn();
95                 });
96             }.bind(this)
97         };
98     }
99 }