1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
9 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
10 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
11 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
12 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
13 | };
|
14 | var __metadata = (this && this.__metadata) || function (k, v) {
|
15 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
16 | };
|
17 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
18 | return function (target, key) { decorator(target, key, paramIndex); }
|
19 | };
|
20 | Object.defineProperty(exports, "__esModule", { value: true });
|
21 | var logger_1 = require("./logger");
|
22 | var utils_1 = require("./utils");
|
23 | var context_1 = require("./context/context");
|
24 | var context_2 = require("./context/context");
|
25 | var gridOptionsWrapper_1 = require("./gridOptionsWrapper");
|
26 | var EventService = (function () {
|
27 | function EventService() {
|
28 | this.allSyncListeners = {};
|
29 | this.allAsyncListeners = {};
|
30 | this.globalSyncListeners = [];
|
31 | this.globalAsyncListeners = [];
|
32 | this.asyncFunctionsQueue = [];
|
33 | this.scheduled = false;
|
34 | }
|
35 | EventService_1 = EventService;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | EventService.prototype.setBeans = function (loggerFactory, gridOptionsWrapper, globalEventListener) {
|
46 | if (globalEventListener === void 0) { globalEventListener = null; }
|
47 | this.logger = loggerFactory.create('EventService');
|
48 | if (globalEventListener) {
|
49 | var async = gridOptionsWrapper.useAsyncEvents();
|
50 | this.addGlobalListener(globalEventListener, async);
|
51 | }
|
52 | };
|
53 | EventService.prototype.getListenerList = function (eventType, async) {
|
54 | var listenerMap = async ? this.allAsyncListeners : this.allSyncListeners;
|
55 | var listenerList = listenerMap[eventType];
|
56 | if (!listenerList) {
|
57 | listenerList = [];
|
58 | listenerMap[eventType] = listenerList;
|
59 | }
|
60 | return listenerList;
|
61 | };
|
62 | EventService.prototype.addEventListener = function (eventType, listener, async) {
|
63 | if (async === void 0) { async = false; }
|
64 | var listenerList = this.getListenerList(eventType, async);
|
65 | if (listenerList.indexOf(listener) < 0) {
|
66 | listenerList.push(listener);
|
67 | }
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 | EventService.prototype.addModalPriorityEventListener = function (eventType, listener, async) {
|
73 | if (async === void 0) { async = false; }
|
74 | this.addEventListener(eventType + EventService_1.PRIORITY, listener, async);
|
75 | };
|
76 | EventService.prototype.addGlobalListener = function (listener, async) {
|
77 | if (async === void 0) { async = false; }
|
78 | if (async) {
|
79 | this.globalAsyncListeners.push(listener);
|
80 | }
|
81 | else {
|
82 | this.globalSyncListeners.push(listener);
|
83 | }
|
84 | };
|
85 | EventService.prototype.removeEventListener = function (eventType, listener, async) {
|
86 | if (async === void 0) { async = false; }
|
87 | var listenerList = this.getListenerList(eventType, async);
|
88 | utils_1.Utils.removeFromArray(listenerList, listener);
|
89 | };
|
90 | EventService.prototype.removeGlobalListener = function (listener, async) {
|
91 | if (async === void 0) { async = false; }
|
92 | if (async) {
|
93 | utils_1.Utils.removeFromArray(this.globalAsyncListeners, listener);
|
94 | }
|
95 | else {
|
96 | utils_1.Utils.removeFromArray(this.globalSyncListeners, listener);
|
97 | }
|
98 | };
|
99 |
|
100 |
|
101 | EventService.prototype.dispatchEvent = function (event) {
|
102 |
|
103 | this.dispatchToListeners(event, true);
|
104 | this.dispatchToListeners(event, false);
|
105 | };
|
106 | EventService.prototype.dispatchToListeners = function (event, async) {
|
107 | var _this = this;
|
108 | var globalListeners = async ? this.globalAsyncListeners : this.globalSyncListeners;
|
109 | var eventType = event.type;
|
110 |
|
111 | var p1ListenerList = this.getListenerList(eventType + EventService_1.PRIORITY, async);
|
112 | utils_1.Utils.forEachSnapshotFirst(p1ListenerList, function (listener) {
|
113 | if (async) {
|
114 | _this.dispatchAsync(function () { return listener(event); });
|
115 | }
|
116 | else {
|
117 | listener(event);
|
118 | }
|
119 | });
|
120 | var listenerList = this.getListenerList(eventType, async);
|
121 | utils_1.Utils.forEachSnapshotFirst(listenerList, function (listener) {
|
122 | if (async) {
|
123 | _this.dispatchAsync(function () { return listener(event); });
|
124 | }
|
125 | else {
|
126 | listener(event);
|
127 | }
|
128 | });
|
129 | utils_1.Utils.forEachSnapshotFirst(globalListeners, function (listener) {
|
130 | if (async) {
|
131 | _this.dispatchAsync(function () { return listener(eventType, event); });
|
132 | }
|
133 | else {
|
134 | listener(eventType, event);
|
135 | }
|
136 | });
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | EventService.prototype.dispatchAsync = function (func) {
|
143 |
|
144 | this.asyncFunctionsQueue.push(func);
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | if (!this.scheduled) {
|
150 |
|
151 | setTimeout(this.flushAsyncQueue.bind(this), 0);
|
152 |
|
153 | this.scheduled = true;
|
154 | }
|
155 | };
|
156 |
|
157 | EventService.prototype.flushAsyncQueue = function () {
|
158 | this.scheduled = false;
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | var queueCopy = this.asyncFunctionsQueue.slice();
|
165 | this.asyncFunctionsQueue = [];
|
166 |
|
167 | queueCopy.forEach(function (func) { return func(); });
|
168 | };
|
169 |
|
170 |
|
171 | EventService.PRIORITY = '-P1';
|
172 | __decorate([
|
173 | __param(0, context_2.Qualifier('loggerFactory')),
|
174 | __param(1, context_2.Qualifier('gridOptionsWrapper')),
|
175 | __param(2, context_2.Qualifier('globalEventListener')),
|
176 | __metadata("design:type", Function),
|
177 | __metadata("design:paramtypes", [logger_1.LoggerFactory,
|
178 | gridOptionsWrapper_1.GridOptionsWrapper,
|
179 | Function]),
|
180 | __metadata("design:returntype", void 0)
|
181 | ], EventService.prototype, "setBeans", null);
|
182 | EventService = EventService_1 = __decorate([
|
183 | context_1.Bean('eventService')
|
184 | ], EventService);
|
185 | return EventService;
|
186 | var EventService_1;
|
187 | }());
|
188 | exports.EventService = EventService;
|