Improve Base.read() to support cloning of objects that are already provided in the required type.

This commit is contained in:
Jürg Lehni 2012-10-10 19:26:00 -07:00
parent 0a93465e94
commit 4d1920ee8b
2 changed files with 18 additions and 11 deletions

View file

@ -133,8 +133,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
},
setOrigin: function(origin) {
// PORT: Add clone to Scriptographer
origin = Point.read(arguments).clone();
// PORT: Add origin cloning to Scriptographer
origin = Point.read(arguments, 0, 0, true); // clone
this._origin = origin;
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
@ -174,8 +174,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
},
setDestination: function(destination) {
// PORT: Add clone to Scriptographer
destination = Point.read(arguments).clone();
// PORT: Add destination cloning to Scriptographer
destination = Point.read(arguments, 0, 0, true); // clone
this._destination = destination;
this._radius = this._destination.getDistance(this._origin);
this._changed();
@ -213,8 +213,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
},
setHilite: function(hilite) {
// PORT: Add clone to Scriptographer
hilite = Point.read(arguments).clone();
// PORT: Add hilite cloning to Scriptographer
hilite = Point.read(arguments, 0, 0, true); // clone
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(

View file

@ -57,8 +57,12 @@ this.Base = Base.inject(/** @lends Base# */{
* the specified length. This is used in argument conversion, e.g. by
* all basic types (Point, Size, Rectangle) and also higher classes such
* as Color and Segment.
* @param {Number} start the index at which to start reading in the list
* @param {Number} length the amount of elements that can be read
* @param {Boolean} clone controls wether passed objects should be
* cloned if they are already provided in the required type
*/
read: function(list, start, length) {
read: function(list, start, length, clone) {
var start = start || 0,
length = length || list.length - start;
var obj = list[start];
@ -66,7 +70,7 @@ this.Base = Base.inject(/** @lends Base# */{
// If the class defines _readNull, return null when nothing
// was provided
|| this.prototype._readNull && obj == null && length <= 1)
return obj;
return obj && clone ? obj.clone() : obj;
obj = new this(this.dont);
return obj.initialize.apply(obj, start > 0 || length < list.length
? Array.prototype.slice.call(list, start, start + length)
@ -76,13 +80,16 @@ this.Base = Base.inject(/** @lends Base# */{
/**
* Reads all readable arguments from the list, handling nested arrays
* seperately.
* @param {Number} start the index at which to start reading in the list
* @param {Boolean} clone controls wether passed objects should be
* cloned if they are already provided in the required type
*/
readAll: function(list, start) {
readAll: function(list, start, clone) {
var res = [], entry;
for (var i = start || 0, l = list.length; i < l; i++) {
res.push(Array.isArray(entry = list[i])
? this.read(entry, 0)
: this.read(list, i, 1));
? this.read(entry, 0, 0, clone) // 0 for length = max
: this.read(list, i, 1, clone));
}
return res;
},