This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
customevents.js
59 lines (52 loc) · 2 KB
/
customevents.js
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
goog.provide('jsaction.testing.CustomEvents');
goog.setTestOnly('jsaction.testing.CustomEvents');
goog.require('goog.Disposable');
goog.require('jsaction.ActionFlow');
goog.require('jsaction.EventType');
/**
* Testing context for listening to jsaction custom events. This class should be
* instantiated in a test's setUp. All listeners created in this context are
* removed in CustomEvents#dispose, which should be called from the test's
* tearDown.
*/
jsaction.testing.CustomEvents = class extends goog.Disposable {
constructor() {
super();
this.managedListeners_ = [];
}
/**
* Adds a listener for a jsaction custom event.
* @param {!Element} element The element on which to listen.
* @param {string} eventType The custom event type.
* @param {function(this:T, !jsaction.ActionFlow)} listener A listener
* callback.
* @param {!T=} opt_context The context in which to call the callback.
* @param {string=} opt_flowType The ActionFlow type given to the listener.
* @template T
*/
listen(element, eventType, listener, opt_context, opt_flowType) {
const jsactionListener = function(event) {
if (event.detail['_type'] == eventType) {
const actionFlow = new jsaction.ActionFlow(
opt_flowType || jsaction.testing.CustomEvents.DEFAULT_FLOWTYPE,
element, event, undefined /* startTime */, eventType);
listener.call(opt_context, actionFlow);
}
};
element.addEventListener(jsaction.EventType.CUSTOM, jsactionListener);
this.managedListeners_.push(
{'element': element, 'listener': jsactionListener});
}
/**
* Removes all listeners.
*/
disposeInternal() {
for (let idx = 0; idx < this.managedListeners_.length; idx++) {
const listenerInfo = this.managedListeners_[idx];
listenerInfo['element'].removeEventListener(
jsaction.EventType.CUSTOM, listenerInfo['listener']);
}
this.managedListeners_ = [];
}
};
jsaction.testing.CustomEvents.DEFAULT_FLOWTYPE = 'jsaction.test';