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> * // set clip height<br>
* container.clipHeight(100); * container.clipHeight(100);
*/ */
Kinetic.Collection.mapMethods(Kinetic.Container);
})(); })();

View File

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

View File

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

View File

@@ -594,7 +594,7 @@
*/ */
shouldDrawHit: function() { shouldDrawHit: function() {
var layer = this.getLayer(); 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 * show node
@@ -1934,21 +1934,5 @@
getRotationDeg: 'getRotation' getRotationDeg: 'getRotation'
}); });
Kinetic.Collection.mapMethods([ Kinetic.Collection.mapMethods(Kinetic.Node);
'on',
'off',
'remove',
'destroy',
'show',
'hide',
'move',
'rotate',
'moveToTop',
'moveUp',
'moveDown',
'moveToBottom',
'moveTo',
'fire',
'draw'
]);
})(); })();

View File

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

View File

@@ -64,24 +64,23 @@
return collection; return collection;
}; };
Kinetic.Collection.mapMethods = function(arr) { Kinetic.Collection.mapMethods = function(constructor) {
var leng = arr.length, var prot = constructor.prototype,
n; key;
for(n = 0; n < leng; n++) { for(key in prot) {
// induce scope // induce scope
(function(i) { (function(methodName) {
var method = arr[i]; Kinetic.Collection.prototype[methodName] = function() {
Kinetic.Collection.prototype[method] = function() {
var len = this.length, var len = this.length,
i; i;
args = [].slice.call(arguments); args = [].slice.call(arguments);
for(i = 0; i < len; i++) { 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.Util.extend(Kinetic.Label, Kinetic.Group);
Kinetic.Collection.mapMethods(Kinetic.Label);
/** /**
* Tag constructor.&nbsp; A Tag can be configured * Tag constructor.&nbsp; A Tag can be configured
* to have a pointer element that points up, right, down, or left * to have a pointer element that points up, right, down, or left
@@ -291,4 +293,6 @@
* @method * @method
* @memberof Kinetic.Tag.prototype * @memberof Kinetic.Tag.prototype
*/ */
Kinetic.Collection.mapMethods(Kinetic.Tag);
})(); })();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -50,6 +50,7 @@
<script src="unit/Stage-test.js"></script> <script src="unit/Stage-test.js"></script>
<script src="unit/Layer-test.js"></script> <script src="unit/Layer-test.js"></script>
<script src="unit/Shape-test.js"></script> <script src="unit/Shape-test.js"></script>
<script src="unit/Collection-test.js"></script>
<!-- shapes --> <!-- shapes -->
<script src="unit/shapes/Rect-test.js"></script> <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); layer.add(circle);
stage.add(layer); stage.add(layer);
assert.equal(layer.isHitGraphEnabled(), true); assert.equal(layer.hitGraphEnabled(), true);
assert.equal(layer.shouldDrawHit(), true); assert.equal(layer.shouldDrawHit(), true);
layer.disableHitGraph(); layer.disableHitGraph();
assert.equal(layer.isHitGraphEnabled(), false); assert.equal(layer.hitGraphEnabled(), false);
assert.equal(layer.shouldDrawHit(), false); assert.equal(layer.shouldDrawHit(), false);
layer.enableHitGraph(); layer.enableHitGraph();
assert.equal(layer.isHitGraphEnabled(), true); assert.equal(layer.hitGraphEnabled(), true);
assert.equal(layer.shouldDrawHit(), true); assert.equal(layer.shouldDrawHit(), true);
}); });
}); });