blob: 953d6746658d435a3040091bbd143ec153c03f4b [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8var listen;
9var unlisten;
10var unlistenByKey;
11
12(function() {
13 let listeners = {};
14 let nextId = 0;
15
16 function getHashCode_(obj) {
17 if (obj.listen_hc_ == null) {
18 obj.listen_hc_ = ++nextId;
19 }
20 return obj.listen_hc_;
21 }
22
23 /**
24 * Takes a node, event, listener, and capture flag to create a key
25 * to identify the tuple in the listeners hash.
26 *
27 * @param {Element} node The node to listen to events on.
28 * @param {string} event The name of the event without the "on" prefix.
29 * @param {Function} listener A function to call when the event occurs.
30 * @param {boolean} opt_useCapture In DOM-compliant browsers, this determines
31 * whether the listener is fired during the
32 * capture or bubble phase of the event.
33 * @return {string} key to identify this tuple in the listeners hash.
34 */
35 function createKey_(node, event, listener, opt_useCapture) {
36 let nodeHc = getHashCode_(node);
37 let listenerHc = getHashCode_(listener);
38 opt_useCapture = !!opt_useCapture;
39 let key = nodeHc + '_' + event + '_' + listenerHc + '_' + opt_useCapture;
40 return key;
41 }
42
43 /**
44 * Adds an event listener to a DOM node for a specific event.
45 *
46 * Listen() and unlisten() use an indirect lookup of listener functions
47 * to avoid circular references between DOM (in IE) or XPCOM (in Mozilla)
48 * objects which leak memory. This makes it easier to write OO
49 * Javascript/DOM code.
50 *
51 * Examples:
52 * listen(myButton, 'click', myHandler, true);
53 * listen(myButton, 'click', this.myHandler.bind(this), true);
54 *
55 * @param {Element} node The node to listen to events on.
56 * @param {string} event The name of the event without the "on" prefix.
57 * @param {Function} listener A function to call when the event occurs.
58 * @param {boolean} opt_useCapture In DOM-compliant browsers, this determines
59 * whether the listener is fired during the
60 * capture or bubble phase of the event.
61 * @return {string} a unique key to indentify this listener.
62 */
63 listen = function(node, event, listener, opt_useCapture) {
64 let key = createKey_(node, event, listener, opt_useCapture);
65
66 // addEventListener does not allow multiple listeners
67 if (key in listeners) {
68 return key;
69 }
70
71 let proxy = handleEvent.bind(null, key);
72 listeners[key] = {
73 listener: listener,
74 proxy: proxy,
75 event: event,
76 node: node,
77 useCapture: opt_useCapture,
78 };
79
80 if (node.addEventListener) {
81 node.addEventListener(event, proxy, opt_useCapture);
82 } else if (node.attachEvent) {
83 node.attachEvent('on' + event, proxy);
84 } else {
85 throw new Error('Node {' + node + '} does not support event listeners.');
86 }
87
88 return key;
89 };
90
91 /**
92 * Removes an event listener which was added with listen().
93 *
94 * @param {Element} node The node to stop listening to events on.
95 * @param {string} event The name of the event without the "on" prefix.
96 * @param {Function} listener The listener function to remove.
97 * @param {boolean} opt_useCapture In DOM-compliant browsers, this determines
98 * whether the listener is fired during the
99 * capture or bubble phase of the event.
100 * @return {boolean} indicating whether the listener was there to remove.
101 */
102 unlisten = function(node, event, listener, opt_useCapture) {
103 let key = createKey_(node, event, listener, opt_useCapture);
104
105 return unlistenByKey(key);
106 };
107
108 /**
109 * Variant of {@link unlisten} that takes a key that was returned by
110 * {@link listen} and removes that listener.
111 *
112 * @param {string} key Key of event to be unlistened.
113 * @return {boolean} indicating whether it was there to be removed.
114 */
115 unlistenByKey = function(key) {
116 if (!(key in listeners)) {
117 return false;
118 }
119 let listener = listeners[key];
120 let proxy = listener.proxy;
121 let event = listener.event;
122 let node = listener.node;
123 let useCapture = listener.useCapture;
124
125 if (node.removeEventListener) {
126 node.removeEventListener(event, proxy, useCapture);
127 } else if (node.detachEvent) {
128 node.detachEvent('on' + event, proxy);
129 }
130
131 delete listeners[key];
132 return true;
133 };
134
135 /**
136 * The function which is actually called when the DOM event occurs. This
137 * function is a proxy for the real listener the user specified.
138 */
139 function handleEvent(key) {
140 // pass all arguments which were sent to this function except listenerID
141 // on to the actual listener.
142 let args = Array.prototype.splice.call(arguments, 1, arguments.length);
143 return listeners[key].listener.apply(null, args);
144 }
145})();