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 /** | 10 /** |
11 * @typedef {EventListener|function(!Event):*} | 11 * @typedef {EventListener|function(!Event):*} |
12 */ | 12 */ |
13 var EventListenerType; | 13 var EventListenerType; |
14 | 14 |
15 cr.define('cr', function() { | 15 cr.define('cr', function() { |
16 | 16 |
17 /** | 17 /** |
18 * Creates a new EventTarget. This class implements the DOM level 2 | 18 * Creates a new EventTarget. This class implements the DOM level 2 |
19 * EventTarget interface and can be used wherever those are used. | 19 * EventTarget interface and can be used wherever those are used. |
20 * @constructor | 20 * @constructor |
21 * @implements {EventTarget} | 21 * @implements {EventTarget} |
22 */ | 22 */ |
23 function EventTarget() {} | 23 function EventTarget() { |
| 24 } |
24 | 25 |
25 EventTarget.prototype = { | 26 EventTarget.prototype = { |
26 /** | 27 /** |
27 * Adds an event listener to the target. | 28 * Adds an event listener to the target. |
28 * @param {string} type The name of the event. | 29 * @param {string} type The name of the event. |
29 * @param {EventListenerType} handler The handler for the event. This is | 30 * @param {EventListenerType} handler The handler for the event. This is |
30 * called when the event is dispatched. | 31 * called when the event is dispatched. |
31 */ | 32 */ |
32 addEventListener: function(type, handler) { | 33 addEventListener: function(type, handler) { |
33 if (!this.listeners_) | 34 if (!this.listeners_) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 * @return {boolean} Whether the default action was prevented. If someone | 70 * @return {boolean} Whether the default action was prevented. If someone |
70 * calls preventDefault on the event object then this returns false. | 71 * calls preventDefault on the event object then this returns false. |
71 */ | 72 */ |
72 dispatchEvent: function(event) { | 73 dispatchEvent: function(event) { |
73 if (!this.listeners_) | 74 if (!this.listeners_) |
74 return true; | 75 return true; |
75 | 76 |
76 // Since we are using DOM Event objects we need to override some of the | 77 // Since we are using DOM Event objects we need to override some of the |
77 // properties and methods so that we can emulate this correctly. | 78 // properties and methods so that we can emulate this correctly. |
78 var self = this; | 79 var self = this; |
79 event.__defineGetter__('target', function() { return self; }); | 80 event.__defineGetter__('target', function() { |
| 81 return self; |
| 82 }); |
80 | 83 |
81 var type = event.type; | 84 var type = event.type; |
82 var prevented = 0; | 85 var prevented = 0; |
83 if (type in this.listeners_) { | 86 if (type in this.listeners_) { |
84 // Clone to prevent removal during dispatch | 87 // Clone to prevent removal during dispatch |
85 var handlers = this.listeners_[type].concat(); | 88 var handlers = this.listeners_[type].concat(); |
86 for (var i = 0, handler; handler = handlers[i]; i++) { | 89 for (var i = 0, handler; handler = handlers[i]; i++) { |
87 if (handler.handleEvent) | 90 if (handler.handleEvent) |
88 prevented |= handler.handleEvent.call(handler, event) === false; | 91 prevented |= handler.handleEvent.call(handler, event) === false; |
89 else | 92 else |
90 prevented |= handler.call(this, event) === false; | 93 prevented |= handler.call(this, event) === false; |
91 } | 94 } |
92 } | 95 } |
93 | 96 |
94 return !prevented && !event.defaultPrevented; | 97 return !prevented && !event.defaultPrevented; |
95 } | 98 } |
96 }; | 99 }; |
97 | 100 |
98 // Export | 101 // Export |
99 return {EventTarget: EventTarget}; | 102 return {EventTarget: EventTarget}; |
100 }); | 103 }); |
OLD | NEW |