Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 /** | 5 /** |
| 6 * @fileoverview This contains an implementation of the EventTarget interface | 6 * @fileoverview This contains an implementation of the EventTarget interface |
| 7 * as defined by DOM Level 2 Events. | 7 * as defined by DOM Level 2 Events. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('cr', function() { | 10 cr.define('cr', function() { |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Creates a new EventTarget. This class implements the DOM level 2 | 13 * Creates a new EventTarget. This class implements the DOM level 2 |
| 14 * EventTarget interface and can be used wherever those are used. | 14 * EventTarget interface and can be used wherever those are used. |
| 15 * @constructor | 15 * @constructor |
| 16 * @implements {EventTarget} | 16 * @implements {EventTarget} |
| 17 */ | 17 */ |
| 18 function EventTarget() { | 18 function EventTarget() { |
| 19 } | 19 } |
| 20 | 20 |
| 21 EventTarget.prototype = { | 21 EventTarget.prototype = { |
| 22 | 22 |
|
Dan Beam
2014/09/29 18:42:00
nit: remove \n here
Vitaly Pavlenko
2014/10/01 00:33:38
Done.
| |
| 23 /** | 23 /** |
| 24 * Adds an event listener to the target. | 24 * Adds an event listener to the target. |
| 25 * @param {string} type The name of the event. | 25 * @param {string} type The name of the event. |
| 26 * @param {!Function|{handleEvent:Function}} handler The handler for the | 26 * @param {EventListener|function(!Event):(boolean|undefined)} handler The |
|
Dan Beam
2014/09/29 18:42:00
nit: typedef
Dan Beam
2014/09/29 18:42:01
why boolean|undefined rather than just *? a metho
Vitaly Pavlenko
2014/10/01 00:33:37
Done.
Vitaly Pavlenko
2014/10/01 00:33:38
Done.
| |
| 27 * event. This is called when the event is dispatched. | 27 * handler for the event. This is called when the event is dispatched. |
| 28 */ | 28 */ |
| 29 addEventListener: function(type, handler) { | 29 addEventListener: function(type, handler) { |
| 30 if (!this.listeners_) | 30 if (!this.listeners_) |
| 31 this.listeners_ = Object.create(null); | 31 this.listeners_ = Object.create(null); |
| 32 if (!(type in this.listeners_)) { | 32 if (!(type in this.listeners_)) { |
| 33 this.listeners_[type] = [handler]; | 33 this.listeners_[type] = [handler]; |
| 34 } else { | 34 } else { |
| 35 var handlers = this.listeners_[type]; | 35 var handlers = this.listeners_[type]; |
| 36 if (handlers.indexOf(handler) < 0) | 36 if (handlers.indexOf(handler) < 0) |
| 37 handlers.push(handler); | 37 handlers.push(handler); |
| 38 } | 38 } |
| 39 }, | 39 }, |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Removes an event listener from the target. | 42 * Removes an event listener from the target. |
| 43 * @param {string} type The name of the event. | 43 * @param {string} type The name of the event. |
| 44 * @param {!Function|{handleEvent:Function}} handler The handler for the | 44 * @param {EventListener|function(!Event):(boolean|undefined)} handler The |
| 45 * event. | 45 * handler for the event. |
| 46 */ | 46 */ |
| 47 removeEventListener: function(type, handler) { | 47 removeEventListener: function(type, handler) { |
| 48 if (!this.listeners_) | 48 if (!this.listeners_) |
| 49 return; | 49 return; |
| 50 if (type in this.listeners_) { | 50 if (type in this.listeners_) { |
| 51 var handlers = this.listeners_[type]; | 51 var handlers = this.listeners_[type]; |
| 52 var index = handlers.indexOf(handler); | 52 var index = handlers.indexOf(handler); |
| 53 if (index >= 0) { | 53 if (index >= 0) { |
| 54 // Clean up if this was the last listener. | 54 // Clean up if this was the last listener. |
| 55 if (handlers.length == 1) | 55 if (handlers.length == 1) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 | 93 |
| 94 return !prevented && !event.defaultPrevented; | 94 return !prevented && !event.defaultPrevented; |
| 95 } | 95 } |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 // Export | 98 // Export |
| 99 return { | 99 return { |
| 100 EventTarget: EventTarget | 100 EventTarget: EventTarget |
| 101 }; | 101 }; |
| 102 }); | 102 }); |
| OLD | NEW |