Improve flexibility of reading rectangles in Path.Constructor.

We can now define the rectangle by any property that it has setters for.
This commit is contained in:
Jürg Lehni 2013-03-01 13:52:11 -08:00
parent b9c58d1732
commit 06e33ba412
2 changed files with 21 additions and 14 deletions

View file

@ -61,12 +61,13 @@ this.Base = Base.inject(/** @lends Base# */{
/**
* Sets all the properties of the passed object literal to their values on
* the item it is called on, and returns the item itself.
* the item it is called on, if the item has property of the given name (or
* a setter defined for it), annd returns the item itself.
*/
set: function(props) {
if (props) {
for (var key in props)
if (props.hasOwnProperty(key))
if (props.hasOwnProperty(key) && key in this)
this[key] = props[key];
}
return this;
@ -79,8 +80,7 @@ this.Base = Base.inject(/** @lends Base# */{
* object. It returns undefined otherwise.
*/
_set: function(props) {
if (Base.isPlainObject(props))
return this.set(props);
return Base.isPlainObject(props) && this.set(props);
},
statics: /** @lends Base */{
@ -257,23 +257,30 @@ this.Base = Base.inject(/** @lends Base# */{
/**
* @return the named value if the list provides an arguments object,
* null if the named value is null or undefined, and undefined if there
* is no arguments object.
* {@code null} if the named value is {@code null} or {@code undefined},
* and {@code undefined} if there is no arguments object.
* If no name is provided, it returns the whole arguments object.
*/
getNamed: function(list, name) {
var arg = list[0];
if (list._hasObject === undefined)
list._hasObject = list.length === 1 && Base.isPlainObject(arg);
if (list._hasObject) {
value = arg[name];
// Return the whole arguments object if no name is provided.
value = name ? arg[name] : arg;
// Convert undefined to null, to distinguish from undefined
// result, when there is no arguments object.
return value !== undefined ? value : null;
}
},
/**
* Checks if the argument list has a named argument with the given name.
* If name is {@code null}, it returns {@code true} if there are any
* named arguments.
*/
hasNamed: function(list, name) {
return !!this.getNamed(list, name);
return !name && list._hasObject || !!this.getNamed(list, name);
},
/**

View file

@ -17,13 +17,13 @@ Path.inject({ statics: new function() {
if (Base.hasNamed(list, 'from')) {
rect = new Rectangle(Point.readNamed(list, 'from'),
Point.readNamed(list, 'to'));
} else if (Base.hasNamed(list, 'size')) {
rect = new Rectangle(Point.readNamed(list, 'point'),
Size.readNamed(list, 'size'));
if (Base.hasNamed(list, 'center'))
rect.setCenter(Point.readNamed(list, 'center'));
} else if (Base.hasNamed(list)) {
rect = Base.each(Base.getNamed(list), function(value, key) {
if (key in this)
this[key] = Base.readNamed(list, key);
}, new Rectangle());
} else {
rect = Rectangle.readNamed(list, 'rectangle');
rect = Rectangle.read(list);
}
return rect;
}