mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
last pull request completely broke the unit tests. had to rewrite the get() logic so that it was correct
This commit is contained in:
@@ -95,39 +95,43 @@
|
||||
* var nodes = layer.get('Group');<br><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>
|
||||
* var nodes = layer.get('#foo .bar');
|
||||
* var nodes = layer.get('#foo, .bar');
|
||||
*/
|
||||
get: function(selector) {
|
||||
var collection = new Kinetic.Collection();
|
||||
// ID selector
|
||||
selector = selector.split(" ");
|
||||
for (index = 0; index < selector.length; ++index) {
|
||||
if(selector.charAt(0) === '#') {
|
||||
var node = this._getNodeById(selector.slice(1));
|
||||
var retArr = [],
|
||||
selectorArr = selector.replace(/ /g, '').split(','),
|
||||
len = selectorArr.length,
|
||||
n, i, sel, arr, node, children, clen;
|
||||
|
||||
for (n = 0; n < len; n++) {
|
||||
sel = selectorArr[n];
|
||||
|
||||
// id selector
|
||||
if(sel.charAt(0) === '#') {
|
||||
node = this._getNodeById(sel.slice(1));
|
||||
if(node) {
|
||||
collection.push(node);
|
||||
retArr.push(node);
|
||||
}
|
||||
}
|
||||
// name selector
|
||||
else if(selector.charAt(0) === '.') {
|
||||
var nodeList = this._getNodesByName(selector.slice(1));
|
||||
Kinetic.Collection.apply(collection, nodeList);
|
||||
else if(sel.charAt(0) === '.') {
|
||||
arr = this._getNodesByName(sel.slice(1));
|
||||
retArr = retArr.concat(arr);
|
||||
}
|
||||
// unrecognized selector, pass to children
|
||||
else {
|
||||
var retArr = [];
|
||||
var children = this.getChildren();
|
||||
var len = children.length;
|
||||
for(var n = 0; n < len; n++) {
|
||||
retArr = retArr.concat(children[n]._get(selector));
|
||||
children = this.getChildren();
|
||||
clen = children.length;
|
||||
for(i = 0; i < clen; i++) {
|
||||
retArr = retArr.concat(children[i]._get(sel));
|
||||
}
|
||||
Kinetic.Collection.apply(collection, retArr);
|
||||
}
|
||||
}
|
||||
return collection;
|
||||
|
||||
return Kinetic.Collection.toCollection(retArr);
|
||||
},
|
||||
_getNodeById: function(key) {
|
||||
var stage = this.getStage(), go = Kinetic.Global, node = go.ids[key];
|
||||
|
||||
23
src/Util.js
23
src/Util.js
@@ -38,12 +38,31 @@
|
||||
* @memberof Kinetic.Collection.prototype
|
||||
*/
|
||||
Kinetic.Collection.prototype.toArray = function() {
|
||||
var arr = [];
|
||||
for(var n = 0; n < this.length; n++) {
|
||||
var arr = [],
|
||||
len = this.length,
|
||||
n;
|
||||
|
||||
for(n = 0; n < len; n++) {
|
||||
arr.push(this[n]);
|
||||
}
|
||||
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) {
|
||||
var leng = arr.length,
|
||||
|
||||
@@ -54,7 +54,7 @@ Test.Modules.ANIMATION = {
|
||||
anim.stop();
|
||||
test(a.animations.length === 0, '7should be no animations running');
|
||||
},
|
||||
'*batch draw': function(containerId) {
|
||||
'batch draw': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@@ -77,7 +77,7 @@ Test.Modules.ANIMATION = {
|
||||
draws = 0;
|
||||
|
||||
layer.on('draw', function() {
|
||||
console.log('draw')
|
||||
//console.log('draw')
|
||||
draws++;
|
||||
});
|
||||
|
||||
|
||||
@@ -119,6 +119,51 @@ Test.Modules.CONTAINER = {
|
||||
node = stage.get('#myLayer')[0];
|
||||
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) {
|
||||
var stage = new Kinetic.Stage({
|
||||
|
||||
Reference in New Issue
Block a user