Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / components / ObjectPopoverHelper.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.PopoverHelper}
34  * @param {!Element} panelElement
35  * @param {function(!Element, !Event):(!Element|!AnchorBox|undefined)} getAnchor
36  * @param {function(!Element, function(!WebInspector.RemoteObject, boolean, !Element=):undefined, string):undefined} queryObject
37  * @param {function()=} onHide
38  * @param {boolean=} disableOnClick
39  */
40 WebInspector.ObjectPopoverHelper = function(panelElement, getAnchor, queryObject, onHide, disableOnClick)
41 {
42     WebInspector.PopoverHelper.call(this, panelElement, getAnchor, this._showObjectPopover.bind(this), this._onHideObjectPopover.bind(this), disableOnClick);
43     this._queryObject = queryObject;
44     this._onHideCallback = onHide;
45     this._popoverObjectGroup = "popover";
46     panelElement.addEventListener("scroll", this.hidePopover.bind(this), true);
47 };
48
49 WebInspector.ObjectPopoverHelper.MaxPopoverTextLength = 10000;
50
51 WebInspector.ObjectPopoverHelper.prototype = {
52     /**
53      * @param {function(!WebInspector.RemoteObject):string} formatter
54      */
55     setRemoteObjectFormatter: function(formatter)
56     {
57         this._remoteObjectFormatter = formatter;
58     },
59
60     /**
61      * @param {!Element} element
62      * @param {!WebInspector.Popover} popover
63      */
64     _showObjectPopover: function(element, popover)
65     {
66         /**
67          * @param {!WebInspector.Target} target
68          * @param {!Element} anchorElement
69          * @param {!Element} popoverContentElement
70          * @param {?WebInspector.DebuggerModel.FunctionDetails} response
71          * @this {WebInspector.ObjectPopoverHelper}
72          */
73         function didGetDetails(target, anchorElement, popoverContentElement, response)
74         {
75             if (!response)
76                 return;
77
78             var container = createElement("div");
79             container.className = "inline-block";
80
81             var title = container.createChild("div", "function-popover-title source-code");
82             var functionName = title.createChild("span", "function-name");
83             functionName.textContent = response.functionName || WebInspector.UIString("(anonymous function)");
84
85             var rawLocation = response.location;
86             var sourceURL = response.sourceURL;
87             if (rawLocation && sourceURL) {
88                 this._linkifier = new WebInspector.Linkifier();
89                 var link = this._linkifier.linkifyRawLocation(rawLocation, sourceURL, "function-location-link");
90                 title.appendChild(link);
91             }
92
93             container.appendChild(popoverContentElement);
94             popover.show(container, anchorElement);
95         }
96
97         /**
98          * @param {!WebInspector.RemoteObject} result
99          * @param {boolean} wasThrown
100          * @param {!Element=} anchorOverride
101          * @this {WebInspector.ObjectPopoverHelper}
102          */
103         function showObjectPopover(result, wasThrown, anchorOverride)
104         {
105             if (popover.disposed)
106                 return;
107             if (wasThrown) {
108                 this.hidePopover();
109                 return;
110             }
111             this._objectTarget = result.target();
112             var anchorElement = anchorOverride || element;
113             var description = (this._remoteObjectFormatter && this._remoteObjectFormatter(result)) || result.description;
114             description = description.trimEnd(WebInspector.ObjectPopoverHelper.MaxPopoverTextLength);
115             var popoverContentElement = null;
116             if (result.type !== "object") {
117                 popoverContentElement = createElement("span");
118                 popoverContentElement.className = "monospace console-formatted-" + result.type;
119                 popoverContentElement.style.whiteSpace = "pre";
120                 if (result.type === "string")
121                     popoverContentElement.createTextChildren("\"", description, "\"");
122                 else
123                     popoverContentElement.textContent = description;
124                 if (result.type === "function") {
125                     result.functionDetails(didGetDetails.bind(this, result.target(), anchorElement, popoverContentElement));
126                     return;
127                 }
128                 popover.show(popoverContentElement, anchorElement);
129             } else {
130                 if (result.subtype === "node") {
131                     result.highlightAsDOMNode();
132                     this._resultHighlightedAsDOM = result;
133                 }
134                 popoverContentElement = createElement("div");
135                 this._titleElement = createElement("div");
136                 this._titleElement.className = "source-frame-popover-title monospace";
137                 this._titleElement.textContent = description;
138                 popoverContentElement.appendChild(this._titleElement);
139
140                 var section = new WebInspector.ObjectPropertiesSection(result);
141                 // For HTML DOM wrappers, append "#id" to title, if not empty.
142                 if (description.substr(0, 4) === "HTML") {
143                     this._sectionUpdateProperties = section.updateProperties.bind(section);
144                     section.updateProperties = this._updateHTMLId.bind(this);
145                 }
146                 section.expanded = true;
147                 section.element.classList.add("source-frame-popover-tree");
148                 section.headerElement.classList.add("hidden");
149                 popoverContentElement.appendChild(section.element);
150
151                 const popoverWidth = 300;
152                 const popoverHeight = 250;
153                 popover.show(popoverContentElement, anchorElement, popoverWidth, popoverHeight);
154             }
155         }
156         this._queryObject(element, showObjectPopover.bind(this), this._popoverObjectGroup);
157     },
158
159     _onHideObjectPopover: function()
160     {
161         if (this._resultHighlightedAsDOM) {
162             this._resultHighlightedAsDOM.target().domModel.hideDOMNodeHighlight();
163             delete this._resultHighlightedAsDOM;
164         }
165         if (this._linkifier) {
166             this._linkifier.reset();
167             delete this._linkifier;
168         }
169         if (this._onHideCallback)
170             this._onHideCallback();
171         if (this._objectTarget) {
172             this._objectTarget.runtimeAgent().releaseObjectGroup(this._popoverObjectGroup);
173             delete this._objectTarget;
174         }
175     },
176
177     _updateHTMLId: function(properties, rootTreeElementConstructor, rootPropertyComparer)
178     {
179         for (var i = 0; i < properties.length; ++i) {
180             if (properties[i].name === "id") {
181                 if (properties[i].value.description)
182                     this._titleElement.textContent += "#" + properties[i].value.description;
183                 break;
184             }
185         }
186         this._sectionUpdateProperties(properties, rootTreeElementConstructor, rootPropertyComparer);
187     },
188
189     __proto__: WebInspector.PopoverHelper.prototype
190 }