1/* 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26/** 27 * @constructor 28 * @implements {WebInspector.EventTarget} 29 */ 30WebInspector.Object = function() { 31} 32 33WebInspector.Object.prototype = { 34 /** 35 * @param {string} eventType 36 * @param {function(WebInspector.Event)} listener 37 * @param {Object=} thisObject 38 */ 39 addEventListener: function(eventType, listener, thisObject) 40 { 41 console.assert(listener); 42 43 if (!this._listeners) 44 this._listeners = {}; 45 if (!this._listeners[eventType]) 46 this._listeners[eventType] = []; 47 this._listeners[eventType].push({ thisObject: thisObject, listener: listener }); 48 }, 49 50 /** 51 * @param {string} eventType 52 * @param {function(WebInspector.Event)} listener 53 * @param {Object=} thisObject 54 */ 55 removeEventListener: function(eventType, listener, thisObject) 56 { 57 console.assert(listener); 58 59 if (!this._listeners || !this._listeners[eventType]) 60 return; 61 var listeners = this._listeners[eventType]; 62 for (var i = 0; i < listeners.length; ++i) { 63 if (listener && listeners[i].listener === listener && listeners[i].thisObject === thisObject) 64 listeners.splice(i, 1); 65 else if (!listener && thisObject && listeners[i].thisObject === thisObject) 66 listeners.splice(i, 1); 67 } 68 69 if (!listeners.length) 70 delete this._listeners[eventType]; 71 }, 72 73 removeAllListeners: function() 74 { 75 delete this._listeners; 76 }, 77 78 /** 79 * @param {string} eventType 80 * @return {boolean} 81 */ 82 hasEventListeners: function(eventType) 83 { 84 if (!this._listeners || !this._listeners[eventType]) 85 return false; 86 return true; 87 }, 88 89 /** 90 * @param {string} eventType 91 * @param {*=} eventData 92 * @return {boolean} 93 */ 94 dispatchEventToListeners: function(eventType, eventData) 95 { 96 if (!this._listeners || !this._listeners[eventType]) 97 return false; 98 99 var event = new WebInspector.Event(this, eventType, eventData); 100 var listeners = this._listeners[eventType].slice(0); 101 for (var i = 0; i < listeners.length; ++i) { 102 listeners[i].listener.call(listeners[i].thisObject, event); 103 if (event._stoppedPropagation) 104 break; 105 } 106 107 return event.defaultPrevented; 108 } 109} 110 111/** 112 * @constructor 113 * @param {WebInspector.EventTarget} target 114 * @param {string} type 115 * @param {*=} data 116 */ 117WebInspector.Event = function(target, type, data) 118{ 119 this.target = target; 120 this.type = type; 121 this.data = data; 122 this.defaultPrevented = false; 123 this._stoppedPropagation = false; 124} 125 126WebInspector.Event.prototype = { 127 stopPropagation: function() 128 { 129 this._stoppedPropagation = true; 130 }, 131 132 preventDefault: function() 133 { 134 this.defaultPrevented = true; 135 }, 136 137 /** 138 * @param {boolean=} preventDefault 139 */ 140 consume: function(preventDefault) 141 { 142 this.stopPropagation(); 143 if (preventDefault) 144 this.preventDefault(); 145 } 146} 147 148/** 149 * @interface 150 */ 151WebInspector.EventTarget = function() 152{ 153} 154 155WebInspector.EventTarget.prototype = { 156 /** 157 * @param {string} eventType 158 * @param {function(WebInspector.Event)} listener 159 * @param {Object=} thisObject 160 */ 161 addEventListener: function(eventType, listener, thisObject) { }, 162 163 /** 164 * @param {string} eventType 165 * @param {function(WebInspector.Event)} listener 166 * @param {Object=} thisObject 167 */ 168 removeEventListener: function(eventType, listener, thisObject) { }, 169 170 removeAllListeners: function() { }, 171 172 /** 173 * @param {string} eventType 174 * @return {boolean} 175 */ 176 hasEventListeners: function(eventType) { }, 177 178 /** 179 * @param {string} eventType 180 * @param {*=} eventData 181 * @return {boolean} 182 */ 183 dispatchEventToListeners: function(eventType, eventData) { }, 184} 185 186WebInspector.notifications = new WebInspector.Object(); 187