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:
Eric Rowell
2012-04-08 21:26:13 -07:00
parent 63c8dde6d5
commit b8516b1b0c
5 changed files with 124 additions and 60 deletions

View File

@@ -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
*/

View File

@@ -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