Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/events/eventtarget.js
4561 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A disposable implementation of a custom
9
* listenable/event target. See also: documentation for
10
* `goog.events.Listenable`.
11
*
12
* @see ../demos/eventtarget.html
13
* @see goog.events.Listenable
14
*/
15
16
goog.provide('goog.events.EventTarget');
17
18
goog.require('goog.Disposable');
19
goog.require('goog.asserts');
20
goog.require('goog.events');
21
goog.require('goog.events.Event');
22
goog.require('goog.events.Listenable');
23
goog.require('goog.events.ListenerMap');
24
goog.require('goog.object');
25
goog.requireType('goog.events.EventId');
26
goog.requireType('goog.events.EventLike');
27
goog.requireType('goog.events.ListenableKey');
28
29
30
31
/**
32
* An implementation of `goog.events.Listenable` with full W3C
33
* EventTarget-like support (capture/bubble mechanism, stopping event
34
* propagation, preventing default actions).
35
*
36
* You may subclass this class to turn your class into a Listenable.
37
*
38
* Unless propagation is stopped, an event dispatched by an
39
* EventTarget will bubble to the parent returned by
40
* `getParentEventTarget`. To set the parent, call
41
* `setParentEventTarget`. Subclasses that don't support
42
* changing the parent can override the setter to throw an error.
43
*
44
* Example usage:
45
* <pre>
46
* var source = new goog.events.EventTarget();
47
* function handleEvent(e) {
48
* alert('Type: ' + e.type + '; Target: ' + e.target);
49
* }
50
* source.listen('foo', handleEvent);
51
* // Or: goog.events.listen(source, 'foo', handleEvent);
52
* ...
53
* source.dispatchEvent('foo'); // will call handleEvent
54
* ...
55
* source.unlisten('foo', handleEvent);
56
* // Or: goog.events.unlisten(source, 'foo', handleEvent);
57
* </pre>
58
*
59
* @constructor
60
* @extends {goog.Disposable}
61
* @implements {goog.events.Listenable}
62
*/
63
goog.events.EventTarget = function() {
64
'use strict';
65
goog.Disposable.call(this);
66
67
/**
68
* Maps of event type to an array of listeners.
69
* @private {!goog.events.ListenerMap}
70
*/
71
this.eventTargetListeners_ = new goog.events.ListenerMap(this);
72
73
/**
74
* The object to use for event.target. Useful when mixing in an
75
* EventTarget to another object.
76
* @private {!Object}
77
*/
78
this.actualEventTarget_ = this;
79
80
/**
81
* Parent event target, used during event bubbling.
82
*
83
* TODO(chrishenry): Change this to goog.events.Listenable. This
84
* currently breaks people who expect getParentEventTarget to return
85
* goog.events.EventTarget.
86
*
87
* @private {?goog.events.EventTarget}
88
*/
89
this.parentEventTarget_ = null;
90
};
91
goog.inherits(goog.events.EventTarget, goog.Disposable);
92
goog.events.Listenable.addImplementation(goog.events.EventTarget);
93
94
95
/**
96
* An artificial cap on the number of ancestors you can have. This is mainly
97
* for loop detection.
98
* @const {number}
99
* @private
100
*/
101
goog.events.EventTarget.MAX_ANCESTORS_ = 1000;
102
103
104
/**
105
* Returns the parent of this event target to use for bubbling.
106
*
107
* @return {goog.events.EventTarget} The parent EventTarget or null if
108
* there is no parent.
109
* @override
110
*/
111
goog.events.EventTarget.prototype.getParentEventTarget = function() {
112
'use strict';
113
return this.parentEventTarget_;
114
};
115
116
117
/**
118
* Sets the parent of this event target to use for capture/bubble
119
* mechanism.
120
* @param {goog.events.EventTarget} parent Parent listenable (null if none).
121
*/
122
goog.events.EventTarget.prototype.setParentEventTarget = function(parent) {
123
'use strict';
124
this.parentEventTarget_ = parent;
125
};
126
127
128
/**
129
* Adds an event listener to the event target. The same handler can only be
130
* added once per the type. Even if you add the same handler multiple times
131
* using the same type then it will only be called once when the event is
132
* dispatched.
133
*
134
* @param {string|!goog.events.EventId} type The type of the event to listen for
135
* @param {function(?):?|{handleEvent:function(?):?}|null} handler The function
136
* to handle the event. The handler can also be an object that implements
137
* the handleEvent method which takes the event object as argument.
138
* @param {boolean=} opt_capture In DOM-compliant browsers, this determines
139
* whether the listener is fired during the capture or bubble phase
140
* of the event.
141
* @param {Object=} opt_handlerScope Object in whose scope to call
142
* the listener.
143
* @deprecated Use `#listen` instead, when possible. Otherwise, use
144
* `goog.events.listen` if you are passing Object
145
* (instead of Function) as handler.
146
*/
147
goog.events.EventTarget.prototype.addEventListener = function(
148
type, handler, opt_capture, opt_handlerScope) {
149
'use strict';
150
goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
151
};
152
153
154
/**
155
* Removes an event listener from the event target. The handler must be the
156
* same object as the one added. If the handler has not been added then
157
* nothing is done.
158
*
159
* @param {string|!goog.events.EventId} type The type of the event to listen for
160
* @param {function(?):?|{handleEvent:function(?):?}|null} handler The function
161
* to handle the event. The handler can also be an object that implements
162
* the handleEvent method which takes the event object as argument.
163
* @param {boolean=} opt_capture In DOM-compliant browsers, this determines
164
* whether the listener is fired during the capture or bubble phase
165
* of the event.
166
* @param {Object=} opt_handlerScope Object in whose scope to call
167
* the listener.
168
* @deprecated Use `#unlisten` instead, when possible. Otherwise, use
169
* `goog.events.unlisten` if you are passing Object
170
* (instead of Function) as handler.
171
*/
172
goog.events.EventTarget.prototype.removeEventListener = function(
173
type, handler, opt_capture, opt_handlerScope) {
174
'use strict';
175
goog.events.unlisten(this, type, handler, opt_capture, opt_handlerScope);
176
};
177
178
179
/**
180
* @param {?goog.events.EventLike} e Event object.
181
* @return {boolean} If anyone called preventDefault on the event object (or
182
* if any of the listeners returns false) this will also return false.
183
* @override
184
*/
185
goog.events.EventTarget.prototype.dispatchEvent = function(e) {
186
'use strict';
187
this.assertInitialized_();
188
189
var ancestorsTree, ancestor = this.getParentEventTarget();
190
if (ancestor) {
191
ancestorsTree = [];
192
var ancestorCount = 1;
193
for (; ancestor; ancestor = ancestor.getParentEventTarget()) {
194
ancestorsTree.push(ancestor);
195
goog.asserts.assert(
196
(++ancestorCount < goog.events.EventTarget.MAX_ANCESTORS_),
197
'infinite loop');
198
}
199
}
200
201
return goog.events.EventTarget.dispatchEventInternal_(
202
this.actualEventTarget_, e, ancestorsTree);
203
};
204
205
206
/**
207
* Removes listeners from this object. Classes that extend EventTarget may
208
* need to override this method in order to remove references to DOM Elements
209
* and additional listeners.
210
* @override
211
* @protected
212
*/
213
goog.events.EventTarget.prototype.disposeInternal = function() {
214
'use strict';
215
goog.events.EventTarget.superClass_.disposeInternal.call(this);
216
217
this.removeAllListeners();
218
this.parentEventTarget_ = null;
219
};
220
221
222
/**
223
* @param {string|!goog.events.EventId<EVENTOBJ>} type The event type id.
224
* @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
225
* method.
226
* @param {boolean=} opt_useCapture Whether to fire in capture phase
227
* (defaults to false).
228
* @param {SCOPE=} opt_listenerScope Object in whose scope to call the
229
* listener.
230
* @return {!goog.events.ListenableKey} Unique key for the listener.
231
* @template SCOPE,EVENTOBJ
232
* @override
233
*/
234
goog.events.EventTarget.prototype.listen = function(
235
type, listener, opt_useCapture, opt_listenerScope) {
236
'use strict';
237
this.assertInitialized_();
238
return this.eventTargetListeners_.add(
239
String(type), listener, false /* callOnce */, opt_useCapture,
240
opt_listenerScope);
241
};
242
243
244
/**
245
* @param {string|!goog.events.EventId<EVENTOBJ>} type The event type id.
246
* @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
247
* method.
248
* @param {boolean=} opt_useCapture Whether to fire in capture phase
249
* (defaults to false).
250
* @param {SCOPE=} opt_listenerScope Object in whose scope to call the
251
* listener.
252
* @return {!goog.events.ListenableKey} Unique key for the listener.
253
* @template SCOPE,EVENTOBJ
254
* @override
255
*/
256
goog.events.EventTarget.prototype.listenOnce = function(
257
type, listener, opt_useCapture, opt_listenerScope) {
258
'use strict';
259
return this.eventTargetListeners_.add(
260
String(type), listener, true /* callOnce */, opt_useCapture,
261
opt_listenerScope);
262
};
263
264
265
/**
266
* @param {string|!goog.events.EventId<EVENTOBJ>} type The event type id.
267
* @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
268
* method.
269
* @param {boolean=} opt_useCapture Whether to fire in capture phase
270
* (defaults to false).
271
* @param {SCOPE=} opt_listenerScope Object in whose scope to call
272
* the listener.
273
* @return {boolean} Whether any listener was removed.
274
* @template SCOPE,EVENTOBJ
275
* @override
276
*/
277
goog.events.EventTarget.prototype.unlisten = function(
278
type, listener, opt_useCapture, opt_listenerScope) {
279
'use strict';
280
return this.eventTargetListeners_.remove(
281
String(type), listener, opt_useCapture, opt_listenerScope);
282
};
283
284
285
/**
286
* @param {!goog.events.ListenableKey} key The key returned by
287
* listen() or listenOnce().
288
* @return {boolean} Whether any listener was removed.
289
* @override
290
*/
291
goog.events.EventTarget.prototype.unlistenByKey = function(key) {
292
'use strict';
293
return this.eventTargetListeners_.removeByKey(key);
294
};
295
296
297
/**
298
* @param {string|!goog.events.EventId=} opt_type Type of event to remove,
299
* default is to remove all types.
300
* @return {number} Number of listeners removed.
301
* @override
302
*/
303
goog.events.EventTarget.prototype.removeAllListeners = function(opt_type) {
304
'use strict';
305
// TODO(chrishenry): Previously, removeAllListeners can be called on
306
// uninitialized EventTarget, so we preserve that behavior. We
307
// should remove this when usages that rely on that fact are purged.
308
if (!this.eventTargetListeners_) {
309
return 0;
310
}
311
return this.eventTargetListeners_.removeAll(opt_type);
312
};
313
314
315
/**
316
* @param {string|!goog.events.EventId<EVENTOBJ>} type The type of the
317
* listeners to fire.
318
* @param {boolean} capture The capture mode of the listeners to fire.
319
* @param {EVENTOBJ} eventObject The event object to fire.
320
* @return {boolean} Whether all listeners succeeded without
321
* attempting to prevent default behavior. If any listener returns
322
* false or called goog.events.Event#preventDefault, this returns
323
* false.
324
* @template EVENTOBJ
325
* @override
326
*/
327
goog.events.EventTarget.prototype.fireListeners = function(
328
type, capture, eventObject) {
329
'use strict';
330
// TODO(chrishenry): Original code avoids array creation when there
331
// is no listener, so we do the same. If this optimization turns
332
// out to be not required, we can replace this with
333
// getListeners(type, capture) instead, which is simpler.
334
var listenerArray = this.eventTargetListeners_.listeners[String(type)];
335
if (!listenerArray) {
336
return true;
337
}
338
listenerArray = listenerArray.concat();
339
340
var rv = true;
341
for (var i = 0; i < listenerArray.length; ++i) {
342
var listener = listenerArray[i];
343
// We might not have a listener if the listener was removed.
344
if (listener && !listener.removed && listener.capture == capture) {
345
var listenerFn = listener.listener;
346
var listenerHandler = listener.handler || listener.src;
347
348
if (listener.callOnce) {
349
this.unlistenByKey(listener);
350
}
351
rv = listenerFn.call(listenerHandler, eventObject) !== false && rv;
352
}
353
}
354
355
return rv && !eventObject.defaultPrevented;
356
};
357
358
359
/**
360
* @param {string|!goog.events.EventId} type The type of the listeners to fire.
361
* @param {boolean} capture The capture mode of the listeners to fire.
362
* @return {!Array<!goog.events.ListenableKey>} An array of registered
363
* listeners.
364
* @template EVENTOBJ
365
* @override
366
*/
367
goog.events.EventTarget.prototype.getListeners = function(type, capture) {
368
'use strict';
369
return this.eventTargetListeners_.getListeners(String(type), capture);
370
};
371
372
373
/**
374
* @param {string|!goog.events.EventId<EVENTOBJ>} type The name of the event
375
* without the 'on' prefix.
376
* @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener The
377
* listener function to get.
378
* @param {boolean} capture Whether the listener is a capturing listener.
379
* @param {SCOPE=} opt_listenerScope Object in whose scope to call the
380
* listener.
381
* @return {?goog.events.ListenableKey} the found listener or null if not found.
382
* @template SCOPE,EVENTOBJ
383
* @override
384
*/
385
goog.events.EventTarget.prototype.getListener = function(
386
type, listener, capture, opt_listenerScope) {
387
'use strict';
388
return this.eventTargetListeners_.getListener(
389
String(type), listener, capture, opt_listenerScope);
390
};
391
392
393
/**
394
* @param {string|!goog.events.EventId<EVENTOBJ>=} opt_type Event type.
395
* @param {boolean=} opt_capture Whether to check for capture or bubble
396
* listeners.
397
* @return {boolean} Whether there is any active listeners matching
398
* the requested type and/or capture phase.
399
* @template EVENTOBJ
400
* @override
401
*/
402
goog.events.EventTarget.prototype.hasListener = function(
403
opt_type, opt_capture) {
404
'use strict';
405
var id = (opt_type !== undefined) ? String(opt_type) : undefined;
406
return this.eventTargetListeners_.hasListener(id, opt_capture);
407
};
408
409
410
/**
411
* Sets the target to be used for `event.target` when firing
412
* event. Mainly used for testing. For example, see
413
* `goog.testing.events.mixinListenable`.
414
* @param {!Object} target The target.
415
*/
416
goog.events.EventTarget.prototype.setTargetForTesting = function(target) {
417
'use strict';
418
this.actualEventTarget_ = target;
419
};
420
421
422
/**
423
* Asserts that the event target instance is initialized properly.
424
* @private
425
*/
426
goog.events.EventTarget.prototype.assertInitialized_ = function() {
427
'use strict';
428
goog.asserts.assert(
429
this.eventTargetListeners_,
430
'Event target is not initialized. Did you call the superclass ' +
431
'(goog.events.EventTarget) constructor?');
432
};
433
434
435
/**
436
* Dispatches the given event on the ancestorsTree.
437
*
438
* @param {!Object} target The target to dispatch on.
439
* @param {goog.events.Event|Object|string} e The event object.
440
* @param {Array<goog.events.Listenable>=} opt_ancestorsTree The ancestors
441
* tree of the target, in reverse order from the closest ancestor
442
* to the root event target. May be null if the target has no ancestor.
443
* @return {boolean} If anyone called preventDefault on the event object (or
444
* if any of the listeners returns false) this will also return false.
445
* @private
446
*/
447
goog.events.EventTarget.dispatchEventInternal_ = function(
448
target, e, opt_ancestorsTree) {
449
'use strict';
450
/** @suppress {missingProperties} */
451
var type = e.type || /** @type {string} */ (e);
452
453
// If accepting a string or object, create a custom event object so that
454
// preventDefault and stopPropagation work with the event.
455
if (typeof e === 'string') {
456
e = new goog.events.Event(e, target);
457
} else if (!(e instanceof goog.events.Event)) {
458
var oldEvent = e;
459
e = new goog.events.Event(type, target);
460
goog.object.extend(e, oldEvent);
461
} else {
462
e.target = e.target || target;
463
}
464
465
var rv = true, currentTarget;
466
467
// Executes all capture listeners on the ancestors, if any.
468
if (opt_ancestorsTree) {
469
for (var i = opt_ancestorsTree.length - 1;
470
!e.hasPropagationStopped() && i >= 0; i--) {
471
currentTarget = e.currentTarget = opt_ancestorsTree[i];
472
rv = currentTarget.fireListeners(type, true, e) && rv;
473
}
474
}
475
476
// Executes capture and bubble listeners on the target.
477
if (!e.hasPropagationStopped()) {
478
currentTarget = /** @type {?} */ (e.currentTarget = target);
479
rv = currentTarget.fireListeners(type, true, e) && rv;
480
if (!e.hasPropagationStopped()) {
481
rv = currentTarget.fireListeners(type, false, e) && rv;
482
}
483
}
484
485
// Executes all bubble listeners on the ancestors, if any.
486
if (opt_ancestorsTree) {
487
for (i = 0; !e.hasPropagationStopped() && i < opt_ancestorsTree.length;
488
i++) {
489
currentTarget = e.currentTarget = opt_ancestorsTree[i];
490
rv = currentTarget.fireListeners(type, false, e) && rv;
491
}
492
}
493
494
return rv;
495
};
496
497