MetricsSidebarPane.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (C) 2007 Apple 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
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.SidebarPane}
  31. */
  32. WebInspector.MetricsSidebarPane = function()
  33. {
  34. WebInspector.SidebarPane.call(this, WebInspector.UIString("Metrics"));
  35. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this._styleSheetOrMediaQueryResultChanged, this);
  36. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this._styleSheetOrMediaQueryResultChanged, this);
  37. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrModified, this._attributesUpdated, this);
  38. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrRemoved, this._attributesUpdated, this);
  39. }
  40. WebInspector.MetricsSidebarPane.prototype = {
  41. /**
  42. * @param {WebInspector.DOMNode=} node
  43. */
  44. update: function(node)
  45. {
  46. if (node)
  47. this.node = node;
  48. this._innerUpdate();
  49. },
  50. _innerUpdate: function()
  51. {
  52. // "style" attribute might have changed. Update metrics unless they are being edited
  53. // (if a CSS property is added, a StyleSheetChanged event is dispatched).
  54. if (this._isEditingMetrics)
  55. return;
  56. // FIXME: avoid updates of a collapsed pane.
  57. var node = this.node;
  58. if (!node || node.nodeType() !== Node.ELEMENT_NODE) {
  59. this.bodyElement.removeChildren();
  60. return;
  61. }
  62. function callback(style)
  63. {
  64. if (!style || this.node !== node)
  65. return;
  66. this._updateMetrics(style);
  67. }
  68. WebInspector.cssModel.getComputedStyleAsync(node.id, callback.bind(this));
  69. function inlineStyleCallback(style)
  70. {
  71. if (!style || this.node !== node)
  72. return;
  73. this.inlineStyle = style;
  74. }
  75. WebInspector.cssModel.getInlineStylesAsync(node.id, inlineStyleCallback.bind(this));
  76. },
  77. _styleSheetOrMediaQueryResultChanged: function()
  78. {
  79. this._innerUpdate();
  80. },
  81. _attributesUpdated: function(event)
  82. {
  83. if (this.node !== event.data.node)
  84. return;
  85. this._innerUpdate();
  86. },
  87. _getPropertyValueAsPx: function(style, propertyName)
  88. {
  89. return Number(style.getPropertyValue(propertyName).replace(/px$/, "") || 0);
  90. },
  91. _getBox: function(computedStyle, componentName)
  92. {
  93. var suffix = componentName === "border" ? "-width" : "";
  94. var left = this._getPropertyValueAsPx(computedStyle, componentName + "-left" + suffix);
  95. var top = this._getPropertyValueAsPx(computedStyle, componentName + "-top" + suffix);
  96. var right = this._getPropertyValueAsPx(computedStyle, componentName + "-right" + suffix);
  97. var bottom = this._getPropertyValueAsPx(computedStyle, componentName + "-bottom" + suffix);
  98. return { left: left, top: top, right: right, bottom: bottom };
  99. },
  100. _highlightDOMNode: function(showHighlight, mode, event)
  101. {
  102. event.consume();
  103. var nodeId = showHighlight && this.node ? this.node.id : 0;
  104. if (nodeId) {
  105. if (this._highlightMode === mode)
  106. return;
  107. this._highlightMode = mode;
  108. WebInspector.domAgent.highlightDOMNode(nodeId, mode);
  109. } else {
  110. delete this._highlightMode;
  111. WebInspector.domAgent.hideDOMNodeHighlight();
  112. }
  113. for (var i = 0; this._boxElements && i < this._boxElements.length; ++i) {
  114. var element = this._boxElements[i];
  115. if (!nodeId || mode === "all" || element._name === mode)
  116. element.style.backgroundColor = element._backgroundColor;
  117. else
  118. element.style.backgroundColor = "";
  119. }
  120. },
  121. _updateMetrics: function(style)
  122. {
  123. // Updating with computed style.
  124. var metricsElement = document.createElement("div");
  125. metricsElement.className = "metrics";
  126. var self = this;
  127. function createBoxPartElement(style, name, side, suffix)
  128. {
  129. var propertyName = (name !== "position" ? name + "-" : "") + side + suffix;
  130. var value = style.getPropertyValue(propertyName);
  131. if (value === "" || (name !== "position" && value === "0px"))
  132. value = "\u2012";
  133. else if (name === "position" && value === "auto")
  134. value = "\u2012";
  135. value = value.replace(/px$/, "");
  136. value = Number.toFixedIfFloating(value);
  137. var element = document.createElement("div");
  138. element.className = side;
  139. element.textContent = value;
  140. element.addEventListener("dblclick", this.startEditing.bind(this, element, name, propertyName, style), false);
  141. return element;
  142. }
  143. function getContentAreaWidthPx(style)
  144. {
  145. var width = style.getPropertyValue("width").replace(/px$/, "");
  146. if (!isNaN(width) && style.getPropertyValue("box-sizing") === "border-box") {
  147. var borderBox = self._getBox(style, "border");
  148. var paddingBox = self._getBox(style, "padding");
  149. width = width - borderBox.left - borderBox.right - paddingBox.left - paddingBox.right;
  150. }
  151. return Number.toFixedIfFloating(width);
  152. }
  153. function getContentAreaHeightPx(style)
  154. {
  155. var height = style.getPropertyValue("height").replace(/px$/, "");
  156. if (!isNaN(height) && style.getPropertyValue("box-sizing") === "border-box") {
  157. var borderBox = self._getBox(style, "border");
  158. var paddingBox = self._getBox(style, "padding");
  159. height = height - borderBox.top - borderBox.bottom - paddingBox.top - paddingBox.bottom;
  160. }
  161. return Number.toFixedIfFloating(height);
  162. }
  163. // Display types for which margin is ignored.
  164. var noMarginDisplayType = {
  165. "table-cell": true,
  166. "table-column": true,
  167. "table-column-group": true,
  168. "table-footer-group": true,
  169. "table-header-group": true,
  170. "table-row": true,
  171. "table-row-group": true
  172. };
  173. // Display types for which padding is ignored.
  174. var noPaddingDisplayType = {
  175. "table-column": true,
  176. "table-column-group": true,
  177. "table-footer-group": true,
  178. "table-header-group": true,
  179. "table-row": true,
  180. "table-row-group": true
  181. };
  182. // Position types for which top, left, bottom and right are ignored.
  183. var noPositionType = {
  184. "static": true
  185. };
  186. var boxes = ["content", "padding", "border", "margin", "position"];
  187. var boxColors = [
  188. WebInspector.Color.PageHighlight.Content,
  189. WebInspector.Color.PageHighlight.Padding,
  190. WebInspector.Color.PageHighlight.Border,
  191. WebInspector.Color.PageHighlight.Margin,
  192. WebInspector.Color.fromRGBA([0, 0, 0, 0])
  193. ];
  194. var boxLabels = [WebInspector.UIString("content"), WebInspector.UIString("padding"), WebInspector.UIString("border"), WebInspector.UIString("margin"), WebInspector.UIString("position")];
  195. var previousBox = null;
  196. this._boxElements = [];
  197. for (var i = 0; i < boxes.length; ++i) {
  198. var name = boxes[i];
  199. if (name === "margin" && noMarginDisplayType[style.getPropertyValue("display")])
  200. continue;
  201. if (name === "padding" && noPaddingDisplayType[style.getPropertyValue("display")])
  202. continue;
  203. if (name === "position" && noPositionType[style.getPropertyValue("position")])
  204. continue;
  205. var boxElement = document.createElement("div");
  206. boxElement.className = name;
  207. boxElement._backgroundColor = boxColors[i].toString(WebInspector.Color.Format.RGBA);
  208. boxElement._name = name;
  209. boxElement.style.backgroundColor = boxElement._backgroundColor;
  210. boxElement.addEventListener("mouseover", this._highlightDOMNode.bind(this, true, name === "position" ? "all" : name), false);
  211. this._boxElements.push(boxElement);
  212. if (name === "content") {
  213. var widthElement = document.createElement("span");
  214. widthElement.textContent = getContentAreaWidthPx(style);
  215. widthElement.addEventListener("dblclick", this.startEditing.bind(this, widthElement, "width", "width", style), false);
  216. var heightElement = document.createElement("span");
  217. heightElement.textContent = getContentAreaHeightPx(style);
  218. heightElement.addEventListener("dblclick", this.startEditing.bind(this, heightElement, "height", "height", style), false);
  219. boxElement.appendChild(widthElement);
  220. boxElement.appendChild(document.createTextNode(" \u00D7 "));
  221. boxElement.appendChild(heightElement);
  222. } else {
  223. var suffix = (name === "border" ? "-width" : "");
  224. var labelElement = document.createElement("div");
  225. labelElement.className = "label";
  226. labelElement.textContent = boxLabels[i];
  227. boxElement.appendChild(labelElement);
  228. boxElement.appendChild(createBoxPartElement.call(this, style, name, "top", suffix));
  229. boxElement.appendChild(document.createElement("br"));
  230. boxElement.appendChild(createBoxPartElement.call(this, style, name, "left", suffix));
  231. if (previousBox)
  232. boxElement.appendChild(previousBox);
  233. boxElement.appendChild(createBoxPartElement.call(this, style, name, "right", suffix));
  234. boxElement.appendChild(document.createElement("br"));
  235. boxElement.appendChild(createBoxPartElement.call(this, style, name, "bottom", suffix));
  236. }
  237. previousBox = boxElement;
  238. }
  239. metricsElement.appendChild(previousBox);
  240. metricsElement.addEventListener("mouseover", this._highlightDOMNode.bind(this, false, ""), false);
  241. this.bodyElement.removeChildren();
  242. this.bodyElement.appendChild(metricsElement);
  243. },
  244. startEditing: function(targetElement, box, styleProperty, computedStyle)
  245. {
  246. if (WebInspector.isBeingEdited(targetElement))
  247. return;
  248. var context = { box: box, styleProperty: styleProperty, computedStyle: computedStyle };
  249. var boundKeyDown = this._handleKeyDown.bind(this, context, styleProperty);
  250. context.keyDownHandler = boundKeyDown;
  251. targetElement.addEventListener("keydown", boundKeyDown, false);
  252. this._isEditingMetrics = true;
  253. var config = new WebInspector.EditingConfig(this.editingCommitted.bind(this), this.editingCancelled.bind(this), context);
  254. WebInspector.startEditing(targetElement, config);
  255. window.getSelection().setBaseAndExtent(targetElement, 0, targetElement, 1);
  256. },
  257. _handleKeyDown: function(context, styleProperty, event)
  258. {
  259. var element = event.currentTarget;
  260. function finishHandler(originalValue, replacementString)
  261. {
  262. this._applyUserInput(element, replacementString, originalValue, context, false);
  263. }
  264. function customNumberHandler(number)
  265. {
  266. if (styleProperty !== "margin" && number < 0)
  267. number = 0;
  268. return number;
  269. }
  270. WebInspector.handleElementValueModifications(event, element, finishHandler.bind(this), undefined, customNumberHandler);
  271. },
  272. editingEnded: function(element, context)
  273. {
  274. delete this.originalPropertyData;
  275. delete this.previousPropertyDataCandidate;
  276. element.removeEventListener("keydown", context.keyDownHandler, false);
  277. delete this._isEditingMetrics;
  278. },
  279. editingCancelled: function(element, context)
  280. {
  281. if ("originalPropertyData" in this && this.inlineStyle) {
  282. if (!this.originalPropertyData) {
  283. // An added property, remove the last property in the style.
  284. var pastLastSourcePropertyIndex = this.inlineStyle.pastLastSourcePropertyIndex();
  285. if (pastLastSourcePropertyIndex)
  286. this.inlineStyle.allProperties[pastLastSourcePropertyIndex - 1].setText("", false);
  287. } else
  288. this.inlineStyle.allProperties[this.originalPropertyData.index].setText(this.originalPropertyData.propertyText, false);
  289. }
  290. this.editingEnded(element, context);
  291. this.update();
  292. },
  293. _applyUserInput: function(element, userInput, previousContent, context, commitEditor)
  294. {
  295. if (!this.inlineStyle) {
  296. // Element has no renderer.
  297. return this.editingCancelled(element, context); // nothing changed, so cancel
  298. }
  299. if (commitEditor && userInput === previousContent)
  300. return this.editingCancelled(element, context); // nothing changed, so cancel
  301. if (context.box !== "position" && (!userInput || userInput === "\u2012"))
  302. userInput = "0px";
  303. else if (context.box === "position" && (!userInput || userInput === "\u2012"))
  304. userInput = "auto";
  305. userInput = userInput.toLowerCase();
  306. // Append a "px" unit if the user input was just a number.
  307. if (/^\d+$/.test(userInput))
  308. userInput += "px";
  309. var styleProperty = context.styleProperty;
  310. var computedStyle = context.computedStyle;
  311. if (computedStyle.getPropertyValue("box-sizing") === "border-box" && (styleProperty === "width" || styleProperty === "height")) {
  312. if (!userInput.match(/px$/)) {
  313. WebInspector.log("For elements with box-sizing: border-box, only absolute content area dimensions can be applied", WebInspector.ConsoleMessage.MessageLevel.Error, true);
  314. return;
  315. }
  316. var borderBox = this._getBox(computedStyle, "border");
  317. var paddingBox = this._getBox(computedStyle, "padding");
  318. var userValuePx = Number(userInput.replace(/px$/, ""));
  319. if (isNaN(userValuePx))
  320. return;
  321. if (styleProperty === "width")
  322. userValuePx += borderBox.left + borderBox.right + paddingBox.left + paddingBox.right;
  323. else
  324. userValuePx += borderBox.top + borderBox.bottom + paddingBox.top + paddingBox.bottom;
  325. userInput = userValuePx + "px";
  326. }
  327. this.previousPropertyDataCandidate = null;
  328. var self = this;
  329. var callback = function(style) {
  330. if (!style)
  331. return;
  332. self.inlineStyle = style;
  333. if (!("originalPropertyData" in self))
  334. self.originalPropertyData = self.previousPropertyDataCandidate;
  335. if (typeof self._highlightMode !== "undefined") {
  336. WebInspector.domAgent.highlightDOMNode(self.node.id, self._highlightMode);
  337. }
  338. if (commitEditor) {
  339. self.dispatchEventToListeners("metrics edited");
  340. self.update();
  341. }
  342. };
  343. var allProperties = this.inlineStyle.allProperties;
  344. for (var i = 0; i < allProperties.length; ++i) {
  345. var property = allProperties[i];
  346. if (property.name !== context.styleProperty || property.inactive)
  347. continue;
  348. this.previousPropertyDataCandidate = property;
  349. property.setValue(userInput, commitEditor, true, callback);
  350. return;
  351. }
  352. this.inlineStyle.appendProperty(context.styleProperty, userInput, callback);
  353. },
  354. editingCommitted: function(element, userInput, previousContent, context)
  355. {
  356. this.editingEnded(element, context);
  357. this._applyUserInput(element, userInput, previousContent, context, true);
  358. },
  359. __proto__: WebInspector.SidebarPane.prototype
  360. }