home *** CD-ROM | disk | FTP | other *** search
/ Computer Active Guide 2009 July / CAG7.ISO / Internetas / SafariSetup.exe / AppleApplicationSupport.msi / WebKit.resources_inspector_MetricsSidebarPane.js < prev    next >
Encoding:
Text File  |  2010-06-03  |  8.7 KB  |  218 lines

  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. WebInspector.MetricsSidebarPane = function()
  30. {
  31.     WebInspector.SidebarPane.call(this, WebInspector.UIString("Metrics"));
  32.     this._inlineStyleId = null;
  33. }
  34.  
  35. WebInspector.MetricsSidebarPane.prototype = {
  36.     update: function(node)
  37.     {
  38.         if (node)
  39.             this.node = node;
  40.         else
  41.             node = this.node;
  42.  
  43.         if (!node || !node.ownerDocument.defaultView || node.nodeType !== Node.ELEMENT_NODE) {
  44.             this.bodyElement.removeChildren();
  45.             return;
  46.         }
  47.  
  48.         var self = this;
  49.         var callback = function(stylePayload) {
  50.             if (!stylePayload)
  51.                 return;
  52.             var style = WebInspector.CSSStyleDeclaration.parseStyle(stylePayload);
  53.             self._update(style);
  54.         };
  55.         InspectorBackend.getComputedStyle(WebInspector.Callback.wrap(callback), node.id);
  56.  
  57.         var inlineStyleCallback = function(stylePayload) {
  58.             if (!stylePayload)
  59.                 return;
  60.             self._inlineStyleId = stylePayload.id;
  61.         };
  62.         InspectorBackend.getInlineStyle(WebInspector.Callback.wrap(inlineStyleCallback), node.id);
  63.     },
  64.  
  65.     _update: function(style)
  66.     {
  67.         var metricsElement = document.createElement("div");
  68.         metricsElement.className = "metrics";
  69.  
  70.         function createBoxPartElement(style, name, side, suffix)
  71.         {
  72.             var propertyName = (name !== "position" ? name + "-" : "") + side + suffix;
  73.             var value = style.getPropertyValue(propertyName);
  74.             if (value === "" || (name !== "position" && value === "0px"))
  75.                 value = "\u2012";
  76.             else if (name === "position" && value === "auto")
  77.                 value = "\u2012";
  78.             value = value.replace(/px$/, "");
  79.  
  80.             var element = document.createElement("div");
  81.             element.className = side;
  82.             element.textContent = value;
  83.             element.addEventListener("dblclick", this.startEditing.bind(this, element, name, propertyName), false);
  84.             return element;
  85.         }
  86.  
  87.         // Display types for which margin is ignored.
  88.         var noMarginDisplayType = {
  89.             "table-cell": true,
  90.             "table-column": true,
  91.             "table-column-group": true,
  92.             "table-footer-group": true,
  93.             "table-header-group": true,
  94.             "table-row": true,
  95.             "table-row-group": true
  96.         };
  97.  
  98.         // Display types for which padding is ignored.
  99.         var noPaddingDisplayType = {
  100.             "table-column": true,
  101.             "table-column-group": true,
  102.             "table-footer-group": true,
  103.             "table-header-group": true,
  104.             "table-row": true,
  105.             "table-row-group": true
  106.         };
  107.  
  108.         // Position types for which top, left, bottom and right are ignored.
  109.         var noPositionType = {
  110.             "static": true
  111.         };
  112.  
  113.         var boxes = ["content", "padding", "border", "margin", "position"];
  114.         var boxLabels = [WebInspector.UIString("content"), WebInspector.UIString("padding"), WebInspector.UIString("border"), WebInspector.UIString("margin"), WebInspector.UIString("position")];
  115.         var previousBox;
  116.         for (var i = 0; i < boxes.length; ++i) {
  117.             var name = boxes[i];
  118.  
  119.             if (name === "margin" && noMarginDisplayType[style.display])
  120.                 continue;
  121.             if (name === "padding" && noPaddingDisplayType[style.display])
  122.                 continue;
  123.             if (name === "position" && noPositionType[style.position])
  124.                 continue;
  125.  
  126.             var boxElement = document.createElement("div");
  127.             boxElement.className = name;
  128.  
  129.             if (name === "content") {
  130.                 var width = style.width.replace(/px$/, "");
  131.                 var widthElement = document.createElement("span");
  132.                 widthElement.textContent = width;
  133.                 widthElement.addEventListener("dblclick", this.startEditing.bind(this, widthElement, "width", "width"), false);
  134.  
  135.                 var height = style.height.replace(/px$/, "");
  136.                 var heightElement = document.createElement("span");
  137.                 heightElement.textContent = height;
  138.                 heightElement.addEventListener("dblclick", this.startEditing.bind(this, heightElement, "height", "height"), false);
  139.  
  140.                 boxElement.appendChild(widthElement);
  141.                 boxElement.appendChild(document.createTextNode(" \u00D7 "));
  142.                 boxElement.appendChild(heightElement);
  143.             } else {
  144.                 var suffix = (name === "border" ? "-width" : "");
  145.  
  146.                 var labelElement = document.createElement("div");
  147.                 labelElement.className = "label";
  148.                 labelElement.textContent = boxLabels[i];
  149.                 boxElement.appendChild(labelElement);
  150.  
  151.                 boxElement.appendChild(createBoxPartElement.call(this, style, name, "top", suffix));
  152.                 boxElement.appendChild(document.createElement("br"));
  153.                 boxElement.appendChild(createBoxPartElement.call(this, style, name, "left", suffix));
  154.  
  155.                 if (previousBox)
  156.                     boxElement.appendChild(previousBox);
  157.  
  158.                 boxElement.appendChild(createBoxPartElement.call(this, style, name, "right", suffix));
  159.                 boxElement.appendChild(document.createElement("br"));
  160.                 boxElement.appendChild(createBoxPartElement.call(this, style, name, "bottom", suffix));
  161.             }
  162.  
  163.             previousBox = boxElement;
  164.         }
  165.  
  166.         metricsElement.appendChild(previousBox);
  167.         this.bodyElement.removeChildren();
  168.         this.bodyElement.appendChild(metricsElement);
  169.     },
  170.  
  171.     startEditing: function(targetElement, box, styleProperty)
  172.     {
  173.         if (WebInspector.isBeingEdited(targetElement))
  174.             return;
  175.  
  176.         var context = { box: box, styleProperty: styleProperty };
  177.  
  178.         WebInspector.startEditing(targetElement, this.editingCommitted.bind(this), this.editingCancelled.bind(this), context);
  179.     },
  180.  
  181.     editingCancelled: function(element, context)
  182.     {
  183.         this.update();
  184.     },
  185.  
  186.     editingCommitted: function(element, userInput, previousContent, context)
  187.     {
  188.         if (!this._inlineStyleId) {
  189.             // Element has no renderer.
  190.             return this.editingCancelled(element, context); // nothing changed, so cancel
  191.         }
  192.  
  193.         if (userInput === previousContent)
  194.             return this.editingCancelled(element, context); // nothing changed, so cancel
  195.  
  196.         if (context.box !== "position" && (!userInput || userInput === "\u2012"))
  197.             userInput = "0px";
  198.         else if (context.box === "position" && (!userInput || userInput === "\u2012"))
  199.             userInput = "auto";
  200.  
  201.         // Append a "px" unit if the user input was just a number.
  202.         if (/^\d+$/.test(userInput))
  203.             userInput += "px";
  204.  
  205.         var self = this;
  206.         var callback = function(success) {
  207.             if (!success)
  208.                 return;
  209.             self.dispatchEventToListeners("metrics edited");
  210.             self.update();
  211.         };
  212.  
  213.         InspectorBackend.setStyleProperty(WebInspector.Callback.wrap(callback), this._inlineStyleId, context.styleProperty, userInput);
  214.     }
  215. }
  216.  
  217. WebInspector.MetricsSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;
  218.