From 949dc83fd4533be0077eea5d6c92a78caa0aae98 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sun, 18 Apr 2010 13:32:27 -0400 Subject: [PATCH] Add `Element.Layout#toObject` and `Element.Layout.toHash`. --- CHANGELOG | 2 ++ src/dom/layout.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index de455088a..8c94b1575 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +Add `Element.Layout#toObject` and `Element.Layout.toHash`. (Andrew Dupont) + Make `Element.Layout#toCSS` return camelized property names, as expected by `Element.setStyle`. [#1021 state:resolved] (njakobsen, Andrew Dupont) *1.7_rc1* (April 1, 2010) diff --git a/src/dom/layout.js b/src/dom/layout.js index d38dbb602..7a6881e58 100644 --- a/src/dom/layout.js +++ b/src/dom/layout.js @@ -338,6 +338,45 @@ return this._set(property, COMPUTATIONS[property].call(this, this.element)); }, + /** + * 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; + }); + 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.