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 /** |
| 6 * @fileoverview This contains an implementation of the EventTarget interface |
| 7 * as defined by DOM Level 2 Events. |
| 8 */ |
| 9 |
5 cr.define('cr', function() { | 10 cr.define('cr', function() { |
6 | 11 |
7 // TODO(arv): object.handleEvent | 12 /** |
8 | 13 * Creates a new EventTarget. This class implements the DOM level 2 |
| 14 * EventTarget interface and can be used wherever those are used. |
| 15 * @constructor |
| 16 */ |
9 function EventTarget() { | 17 function EventTarget() { |
10 } | 18 } |
11 | 19 |
12 EventTarget.prototype = { | 20 EventTarget.prototype = { |
13 | 21 |
14 /** | 22 /** |
15 * Adds an event listener to the target. | 23 * Adds an event listener to the target. |
16 * @param {string} type The name of the event. | 24 * @param {string} type The name of the event. |
17 * @param {!Function|{handleEvent:Function}} handler The handler for the | 25 * @param {!Function|{handleEvent:Function}} handler The handler for the |
18 * event. This is called when the event is dispatched. | 26 * event. This is called when the event is dispatched. |
19 */ | 27 */ |
20 addEventListener: function(type, handler) { | 28 addEventListener: function(type, handler) { |
21 if (!this.listeners_) | 29 if (!this.listeners_) |
22 this.listeners_ = {__proto__: null}; | 30 this.listeners_ = Object.create(null); |
23 if (!(type in this.listeners_)) { | 31 if (!(type in this.listeners_)) { |
24 this.listeners_[type] = [handler]; | 32 this.listeners_[type] = [handler]; |
25 } else { | 33 } else { |
26 var handlers = this.listeners_[type]; | 34 var handlers = this.listeners_[type]; |
27 if (handlers.indexOf(handler) < 0) | 35 if (handlers.indexOf(handler) < 0) |
28 handlers.push(handler); | 36 handlers.push(handler); |
29 } | 37 } |
30 }, | 38 }, |
31 | 39 |
32 /** | 40 /** |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 95 |
88 return !prevented && event.returnValue; | 96 return !prevented && event.returnValue; |
89 } | 97 } |
90 }; | 98 }; |
91 | 99 |
92 // Export | 100 // Export |
93 return { | 101 return { |
94 EventTarget: EventTarget | 102 EventTarget: EventTarget |
95 }; | 103 }; |
96 }); | 104 }); |
OLD | NEW |