Permalink
Browse files

Add `Element.Layout#toObject` and `Element.Layout.toHash`.

  • Loading branch information...
1 parent 2fd5c9b commit 949dc83fd4533be0077eea5d6c92a78caa0aae98 @savetheclocktower savetheclocktower committed Apr 18, 2010
Showing with 41 additions and 0 deletions.
  1. +2 −0 CHANGELOG
  2. +39 −0 src/dom/layout.js
View
@@ -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)
View
@@ -339,6 +339,45 @@
},
/**
+ * 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;
+ });
@RStankov

RStankov Apr 18, 2010

Contributor

Here one ",this" is needed to bind the function to Element.Layout scope. Currently this.get(key) gives an error.
I'm wondering if Element.Layout.PROPERTIES.include(key) is needed, since this.get will throw Exception on error, and I don't know which should be the expected behavior here.
Plus Enum.inject will look nice here :)

p.s. Letter today could try to make a patch for this + the units tests

@savetheclocktower

savetheclocktower Apr 18, 2010

Collaborator

That's what I get for copy/pasting code. :-)

@RStankov

RStankov Apr 18, 2010

Contributor

Tell me about it :) I have 2-3 errors like this the past week, but this is the good part of github and the open source as whole :)

+ 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.
*

0 comments on commit 949dc83

Please sign in to comment.