Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Object.js
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  */
30 WebInspector.Object = function() {
31 }
32
33 WebInspector.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         if (!listener)
42             console.assert(false);
43
44         if (!this._listeners)
45             this._listeners = {};
46         if (!this._listeners[eventType])
47             this._listeners[eventType] = [];
48         this._listeners[eventType].push({ thisObject: thisObject, listener: listener });
49     },
50
51     /**
52      * @param {string} eventType
53      * @param {function(!WebInspector.Event)} listener
54      * @param {!Object=} thisObject
55      */
56     removeEventListener: function(eventType, listener, thisObject)
57     {
58         console.assert(listener);
59
60         if (!this._listeners || !this._listeners[eventType])
61             return;
62         var listeners = this._listeners[eventType];
63         for (var i = 0; i < listeners.length; ++i) {
64             if (listener && listeners[i].listener === listener && listeners[i].thisObject === thisObject)
65                 listeners.splice(i, 1);
66             else if (!listener && thisObject && listeners[i].thisObject === thisObject)
67                 listeners.splice(i, 1);
68         }
69
70         if (!listeners.length)
71             delete this._listeners[eventType];
72     },
73
74     removeAllListeners: function()
75     {
76         delete this._listeners;
77     },
78
79     /**
80      * @param {string} eventType
81      * @return {boolean}
82      */
83     hasEventListeners: function(eventType)
84     {
85         if (!this._listeners || !this._listeners[eventType])
86             return false;
87         return true;
88     },
89
90     /**
91      * @param {string} eventType
92      * @param {*=} eventData
93      * @return {boolean}
94      */
95     dispatchEventToListeners: function(eventType, eventData)
96     {
97         if (!this._listeners || !this._listeners[eventType])
98             return false;
99
100         var event = new WebInspector.Event(this, eventType, eventData);
101         var listeners = this._listeners[eventType].slice(0);
102         for (var i = 0; i < listeners.length; ++i) {
103             listeners[i].listener.call(listeners[i].thisObject, event);
104             if (event._stoppedPropagation)
105                 break;
106         }
107
108         return event.defaultPrevented;
109     }
110 }
111
112 /**
113  * @constructor
114  * @param {!WebInspector.EventTarget} target
115  * @param {string} type
116  * @param {*=} data
117  */
118 WebInspector.Event = function(target, type, data)
119 {
120     this.target = target;
121     this.type = type;
122     this.data = data;
123     this.defaultPrevented = false;
124     this._stoppedPropagation = false;
125 }
126
127 WebInspector.Event.prototype = {
128     stopPropagation: function()
129     {
130         this._stoppedPropagation = true;
131     },
132
133     preventDefault: function()
134     {
135         this.defaultPrevented = true;
136     },
137
138     /**
139      * @param {boolean=} preventDefault
140      */
141     consume: function(preventDefault)
142     {
143         this.stopPropagation();
144         if (preventDefault)
145             this.preventDefault();
146     }
147 }
148
149 /**
150  * @interface
151  */
152 WebInspector.EventTarget = function()
153 {
154 }
155
156 WebInspector.EventTarget.prototype = {
157     /**
158      * @param {string} eventType
159      * @param {function(!WebInspector.Event)} listener
160      * @param {!Object=} thisObject
161      */
162     addEventListener: function(eventType, listener, thisObject) { },
163
164     /**
165      * @param {string} eventType
166      * @param {function(!WebInspector.Event)} listener
167      * @param {!Object=} thisObject
168      */
169     removeEventListener: function(eventType, listener, thisObject) { },
170
171     removeAllListeners: function() { },
172
173     /**
174      * @param {string} eventType
175      * @return {boolean}
176      */
177     hasEventListeners: function(eventType) { },
178
179     /**
180      * @param {string} eventType
181      * @param {*=} eventData
182      * @return {boolean}
183      */
184     dispatchEventToListeners: function(eventType, eventData) { },
185 }