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

75
dist/kinetic-core.js vendored
View File

@ -906,6 +906,58 @@ Kinetic.Container.prototype = {
child = undefined; 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 * draw children
*/ */
@ -1083,29 +1135,6 @@ Kinetic.Stage.prototype = {
draw: function() { draw: function() {
this._drawChildren(); 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 * set stage size
* @param {int} width * @param {int} width

File diff suppressed because one or more lines are too long

View File

@ -53,6 +53,58 @@ Kinetic.Container.prototype = {
child = undefined; 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 * draw children
*/ */

View File

@ -103,29 +103,6 @@ Kinetic.Stage.prototype = {
draw: function() { draw: function() {
this._drawChildren(); 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 * set stage size
* @param {int} width * @param {int} width

View File

@ -196,7 +196,7 @@ Test.prototype.tests = {
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}'; var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
stage.load(json); stage.load(json);
var customShape = stage.get('#myTriangle'); var customShape = stage.get('#myTriangle')[0];
customShape.setDrawFunc(drawTriangle); customShape.setDrawFunc(drawTriangle);
@ -296,7 +296,9 @@ Test.prototype.tests = {
width: 578, width: 578,
height: 200 height: 200
}); });
var layer = new Kinetic.Layer(); var layer = new Kinetic.Layer({
id: 'myLayer'
});
var circle = new Kinetic.Circle({ var circle = new Kinetic.Circle({
x: stage.getWidth() / 2, x: stage.getWidth() / 2,
y: stage.getHeight() / 2, y: stage.getHeight() / 2,
@ -322,11 +324,15 @@ Test.prototype.tests = {
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
var node = stage.get('#myCircle'); var node;
var nodes = stage.get('.myRect'); node = stage.get('#myCircle')[0];
test(node.shapeType === 'Circle', 'shape type should be circle'); test(node.shapeType === 'Circle', 'shape type should be circle');
test(nodes[0].shapeType === 'Rect', 'shape type should be rect'); node = layer.get('.myRect')[0];
test(node.shapeType === 'Rect', 'shape type should be rect');
node = layer.get('#myLayer')[0];
test(node === undefined, 'node should be undefined');
node = stage.get('#myLayer')[0];
test(node.nodeType === 'Layer', 'node type should be Layer');
}, },
'STAGE - remove shape by id or name': function(containerId) { 'STAGE - remove shape by id or name': function(containerId) {
@ -361,13 +367,13 @@ Test.prototype.tests = {
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
var node = stage.get('#myCircle'); var node = stage.get('#myCircle')[0];
var nodes = stage.get('.myRect'); var nodes = stage.get('.myRect');
test(stage.ids.myCircle._id === circle._id, 'circle not in ids hash'); test(stage.ids.myCircle._id === circle._id, 'circle not in ids hash');
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash'); test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
var node = stage.get('#myCircle'); var node = stage.get('#myCircle')[0];
var parent = node.getParent(); var parent = node.getParent();
parent.remove(node); parent.remove(node);
@ -400,15 +406,15 @@ Test.prototype.tests = {
}); });
var go = Kinetic.GlobalObject; var go = Kinetic.GlobalObject;
test(go.tempNodes.length === 0, 'shouldn\'t be nodes in the tempNdoes array'); test(go.tempNodes.length === 0, 'shouldn\'t be nodes in the tempNdoes array');
layer.add(circle); layer.add(circle);
var node = stage.get('#myCircle'); var node = stage.get('#myCircle')[0];
test(node === undefined, 'node should be undefined'); test(node === undefined, 'node should be undefined');
test(go.tempNodes.length === 1, 'tempNodes array should have one node'); test(go.tempNodes.length === 1, 'tempNodes array should have one node');
layer.remove(circle); layer.remove(circle);
@ -861,7 +867,7 @@ Test.prototype.tests = {
stage.load(json); stage.load(json);
var image = stage.get('#darth'); var image = stage.get('#darth')[0];
image.setImage(imageObj); image.setImage(imageObj);