2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * @implements {WebInspector.EventTarget}
30 WebInspector.Object = function() {
33 WebInspector.Object.prototype = {
35 * @param {string} eventType
36 * @param {function(!WebInspector.Event)} listener
37 * @param {!Object=} thisObject
39 addEventListener: function(eventType, listener, thisObject)
42 console.assert(false);
45 this._listeners = new Map();
46 if (!this._listeners.has(eventType))
47 this._listeners.set(eventType, []);
48 this._listeners.get(eventType).push({ thisObject: thisObject, listener: listener });
52 * @param {string} eventType
53 * @param {function(!WebInspector.Event)} listener
54 * @param {!Object=} thisObject
56 removeEventListener: function(eventType, listener, thisObject)
58 console.assert(listener);
60 if (!this._listeners || !this._listeners.has(eventType))
62 var listeners = this._listeners.get(eventType);
63 for (var i = 0; i < listeners.length; ++i) {
64 if (listeners[i].listener === listener && listeners[i].thisObject === thisObject)
65 listeners.splice(i--, 1);
68 if (!listeners.length)
69 this._listeners.delete(eventType);
72 removeAllListeners: function()
74 delete this._listeners;
78 * @param {string} eventType
81 hasEventListeners: function(eventType)
83 if (!this._listeners || !this._listeners.has(eventType))
89 * @param {string} eventType
90 * @param {*=} eventData
93 dispatchEventToListeners: function(eventType, eventData)
95 if (!this._listeners || !this._listeners.has(eventType))
98 var event = new WebInspector.Event(this, eventType, eventData);
99 var listeners = this._listeners.get(eventType).slice(0);
100 for (var i = 0; i < listeners.length; ++i) {
101 listeners[i].listener.call(listeners[i].thisObject, event);
102 if (event._stoppedPropagation)
106 return event.defaultPrevented;
112 * @param {!WebInspector.EventTarget} target
113 * @param {string} type
116 WebInspector.Event = function(target, type, data)
118 this.target = target;
121 this.defaultPrevented = false;
122 this._stoppedPropagation = false;
125 WebInspector.Event.prototype = {
126 stopPropagation: function()
128 this._stoppedPropagation = true;
131 preventDefault: function()
133 this.defaultPrevented = true;
137 * @param {boolean=} preventDefault
139 consume: function(preventDefault)
141 this.stopPropagation();
143 this.preventDefault();
150 WebInspector.EventTarget = function()
154 WebInspector.EventTarget.prototype = {
156 * @param {string} eventType
157 * @param {function(!WebInspector.Event)} listener
158 * @param {!Object=} thisObject
160 addEventListener: function(eventType, listener, thisObject) { },
163 * @param {string} eventType
164 * @param {function(!WebInspector.Event)} listener
165 * @param {!Object=} thisObject
167 removeEventListener: function(eventType, listener, thisObject) { },
169 removeAllListeners: function() { },
172 * @param {string} eventType
175 hasEventListeners: function(eventType) { },
178 * @param {string} eventType
179 * @param {*=} eventData
182 dispatchEventToListeners: function(eventType, eventData) { },