Merge pull request #101 from davonium/collection

Corrected behaviour of the collections apply method. and some more.
This commit is contained in:
ericdrowell
2012-09-23 15:43:48 -07:00
5 changed files with 69 additions and 19 deletions

18
dist/kinetic-core.js vendored
View File

@@ -1052,11 +1052,15 @@ Kinetic.Collection.prototype = new Array();
* @param {String} method * @param {String} method
* @param val * @param val
*/ */
Kinetic.Collection.prototype.apply = function(method, val) { Kinetic.Collection.prototype.apply = function(method) {
for (var n=0; n<this.length; n++) { args = [].slice.call(arguments);
this[n][method](val); args.shift();
} for (var n=0; n<this.length; n++) {
}; if(Kinetic.Type._isFunction(this[n][method])) {
this[n][method].apply(this[n],args);
}
}
};
/** /**
* iterate through node array * iterate through node array
* @name each * @name each
@@ -1065,7 +1069,7 @@ Kinetic.Collection.prototype.apply = function(method, val) {
*/ */
Kinetic.Collection.prototype.each = function(func) { Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) { for (var n=0; n<this.length; n++) {
func(this[n]); func.call(this[n],n,this[n]);
} }
}; };
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
@@ -2689,7 +2693,7 @@ Kinetic.Container.prototype = {
* get all shapes inside container * get all shapes inside container
*/ */
_getNodes: function(sel) { _getNodes: function(sel) {
var arr = []; var arr = new Kinetic.Collection();
function traverse(cont) { function traverse(cont) {
var children = cont.getChildren(); var children = cont.getChildren();
for(var n = 0; n < children.length; n++) { for(var n = 0; n < children.length; n++) {

File diff suppressed because one or more lines are too long

View File

@@ -229,7 +229,7 @@ Kinetic.Container.prototype = {
* get all shapes inside container * get all shapes inside container
*/ */
_getNodes: function(sel) { _getNodes: function(sel) {
var arr = []; var arr = new Kinetic.Collection();
function traverse(cont) { function traverse(cont) {
var children = cont.getChildren(); var children = cont.getChildren();
for(var n = 0; n < children.length; n++) { for(var n = 0; n < children.length; n++) {

View File

@@ -21,11 +21,15 @@ Kinetic.Collection.prototype = new Array();
* @param {String} method * @param {String} method
* @param val * @param val
*/ */
Kinetic.Collection.prototype.apply = function(method, val) { Kinetic.Collection.prototype.apply = function(method) {
for (var n=0; n<this.length; n++) { args = [].slice.call(arguments);
this[n][method](val); args.shift();
} for (var n=0; n<this.length; n++) {
}; if(Kinetic.Type._isFunction(this[n][method])) {
this[n][method].apply(this[n],args);
}
}
};
/** /**
* iterate through node array * iterate through node array
* @name each * @name each
@@ -34,6 +38,6 @@ Kinetic.Collection.prototype.apply = function(method, val) {
*/ */
Kinetic.Collection.prototype.each = function(func) { Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) { for (var n=0; n<this.length; n++) {
func(this[n]); func.call(this[n],n,this[n]);
} }
}; };

View File

@@ -618,7 +618,7 @@ Test.prototype.tests = {
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash'); test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
test(Kinetic.Global.shapes[rectColorKey] === undefined, 'rect color key should not be in shapes hash'); test(Kinetic.Global.shapes[rectColorKey] === undefined, 'rect color key should not be in shapes hash');
}, },
'*SELECTOR - show and hide an array of nodes': function(containerId) { 'SELECTOR - set x on an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,
@@ -658,10 +658,52 @@ Test.prototype.tests = {
layer.draw(); layer.draw();
shapes.each(function(shape) { shapes.each(function() {
test(shape.getX() === 200, 'shape x should be 200'); test(this.getX() === 200, 'shape x should be 200');
}); });
}, },
'SELECTOR - add listener to an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
var rect = new Kinetic.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
var shapes = layer.get('.myShape');
test(shapes.length === 2, 'shapes array should have 2 elements');
var a = 0;
shapes.apply('on', 'mouseover', function () {a++;});
circle.simulate('mouseover');
test(a === 1, 'listener should have fired for circle');
rect.simulate('mouseover');
test(a === 2, 'listener should have fired for rect');
},
'STAGE - test ids and names hashes': function(containerId) { 'STAGE - test ids and names hashes': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,