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