tizen beta release
[profile/ivi/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / ExtensionPanel.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.View}
34  * @param {string} id
35  * @param {Element} parent
36  * @param {string} src
37  * @param {string} className
38  */
39 WebInspector.ExtensionView = function(id, parent, src, className)
40 {
41     WebInspector.View.call(this);
42
43     this._id = id;
44     this._iframe = document.createElement("iframe");
45     this._iframe.addEventListener("load", this._onLoad.bind(this), false);
46     this._iframe.src = src;
47     this._iframe.className = className;
48
49     this.element.appendChild(this._iframe);
50     this.show(parent);
51 }
52
53 WebInspector.ExtensionView.prototype = {
54     wasShown: function()
55     {
56         if (typeof this._frameIndex === "number")
57             WebInspector.extensionServer.notifyViewShown(this._id, this._frameIndex);
58     },
59
60     willHide: function()
61     {
62         if (typeof this._frameIndex === "number")
63             WebInspector.extensionServer.notifyViewHidden(this._id);
64     },
65
66     _onLoad: function()
67     {
68         this._frameIndex = Array.prototype.indexOf.call(window.frames, this._iframe.contentWindow);
69         if (this.isShowing())
70             WebInspector.extensionServer.notifyViewShown(this._id, this._frameIndex);
71     }
72 }
73
74 WebInspector.ExtensionView.prototype.__proto__ = WebInspector.View.prototype;
75
76 /**
77  * @constructor
78  * @extends {WebInspector.View}
79  * @param {string} id
80  */
81 WebInspector.ExtensionNotifierView = function(id)
82 {
83     WebInspector.View.call(this);
84
85     this._id = id;
86 }
87
88 WebInspector.ExtensionNotifierView.prototype = {
89     wasShown: function()
90     {
91         WebInspector.extensionServer.notifyViewShown(this._id);
92     },
93
94     willHide: function()
95     {
96         WebInspector.extensionServer.notifyViewHidden(this._id);
97     }
98 }
99
100 WebInspector.ExtensionNotifierView.prototype.__proto__ = WebInspector.View.prototype;
101
102 /**
103  * @constructor
104  * @extends {WebInspector.Panel}
105  * @param {string} id
106  * @param {string} label
107  * @param {string} iconURL
108  */
109 WebInspector.ExtensionPanel = function(id, label, pageURL, iconURL)
110 {
111     WebInspector.Panel.call(this, id);
112     this.setHideOnDetach();
113     this._toolbarItemLabel = label;
114     this._statusBarItems = [];
115
116     if (iconURL) {
117         this._addStyleRule(".toolbar-item." + id + " .toolbar-icon", "background-image: url(" + iconURL + ");");
118         this._addStyleRule(".toolbar-small .toolbar-item." + id + " .toolbar-icon", "background-position-x: -32px;");
119     }
120     new WebInspector.ExtensionView(id, this.element, pageURL, "extension panel");
121 }
122
123 WebInspector.ExtensionPanel.prototype = {
124     get toolbarItemLabel()
125     {
126         return this._toolbarItemLabel;
127     },
128
129     get defaultFocusedElement()
130     {
131         return this.sidebarTreeElement || this.element;
132     },
133
134     get statusBarItems()
135     {
136         return this._statusBarItems;
137     },
138
139     /**
140      * @param {Element} element
141      */
142     addStatusBarItem: function(element)
143     {
144         this._statusBarItems.push(element);
145     },
146
147     searchCanceled: function(startingNewSearch)
148     {
149         WebInspector.extensionServer.notifySearchAction(this._id, "cancelSearch");
150         WebInspector.Panel.prototype.searchCanceled.apply(this, arguments);
151     },
152
153     performSearch: function(query)
154     {
155         WebInspector.extensionServer.notifySearchAction(this._id, "performSearch", query);
156         WebInspector.Panel.prototype.performSearch.apply(this, arguments);
157     },
158
159     jumpToNextSearchResult: function()
160     {
161         WebInspector.extensionServer.notifySearchAction(this._id, "nextSearchResult");
162         WebInspector.Panel.prototype.jumpToNextSearchResult.call(this);
163     },
164
165     jumpToPreviousSearchResult: function()
166     {
167         WebInspector.extensionServer.notifySearchAction(this._id, "previousSearchResult");
168         WebInspector.Panel.prototype.jumpToPreviousSearchResult.call(this);
169     },
170
171     _addStyleRule: function(selector, body)
172     {
173         var style = document.createElement("style");
174         style.textContent = selector + " { " + body + " }";
175         document.head.appendChild(style);
176     }
177 }
178
179 WebInspector.ExtensionPanel.prototype.__proto__ = WebInspector.Panel.prototype;
180
181 /**
182  * @constructor
183  * @param {string} id
184  * @param {string} iconURL
185  * @param {string=} tooltip
186  * @param {boolean=} disabled
187  */
188 WebInspector.ExtensionButton = function(id, iconURL, tooltip, disabled)
189 {
190     this._id = id;
191     this.element = document.createElement("button");
192     this.element.className = "status-bar-item extension";
193     this.element.addEventListener("click", this._onClicked.bind(this), false);
194     this.update(iconURL, tooltip, disabled);
195 }
196
197 WebInspector.ExtensionButton.prototype = {
198     /**
199      * @param {string} iconURL
200      * @param {string=} tooltip
201      * @param {boolean=} disabled
202      */
203     update: function(iconURL, tooltip, disabled)
204     {
205         if (typeof iconURL === "string")
206             this.element.style.backgroundImage = "url(" + iconURL + ")";
207         if (typeof tooltip === "string")
208             this.element.title = tooltip;
209         if (typeof disabled === "boolean")
210             this.element.disabled = disabled;
211     },
212
213     _onClicked: function()
214     {
215         WebInspector.extensionServer.notifyButtonClicked(this._id);
216     }
217 }
218
219 /**
220  * @constructor
221  * @extends {WebInspector.SidebarPane}
222  * @param {string} title
223  * @param {string} id
224  */
225 WebInspector.ExtensionSidebarPane = function(title, id)
226 {
227     WebInspector.SidebarPane.call(this, title);
228     this._id = id;
229 }
230
231 WebInspector.ExtensionSidebarPane.prototype = {
232     /**
233      * @param {Object} object
234      * @param {string} title
235      * @param {function(?string=)} callback
236      */
237     setObject: function(object, title, callback)
238     {
239         this._createObjectPropertiesView();
240         this._setObject(WebInspector.RemoteObject.fromLocalObject(object), title, callback);
241     },
242
243     /**
244      * @param {string} expression
245      * @param {string} title
246      * @param {function(?string=)} callback
247      */
248     setExpression: function(expression, title, callback)
249     {
250         this._createObjectPropertiesView();
251         RuntimeAgent.evaluate(expression, "extension-watch", true, undefined, undefined, undefined, this._onEvaluate.bind(this, title, callback));
252     },
253
254     /**
255      * @param {string} url
256      */
257     setPage: function(url)
258     {
259         if (this._objectPropertiesView) {
260             this._objectPropertiesView.detach();
261             delete this._objectPropertiesView;
262         }
263         if (this._extensionView)
264             this._extensionView.detach(true);
265
266         this._extensionView = new WebInspector.ExtensionView(this._id, this.bodyElement, url, "extension");
267     },
268
269     /**
270      * @param {string} height
271      */
272     setHeight: function(height)
273     {
274         this.bodyElement.style.height = height;
275     },
276
277     /**
278      * @param {string} title
279      * @param {function(?string=)} callback
280      * @param {Protocol.Error} error
281      * @param {Object} result
282      * @param {boolean} wasThrown
283      */
284     _onEvaluate: function(title, callback, error, result, wasThrown)
285     {
286         if (error)
287             callback(error.toString());
288         else
289             this._setObject(WebInspector.RemoteObject.fromPayload(result), title, callback);
290     },
291
292     _createObjectPropertiesView: function()
293     {
294         if (this._objectPropertiesView)
295             return;
296         if (this._extensionView) {
297             this._extensionView.detach(true);
298             delete this._extensionView;
299         }
300         this._objectPropertiesView = new WebInspector.ExtensionNotifierView(this._id);
301         this._objectPropertiesView.show(this.bodyElement);
302     },
303
304     /**
305      * @param {Object} object
306      * @param {string} title
307      * @param {function(?string=)} callback
308      */
309     _setObject: function(object, title, callback)
310     {
311         // This may only happen if setPage() was called while we were evaluating the expression.
312         if (!this._objectPropertiesView) {
313             callback("operation cancelled");
314             return;
315         }
316         this._objectPropertiesView.element.removeChildren();
317         var section = new WebInspector.ObjectPropertiesSection(object, title);
318         if (!title)
319             section.headerElement.addStyleClass("hidden");
320         section.expanded = true;
321         section.editable = false;
322         this._objectPropertiesView.element.appendChild(section.element);
323         callback();
324     }
325 }
326
327 WebInspector.ExtensionSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;