mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 06:55:17 +08:00
add findWhere method to Containers
This commit is contained in:
parent
248f57cd3f
commit
8bb59548e5
1
konva.d.ts
vendored
1
konva.d.ts
vendored
@ -324,6 +324,7 @@ declare namespace Konva {
|
||||
clipFunc(ctx: CanvasRenderingContext2D | undefined | null): void;
|
||||
destroyChildren(): void;
|
||||
find(selector?: string): Collection;
|
||||
findWhere(fn: (Node) => boolean): Collection;
|
||||
findOne<T extends Node>(selector: string): T;
|
||||
getAllIntersections(pos: Vector2d): Shape[];
|
||||
hasChildren(): boolean;
|
||||
|
@ -227,6 +227,52 @@
|
||||
findOne: function(selector) {
|
||||
return this.find(selector)[0];
|
||||
},
|
||||
/**
|
||||
* return a {@link Konva.Collection} of nodes that return true when passed through your function argument.
|
||||
* Your function has access to the currentNode argument, and should return a boolean type.
|
||||
* See examples for more details.
|
||||
* @method
|
||||
* @memberof Konva.Container.prototype
|
||||
* @param {Function} fn
|
||||
* @returns {Collection}
|
||||
* @example
|
||||
* // get all Groups
|
||||
* var groups = stage.findWhere(el => {
|
||||
* return el.getType() === 'Group';
|
||||
* });
|
||||
*
|
||||
* // get only Nodes with partial opacity
|
||||
* var alphaNodes = layer.findWhere(el => {
|
||||
* return el.getType() === 'Node' && el.getAbsoluteOpacity() < 1;
|
||||
* });
|
||||
*/
|
||||
findWhere: function(fn) {
|
||||
if (typeof fn !== 'function') {
|
||||
Konva.Util.warn(
|
||||
'You must pass a function with a return value as your argument here. Are you maybe looking for .find instead?'
|
||||
);
|
||||
Konva.Util.warn('Konva is awesome, right?');
|
||||
}
|
||||
|
||||
var retArr = [];
|
||||
|
||||
var addItems = function(el) {
|
||||
var children = el.getChildren();
|
||||
var clen = children.length;
|
||||
|
||||
if (fn(el)) {
|
||||
retArr = retArr.concat(el);
|
||||
}
|
||||
|
||||
for (var i = 0; i < clen; i++) {
|
||||
addItems(children[i]);
|
||||
}
|
||||
};
|
||||
|
||||
addItems(this);
|
||||
|
||||
return Konva.Collection.toCollection(retArr);
|
||||
},
|
||||
_getNodeById: function(key) {
|
||||
var node = Konva.ids[key];
|
||||
|
||||
|
@ -406,6 +406,37 @@ suite('Container', function() {
|
||||
);
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
test('select shape by function', function() {
|
||||
var stage = addStage();
|
||||
var layer = new Konva.Layer();
|
||||
|
||||
var rect = new Konva.Rect({
|
||||
x: 300,
|
||||
y: 100,
|
||||
width: 100,
|
||||
height: 50,
|
||||
fill: 'purple',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4,
|
||||
name: 'myRect'
|
||||
});
|
||||
|
||||
layer.add(rect);
|
||||
stage.add(layer);
|
||||
|
||||
var fn = function(node) {
|
||||
return node.nodeType === 'Shape';
|
||||
};
|
||||
|
||||
var noOp = function(node) {
|
||||
return false;
|
||||
};
|
||||
|
||||
assert.equal(stage.findWhere(fn)[0], rect);
|
||||
assert.equal(stage.findWhere(noOp).length, 0);
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
test('set x on an array of nodes', function() {
|
||||
var stage = addStage();
|
||||
|
Loading…
Reference in New Issue
Block a user