Generalise all read() functions in Base.read(), fix an issue in by making sure arguments passed to initialie are never larger than length, and have Color's initialize create an RGBColor, to still be able to use Color.read().

This commit is contained in:
Jürg Lehni 2011-03-04 13:19:07 +00:00
parent 3837ca1f14
commit c6d79f964c
7 changed files with 22 additions and 86 deletions

16
lib/bootstrap.js vendored
View file

@ -199,6 +199,22 @@ new function() {
return null;
},
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof this) {
return args[index];
} else if (length != 0) {
var obj = new this(this.dont);
if (!obj.initialize)
debugger;
obj.initialize.apply(obj, index > 0 || length < args.length
? Array.prototype.slice.call(args, index, index + length)
: args);
return obj;
}
return null;
},
iterator: function(iter) {
return !iter
? function(val) { return val }

View file

@ -477,19 +477,6 @@ var Point = Base.extend({
return point;
},
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Point) {
return args[index];
} else if (length != 0) {
var point = new Point(Point.dont);
point.initialize.apply(point, index > 0
? Array.prototype.slice.call(args, index) : args);
return point;
}
return null;
},
/**
* Returns a new point object with the smallest {@link #x} and
* {@link #y} of the supplied points.

View file

@ -282,20 +282,5 @@ var Rectangle = Base.extend({
+ ', width: ' + this.width
+ ', height: ' + this.height
+ ' }';
},
statics: {
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Rectangle) {
return args[index];
} else if (length != 0) {
var rect = new Rectangle(Rectangle.dont);
rect.initialize.apply(rect, index > 0
? Array.prototype.slice.call(args, index) : args);
return rect;
}
return null;
}
}
});

View file

@ -102,19 +102,6 @@ var Size = Base.extend({
return size;
},
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Size) {
return args[index];
} else if (length != 0) {
var size = new Size(Size.dont);
size.initialize.apply(size, index > 0
? Array.prototype.slice.call(args, index) : args);
return size;
}
return null;
},
min: function(Size1, Size2) {
return Size.create(
Math.min(Size1.width, Size2.width),

View file

@ -1,6 +1,12 @@
var Color = Base.extend({
beans: true,
initialize: function() {
var rgb = new RGBColor(RGBColor.dont);
rgb.initialize.apply(rgb, arguments);
return rgb;
},
/**
* A value between 0 and 1 that specifies the color's alpha value.
* All colors of the different subclasses support alpha values.
@ -28,20 +34,5 @@ var Color = Base.extend({
getCanvasStyle: function() {
return this.cssString;
},
statics: {
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Color) {
return args[index];
} else if (length != 0 && args[0] !== null) {
var rgbColor = new RGBColor(RGBColor.dont);
rgbColor.initialize.apply(rgbColor, index > 0
? Array.prototype.slice.call(args, index) : args);
return rgbColor;
}
return null;
}
}
});

View file

@ -1,19 +1,4 @@
var Curve = Base.extend({
initialize: function() {
},
statics: {
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Curve) {
return args[index];
} else if (length != 0) {
var curve = new Curve(Curve.dont);
curve.initialize.apply(curve, index > 0
? Array.prototype.slice.call(args, index) : args);
return curve;
}
return null;
}
}
});

View file

@ -118,20 +118,5 @@ var Segment = Base.extend({
+ (this.handleIn ? ', handleIn '+ this.handleIn : '')
+ (this.handleOut ? ', handleOut ' + this.handleOut : '')
+ ' }';
},
statics: {
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
if (length == 1 && args[index] instanceof Segment) {
return args[index];
} else if (length != 0) {
var segment = new Segment(Segment.dont);
segment.initialize.apply(segment, index > 0
? Array.prototype.slice.call(args, index) : args);
return segment;
}
return null;
}
}
});