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. var element = document.createElement("div");
  137. element.className = side;
  138. element.textContent = value;
  139. element.addEventListener("dblclick", this.startEditing.bind(this, element, name, propertyName, style), false);
  140. return element;
  141. }
  142. function getContentAreaWidthPx(style)
  143. {
  144. var width = style.getPropertyValue("width").replace(/px$/, "");
  145. if (style.getPropertyValue("box-sizing") === "border-box") {
  146. var borderBox = self._getBox(style, "border");
  147. var paddingBox = self._getBox(style, "padding");
  148. width = width - borderBox.left - borderBox.right - paddingBox.left - paddingBox.right;
  149. }
  150. return width;
  151. }
  152. function getContentAreaHeightPx(style)
  153. {
  154. var height = style.getPropertyValue("height").replace(/px$/, "");
  155. if (style.getPropertyValue("box-sizing") === "border-box") {
  156. var borderBox = self._getBox(style, "border");
  157. var paddingBox = self._getBox(style, "padding");
  158. height = height - borderBox.top - borderBox.bottom - paddingBox.top - paddingBox.bottom;
  159. }
  160. return height;
  161. }
  162. // Display types for which margin is ignored.
  163. var noMarginDisplayType = {
  164. "table-cell": true,
  165. "table-column": true,
  166. "table-column-group": true,
  167. "table-footer-group": true,
  168. "table-header-group": true,
  169. "table-row": true,
  170. "table-row-group": true
  171. };
  172. // Display types for which padding is ignored.
  173. var noPaddingDisplayType = {
  174. "table-column": true,
  175. "table-column-group": true,
  176. "table-footer-group": true,
  177. "table-header-group": true,
  178. "table-row": true,
  179. "table-row-group": true
  180. };
  181. // Position types for which top, left, bottom and right are ignored.
  182. var noPositionType = {
  183. "static": true
  184. };
  185. var boxes = ["content", "padding", "border", "margin", "position"];
  186. var boxColors = [
  187. WebInspector.Color.PageHighlight.Content,
  188. WebInspector.Color.PageHighlight.Padding,
  189. WebInspector.Color.PageHighlight.Border,
  190. WebInspector.Color.PageHighlight.Margin,
  191. WebInspector.Color.fromRGBA(0, 0, 0, 0)
  192. ];
  193. var boxLabels = [WebInspector.UIString("content"), WebInspector.UIString("padding"), WebInspector.UIString("border"), WebInspector.UIString("margin"), WebInspector.UIString("position")];
  194. var previousBox = null;
  195. this._boxElements = [];
  196. for (var i = 0; i < boxes.length; ++i) {
  197. var name = boxes[i];
  198. if (name === "margin" && noMarginDisplayType[style.getPropertyValue("display")])
  199. continue;
  200. if (name === "padding" && noPaddingDisplayType[style.getPropertyValue("display")])
  201. continue;
  202. if (name === "position" && noPositionType[style.getPropertyValue("position")])
  203. continue;
  204. var boxElement = document.createElement("div");
  205. boxElement.className = name;
  206. boxElement._backgroundColor = boxColors[i].toString("original");
  207. boxElement._name = name;
  208. boxElement.style.backgroundColor = boxElement._backgroundColor;
  209. boxElement.addEventListener("mouseover", this._highlightDOMNode.bind(this, true, name === "position" ? "all" : name), false);
  210. this._boxElements.push(boxElement);
  211. if (name === "content") {
  212. var widthElement = document.createElement("span");
  213. widthElement.textContent = getContentAreaWidthPx(style);
  214. widthElement.addEventListener("dblclick", this.startEditing.bind(this, widthElement, "width", "width", style), false);
  215. var heightElement = document.createElement("span");
  216. heightElement.textContent = getContentAreaHeightPx(style);
  217. heightElement.addEventListener("dblclick", this.startEditing.bind(this, heightElement, "height", "height", style), false);
  218. boxElement.appendChild(widthElement);
  219. boxElement.appendChild(document.createTextNode(" \u00D7 "));
  220. boxElement.appendChild(heightElement);
  221. } else {
  222. var suffix = (name === "border" ? "-width" : "");
  223. var labelElement = document.createElement("div");
  224. labelElement.className = "label";
  225. labelElement.textContent = boxLabels[i];
  226. boxElement.appendChild(labelElement);
  227. boxElement.appendChild(createBoxPartElement.call(this, style, name, "top", suffix));
  228. boxElement.appendChild(document.createElement("br"));
  229. boxElement.appendChild(createBoxPartElement.call(this, style, name, "left", suffix));
  230. if (previousBox)
  231. boxElement.appendChild(previousBox);
  232. boxElement.appendChild(createBoxPartElement.call(this, style, name, "right", suffix));
  233. boxElement.appendChild(document.createElement("br"));
  234. boxElement.appendChild(createBoxPartElement.call(this, style, name, "bottom", suffix));
  235. }
  236. previousBox = boxElement;
  237. }
  238. metricsElement.appendChild(previousBox);
  239. metricsElement.addEventListener("mouseover", this._highlightDOMNode.bind(this, false, ""), false);
  240. this.bodyElement.removeChildren();
  241. this.bodyElement.appendChild(metricsElement);
  242. },
  243. startEditing: function(targetElement, box, styleProperty, computedStyle)
  244. {
  245. if (WebInspector.isBeingEdited(targetElement))
  246. return;
  247. var context = { box: box, styleProperty: styleProperty, computedStyle: computedStyle };
  248. var boundKeyDown = this._handleKeyDown.bind(this, context, styleProperty);
  249. context.keyDownHandler = boundKeyDown;
  250. targetElement.addEventListener("keydown", boundKeyDown, false);
  251. this._isEditingMetrics = true;
  252. var config = new WebInspector.EditingConfig(this.editingCommitted.bind(this), this.editingCancelled.bind(this), context);
  253. WebInspector.startEditing(targetElement, config);
  254. window.getSelection().setBaseAndExtent(targetElement, 0, targetElement, 1);
  255. },
  256. _handleKeyDown: function(context, styleProperty, event)
  257. {
  258. var element = event.currentTarget;
  259. function finishHandler(originalValue, replacementString)
  260. {
  261. this._applyUserInput(element, replacementString, originalValue, context, false);
  262. }
  263. function customNumberHandler(number)
  264. {
  265. if (styleProperty !== "margin" && number < 0)
  266. number = 0;
  267. return number;
  268. }
  269. WebInspector.handleElementValueModifications(event, element, finishHandler.bind(this), undefined, customNumberHandler);
  270. },
  271. editingEnded: function(element, context)
  272. {
  273. delete this.originalPropertyData;
  274. delete this.previousPropertyDataCandidate;
  275. element.removeEventListener("keydown", context.keyDownHandler, false);
  276. delete this._isEditingMetrics;
  277. },
  278. editingCancelled: function(element, context)
  279. {
  280. if ("originalPropertyData" in this && this.inlineStyle) {
  281. if (!this.originalPropertyData) {
  282. // An added property, remove the last property in the style.
  283. var pastLastSourcePropertyIndex = this.inlineStyle.pastLastSourcePropertyIndex();
  284. if (pastLastSourcePropertyIndex)
  285. this.inlineStyle.allProperties[pastLastSourcePropertyIndex - 1].setText("", false);
  286. } else
  287. this.inlineStyle.allProperties[this.originalPropertyData.index].setText(this.originalPropertyData.propertyText, false);
  288. }
  289. this.editingEnded(element, context);
  290. this.update();
  291. },
  292. _applyUserInput: function(element, userInput, previousContent, context, commitEditor)
  293. {
  294. if (!this.inlineStyle) {
  295. // Element has no renderer.
  296. return this.editingCancelled(element, context); // nothing changed, so cancel
  297. }
  298. if (commitEditor && userInput === previousContent)
  299. return this.editingCancelled(element, context); // nothing changed, so cancel
  300. if (context.box !== "position" && (!userInput || userInput === "\u2012"))
  301. userInput = "0px";
  302. else if (context.box === "position" && (!userInput || userInput === "\u2012"))
  303. userInput = "auto";
  304. userInput = userInput.toLowerCase();
  305. // Append a "px" unit if the user input was just a number.
  306. if (/^\d+$/.test(userInput))
  307. userInput += "px";
  308. var styleProperty = context.styleProperty;
  309. var computedStyle = context.computedStyle;
  310. if (computedStyle.getPropertyValue("box-sizing") === "border-box" && (styleProperty === "width" || styleProperty === "height")) {
  311. if (!userInput.match(/px$/)) {
  312. WebInspector.log("For elements with box-sizing: border-box, only absolute content area dimensions can be applied", WebInspector.ConsoleMessage.MessageLevel.Error, true);
  313. return;
  314. }
  315. var borderBox = this._getBox(computedStyle, "border");
  316. var paddingBox = this._getBox(computedStyle, "padding");
  317. var userValuePx = Number(userInput.replace(/px$/, ""));
  318. if (isNaN(userValuePx))
  319. return;
  320. if (styleProperty === "width")
  321. userValuePx += borderBox.left + borderBox.right + paddingBox.left + paddingBox.right;
  322. else
  323. userValuePx += borderBox.top + borderBox.bottom + paddingBox.top + paddingBox.bottom;
  324. userInput = userValuePx + "px";
  325. }
  326. this.previousPropertyDataCandidate = null;
  327. var self = this;
  328. var callback = function(style) {
  329. if (!style)
  330. return;
  331. self.inlineStyle = style;
  332. if (!("originalPropertyData" in self))
  333. self.originalPropertyData = self.previousPropertyDataCandidate;
  334. if (typeof self._highlightMode !== "undefined") {
  335. WebInspector.domAgent.highlightDOMNode(self.node.id, self._highlightMode);
  336. }
  337. if (commitEditor) {
  338. self.dispatchEventToListeners("metrics edited");
  339. self.update();
  340. }
  341. };
  342. var allProperties = this.inlineStyle.allProperties;
  343. for (var i = 0; i < allProperties.length; ++i) {
  344. var property = allProperties[i];
  345. if (property.name !== context.styleProperty || property.inactive)
  346. continue;
  347. this.previousPropertyDataCandidate = property;
  348. property.setValue(userInput, commitEditor, true, callback);
  349. return;
  350. }
  351. this.inlineStyle.appendProperty(context.styleProperty, userInput, callback);
  352. },
  353. editingCommitted: function(element, userInput, previousContent, context)
  354. {
  355. this.editingEnded(element, context);
  356. this._applyUserInput(element, userInput, previousContent, context, true);
  357. },
  358. __proto__: WebInspector.SidebarPane.prototype
  359. }