ObjectPopoverHelper.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * @constructor
  32. * @extends {WebInspector.PopoverHelper}
  33. * @param {Element} panelElement
  34. * @param {function(Element, Event):Element|undefined} getAnchor
  35. * @param {function(Element, function(WebInspector.RemoteObject, boolean, Element=):undefined, string):undefined} queryObject
  36. * @param {function()=} onHide
  37. * @param {boolean=} disableOnClick
  38. */
  39. WebInspector.ObjectPopoverHelper = function(panelElement, getAnchor, queryObject, onHide, disableOnClick)
  40. {
  41. WebInspector.PopoverHelper.call(this, panelElement, getAnchor, this._showObjectPopover.bind(this), this._onHideObjectPopover.bind(this), disableOnClick);
  42. this._queryObject = queryObject;
  43. this._onHideCallback = onHide;
  44. this._popoverObjectGroup = "popover";
  45. panelElement.addEventListener("scroll", this.hidePopover.bind(this), true);
  46. };
  47. WebInspector.ObjectPopoverHelper.prototype = {
  48. /**
  49. * @param {function(WebInspector.RemoteObject):string} formatter
  50. */
  51. setRemoteObjectFormatter: function(formatter)
  52. {
  53. this._remoteObjectFormatter = formatter;
  54. },
  55. /**
  56. * @param {Element} element
  57. * @param {WebInspector.Popover} popover
  58. */
  59. _showObjectPopover: function(element, popover)
  60. {
  61. /**
  62. * @param {WebInspector.RemoteObject} result
  63. * @param {boolean} wasThrown
  64. * @param {Element=} anchorOverride
  65. */
  66. function showObjectPopover(result, wasThrown, anchorOverride)
  67. {
  68. if (popover.disposed)
  69. return;
  70. if (wasThrown) {
  71. this.hidePopover();
  72. return;
  73. }
  74. var anchorElement = anchorOverride || element;
  75. var description = (this._remoteObjectFormatter && this._remoteObjectFormatter(result)) || result.description;
  76. var popoverContentElement = null;
  77. if (result.type !== "object") {
  78. popoverContentElement = document.createElement("span");
  79. popoverContentElement.className = "monospace console-formatted-" + result.type;
  80. popoverContentElement.style.whiteSpace = "pre";
  81. popoverContentElement.textContent = description;
  82. if (result.type === "function") {
  83. function didGetDetails(error, response)
  84. {
  85. if (error) {
  86. console.error(error);
  87. return;
  88. }
  89. var container = document.createElement("div");
  90. container.className = "inline-block";
  91. var title = container.createChild("div", "function-popover-title source-code");
  92. var functionName = title.createChild("span", "function-name");
  93. functionName.textContent = response.name || response.inferredName || response.displayName || WebInspector.UIString("(anonymous function)");
  94. this._linkifier = new WebInspector.Linkifier();
  95. var rawLocation = /** @type {WebInspector.DebuggerModel.Location} */ (response.location);
  96. var link = this._linkifier.linkifyRawLocation(rawLocation, "function-location-link");
  97. if (link)
  98. title.appendChild(link);
  99. container.appendChild(popoverContentElement);
  100. popover.show(container, anchorElement);
  101. }
  102. DebuggerAgent.getFunctionDetails(result.objectId, didGetDetails.bind(this));
  103. return;
  104. }
  105. if (result.type === "string")
  106. popoverContentElement.textContent = "\"" + popoverContentElement.textContent + "\"";
  107. popover.show(popoverContentElement, anchorElement);
  108. } else {
  109. if (result.subtype === "node")
  110. result.highlightAsDOMNode();
  111. popoverContentElement = document.createElement("div");
  112. this._titleElement = document.createElement("div");
  113. this._titleElement.className = "source-frame-popover-title monospace";
  114. this._titleElement.textContent = description;
  115. popoverContentElement.appendChild(this._titleElement);
  116. var section = new WebInspector.ObjectPropertiesSection(result);
  117. // For HTML DOM wrappers, append "#id" to title, if not empty.
  118. if (description.substr(0, 4) === "HTML") {
  119. this._sectionUpdateProperties = section.updateProperties.bind(section);
  120. section.updateProperties = this._updateHTMLId.bind(this);
  121. }
  122. section.expanded = true;
  123. section.element.addStyleClass("source-frame-popover-tree");
  124. section.headerElement.addStyleClass("hidden");
  125. popoverContentElement.appendChild(section.element);
  126. const popoverWidth = 300;
  127. const popoverHeight = 250;
  128. popover.show(popoverContentElement, anchorElement, popoverWidth, popoverHeight);
  129. }
  130. }
  131. this._queryObject(element, showObjectPopover.bind(this), this._popoverObjectGroup);
  132. },
  133. _onHideObjectPopover: function()
  134. {
  135. WebInspector.domAgent.hideDOMNodeHighlight();
  136. if (this._linkifier) {
  137. this._linkifier.reset();
  138. delete this._linkifier;
  139. }
  140. if (this._onHideCallback)
  141. this._onHideCallback();
  142. RuntimeAgent.releaseObjectGroup(this._popoverObjectGroup);
  143. },
  144. _updateHTMLId: function(properties, rootTreeElementConstructor, rootPropertyComparer)
  145. {
  146. for (var i = 0; i < properties.length; ++i) {
  147. if (properties[i].name === "id") {
  148. if (properties[i].value.description)
  149. this._titleElement.textContent += "#" + properties[i].value.description;
  150. break;
  151. }
  152. }
  153. this._sectionUpdateProperties(properties, rootTreeElementConstructor, rootPropertyComparer);
  154. },
  155. __proto__: WebInspector.PopoverHelper.prototype
  156. }