mirror of
https://github.com/konvajs/konva.git
synced 2025-09-18 18:27:58 +08:00
removed apply() Collection method. Added new mapMethods() Collection method, which is used internally to map node methods to collection methods
This commit is contained in:
16
src/Node.js
16
src/Node.js
@@ -1316,18 +1316,6 @@
|
|||||||
* @methodOf Kinetic.Node.prototype
|
* @methodOf Kinetic.Node.prototype
|
||||||
*/
|
*/
|
||||||
Kinetic.Node.prototype.isVisible = Kinetic.Node.prototype.getVisible;
|
Kinetic.Node.prototype.isVisible = Kinetic.Node.prototype.getVisible;
|
||||||
|
|
||||||
// collection mappings
|
Kinetic.Collection.mapMethods(['on', 'off']);
|
||||||
var collectionMappings = [ON, OFF];
|
|
||||||
for(var n = 0; n < 2; n++) {
|
|
||||||
// induce scope
|
|
||||||
(function(i) {
|
|
||||||
var method = collectionMappings[i];
|
|
||||||
Kinetic.Collection.prototype[method] = function() {
|
|
||||||
var args = [].slice.call(arguments);
|
|
||||||
args.unshift(method);
|
|
||||||
this.apply.apply(this, args);
|
|
||||||
};
|
|
||||||
})(n);
|
|
||||||
}
|
|
||||||
})();
|
})();
|
||||||
|
@@ -14,22 +14,6 @@
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Kinetic.Collection.prototype = new Array();
|
Kinetic.Collection.prototype = new Array();
|
||||||
/**
|
|
||||||
* apply a method to all nodes in the array
|
|
||||||
* @name apply
|
|
||||||
* @methodOf Kinetic.Collection.prototype
|
|
||||||
* @param {String} method
|
|
||||||
* @param val
|
|
||||||
*/
|
|
||||||
Kinetic.Collection.prototype.apply = function(method) {
|
|
||||||
args = [].slice.call(arguments);
|
|
||||||
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
|
||||||
@@ -38,7 +22,28 @@
|
|||||||
*/
|
*/
|
||||||
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.call(this[n], n, this[n]);
|
func(this[n], n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Kinetic.Collection.mapMethods = function(arr) {
|
||||||
|
var leng = arr.length,
|
||||||
|
n;
|
||||||
|
|
||||||
|
for(n = 0; n < leng; n++) {
|
||||||
|
// induce scope
|
||||||
|
(function(i) {
|
||||||
|
var method = arr[i];
|
||||||
|
Kinetic.Collection.prototype[method] = function() {
|
||||||
|
var len = this.length,
|
||||||
|
i;
|
||||||
|
|
||||||
|
args = [].slice.call(arguments);
|
||||||
|
for(i = 0; i < len; i++) {
|
||||||
|
this[i][method].apply(this[i], args);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})(n);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
@@ -156,12 +156,14 @@ Test.Modules.CONTAINER = {
|
|||||||
|
|
||||||
test(shapes.length === 2, 'shapes array should have 2 elements');
|
test(shapes.length === 2, 'shapes array should have 2 elements');
|
||||||
|
|
||||||
shapes.apply('setX', 200);
|
shapes.each(function(node) {
|
||||||
|
node.setX(200);
|
||||||
|
});
|
||||||
|
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
shapes.each(function() {
|
shapes.each(function(node) {
|
||||||
test(this.getX() === 200, 'shape x should be 200');
|
test(node.getX() === 200, 'shape x should be 200');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
'set fill on array by Shape-selector': function(containerId) {
|
'set fill on array by Shape-selector': function(containerId) {
|
||||||
@@ -200,12 +202,14 @@ Test.Modules.CONTAINER = {
|
|||||||
|
|
||||||
test(shapes.length === 2, 'shapes array should have 2 elements');
|
test(shapes.length === 2, 'shapes array should have 2 elements');
|
||||||
|
|
||||||
shapes.apply('setFill', 'gray');
|
shapes.each(function(node) {
|
||||||
|
node.setFill('gray');
|
||||||
|
});
|
||||||
|
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
shapes.each(function() {
|
shapes.each(function(node) {
|
||||||
test(this.getFill() === 'gray', 'shape x should be 200');
|
test(node.getFill() === 'gray', 'shape x should be 200');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
'add listener to an array of nodes': function(containerId) {
|
'add listener to an array of nodes': function(containerId) {
|
||||||
|
@@ -2116,7 +2116,9 @@ Test.Modules.NODE = {
|
|||||||
|
|
||||||
var stage = Kinetic.Node.create(json, containerId);
|
var stage = Kinetic.Node.create(json, containerId);
|
||||||
|
|
||||||
stage.get('#myTriangle').apply('setDrawFunc', drawTriangle);
|
stage.get('#myTriangle').each(function(node) {
|
||||||
|
node.setDrawFunc(drawTriangle);
|
||||||
|
});
|
||||||
|
|
||||||
stage.draw();
|
stage.draw();
|
||||||
|
|
||||||
@@ -2157,7 +2159,9 @@ Test.Modules.NODE = {
|
|||||||
var stage = Kinetic.Node.create(json, containerId);
|
var stage = Kinetic.Node.create(json, containerId);
|
||||||
|
|
||||||
testJSON(stage.toJSON(), json, 'problem loading stage json with image');
|
testJSON(stage.toJSON(), json, 'problem loading stage json with image');
|
||||||
stage.get('#darth').apply('setImage', imageObj);
|
stage.get('#darth').each(function(node) {
|
||||||
|
node.setImage(imageObj);
|
||||||
|
});
|
||||||
stage.draw();
|
stage.draw();
|
||||||
};
|
};
|
||||||
imageObj.src = '../assets/darth-vader.jpg';
|
imageObj.src = '../assets/darth-vader.jpg';
|
||||||
|
Reference in New Issue
Block a user