OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('cr', function() { |
| 6 |
| 7 // TODO(arv): object.handleEvent |
| 8 |
| 9 function EventTarget() { |
| 10 } |
| 11 |
| 12 EventTarget.prototype = { |
| 13 |
| 14 /** |
| 15 * Adds an event listener to the target. |
| 16 * @param {string} type The name of the event. |
| 17 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| 18 * event. This is called when the event is dispatched. |
| 19 */ |
| 20 addEventListener: function(type, handler) { |
| 21 if (!this.listeners_) |
| 22 this.listeners_ = {__proto__: null}; |
| 23 if (!(type in this.listeners_)) { |
| 24 this.listeners_[type] = [handler]; |
| 25 } else { |
| 26 var handlers = this.listeners_[type]; |
| 27 if (handlers.indexOf(handler) < 0) |
| 28 handlers.push(handler); |
| 29 } |
| 30 }, |
| 31 |
| 32 /** |
| 33 * Removes an event listener from the target. |
| 34 * @param {string} type The name of the event. |
| 35 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| 36 * event. |
| 37 */ |
| 38 removeEventListener: function(type, handler) { |
| 39 if (!this.listeners_) |
| 40 return; |
| 41 if (type in this.listeners_) { |
| 42 var handlers = this.listeners_[type]; |
| 43 var index = handlers.indexOf(handler); |
| 44 if (index >= 0) { |
| 45 // Clean up if this was the last listener. |
| 46 if (handlers.length == 1) |
| 47 delete this.listeners_[type]; |
| 48 else |
| 49 handlers.splice(index, 1); |
| 50 } |
| 51 } |
| 52 }, |
| 53 |
| 54 /** |
| 55 * Dispatches an event and calls all the listeners that are listening to |
| 56 * the type of the event. |
| 57 * @param {!cr.event.Event} event The event to dispatch. |
| 58 * @return {boolean} Whether the default action was prevented. If someone |
| 59 * calls preventDefault on the event object then this returns false. |
| 60 */ |
| 61 dispatchEvent: function(event) { |
| 62 if (!this.listeners_) |
| 63 return true; |
| 64 |
| 65 // Since we are using DOM Event objects we need to override some of the |
| 66 // properties and methods so that we can emulate this correctly. |
| 67 var self = this; |
| 68 event.__defineGetter__('target', function() { |
| 69 return self; |
| 70 }); |
| 71 event.preventDefault = function() { |
| 72 this.returnValue = false; |
| 73 }; |
| 74 |
| 75 var type = event.type; |
| 76 var prevented = 0; |
| 77 if (type in this.listeners_) { |
| 78 // Clone to prevent removal during dispatch |
| 79 var handlers = this.listeners_[type].concat(); |
| 80 for (var i = 0, handler; handler = handlers[i]; i++) { |
| 81 if (handler.handleEvent) |
| 82 prevented |= handler.handleEvent.call(handler, event) === false; |
| 83 else |
| 84 prevented |= handler.call(this, event) === false; |
| 85 } |
| 86 } |
| 87 |
| 88 return !prevented && event.returnValue; |
| 89 } |
| 90 }; |
| 91 |
| 92 // Export |
| 93 return { |
| 94 EventTarget: EventTarget |
| 95 }; |
| 96 }); |
OLD | NEW |