Element.Layout fixes.
authorJan Schneider <jan@horde.org>
Tue, 4 May 2010 15:42:16 +0000 (17:42 +0200)
committerJan Schneider <jan@horde.org>
Mon, 10 May 2010 13:49:14 +0000 (15:49 +0200)
horde/js/prototype.js

index e761871..90a583f 100644 (file)
@@ -4967,8 +4967,8 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
   // Converts the layout hash property names back to the CSS equivalents.
   // For now, only the border properties differ.
   function cssNameFor(key) {
-    if (key.includes('border')) return key + '-width';
-    return key;
+    if (key.include('border')) key = key + '-width';
+    return key.camelize();
   }
   
   /**
@@ -5173,8 +5173,8 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
         // Either way, it means the element is the width it needs to be
         // in order to report an accurate height.
         newWidth = getPixelValue(width);
-      } else if (positionedWidth && (position === 'absolute' || position === 'fixed')) {
-        newWidth = getPixelValue(positionedWidth);
+      } else if (width && (position === 'absolute' || position === 'fixed')) {
+        newWidth = getPixelValue(width);
       } else {
         // If not, that means the element's width depends upon the width of
         // its parent.
@@ -5212,6 +5212,45 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
     },
     
     /**
+     *  Element.Layout#toObject([keys...]) -> Object
+     *  - keys (String): A space-separated list of keys to include.
+     *  
+     *  Converts the layout hash to a plain object of key/value pairs,
+     *  optionally including only the given keys.
+     * 
+     *  Keys can be passed into this method as individual arguments _or_
+     *  separated by spaces within a string.
+    **/
+    toObject: function() {
+      var args = $A(arguments);
+      var keys = (args.length === 0) ? Element.Layout.PROPERTIES :
+       args.join(' ').split(' ');
+      var obj = {};
+      keys.each( function(key) {
+        // Key needs to be a valid Element.Layout property.
+        if (!Element.Layout.PROPERTIES.include(key)) return;
+        var value = this.get(key);
+        if (value != null) obj[key] = value;
+      }, this);
+      return obj;
+    },
+    
+    /**
+     *  Element.Layout#toHash([keys...]) -> Hash
+     *  - keys (String): A space-separated list of keys to include.
+     *  
+     *  Converts the layout hash to an ordinary hash of key/value pairs,
+     *  optionally including only the given keys.
+     * 
+     *  Keys can be passed into this method as individual arguments _or_
+     *  separated by spaces within a string.
+    **/
+    toHash: function() {
+      var obj = this.toObject.apply(this, arguments);
+      return new Hash(obj);
+    },
+    
+    /**
      *  Element.Layout#toCSS([keys...]) -> Object
      *  - keys (String): A space-separated list of keys to include.
      *
@@ -5232,6 +5271,7 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
       var keys = (args.length === 0) ? Element.Layout.PROPERTIES :
        args.join(' ').split(' ');
       var css = {};
+
       keys.each( function(key) {
         // Key needs to be a valid Element.Layout property...
         if (!Element.Layout.PROPERTIES.include(key)) return;        
@@ -5241,8 +5281,8 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
         var value = this.get(key);
         // Unless the value is null, add 'px' to the end and add it to the
         // returned object.
-        if (value) css[cssNameFor(key)] = value + 'px';
-      });
+        if (value != null) css[cssNameFor(key)] = value + 'px';
+      }, this);
       return css;
     },
     
@@ -5277,12 +5317,12 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
         
         var bTop = this.get('border-top'),
          bBottom = this.get('border-bottom');
-         
+
         var pTop = this.get('padding-top'),
          pBottom = this.get('padding-bottom');
-         
+
         if (!this._preComputing) this._end();
-        
+
         return bHeight - bTop - bBottom - pTop - pBottom;
       },
       
@@ -5719,11 +5759,11 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
     }
     
     var offsetParent = getOffsetParent(element);    
-    var eOffset = element.viewportOffset(), pOffset = 
-     offsetParent.viewportOffset();
+    var eOffset = element.viewportOffset(),
+     pOffset = offsetParent.viewportOffset();
      
-    var offset = eOffset.relativeTo(pOffset);    
-    var layout = element.get('layout');    
+    var offset = eOffset.relativeTo(pOffset);
+    var layout = element.getLayout();    
     
     element.store('prototype_absolutize_original_styles', {
       left:   element.getStyle('left'),
@@ -5768,7 +5808,7 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
   Element.addMethods({
     getLayout:              getLayout
   });
-
+  
   function isBody(element) {
     return element.nodeName.toUpperCase() === 'BODY';
   }
@@ -5833,3 +5873,4 @@ Object.extend(Element.ClassNames.prototype, Enumerable);
     });    
   }  
 })();
+