Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / components / ObjectPopoverHelper.js
blob0c19759d2154f78bac8ef195c0eba1bacaae46e6
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
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
40 WebInspector.ObjectPopoverHelper = function(panelElement, getAnchor, queryObject, onHide, disableOnClick)
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);
49 WebInspector.ObjectPopoverHelper.MaxPopoverTextLength = 10000;
51 WebInspector.ObjectPopoverHelper.prototype = {
52 /**
53 * @param {!Element} element
54 * @param {!WebInspector.Popover} popover
56 _showObjectPopover: function(element, popover)
58 /**
59 * @param {!WebInspector.RemoteObject} funcObject
60 * @param {!Element} popoverContentElement
61 * @param {!Element} anchorElement
62 * @param {?Array.<!WebInspector.RemoteObjectProperty>} properties
63 * @param {?Array.<!WebInspector.RemoteObjectProperty>} internalProperties
64 * @this {WebInspector.ObjectPopoverHelper}
66 function didGetFunctionProperties(funcObject, popoverContentElement, anchorElement, properties, internalProperties)
68 if (internalProperties) {
69 for (var i = 0; i < internalProperties.length; i++) {
70 if (internalProperties[i].name === "[[TargetFunction]]") {
71 funcObject = internalProperties[i].value;
72 break;
76 funcObject.functionDetails(didGetFunctionDetails.bind(this, popoverContentElement, anchorElement));
79 /**
80 * @param {!Element} popoverContentElement
81 * @param {!Element} anchorElement
82 * @param {?WebInspector.DebuggerModel.FunctionDetails} response
83 * @this {WebInspector.ObjectPopoverHelper}
85 function didGetFunctionDetails(popoverContentElement, anchorElement, response)
87 if (!response || popover.disposed)
88 return;
90 var container = createElementWithClass("div", "object-popover-container");
91 var title = container.createChild("div", "function-popover-title source-code");
92 var functionName = title.createChild("span", "function-name");
93 functionName.textContent = WebInspector.beautifyFunctionName(response.functionName);
95 var rawLocation = response.location;
96 var sourceURL = response.sourceURL;
97 if (rawLocation && sourceURL) {
98 var link = this._lazyLinkifier().linkifyRawLocation(rawLocation, sourceURL, "function-location-link");
99 title.appendChild(link);
102 container.appendChild(popoverContentElement);
103 popover.showForAnchor(container, anchorElement);
107 * @param {?WebInspector.DebuggerModel.GeneratorObjectDetails} response
108 * @this {WebInspector.ObjectPopoverHelper}
110 function didGetGeneratorObjectDetails(response)
112 if (!response || popover.disposed)
113 return;
115 var rawLocation = response.location;
116 var sourceURL = response.sourceURL;
117 if (rawLocation && sourceURL) {
118 var link = this._lazyLinkifier().linkifyRawLocation(rawLocation, sourceURL, "function-location-link");
119 this._titleElement.appendChild(link);
124 * @param {!WebInspector.RemoteObject} result
125 * @param {boolean} wasThrown
126 * @param {!Element=} anchorOverride
127 * @this {WebInspector.ObjectPopoverHelper}
129 function didQueryObject(result, wasThrown, anchorOverride)
131 if (popover.disposed)
132 return;
133 if (wasThrown) {
134 this.hidePopover();
135 return;
137 this._objectTarget = result.target();
138 var anchorElement = anchorOverride || element;
139 var description = result.description.trimEnd(WebInspector.ObjectPopoverHelper.MaxPopoverTextLength);
140 var popoverContentElement = null;
141 if (result.type !== "object") {
142 popoverContentElement = createElement("span");
143 popoverContentElement.appendChild(WebInspector.Widget.createStyleElement("components/objectValue.css"));
144 var valueElement = popoverContentElement.createChild("span", "monospace object-value-" + result.type);
145 valueElement.style.whiteSpace = "pre";
147 if (result.type === "string")
148 valueElement.createTextChildren("\"", description, "\"");
149 else if (result.type === "function")
150 WebInspector.ObjectPropertiesSection.formatObjectAsFunction(result, valueElement, true);
151 else
152 valueElement.textContent = description;
154 if (result.type === "function") {
155 result.getOwnProperties(didGetFunctionProperties.bind(this, result, popoverContentElement, anchorElement));
156 return;
158 popover.showForAnchor(popoverContentElement, anchorElement);
159 } else {
160 if (result.subtype === "node") {
161 WebInspector.DOMModel.highlightObjectAsDOMNode(result);
162 this._resultHighlightedAsDOM = true;
165 if (result.customPreview()) {
166 var customPreviewComponent = new WebInspector.CustomPreviewComponent(result);
167 customPreviewComponent.expandIfPossible();
168 popoverContentElement = customPreviewComponent.element;
169 } else {
170 popoverContentElement = createElement("div");
171 this._titleElement = popoverContentElement.createChild("div", "monospace");
172 this._titleElement.createChild("span", "source-frame-popover-title").textContent = description;
173 var section = new WebInspector.ObjectPropertiesSection(result, "");
174 section.element.classList.add("source-frame-popover-tree");
175 section.titleLessMode();
176 popoverContentElement.appendChild(section.element);
178 if (result.subtype === "generator")
179 result.generatorObjectDetails(didGetGeneratorObjectDetails.bind(this));
181 var popoverWidth = 300;
182 var popoverHeight = 250;
183 popover.showForAnchor(popoverContentElement, anchorElement, popoverWidth, popoverHeight);
187 this._queryObject(element, didQueryObject.bind(this), this._popoverObjectGroup);
190 _onHideObjectPopover: function()
192 if (this._resultHighlightedAsDOM) {
193 WebInspector.DOMModel.hideDOMNodeHighlight();
194 delete this._resultHighlightedAsDOM;
196 if (this._linkifier) {
197 this._linkifier.dispose();
198 delete this._linkifier;
200 if (this._onHideCallback)
201 this._onHideCallback();
202 if (this._objectTarget) {
203 this._objectTarget.runtimeAgent().releaseObjectGroup(this._popoverObjectGroup);
204 delete this._objectTarget;
209 * @return {!WebInspector.Linkifier}
211 _lazyLinkifier: function()
213 if (!this._linkifier)
214 this._linkifier = new WebInspector.Linkifier();
215 return this._linkifier;
218 __proto__: WebInspector.PopoverHelper.prototype