1 // Copyright (c) 2012 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.
6 * @fileoverview This contains an implementation of the EventTarget interface
7 * as defined by DOM Level 2 Events.
9 base.exportTo('ui', function() {
12 * Creates a new EventTarget. This class implements the DOM level 2
13 * EventTarget interface and can be used wherever those are used.
16 function EventTarget() {
19 EventTarget.prototype = {
22 * Adds an event listener to the target.
23 * @param {string} type The name of the event.
24 * @param {!Function|{handleEvent:Function}} handler The handler for the
25 * event. This is called when the event is dispatched.
27 addEventListener: function(type, handler) {
29 this.listeners_ = Object.create(null);
30 if (!(type in this.listeners_)) {
31 this.listeners_[type] = [handler];
33 var handlers = this.listeners_[type];
34 if (handlers.indexOf(handler) < 0)
35 handlers.push(handler);
40 * Removes an event listener from the target.
41 * @param {string} type The name of the event.
42 * @param {!Function|{handleEvent:Function}} handler The handler for the
45 removeEventListener: function(type, handler) {
48 if (type in this.listeners_) {
49 var handlers = this.listeners_[type];
50 var index = handlers.indexOf(handler);
52 // Clean up if this was the last listener.
53 if (handlers.length == 1)
54 delete this.listeners_[type];
56 handlers.splice(index, 1);
62 * Dispatches an event and calls all the listeners that are listening to
63 * the type of the event.
64 * @param {!cr.event.Event} event The event to dispatch.
65 * @return {boolean} Whether the default action was prevented. If someone
66 * calls preventDefault on the event object then this returns false.
68 dispatchEvent: function(event) {
72 // Since we are using DOM Event objects we need to override some of the
73 // properties and methods so that we can emulate this correctly.
75 event.__defineGetter__('target', function() {
78 event.preventDefault = function() {
79 this.returnValue = false;
82 var type = event.type;
84 if (type in this.listeners_) {
85 // Clone to prevent removal during dispatch
86 var handlers = this.listeners_[type].concat();
87 for (var i = 0, handler; handler = handlers[i]; i++) {
88 if (handler.handleEvent)
89 prevented |= handler.handleEvent.call(handler, event) === false;
91 prevented |= handler.call(this, event) === false;
95 return !prevented && event.returnValue;
101 EventTarget: EventTarget