all methods are now copied to Collection prototype so that you don't have to use the each() method all the time

This commit is contained in:
Eric Rowell 2014-01-19 21:35:28 -08:00
parent 79f86f2fe0
commit f1b1c58166
24 changed files with 112 additions and 37 deletions

View File

@ -394,4 +394,6 @@
* // set clip height<br>
* container.clipHeight(100);
*/
Kinetic.Collection.mapMethods(Kinetic.Container);
})();

View File

@ -13,4 +13,6 @@
}
});
Kinetic.Util.extend(Kinetic.Group, Kinetic.Container);
Kinetic.Collection.mapMethods(Kinetic.Group);
})();

View File

@ -333,5 +333,5 @@
* layer.hitGraphEnabled(true);
*/
Kinetic.Layer.prototype.isHitGraphEnabled = Kinetic.Layer.prototype.getHitGraphEnabled;
Kinetic.Collection.mapMethods(Kinetic.Layer);
})();

View File

@ -594,7 +594,7 @@
*/
shouldDrawHit: function() {
var layer = this.getLayer();
return layer && layer.isHitGraphEnabled() && this.isListening() && this.isVisible() && !Kinetic.isDragging();
return layer && layer.hitGraphEnabled() && this.isListening() && this.isVisible() && !Kinetic.isDragging();
},
/**
* show node
@ -1934,21 +1934,5 @@
getRotationDeg: 'getRotation'
});
Kinetic.Collection.mapMethods([
'on',
'off',
'remove',
'destroy',
'show',
'hide',
'move',
'rotate',
'moveToTop',
'moveUp',
'moveDown',
'moveToBottom',
'moveTo',
'fire',
'draw'
]);
Kinetic.Collection.mapMethods(Kinetic.Node);
})();

View File

@ -1420,4 +1420,6 @@
getDrawHitFunc: 'getHitFunc',
setDrawHitFunc: 'setHitFunc'
});
Kinetic.Collection.mapMethods(Kinetic.Shape);
})();

View File

@ -64,24 +64,23 @@
return collection;
};
Kinetic.Collection.mapMethods = function(arr) {
var leng = arr.length,
n;
Kinetic.Collection.mapMethods = function(constructor) {
var prot = constructor.prototype,
key;
for(n = 0; n < leng; n++) {
for(key in prot) {
// induce scope
(function(i) {
var method = arr[i];
Kinetic.Collection.prototype[method] = function() {
(function(methodName) {
Kinetic.Collection.prototype[methodName] = function() {
var len = this.length,
i;
args = [].slice.call(arguments);
for(i = 0; i < len; i++) {
this[i][method].apply(this[i], args);
this[i][methodName].apply(this[i], args);
}
};
})(n);
})(key);
}
};

View File

@ -154,6 +154,8 @@
Kinetic.Util.extend(Kinetic.Label, Kinetic.Group);
Kinetic.Collection.mapMethods(Kinetic.Label);
/**
* Tag constructor.&nbsp; A Tag can be configured
* to have a pointer element that points up, right, down, or left
@ -291,4 +293,6 @@
* @method
* @memberof Kinetic.Tag.prototype
*/
Kinetic.Collection.mapMethods(Kinetic.Tag);
})();

View File

@ -599,4 +599,6 @@
* @method
* @memberof Kinetic.Path.prototype
*/
Kinetic.Collection.mapMethods(Kinetic.Path);
})();

View File

@ -84,4 +84,6 @@
* @method
* @memberof Kinetic.RegularPolygon.prototype
*/
Kinetic.Collection.mapMethods(Kinetic.RegularPolygon);
})();

View File

@ -105,4 +105,6 @@
* @method
* @memberof Kinetic.Star.prototype
*/
Kinetic.Collection.mapMethods(Kinetic.Star);
})();

View File

@ -375,4 +375,6 @@
* @method
* @memberof Kinetic.TextPath.prototype
*/
Kinetic.Collection.mapMethods(Kinetic.TextPath);
})();

View File

@ -117,4 +117,6 @@
* // draw arc clockwise<br>
* arc.clockwise(true);
*/
Kinetic.Collection.mapMethods(Kinetic.Arc);
})();

View File

@ -76,4 +76,6 @@
* // set radius<br>
* circle.radius(10);<br>
*/
Kinetic.Collection.mapMethods(Kinetic.Circle);
})();

View File

@ -118,4 +118,6 @@
* ellipse.radiusY(200);
*/
Kinetic.Collection.mapMethods(Kinetic.Ellipse);
})();

