| 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 /** | |
| 11 * @typedef {EventListener|function(!Event):(boolean|undefined)} | |
| 12 */ | |
| 13 var EventListenerType; | |
| 14 | |
| 15 cr.define('cr', function() { | 10 cr.define('cr', function() { |
| 16 | 11 |
| 17 /** | 12 /** |
| 18 * Creates a new EventTarget. This class implements the DOM level 2 | 13 * Creates a new EventTarget. This class implements the DOM level 2 |
| 19 * EventTarget interface and can be used wherever those are used. | 14 * EventTarget interface and can be used wherever those are used. |
| 20 * @constructor | 15 * @constructor |
| 21 * @implements {EventTarget} | 16 * @implements {EventTarget} |
| 22 */ | 17 */ |
| 23 function EventTarget() { | 18 function EventTarget() { |
| 24 } | 19 } |
| 25 | 20 |
| 26 EventTarget.prototype = { | 21 EventTarget.prototype = { |
| 27 | 22 |
| 28 /** | 23 /** |
| 29 * Adds an event listener to the target. | 24 * Adds an event listener to the target. |
| 30 * @param {string} type The name of the event. | 25 * @param {string} type The name of the event. |
| 31 * @param {EventListenerType} handler The handler for the event. This is | 26 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| 32 * called when the event is dispatched. | 27 * event. This is called when the event is dispatched. |
| 33 */ | 28 */ |
| 34 addEventListener: function(type, handler) { | 29 addEventListener: function(type, handler) { |
| 35 if (!this.listeners_) | 30 if (!this.listeners_) |
| 36 this.listeners_ = Object.create(null); | 31 this.listeners_ = Object.create(null); |
| 37 if (!(type in this.listeners_)) { | 32 if (!(type in this.listeners_)) { |
| 38 this.listeners_[type] = [handler]; | 33 this.listeners_[type] = [handler]; |
| 39 } else { | 34 } else { |
| 40 var handlers = this.listeners_[type]; | 35 var handlers = this.listeners_[type]; |
| 41 if (handlers.indexOf(handler) < 0) | 36 if (handlers.indexOf(handler) < 0) |
| 42 handlers.push(handler); | 37 handlers.push(handler); |
| 43 } | 38 } |
| 44 }, | 39 }, |
| 45 | 40 |
| 46 /** | 41 /** |
| 47 * Removes an event listener from the target. | 42 * Removes an event listener from the target. |
| 48 * @param {string} type The name of the event. | 43 * @param {string} type The name of the event. |
| 49 * @param {EventListenerType} handler The handler for the event. | 44 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| 45 * event. |
| 50 */ | 46 */ |
| 51 removeEventListener: function(type, handler) { | 47 removeEventListener: function(type, handler) { |
| 52 if (!this.listeners_) | 48 if (!this.listeners_) |
| 53 return; | 49 return; |
| 54 if (type in this.listeners_) { | 50 if (type in this.listeners_) { |
| 55 var handlers = this.listeners_[type]; | 51 var handlers = this.listeners_[type]; |
| 56 var index = handlers.indexOf(handler); | 52 var index = handlers.indexOf(handler); |
| 57 if (index >= 0) { | 53 if (index >= 0) { |
| 58 // Clean up if this was the last listener. | 54 // Clean up if this was the last listener. |
| 59 if (handlers.length == 1) | 55 if (handlers.length == 1) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 93 |
| 98 return !prevented && !event.defaultPrevented; | 94 return !prevented && !event.defaultPrevented; |
| 99 } | 95 } |
| 100 }; | 96 }; |
| 101 | 97 |
| 102 // Export | 98 // Export |
| 103 return { | 99 return { |
| 104 EventTarget: EventTarget | 100 EventTarget: EventTarget |
| 105 }; | 101 }; |
| 106 }); | 102 }); |
| OLD | NEW |