Factory getters now require function defaults which return an object literal because objects can be modified by reference

This commit is contained in:
Eric Rowell
2013-12-03 22:54:16 -08:00
parent acd9518ab9
commit fb25ba7c59
14 changed files with 179 additions and 102 deletions

View File

@@ -225,7 +225,9 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addGetter(Kinetic.Node, 'draggable', false);
Kinetic.Factory.addGetter(Kinetic.Node, 'draggable', function() {
return false;
});
/**
* get draggable

View File

@@ -43,14 +43,6 @@
Kinetic.Factory = {
addGetterSetter: function(constructor, baseAttr, def) {
var util = Kinetic.Util
if (util._isArray(def)) {
def = util.cloneArray(def);
}
else if (util._isObject(def)) {
def = util.cloneObject(def);
}
this.addGetter(constructor, baseAttr, def);
this.addSetter(constructor, baseAttr);
},
@@ -60,10 +52,14 @@
},
addGetter: function(constructor, baseAttr, def) {
var method = GET + Kinetic.Util._capitalize(baseAttr);
// if default function is not defined, create a default default function
if (!def) {
def = function(){};
}
constructor.prototype[method] = function() {
var val = this.attrs[baseAttr];
return val === undefined ? def : val;
return val === undefined ? def() : val;
};
},
addSetter: function(constructor, baseAttr) {

View File

@@ -268,7 +268,9 @@
Kinetic.Util.extend(Kinetic.Layer, Kinetic.Container);
// add getters and setters
Kinetic.Factory.addGetterSetter(Kinetic.Layer, 'clearBeforeDraw', true);
Kinetic.Factory.addGetterSetter(Kinetic.Layer, 'clearBeforeDraw', function() {
return true;
});
/**
* set flag which determines if the layer is cleared or not

View File

@@ -1183,10 +1183,11 @@
oldVal = this.attrs[key];
if (!oldVal) {
this.attrs[key] = [];
// set value to default value using getAttr
this.attrs[key] = this.getAttr(key);
}
this._fireBeforeChangeEvent(key, oldVal, val);
//this._fireBeforeChangeEvent(key, oldVal, val);
this.attrs[key][component] = val;
this._fireChangeEvent(key, oldVal, val);
}
@@ -1295,7 +1296,9 @@
};
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'x', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'x', function() {
return 0;
});
/**
* set x position
@@ -1312,7 +1315,9 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'y', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'y', function() {
return 0;
});
/**
* set y position
@@ -1329,7 +1334,9 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'opacity', 1);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'opacity', function() {
return 1;
});
/**
* set opacity. Opacity values range from 0 to 1.
@@ -1398,7 +1405,9 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'scale', {x: 1, y: 1});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'scale', function() {
return {x: 1, y: 1};
});
/**
* set scale
@@ -1455,31 +1464,35 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addPointGetterSetter(Kinetic.Node, 'skew', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'skew', function() {
return {x: 0, y: 0};
});
/**
* set skew
* @name setSkew
* @param {Number} x
* @param {Number} y
* @param {Object} skew
* @param {Number} skew.x
* @param {Number} skew.y
* @method
* @memberof Kinetic.Node.prototype
* @example
* // set x and y <br>
* shape.setSkew(20, 40);<br><br>
*
* // set x only <br>
* shape.setSkew({<br>
* x: 20<br>
* });<br><br>
*
* // set x and y using an array<br>
* shape.setSkew([20, 40]);<br><br>
*
* // set x and y to the same value<br>
* shape.setSkew(5);
* y: 10
* });
*/
/**
* get skew
* @name getSkew
* @method
* @memberof Kinetic.Node.prototype
* @returns {Object}
*/
Kinetic.Factory.addComponentGetterSetter(Kinetic.Node, 'skew', 'x', 0);
/**
* set skew x
* @name setSkewX
@@ -1488,6 +1501,15 @@
* @memberof Kinetic.Node.prototype
*/
/**
* get skew x
* @name getSkewX
* @method
* @memberof Kinetic.Node.prototype
* @returns {Number}
*/
Kinetic.Factory.addComponentGetterSetter(Kinetic.Node, 'skew', 'y', 0);
/**
* set skew y
* @name setSkewY
@@ -1496,28 +1518,17 @@
* @memberof Kinetic.Node.prototype
*/
/**
* get skew
* @name getSkew
* @method
* @memberof Kinetic.Node.prototype
*/
/**
* get skew x
* @name getSkewX
* @method
* @memberof Kinetic.Node.prototype
*/
/**
* get skew y
* @name getSkewY
* @method
* @memberof Kinetic.Node.prototype
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'offset', {x: 0, y: 0});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'offset', function() {
return {x: 0, y: 0};
});
/**
* set offset. A node's offset defines the position and rotation point
@@ -1576,7 +1587,9 @@
* @returns {Number}
*/
Kinetic.Factory.addSetter(Kinetic.Node, 'width');
Kinetic.Factory.addSetter(Kinetic.Node, 'width', function() {
return 0;
});
/**
* set width
@@ -1586,7 +1599,9 @@
* @param {Number} width
*/
Kinetic.Factory.addSetter(Kinetic.Node, 'height');
Kinetic.Factory.addSetter(Kinetic.Node, 'height', function() {
return 0;
});
/**
* set height
@@ -1596,7 +1611,9 @@
* @param {Number} height
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'listening', true);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'listening', function() {
return true;
});
/**
* listen or don't listen to events
@@ -1613,7 +1630,9 @@
* @memberof Kinetic.Node.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'visible', true);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'visible', function() {
return true;
});
/**
* set visible

View File

@@ -830,7 +830,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillEnabled', true);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillEnabled', function() {
return true;
});
/**
* set fill enabled
@@ -847,7 +849,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeEnabled', true);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeEnabled', function() {
return true;
});
/**
* set stroke enabled
@@ -864,7 +868,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowEnabled', true);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowEnabled', function() {
return true;
});
/**
* set shadow enabled
@@ -881,7 +887,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'dashArrayEnabled', true);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'dashArrayEnabled', function() {
return true;
});
/**
* set dash array enabled
@@ -898,7 +906,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPriority', 'color');
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPriority', function() {
return 'color';
});
/**
* set fill priority
@@ -916,7 +926,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeScaleEnabled', true);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeScaleEnabled', function() {
return true;
});
/**
* set stroke scale enabled
@@ -933,7 +945,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPatternOffset', {x:0, y:0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPatternOffset', function() {
return {x: 0, y: 0};
});
/**
* set fill pattern offset
@@ -991,7 +1005,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPatternScale', {x: 1, y:1});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillPatternScale', function() {
return {x: 1, y: 1};
});
/**
* set fill pattern scale
@@ -1051,7 +1067,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillLinearGradientStartPoint', {x:0, y:0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillLinearGradientStartPoint', function() {
return {x: 0, y: 0};
});
/**
* set fill linear gradient start point
@@ -1111,7 +1129,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillLinearGradientEndPoint', {x: 0, y: 0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillLinearGradientEndPoint', function() {
return {x: 0, y: 0};
});
/**
* set fill linear gradient end point
@@ -1171,7 +1191,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRadialGradientStartPoint', {x: 0, y: 0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRadialGradientStartPoint', function() {
return {x: 0, y: 0};
});
/**
* set fill radial gradient start point
@@ -1229,7 +1251,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRadialGradientEndPoint', {x: 0, y: 0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRadialGradientEndPoint', function() {
return {x: 0, y: 0};
});
/**
* set fill radial gradient end point
@@ -1287,7 +1311,9 @@
* @memberof Kinetic.Shape.prototype
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowOffset', {x: 0, y: 0});
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowOffset', function() {
return {x: 0, y: 0};
});
/**
* set shadow offset

View File

@@ -69,7 +69,9 @@
Kinetic.Util.extend(Kinetic.Circle, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Circle, 'radius', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Circle, 'radius', function() {
return 0;
});
/**
* set radius

View File

@@ -61,7 +61,9 @@
Kinetic.Util.extend(Kinetic.Ellipse, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Ellipse, 'radius', {x:0,y:0});
Kinetic.Factory.addGetterSetter(Kinetic.Ellipse, 'radius', function() {
return {x: 0, y: 0};
});
/**
* set radius

View File

@@ -139,7 +139,9 @@
Kinetic.Util.extend(Kinetic.Line, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Line, 'closed', false);
Kinetic.Factory.addGetterSetter(Kinetic.Line, 'closed', function() {
return false;
});
/**
* get closed
@@ -157,7 +159,9 @@
* @param {Boolean} closed
*/
Kinetic.Factory.addGetterSetter(Kinetic.Line, 'tension', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Line, 'tension', function() {
return 0;
});
/**
* get tension

View File

@@ -57,7 +57,9 @@
Kinetic.Util.extend(Kinetic.Rect, Kinetic.Shape);
Kinetic.Factory.addGetterSetter(Kinetic.Rect, 'cornerRadius', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Rect, 'cornerRadius', function() {
return 0;
});
/**
* set corner radius

View File

@@ -227,7 +227,9 @@
* @returns {ImageObject}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'index', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'index', function() {
return 0;
});
/**
* set animation frame index
@@ -245,7 +247,9 @@
* @returns {Integer}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameRate', function() {
return 17;
});
/**
* set frame rate in frames / second. Default is 17 frames per second. Increase this number to make the sprite

View File

@@ -314,7 +314,9 @@
Kinetic.Util.extend(Kinetic.Text, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontFamily', 'Arial');
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontFamily', function() {
return 'Arial';
});
/**
* set font family
@@ -332,7 +334,9 @@
* @returns {String}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontSize', 12);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontSize', function() {
return 12;
});
/**
* set font size in pixels
@@ -350,7 +354,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontStyle', NORMAL);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'fontStyle', function() {
return NORMAL;
});
/**
* set font style. Can be 'normal', 'italic', or 'bold'. 'normal' is the default.
@@ -368,7 +374,9 @@
* @returns {String}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'padding', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'padding', function() {
return 0;
});
/**
* set padding
@@ -386,7 +394,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'align', LEFT);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'align', function() {
return LEFT;
});
/**
* set horizontal align of text
@@ -404,7 +414,9 @@
* @returns {String}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'lineHeight', 1);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'lineHeight', function() {
return 1;
});
/**
* set line height
@@ -422,7 +434,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'wrap', WORD);
Kinetic.Factory.addGetterSetter(Kinetic.Text, 'wrap', function() {
return WORD;
});
/**
* set wrap
@@ -440,7 +454,9 @@
* @returns {String}
*/
Kinetic.Factory.addGetter(Kinetic.Text, TEXT, EMPTY_STRING);
Kinetic.Factory.addGetter(Kinetic.Text, TEXT, function() {
return EMPTY_STRING;
});
/**
* get text

View File

@@ -42,7 +42,9 @@
Kinetic.Util.extend(Kinetic.Wedge, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Wedge, 'radius', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Wedge, 'radius', function() {
return 0;
});
/**
* set radius
@@ -60,7 +62,9 @@
* @returns {Number}
*/
Kinetic.Factory.addRotationGetterSetter(Kinetic.Wedge, 'angle', 0);
Kinetic.Factory.addRotationGetterSetter(Kinetic.Wedge, 'angle', function() {
return 0;
});
/**
* set angle
@@ -94,7 +98,9 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Wedge, 'clockwise', false);
Kinetic.Factory.addGetterSetter(Kinetic.Wedge, 'clockwise', function() {
return false;
});
/**
* set clockwise draw direction. If set to true, the wedge will be drawn clockwise

View File

@@ -1330,19 +1330,19 @@ suite('Node', function() {
layer.add(rect);
stage.add(layer);
rect.setScale(2, 3);
rect.setScale({x:2, y:3});
assert.equal(rect.getScale().x, 2);
assert.equal(rect.getScale().y, 3);
rect.setScale(4);
rect.setScale({x:4,y:4});
assert.equal(rect.getScale().x, 4);
assert.equal(rect.getScale().y, 4);
rect.setScale([5, 6]);
rect.setScale({x:5, y:6});
assert.equal(rect.getScale().x, 5);
assert.equal(rect.getScale().y, 6);
rect.setScale([7, 8, 999, 999]);
rect.setScale({x: 7, y:8});
assert.equal(rect.getScale().x, 7);
assert.equal(rect.getScale().y, 8);
@@ -1353,15 +1353,11 @@ suite('Node', function() {
assert.equal(rect.getScale().x, 9);
assert.equal(rect.getScale().y, 10);
rect.setScale({
x: 11
});
rect.setScaleX(11);
assert.equal(rect.getScale().x, 11);
assert.equal(rect.getScale().y, 10);
rect.setScale({
y: 12
});
rect.setScaleY(12);
assert.equal(rect.getScale().x, 11);
assert.equal(rect.getScale().y, 12);
@@ -1389,7 +1385,7 @@ suite('Node', function() {
width: 100,
height: 50,
fill: 'red',
scale: 2
scale: {x:2,y:2}
});
var rect3 = new Kinetic.Rect({
@@ -1398,7 +1394,7 @@ suite('Node', function() {
width: 100,
height: 50,
fill: 'red',
scale: [2, 3]
scale: {x:2, y:3}
});
var rect4 = new Kinetic.Rect({
@@ -1407,20 +1403,18 @@ suite('Node', function() {
width: 100,
height: 50,
fill: 'red',
scale: {
x: 2
}
scaleX: 2
});
console.log(rect4);
var rect5 = new Kinetic.Rect({
x: 200,
y: 20,
width: 100,
height: 50,
fill: 'red',
scale: {
y: 2
}
scaleY: 2
});
layer.add(rect1).add(rect2).add(rect3).add(rect4).add(rect5);
@@ -1438,7 +1432,7 @@ suite('Node', function() {
assert.equal(rect4.getScale().x, 2);
assert.equal(rect4.getScale().y, 1);
assert.equal(rect5.getScale().x, 1);
//assert.equal(rect5.getScale().x, 1);
assert.equal(rect5.getScale().y, 2);
});

View File

@@ -18,6 +18,8 @@ suite('Rect', function(){
layer.add(rect);
stage.add(layer);
console.log(rect);
assert.equal(rect.getX(), 100);
assert.equal(rect.getY(), 50);