Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** @fileoverview EventTracker is a simple class that manages the addition and | 5 /** |
| 6 * removal of DOM event listeners. In particular, it keeps track of all | 6 * @fileoverview EventTracker is a simple class that manages the addition and |
| 7 * listeners that have been added and makes it easy to remove some or all of | 7 * removal of DOM event listeners. In particular, it keeps track of all |
| 8 * them without requiring all the information again. This is particularly | 8 * listeners that have been added and makes it easy to remove some or all of |
| 9 * handy when the listener is a generated function such as a lambda or the | 9 * them without requiring all the information again. This is particularly handy |
| 10 * result of calling Function.bind. | 10 * when the listener is a generated function such as a lambda or the result of |
| 11 * calling Function.bind. | |
| 11 */ | 12 */ |
| 12 | 13 |
| 14 /** | |
| 15 * The type of the internal tracking entry. TODO(dbeam): move this back to | |
| 16 * EventTracker.Entry when https://github.com/google/closure-compiler/issues/544 | |
| 17 * is fixed. | |
| 18 * @typedef {{node: !Node, | |
| 19 * eventType: string, | |
| 20 * listener: Function, | |
| 21 * capture: boolean}} | |
| 22 */ | |
| 23 var EventTrackerEntry; | |
| 24 | |
| 13 // Use an anonymous function to enable strict mode just for this file (which | 25 // Use an anonymous function to enable strict mode just for this file (which |
| 14 // will be concatenated with other files when embedded in Chrome) | 26 // will be concatenated with other files when embedded in Chrome). |
| 15 var EventTracker = (function() { | 27 var EventTracker = (function() { |
| 16 'use strict'; | 28 'use strict'; |
| 17 | 29 |
| 18 /** | 30 /** |
| 19 * Create an EventTracker to track a set of events. | 31 * Create an EventTracker to track a set of events. |
| 20 * EventTracker instances are typically tied 1:1 with other objects or | 32 * EventTracker instances are typically tied 1:1 with other objects or |
| 21 * DOM elements whose listeners should be removed when the object is disposed | 33 * DOM elements whose listeners should be removed when the object is disposed |
| 22 * or the corresponding elements are removed from the DOM. | 34 * or the corresponding elements are removed from the DOM. |
| 23 * @constructor | 35 * @constructor |
| 24 */ | 36 */ |
| 25 function EventTracker() { | 37 function EventTracker() { |
| 26 /** | 38 /** |
| 27 * @type {Array.<EventTracker.Entry>} | 39 * @type {Array.<EventTrackerEntry>} |
| 28 * @private | 40 * @private |
| 29 */ | 41 */ |
| 30 this.listeners_ = []; | 42 this.listeners_ = []; |
| 31 } | 43 } |
| 32 | 44 |
| 33 /** | |
| 34 * The type of the internal tracking entry. | |
| 35 * @typedef {{node: !Node, | |
| 36 * eventType: string, | |
| 37 * listener: Function, | |
| 38 * capture: boolean}} | |
| 39 */ | |
| 40 EventTracker.Entry; | |
| 41 | |
| 42 EventTracker.prototype = { | 45 EventTracker.prototype = { |
| 43 /** | 46 /** |
| 44 * Add an event listener - replacement for Node.addEventListener. | 47 * Add an event listener - replacement for Node.addEventListener. |
| 45 * @param {!Node} node The DOM node to add a listener to. | 48 * @param {!Node} node The DOM node to add a listener to. |
| 46 * @param {string} eventType The type of event to subscribe to. | 49 * @param {string} eventType The type of event to subscribe to. |
| 47 * @param {Function} listener The listener to add. | 50 * @param {Function} listener The listener to add. |
| 48 * @param {boolean} capture Whether to invoke during the capture phase. | 51 * @param {boolean} capture Whether to invoke during the capture phase. |
| 49 */ | 52 */ |
| 50 add: function(node, eventType, listener, capture) { | 53 add: function(node, eventType, listener, capture) { |
| 51 var h = { | 54 var h = { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 72 return true; | 75 return true; |
| 73 }); | 76 }); |
| 74 }, | 77 }, |
| 75 | 78 |
| 76 /** | 79 /** |
| 77 * Remove all event listeners added with this EventTracker. | 80 * Remove all event listeners added with this EventTracker. |
| 78 */ | 81 */ |
| 79 removeAll: function() { | 82 removeAll: function() { |
| 80 this.listeners_.forEach(EventTracker.removeEventListener_); | 83 this.listeners_.forEach(EventTracker.removeEventListener_); |
| 81 this.listeners_ = []; | 84 this.listeners_ = []; |
| 82 } | 85 }, |
|
arv (Not doing code reviews)
2014/07/16 18:33:03
useless change
Dan Beam
2014/07/19 02:28:40
Done.
| |
| 83 }; | 86 }; |
| 84 | 87 |
| 85 /** | 88 /** |
| 86 * Remove a single event listener given it's tracker entry. It's up to the | 89 * Remove a single event listener given it's tracking entry. It's up to the |
| 87 * caller to ensure the entry is removed from listeners_. | 90 * caller to ensure the entry is removed from listeners_. |
| 88 * @param {EventTracker.Entry} h The entry describing the listener to remove. | 91 * @param {EventTrackerEntry} h The entry describing the listener to remove. |
| 89 * @private | 92 * @private |
| 90 */ | 93 */ |
| 91 EventTracker.removeEventListener_ = function(h) { | 94 EventTracker.removeEventListener_ = function(h) { |
| 92 h.node.removeEventListener(h.eventType, h.listener, h.capture); | 95 h.node.removeEventListener(h.eventType, h.listener, h.capture); |
| 93 }; | 96 }; |
| 94 | 97 |
| 95 return EventTracker; | 98 return EventTracker; |
| 96 })(); | 99 })(); |
| 97 | 100 |
| OLD | NEW |