mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
moved .get() method to Container so that all containers can use it, not just stage. This allows you to select nodes within other nodes
This commit is contained in:
@@ -53,6 +53,58 @@ Kinetic.Container.prototype = {
|
||||
child = undefined;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* use for selectors. select nodes by id with # and by name
|
||||
* with .
|
||||
* ex:
|
||||
* var node = stage.get('#foo'); // selects node with id foo
|
||||
* var nodes = layer.get('.bar'); // selects nodes with name bar inside layer
|
||||
* @param {String} selector
|
||||
*/
|
||||
get: function(selector) {
|
||||
var stage = this.getStage();
|
||||
var arr;
|
||||
var key = selector.slice(1);
|
||||
if(selector.charAt(0) === '#') {
|
||||
arr = stage.ids[key] !== undefined ? [stage.ids[key]] : [];
|
||||
}
|
||||
else if(selector.charAt(0) === '.') {
|
||||
arr = stage.names[key] !== undefined ? stage.names[key] : [];
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
var retArr = [];
|
||||
for(var n = 0; n < arr.length; n++) {
|
||||
var node = arr[n];
|
||||
if(this.isAncestorOf(node)) {
|
||||
retArr.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
return retArr;
|
||||
},
|
||||
/**
|
||||
* determine if node is an ancestor
|
||||
* of descendant
|
||||
* @param {Kinetic.Node} node
|
||||
*/
|
||||
isAncestorOf: function(node) {
|
||||
if(this.nodeType === 'Stage') {
|
||||
return true;
|
||||
}
|
||||
|
||||
var parent = node.getParent();
|
||||
while(parent) {
|
||||
if(parent._id === this._id) {
|
||||
return true;
|
||||
}
|
||||
parent = parent.getParent();
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
/**
|
||||
* draw children
|
||||
*/
|
||||
|
23
src/Stage.js
23
src/Stage.js
@@ -103,29 +103,6 @@ Kinetic.Stage.prototype = {
|
||||
draw: function() {
|
||||
this._drawChildren();
|
||||
},
|
||||
/**
|
||||
* get selector. can select nodes by id with # and by name
|
||||
* with .
|
||||
* ex:
|
||||
* var node = stage.get('#foo'); // selects node with id foo
|
||||
* var nodes = stage.get('.bar'); // selects nodes with name bar
|
||||
* @param {String} selector
|
||||
*/
|
||||
get: function(selector) {
|
||||
var hash;
|
||||
if(selector.charAt(0) === '#') {
|
||||
hash = this.ids;
|
||||
}
|
||||
else if(selector.charAt(0) === '.') {
|
||||
hash = this.names;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
var key = selector.slice(1);
|
||||
return hash[key];
|
||||
},
|
||||
/**
|
||||
* set stage size
|
||||
* @param {int} width
|
||||
|
Reference in New Issue
Block a user