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 */
38WebInspector.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
53WebInspector.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 */
115WebInspector.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
124WebInspector.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 */
152WebInspector.ExtensionSidebarPane = function(title, id)
153{
154    WebInspector.SidebarPane.call(this, title);
155    this.setHideOnDetach();
156    this._id = id;
157}
158
159WebInspector.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 {!Object} evaluateOptions
175     * @param {string} securityOrigin
176     * @param {function(?string=)} callback
177     */
178    setExpression: function(expression, title, evaluateOptions, securityOrigin, callback)
179    {
180        this._createObjectPropertiesView();
181        WebInspector.extensionServer.evaluate(expression, true, false, evaluateOptions, securityOrigin, this._onEvaluate.bind(this, title, callback));
182    },
183
184    /**
185     * @param {string} url
186     */
187    setPage: function(url)
188    {
189        if (this._objectPropertiesView) {
190            this._objectPropertiesView.detach();
191            delete this._objectPropertiesView;
192        }
193        if (this._extensionView)
194            this._extensionView.detach(true);
195
196        this._extensionView = new WebInspector.ExtensionView(this._id, url, "extension fill");
197        this._extensionView.show(this.bodyElement);
198
199        if (!this.bodyElement.style.height)
200            this.setHeight("150px");
201    },
202
203    /**
204     * @param {string} height
205     */
206    setHeight: function(height)
207    {
208        this.bodyElement.style.height = height;
209    },
210
211    /**
212     * @param {string} title
213     * @param {function(?string=)} callback
214     * @param {?Protocol.Error} error
215     * @param {!RuntimeAgent.RemoteObject} result
216     * @param {boolean=} wasThrown
217     */
218    _onEvaluate: function(title, callback, error, result, wasThrown)
219    {
220        if (error)
221            callback(error.toString());
222        else
223            this._setObject(WebInspector.runtimeModel.createRemoteObject(result), title, callback);
224    },
225
226    _createObjectPropertiesView: function()
227    {
228        if (this._objectPropertiesView)
229            return;
230        if (this._extensionView) {
231            this._extensionView.detach(true);
232            delete this._extensionView;
233        }
234        this._objectPropertiesView = new WebInspector.ExtensionNotifierView(this._id);
235        this._objectPropertiesView.show(this.bodyElement);
236    },
237
238    /**
239     * @param {!WebInspector.RemoteObject} object
240     * @param {string} title
241     * @param {function(?string=)} callback
242     */
243    _setObject: function(object, title, callback)
244    {
245        // This may only happen if setPage() was called while we were evaluating the expression.
246        if (!this._objectPropertiesView) {
247            callback("operation cancelled");
248            return;
249        }
250        this._objectPropertiesView.element.removeChildren();
251        var section = new WebInspector.ObjectPropertiesSection(object, title);
252        if (!title)
253            section.headerElement.classList.add("hidden");
254        section.expanded = true;
255        section.editable = false;
256        this._objectPropertiesView.element.appendChild(section.element);
257        callback();
258    },
259
260    __proto__: WebInspector.SidebarPane.prototype
261}
262