View File

@ -196,4 +196,6 @@
* // set crop height<br>
* image.cropHeight(20);
*/
Kinetic.Collection.mapMethods(Kinetic.Image);
})();

View File

@ -198,4 +198,6 @@
* // push a new point<br>
* line.points(line.points().concat([70, 80]));
*/
Kinetic.Collection.mapMethods(Kinetic.Line);
})();

View File

@ -73,4 +73,6 @@
* // set corner radius<br>
* rect.cornerRadius(10);
*/
Kinetic.Collection.mapMethods(Kinetic.Rect);
})();

View File

@ -63,9 +63,7 @@
Kinetic.Util.extend(Kinetic.Ring, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'innerRadius', function() {
return 0;
});
Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'innerRadius', 0);
/**
* get/set innerRadius
@ -82,9 +80,7 @@
* ring.innerRadius(20);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'outerRadius', function() {
return 0;
});
Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'outerRadius', 0);
/**
* get/set outerRadius
@ -100,4 +96,6 @@
* // set outer radius<br>
* ring.outerRadius(20);
*/
Kinetic.Collection.mapMethods(Kinetic.Ring);
})();

View File

@ -256,4 +256,6 @@
getIndex: 'getFrameIndex',
setIndex: 'setFrameIndex'
});
Kinetic.Collection.mapMethods(Kinetic.Sprite);
})();

View File

@ -450,4 +450,6 @@
* // set text<br>
* text.text('Hello world!');
*/
Kinetic.Collection.mapMethods(Kinetic.Text);
})();

View File

@ -101,4 +101,6 @@
getAngleDeg: 'getAngle',
setAngleDeg: 'setAngle'
});
Kinetic.Collection.mapMethods(Kinetic.Wedge);
})();

View File

@ -50,6 +50,7 @@
<script src="unit/Stage-test.js"></script>
<script src="unit/Layer-test.js"></script>
<script src="unit/Shape-test.js"></script>
<script src="unit/Collection-test.js"></script>
<!-- shapes -->
<script src="unit/shapes/Rect-test.js"></script>

View File

@ -0,0 +1,57 @@
suite('Collection', function(){
var util;
test('test collection method mapping', function(){
// Node method
assert.notEqual(Kinetic.Collection.prototype.on, undefined);
// Layer method
assert.notEqual(Kinetic.Collection.prototype.getContext, undefined);
// Container method
assert.notEqual(Kinetic.Collection.prototype.hasChildren, undefined);
// Shape method
assert.notEqual(Kinetic.Collection.prototype.strokeWidth, undefined);
});
test('add circle to stage', function(){
var stage = addStage();
var layer = new Kinetic.Layer();
var circle1 = new Kinetic.Circle({
x: 100,
y: 100,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myCircle',
draggable: true
});
var circle2 = new Kinetic.Circle({
x:300,
y: 100,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
name: 'myCircle',
draggable: true
});
layer.add(circle1).add(circle2);
stage.add(layer);
layer.find('Circle').fill('blue');
layer.draw();
//console.log(layer.getContext().getTrace());
assert.equal(layer.getContext().getTrace(),'clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();arc(0,0,70,0,6.283,false);closePath();fillStyle=green;fill();lineWidth=4;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,300,100);beginPath();arc(0,0,70,0,6.283,false);closePath();fillStyle=red;fill();lineWidth=4;strokeStyle=black;stroke();restore();clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();arc(0,0,70,0,6.283,false);closePath();fillStyle=blue;fill();lineWidth=4;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,300,100);beginPath();arc(0,0,70,0,6.283,false);closePath();fillStyle=blue;fill();lineWidth=4;strokeStyle=black;stroke();restore();');
});
});

View File

@ -279,17 +279,17 @@ suite('Layer', function() {
layer.add(circle);
stage.add(layer);
assert.equal(layer.isHitGraphEnabled(), true);
assert.equal(layer.hitGraphEnabled(), true);
assert.equal(layer.shouldDrawHit(), true);
layer.disableHitGraph();
assert.equal(layer.isHitGraphEnabled(), false);
assert.equal(layer.hitGraphEnabled(), false);
assert.equal(layer.shouldDrawHit(), false);
layer.enableHitGraph();
assert.equal(layer.isHitGraphEnabled(), true);
assert.equal(layer.hitGraphEnabled(), true);
assert.equal(layer.shouldDrawHit(), true);
});
});