Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: appengine/monorail/static/js/graveyard/listen.js

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /* 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
8 var listen;
9 var unlisten;
10 var unlistenByKey;
11
12 (function() {
13 var listeners = {};
14 var 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 var nodeHc = getHashCode_(node);
37 var listenerHc = getHashCode_(listener);
38 opt_useCapture = !!opt_useCapture;
39 var 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 var 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 var 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 var 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 var listener = listeners[key];
120 var proxy = listener.proxy;
121 var event = listener.event;
122 var node = listener.node;
123 var 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 var args = Array.prototype.splice.call(arguments, 1, arguments.length);
143 return listeners[key].listener.apply(null, args);
144 }
145
146 })();
OLDNEW
« no previous file with comments | « appengine/monorail/static/js/graveyard/geom.js ('k') | appengine/monorail/static/js/graveyard/popup_controller.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698