last pull request completely broke the unit tests. had to rewrite the get() logic so that it was correct

This commit is contained in:
Eric Rowell
2013-07-21 16:05:40 -07:00
parent 78214099b5
commit e6a9324d50
5 changed files with 132 additions and 64 deletions

View File

@@ -95,39 +95,43 @@
* var nodes = layer.get('Group');<br><br> * var nodes = layer.get('Group');<br><br>
* *
* // select all rectangles inside layer<br> * // select all rectangles inside layer<br>
* var nodes = layer.get('Rect'); * var nodes = layer.get('Rect');<br><br>
* *
* // select node with an id of foo or a name of bar inside layer<br> * // select node with an id of foo or a name of bar inside layer<br>
* var nodes = layer.get('#foo .bar'); * var nodes = layer.get('#foo, .bar');
*/ */
get: function(selector) { get: function(selector) {
var collection = new Kinetic.Collection(); var retArr = [],
// ID selector selectorArr = selector.replace(/ /g, '').split(','),
selector = selector.split(" "); len = selectorArr.length,
for (index = 0; index < selector.length; ++index) { n, i, sel, arr, node, children, clen;
if(selector.charAt(0) === '#') {
var node = this._getNodeById(selector.slice(1)); for (n = 0; n < len; n++) {
sel = selectorArr[n];
// id selector
if(sel.charAt(0) === '#') {
node = this._getNodeById(sel.slice(1));
if(node) { if(node) {
collection.push(node); retArr.push(node);
} }
} }
// name selector // name selector
else if(selector.charAt(0) === '.') { else if(sel.charAt(0) === '.') {
var nodeList = this._getNodesByName(selector.slice(1)); arr = this._getNodesByName(sel.slice(1));
Kinetic.Collection.apply(collection, nodeList); retArr = retArr.concat(arr);
} }
// unrecognized selector, pass to children // unrecognized selector, pass to children
else { else {
var retArr = []; children = this.getChildren();
var children = this.getChildren(); clen = children.length;
var len = children.length; for(i = 0; i < clen; i++) {
for(var n = 0; n < len; n++) { retArr = retArr.concat(children[i]._get(sel));
retArr = retArr.concat(children[n]._get(selector));
}
Kinetic.Collection.apply(collection, retArr);
} }
} }
return collection; }
return Kinetic.Collection.toCollection(retArr);
}, },
_getNodeById: function(key) { _getNodeById: function(key) {
var stage = this.getStage(), go = Kinetic.Global, node = go.ids[key]; var stage = this.getStage(), go = Kinetic.Global, node = go.ids[key];

View File

@@ -38,12 +38,31 @@
* @memberof Kinetic.Collection.prototype * @memberof Kinetic.Collection.prototype
*/ */
Kinetic.Collection.prototype.toArray = function() { Kinetic.Collection.prototype.toArray = function() {
var arr = []; var arr = [],
for(var n = 0; n < this.length; n++) { len = this.length,
n;
for(n = 0; n < len; n++) {
arr.push(this[n]); arr.push(this[n]);
} }
return arr; return arr;
}; };
/**
* convert array into a collection
* @method
* @memberof Kinetic.Collection
* @param {Array} arr
*/
Kinetic.Collection.toCollection = function(arr) {
var collection = new Kinetic.Collection(),
len = arr.length,
n;
for(n = 0; n < len; n++) {
collection.push(arr[n]);
}
return collection;
};
Kinetic.Collection.mapMethods = function(arr) { Kinetic.Collection.mapMethods = function(arr) {
var leng = arr.length, var leng = arr.length,

View File

@@ -54,7 +54,7 @@ Test.Modules.ANIMATION = {
anim.stop(); anim.stop();
test(a.animations.length === 0, '7should be no animations running'); test(a.animations.length === 0, '7should be no animations running');
}, },
'*batch draw': function(containerId) { 'batch draw': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,
@@ -77,7 +77,7 @@ Test.Modules.ANIMATION = {
draws = 0; draws = 0;
layer.on('draw', function() { layer.on('draw', function() {
console.log('draw') //console.log('draw')
draws++; draws++;
}); });

View File

@@ -119,6 +119,51 @@ Test.Modules.CONTAINER = {
node = stage.get('#myLayer')[0]; node = stage.get('#myLayer')[0];
test(node.nodeType === 'Layer', 'node type should be Layer'); test(node.nodeType === 'Layer', 'node type should be Layer');
},
'select shapes with multiple selectors': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer({
id: 'myLayer'
});
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
id: 'myCircle'
});
var rect = new Kinetic.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
name: 'myRect'
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
test(layer.get('#myCircle, .myRect').length === 2, 'should be 2 items in the array');
test(layer.get('#myCircle, .myRect')[0]._id === circle._id, 'circle id is wrong');
test(layer.get('#myCircle, .myRect')[1]._id === rect._id, 'rect id is wrong');
test(layer.get('#myCircle, Circle, .myRect, Rect').length === 4, 'should be 4 items in the array');
test(layer.get('#myCircle, Circle, .myRect, Rect')[0]._id === circle._id, 'circle id is wrong');
test(layer.get('#myCircle, Circle, .myRect, Rect')[1]._id === circle._id, 'circle id is wrong');
test(layer.get('#myCircle, Circle, .myRect, Rect')[2]._id === rect._id, 'rect id is wrong');
test(layer.get('#myCircle, Circle, .myRect, Rect')[3]._id === rect._id, 'rect id is wrong');
}, },
'set x on an array of nodes': function(containerId) { 'set x on an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({