1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.getListenersCounter = getListenersCounter;
|
5 | require("core-js/modules/es.error.cause.js");
|
6 | require("core-js/modules/es.array.push.js");
|
7 | var _event = require("./helpers/dom/event");
|
8 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
9 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
10 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let listenersCounter = 0;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | class EventManager {
|
24 | |
25 |
|
26 |
|
27 |
|
28 | constructor() {
|
29 | let context = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
30 | |
31 |
|
32 |
|
33 | _defineProperty(this, "context", void 0);
|
34 | this.context = context || this;
|
35 |
|
36 |
|
37 | if (!this.context.eventListeners) {
|
38 | this.context.eventListeners = [];
|
39 | }
|
40 | }
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | addEventListener(element, eventName, callback) {
|
52 | let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
53 | |
54 |
|
55 |
|
56 |
|
57 | function callbackProxy(event) {
|
58 | callback.call(this, extendEvent(event));
|
59 | }
|
60 | this.context.eventListeners.push({
|
61 | element,
|
62 | event: eventName,
|
63 | callback,
|
64 | callbackProxy,
|
65 | options,
|
66 | eventManager: this
|
67 | });
|
68 | element.addEventListener(eventName, callbackProxy, options);
|
69 | listenersCounter += 1;
|
70 | return () => {
|
71 | this.removeEventListener(element, eventName, callback);
|
72 | };
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | removeEventListener(element, eventName, callback) {
|
84 | let onlyOwnEvents = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
85 | let len = this.context.eventListeners.length;
|
86 | let tmpEvent;
|
87 | while (len) {
|
88 | len -= 1;
|
89 | tmpEvent = this.context.eventListeners[len];
|
90 | if (tmpEvent.event === eventName && tmpEvent.element === element) {
|
91 | if (callback && callback !== tmpEvent.callback) {
|
92 |
|
93 | continue;
|
94 | }
|
95 |
|
96 |
|
97 | if (onlyOwnEvents && tmpEvent.eventManager !== this) {
|
98 | continue;
|
99 | }
|
100 | this.context.eventListeners.splice(len, 1);
|
101 | tmpEvent.element.removeEventListener(tmpEvent.event, tmpEvent.callbackProxy, tmpEvent.options);
|
102 | listenersCounter -= 1;
|
103 | }
|
104 | }
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | clearEvents() {
|
115 | let onlyOwnEvents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
116 | if (!this.context) {
|
117 | return;
|
118 | }
|
119 | let len = this.context.eventListeners.length;
|
120 | while (len) {
|
121 | len -= 1;
|
122 | const event = this.context.eventListeners[len];
|
123 | if (onlyOwnEvents && event.eventManager !== this) {
|
124 | continue;
|
125 | }
|
126 | this.context.eventListeners.splice(len, 1);
|
127 | event.element.removeEventListener(event.event, event.callbackProxy, event.options);
|
128 | listenersCounter -= 1;
|
129 | }
|
130 | }
|
131 |
|
132 | |
133 |
|
134 |
|
135 | clear() {
|
136 | this.clearEvents();
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 | destroy() {
|
143 | this.clearEvents();
|
144 | this.context = null;
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 | destroyWithOwnEventsOnly() {
|
151 | this.clearEvents(true);
|
152 | this.context = null;
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | fireEvent(element, eventName) {
|
162 | let rootDocument = element.document;
|
163 | let rootWindow = element;
|
164 | if (!rootDocument) {
|
165 | rootDocument = element.ownerDocument ? element.ownerDocument : element;
|
166 | rootWindow = rootDocument.defaultView;
|
167 | }
|
168 | const options = {
|
169 | bubbles: true,
|
170 | cancelable: eventName !== 'mousemove',
|
171 | view: rootWindow,
|
172 | detail: 0,
|
173 | screenX: 0,
|
174 | screenY: 0,
|
175 | clientX: 1,
|
176 | clientY: 1,
|
177 | ctrlKey: false,
|
178 | altKey: false,
|
179 | shiftKey: false,
|
180 | metaKey: false,
|
181 | button: 0,
|
182 | relatedTarget: undefined
|
183 | };
|
184 | let event;
|
185 | if (rootDocument.createEvent) {
|
186 | event = rootDocument.createEvent('MouseEvents');
|
187 | event.initMouseEvent(eventName, options.bubbles, options.cancelable, options.view, options.detail, options.screenX, options.screenY, options.clientX, options.clientY, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, options.relatedTarget || rootDocument.body.parentNode);
|
188 | } else {
|
189 | event = rootDocument.createEventObject();
|
190 | }
|
191 | if (element.dispatchEvent) {
|
192 | element.dispatchEvent(event);
|
193 | } else {
|
194 | element.fireEvent(`on${eventName}`, event);
|
195 | }
|
196 | }
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | function extendEvent(event) {
|
205 | const nativeStopImmediatePropagation = event.stopImmediatePropagation;
|
206 | event.stopImmediatePropagation = function () {
|
207 | nativeStopImmediatePropagation.apply(this);
|
208 | (0, _event.stopImmediatePropagation)(this);
|
209 | };
|
210 | return event;
|
211 | }
|
212 | var _default = exports.default = EventManager;
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | function getListenersCounter() {
|
218 | return listenersCounter;
|
219 | } |
\ | No newline at end of file |