mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
refactor findOne method
This commit is contained in:
parent
514e60122b
commit
2466ea61b0
2
konva.d.ts
vendored
2
konva.d.ts
vendored
@ -343,7 +343,7 @@ declare namespace Konva {
|
|||||||
clipFunc(ctx: CanvasRenderingContext2D | undefined | null): void;
|
clipFunc(ctx: CanvasRenderingContext2D | undefined | null): void;
|
||||||
destroyChildren(): void;
|
destroyChildren(): void;
|
||||||
find(selector?: string | ((Node) => boolean)): Collection;
|
find(selector?: string | ((Node) => boolean)): Collection;
|
||||||
findOne<T extends Node>(selector: string): T;
|
findOne<T extends Node>(selector: string | ((Node) => boolean)): T;
|
||||||
getAllIntersections(pos: Vector2d): Shape[];
|
getAllIntersections(pos: Vector2d): Shape[];
|
||||||
hasChildren(): boolean;
|
hasChildren(): boolean;
|
||||||
removeChildren(): void;
|
removeChildren(): void;
|
||||||
|
@ -181,31 +181,42 @@
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
find: function(selector) {
|
find: function(selector) {
|
||||||
var retArr = [];
|
// protecting _findByType to prevent user from accidentally adding
|
||||||
|
// second argument and getting unexpected `findOne` result
|
||||||
if (typeof selector === 'string') {
|
return this._findByType(selector, false);
|
||||||
retArr = this._findByString(selector);
|
|
||||||
} else if (typeof selector === 'function') {
|
|
||||||
retArr = this._findByFunction(selector);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Konva.Collection.toCollection(retArr);
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* return a first node from `find` method
|
* return a first node from `find` method
|
||||||
* @method
|
* @method
|
||||||
* @memberof Konva.Container.prototype
|
* @memberof Konva.Container.prototype
|
||||||
* @param {String} selector
|
* @param {String | Function} selector
|
||||||
* @returns {Konva.Node}
|
* @returns {Konva.Node | Undefined}
|
||||||
* @example
|
* @example
|
||||||
* // select node with id foo
|
* // select node with id foo
|
||||||
* var node = stage.findOne('#foo');
|
* var node = stage.findOne('#foo');
|
||||||
*
|
*
|
||||||
* // select node with name bar inside layer
|
* // select node with name bar inside layer
|
||||||
* var nodes = layer.findOne('.bar');
|
* var nodes = layer.findOne('.bar');
|
||||||
|
*
|
||||||
|
* // select the first node to return true in a function
|
||||||
|
* var node = stage.findOne(node => {
|
||||||
|
* return node.getType() === 'Shape'
|
||||||
|
* })
|
||||||
*/
|
*/
|
||||||
findOne: function(selector) {
|
findOne: function(selector) {
|
||||||
return this.find(selector)[0];
|
var result = this._findByType(selector, true);
|
||||||
|
return result.length > 0 ? result[0] : undefined;
|
||||||
|
},
|
||||||
|
_findByType: function(selector, findOne) {
|
||||||
|
var retArr = [];
|
||||||
|
|
||||||
|
if (typeof selector === 'string') {
|
||||||
|
retArr = this._findByString(selector, findOne);
|
||||||
|
} else if (typeof selector === 'function') {
|
||||||
|
retArr = this._findByFunction(selector, findOne);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Konva.Collection.toCollection(retArr);
|
||||||
},
|
},
|
||||||
_findByString: function(selector) {
|
_findByString: function(selector) {
|
||||||
var retArr = [],
|
var retArr = [],
|
||||||
@ -254,10 +265,16 @@
|
|||||||
|
|
||||||
return retArr;
|
return retArr;
|
||||||
},
|
},
|
||||||
_findByFunction: function(fn) {
|
// (fn: ((Node) => boolean, findOne?: boolean)
|
||||||
|
_findByFunction: function(fn, findOne) {
|
||||||
var retArr = [];
|
var retArr = [];
|
||||||
|
|
||||||
var addItems = function(el) {
|
var addItems = function(el) {
|
||||||
|
// escape function if we've already found one.
|
||||||
|
if (findOne && retArr.length > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var children = el.getChildren();
|
var children = el.getChildren();
|
||||||
var clen = children.length;
|
var clen = children.length;
|
||||||
|
|
||||||
|
@ -291,7 +291,7 @@ suite('Container', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
test('select shape by id and name with findOne', function() {
|
test('select shape with findOne', function() {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
var layer = new Konva.Layer({
|
var layer = new Konva.Layer({
|
||||||
id: 'myLayer'
|
id: 'myLayer'
|
||||||
@ -330,6 +330,10 @@ suite('Container', function() {
|
|||||||
assert.equal(node, undefined, 'node should be undefined');
|
assert.equal(node, undefined, 'node should be undefined');
|
||||||
node = stage.findOne('#myLayer');
|
node = stage.findOne('#myLayer');
|
||||||
assert.equal(node, layer, 'node type should be Layer');
|
assert.equal(node, layer, 'node type should be Layer');
|
||||||
|
node = stage.findOne(function(node) {
|
||||||
|
return node.getType() === 'Shape';
|
||||||
|
});
|
||||||
|
assert.equal(node, circle, 'findOne should work with functions');
|
||||||
});
|
});
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user