mirror of
https://github.com/konvajs/konva.git
synced 2025-08-23 23:50:01 +08:00
optional filter callback for getChildren
function. close #313
This commit is contained in:
parent
3a5b6eb766
commit
86178185bc
@ -8,9 +8,29 @@
|
||||
* returns a {@link Kinetic.Collection} of direct descendant nodes
|
||||
* @method
|
||||
* @memberof Kinetic.Container.prototype
|
||||
* @param {Function} [filterFunc] filter function
|
||||
* @returns {Kinetic.Collection}
|
||||
* @example
|
||||
* // get all children<br>
|
||||
* var children = layer.getChildren();<br><br>
|
||||
*
|
||||
* // get only circles<br>
|
||||
* var circles = layer.getChildren(function(node){<br>
|
||||
* return node.getClassName() === 'Circle';<br>
|
||||
* });
|
||||
*/
|
||||
getChildren: function() {
|
||||
return this.children;
|
||||
getChildren: function(predicate) {
|
||||
if (!predicate) {
|
||||
return this.children;
|
||||
} else {
|
||||
var results = new Kinetic.Collection();
|
||||
this.children.each(function(child){
|
||||
if (predicate(child)) {
|
||||
results.push(child);
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* determine if node has children
|
||||
|
@ -1387,6 +1387,50 @@ suite('Container', function() {
|
||||
|
||||
|
||||
|
||||
layer.draw();
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
test('getChildren may use filter function', function() {
|
||||
var stage = addStage();
|
||||
var layer = new Kinetic.Layer();
|
||||
var group = new Kinetic.Group();
|
||||
|
||||
var circle1 = new Kinetic.Circle({
|
||||
x: 200,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'blue',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
var circle2 = circle1.clone();
|
||||
group.add(circle1).add(circle2);
|
||||
|
||||
var rect = new Kinetic.Rect({
|
||||
name : 'test'
|
||||
});
|
||||
group.add(rect);
|
||||
|
||||
var circles = group.getChildren(function(node){
|
||||
return node.getClassName() === 'Circle';
|
||||
});
|
||||
assert.equal(circles.length, 2, 'group has two circle children');
|
||||
assert.equal(circles.indexOf(circle1) > -1, true);
|
||||
assert.equal(circles.indexOf(circle2) > -1, true);
|
||||
|
||||
var testName = group.getChildren(function(node){
|
||||
return node.name() === 'test';
|
||||
});
|
||||
|
||||
assert.equal(testName.length, 1, 'group has one children with test name');
|
||||
|
||||
|
||||
layer.add(group);
|
||||
|
||||
|
||||
|
||||
|
||||
layer.draw();
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user