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