- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / ExtensionPanel.js
1 /*
2  * Copyright (C) 2012 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.Panel}
34  * @param {string} id
35  * @param {string} pageURL
36  */
37 WebInspector.ExtensionPanel = function(id, pageURL)
38 {
39     WebInspector.Panel.call(this, id);
40     this.setHideOnDetach();
41     this.element.addStyleClass("extension-panel");
42     this._panelStatusBarElement = this.element.createChild("div", "panel-status-bar hidden");
43
44     var extensionView = new WebInspector.ExtensionView(id, pageURL, "extension panel");
45     extensionView.show(this.element);
46     this.setDefaultFocusedElement(extensionView.defaultFocusedElement());
47 }
48
49 WebInspector.ExtensionPanel.prototype = {
50     defaultFocusedElement: function()
51     {
52         return WebInspector.View.prototype.defaultFocusedElement.call(this);
53     },
54
55     /**
56      * @param {Element} element
57      */
58     addStatusBarItem: function(element)
59     {
60         this._panelStatusBarElement.removeStyleClass("hidden");
61         this._panelStatusBarElement.appendChild(element);
62     },
63
64     searchCanceled: function(startingNewSearch)
65     {
66         WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.CancelSearch);
67         WebInspector.Panel.prototype.searchCanceled.apply(this, arguments);
68     },
69
70     /**
71      * @param {string} query
72      * @param {boolean} shouldJump
73      */
74     performSearch: function(query, shouldJump)
75     {
76         WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PerformSearch, query);
77     },
78
79     jumpToNextSearchResult: function()
80     {
81         WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.NextSearchResult);
82         WebInspector.Panel.prototype.jumpToNextSearchResult.call(this);
83     },
84
85     jumpToPreviousSearchResult: function()
86     {
87         WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PreviousSearchResult);
88         WebInspector.Panel.prototype.jumpToPreviousSearchResult.call(this);
89     },
90
91     __proto__: WebInspector.Panel.prototype
92 }
93
94 /**
95  * @constructor
96  * @param {string} id
97  * @param {string} iconURL
98  * @param {string=} tooltip
99  * @param {boolean=} disabled
100  */
101 WebInspector.ExtensionButton = function(id, iconURL, tooltip, disabled)
102 {
103     this._id = id;
104     this.element = document.createElement("button");
105     this.element.className = "status-bar-item extension";
106     this.element.addEventListener("click", this._onClicked.bind(this), false);
107     this.update(iconURL, tooltip, disabled);
108 }
109
110 WebInspector.ExtensionButton.prototype = {
111     /**
112      * @param {string} iconURL
113      * @param {string=} tooltip
114      * @param {boolean=} disabled
115      */
116     update: function(iconURL, tooltip, disabled)
117     {
118         if (typeof iconURL === "string")
119             this.element.style.backgroundImage = "url(" + iconURL + ")";
120         if (typeof tooltip === "string")
121             this.element.title = tooltip;
122         if (typeof disabled === "boolean")
123             this.element.disabled = disabled;
124     },
125
126     _onClicked: function()
127     {
128         WebInspector.extensionServer.notifyButtonClicked(this._id);
129     }
130 }
131
132 /**
133  * @constructor
134  * @extends {WebInspector.SidebarPane}
135  * @param {string} title
136  * @param {string} id
137  */
138 WebInspector.ExtensionSidebarPane = function(title, id)
139 {
140     WebInspector.SidebarPane.call(this, title);
141     this.setHideOnDetach();
142     this._id = id;
143 }
144
145 WebInspector.ExtensionSidebarPane.prototype = {
146     /**
147      * @param {Object} object
148      * @param {string} title
149      * @param {function(?string=)} callback
150      */
151     setObject: function(object, title, callback)
152     {
153         this._createObjectPropertiesView();
154         this._setObject(WebInspector.RemoteObject.fromLocalObject(object), title, callback);
155     },
156
157     /**
158      * @param {string} expression
159      * @param {string} title
160      * @param {function(?string=)} callback
161      */
162     setExpression: function(expression, title, evaluateOptions, securityOrigin, callback)
163     {
164         this._createObjectPropertiesView();
165         return WebInspector.extensionServer.evaluate(expression, true, false, evaluateOptions, securityOrigin, this._onEvaluate.bind(this, title, callback));
166     },
167
168     /**
169      * @param {string} url
170      */
171     setPage: function(url)
172     {
173         if (this._objectPropertiesView) {
174             this._objectPropertiesView.detach();
175             delete this._objectPropertiesView;
176         }
177         if (this._extensionView)
178             this._extensionView.detach(true);
179
180         this._extensionView = new WebInspector.ExtensionView(this._id, url, "extension fill");
181         this._extensionView.show(this.bodyElement);
182
183         if (!this.bodyElement.style.height)
184             this.setHeight("150px");
185     },
186
187     /**
188      * @param {string} height
189      */
190     setHeight: function(height)
191     {
192         this.bodyElement.style.height = height;
193     },
194
195     /**
196      * @param {string} title
197      * @param {function(?string=)} callback
198      * @param {?Protocol.Error} error
199      * @param {RuntimeAgent.RemoteObject} result
200      * @param {boolean=} wasThrown
201      */
202     _onEvaluate: function(title, callback, error, result, wasThrown)
203     {
204         if (error)
205             callback(error.toString());
206         else
207             this._setObject(WebInspector.RemoteObject.fromPayload(result), title, callback);
208     },
209
210     _createObjectPropertiesView: function()
211     {
212         if (this._objectPropertiesView)
213             return;
214         if (this._extensionView) {
215             this._extensionView.detach(true);
216             delete this._extensionView;
217         }
218         this._objectPropertiesView = new WebInspector.ExtensionNotifierView(this._id);
219         this._objectPropertiesView.show(this.bodyElement);
220     },
221
222     /**
223      * @param {WebInspector.RemoteObject} object
224      * @param {string} title
225      * @param {function(?string=)} callback
226      */
227     _setObject: function(object, title, callback)
228     {
229         // This may only happen if setPage() was called while we were evaluating the expression.
230         if (!this._objectPropertiesView) {
231             callback("operation cancelled");
232             return;
233         }
234         this._objectPropertiesView.element.removeChildren();
235         var section = new WebInspector.ObjectPropertiesSection(object, title);
236         if (!title)
237             section.headerElement.addStyleClass("hidden");
238         section.expanded = true;
239         section.editable = false;
240         this._objectPropertiesView.element.appendChild(section.element);
241         callback();
242     },
243
244     __proto__: WebInspector.SidebarPane.prototype
245 }