refactored get() method

This commit is contained in:
ericdrowell
2012-09-25 12:06:02 -07:00
4 changed files with 6747 additions and 38 deletions

6633
dist/kinetic-core.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -142,22 +142,45 @@ Kinetic.Container.prototype = {
*/ */
get: function(selector) { get: function(selector) {
var stage = this.getStage(); var stage = this.getStage();
var arr;
var key = selector.slice(1); // Node type selector
if(selector.charAt(0) === '#') { if(selector === 'Shape' || selector === 'Group' || selector === 'Layer') {
arr = stage.ids[key] !== undefined ? [stage.ids[key]] : []; var retArr = new Kinetic.Collection();
function traverse(cont) {
var children = cont.getChildren();
for(var n = 0; n < children.length; n++) {
var child = children[n];
if(child.nodeType === selector) {
retArr.push(child);
}
else if(child.nodeType !== 'Shape') {
traverse(child);
}
}
}
traverse(this);
return retArr;
} }
// ID selector
else if(selector.charAt(0) === '#') {
var key = selector.slice(1);
var arr = stage.ids[key] !== undefined ? [stage.ids[key]] : [];
return this._getDescendants(arr);
}
// name selector
else if(selector.charAt(0) === '.') { else if(selector.charAt(0) === '.') {
arr = stage.names[key] !== undefined ? stage.names[key] : []; var key = selector.slice(1);
} var arr = stage.names[key] || [];
else if(selector === 'Shape' || selector === 'Group' || selector === 'Layer') { return this._getDescendants(arr);
return this._getNodes(selector);
} }
// unrecognized selector
else { else {
return false; return false;
} }
},
var retArr = new Kinetic.Collection(); _getDescendants: function(arr) {
var retArr = new Kinetic.Collection();
for(var n = 0; n < arr.length; n++) { for(var n = 0; n < arr.length; n++) {
var node = arr[n]; var node = arr[n];
if(this.isAncestorOf(node)) { if(this.isAncestorOf(node)) {
@@ -165,7 +188,7 @@ Kinetic.Container.prototype = {
} }
} }
return retArr; return retArr;
}, },
/** /**
* determine if node is an ancestor * determine if node is an ancestor
@@ -198,7 +221,7 @@ Kinetic.Container.prototype = {
clone: function(obj) { clone: function(obj) {
// call super method // call super method
var node = Kinetic.Node.prototype.clone.call(this, obj) var node = Kinetic.Node.prototype.clone.call(this, obj)
// perform deep clone on containers // perform deep clone on containers
for(var key in this.children) { for(var key in this.children) {
node.add(this.children[key].clone()); node.add(this.children[key].clone());
@@ -225,27 +248,6 @@ Kinetic.Container.prototype = {
return arr; return arr;
}, },
/**
* get all shapes inside container
*/
_getNodes: function(sel) {
var arr = [];
function traverse(cont) {
var children = cont.getChildren();
for(var n = 0; n < children.length; n++) {
var child = children[n];
if(child.nodeType === sel) {
arr.push(child);
}
else if(child.nodeType !== 'Shape') {
traverse(child);
}
}
}
traverse(this);
return arr;
},
/** /**
* set children indices * set children indices
*/ */

View File

@@ -649,19 +649,63 @@ Test.prototype.tests = {
layer.add(circle); layer.add(circle);
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
var shapes = layer.get('.myShape'); var shapes = layer.get('.myShape');
test(shapes.length === 2, 'shapes array should have 2 elements'); test(shapes.length === 2, 'shapes array should have 2 elements');
shapes.apply('setX', 200); shapes.apply('setX', 200);
layer.draw(); layer.draw();
shapes.each(function() { shapes.each(function() {
test(this.getX() === 200, 'shape x should be 200'); test(this.getX() === 200, 'shape x should be 200');
}); });
}, },
'SELECTOR - set fill on array by Shape-selector': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
var rect = new Kinetic.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
var shapes = layer.get('Shape');
test(shapes.length === 2, 'shapes array should have 2 elements');
shapes.apply('setFill', 'gray');
layer.draw();
shapes.each(function() {
test(this.getFill() === 'gray', 'shape x should be 200');
});
},
'SELECTOR - add listener to an array of nodes': function(containerId) { 'SELECTOR - add listener to an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,