3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm.html">
12 * @fileoverview This contains an implementation of the EventTarget interface
13 * as defined by DOM Level 2 Events.
15 tvcm.exportTo('tvcm', function() {
18 * Creates a new EventTarget. This class implements the DOM level 2
19 * EventTarget interface and can be used wherever those are used.
22 function EventTarget() {
25 EventTarget.prototype = {
28 * Adds an event listener to the target.
29 * @param {string} type The name of the event.
30 * @param {!Function|{handleEvent:Function}} handler The handler for the
31 * event. This is called when the event is dispatched.
33 addEventListener: function(type, handler) {
35 this.listeners_ = Object.create(null);
36 if (!(type in this.listeners_)) {
37 this.listeners_[type] = [handler];
39 var handlers = this.listeners_[type];
40 if (handlers.indexOf(handler) < 0)
41 handlers.push(handler);
46 * Removes an event listener from the target.
47 * @param {string} type The name of the event.
48 * @param {!Function|{handleEvent:Function}} handler The handler for the
51 removeEventListener: function(type, handler) {
54 if (type in this.listeners_) {
55 var handlers = this.listeners_[type];
56 var index = handlers.indexOf(handler);
58 // Clean up if this was the last listener.
59 if (handlers.length == 1)
60 delete this.listeners_[type];
62 handlers.splice(index, 1);
68 * Dispatches an event and calls all the listeners that are listening to
69 * the type of the event.
70 * @param {!cr.event.Event} event The event to dispatch.
71 * @return {boolean} Whether the default action was prevented. If someone
72 * calls preventDefault on the event object then this returns false.
74 dispatchEvent: function(event) {
78 // Since we are using DOM Event objects we need to override some of the
79 // properties and methods so that we can emulate this correctly.
81 event.__defineGetter__('target', function() {
84 var realPreventDefault = event.preventDefault;
85 event.preventDefault = function() {
86 realPreventDefault.call(this);
87 this.rawReturnValue = false;
90 var type = event.type;
92 if (type in this.listeners_) {
93 // Clone to prevent removal during dispatch
94 var handlers = this.listeners_[type].concat();
95 for (var i = 0, handler; handler = handlers[i]; i++) {
96 if (handler.handleEvent)
97 prevented |= handler.handleEvent.call(handler, event) === false;
99 prevented |= handler.call(this, event) === false;
103 return !prevented && event.rawReturnValue;
106 hasEventListener: function(type) {
107 return this.listeners_[type] !== undefined;
111 var EventTargetHelper = {
112 decorate: function(target) {
113 for (var k in EventTargetHelper) {
116 var v = EventTargetHelper[k];
117 if (typeof v !== 'function')
121 target.listenerCounts_ = {};
124 addEventListener: function(type, listener, useCapture) {
125 this.__proto__.addEventListener.call(
126 this, type, listener, useCapture);
127 if (this.listenerCounts_[type] === undefined)
128 this.listenerCounts_[type] = 0;
129 this.listenerCounts_[type]++;
132 removeEventListener: function(type, listener, useCapture) {
133 this.__proto__.removeEventListener.call(
134 this, type, listener, useCapture);
135 this.listenerCounts_[type]--;
138 hasEventListener: function(type) {
139 return this.listenerCounts_[type] > 0;
145 EventTarget: EventTarget,
146 EventTargetHelper: EventTargetHelper