remove() method now correctly removes node descendants

This commit is contained in:
Eric Rowell
2012-06-01 23:56:01 -07:00
parent ba796f4cc3
commit 385deb793d
4 changed files with 196 additions and 147 deletions

12
dist/kinetic-core.js vendored
View File

@@ -1300,7 +1300,7 @@ Kinetic.Container.prototype = {
* @param {Node} child
*/
remove: function(child) {
if(child.index !== undefined && this.children[child.index]._id == child._id) {
if(child && child.index !== undefined && this.children[child.index]._id == child._id) {
var stage = this.getStage();
if(stage !== undefined) {
stage._removeId(child);
@@ -1312,19 +1312,25 @@ Kinetic.Container.prototype = {
var node = go.tempNodes[n];
if(node._id === child._id) {
go.tempNodes.splice(n, 1);
n = go.tempNodes.length;
break;
}
}
this.children.splice(child.index, 1);
this._setChildrenIndices();
child = undefined;
// remove children
if(child.children) {
for(var n = 0; n < child.children.length; n++) {
child.remove(child.children[n]);
}
}
// do extra stuff if needed
if(this._remove !== undefined) {
this._remove(child);
}
}
// chainable
return this;

File diff suppressed because one or more lines are too long

View File

@@ -68,7 +68,7 @@ Kinetic.Container.prototype = {
* @param {Node} child
*/
remove: function(child) {
if(child.index !== undefined && this.children[child.index]._id == child._id) {
if(child && child.index !== undefined && this.children[child.index]._id == child._id) {
var stage = this.getStage();
if(stage !== undefined) {
stage._removeId(child);
@@ -80,19 +80,25 @@ Kinetic.Container.prototype = {
var node = go.tempNodes[n];
if(node._id === child._id) {
go.tempNodes.splice(n, 1);
n = go.tempNodes.length;
break;
}
}
this.children.splice(child.index, 1);
this._setChildrenIndices();
child = undefined;
// remove children
if(child.children) {
for(var n = 0; n < child.children.length; n++) {
child.remove(child.children[n]);
}
}
// do extra stuff if needed
if(this._remove !== undefined) {
this._remove(child);
}
}
// chainable
return this;

View File

@@ -645,7 +645,9 @@ Test.prototype.tests = {
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var layer = new Kinetic.Layer({
name: 'myLayer'
});
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
@@ -660,10 +662,14 @@ Test.prototype.tests = {
stage.add(layer);
test(stage.children.length === 1, 'stage should have 1 children');
test(stage.get('.myLayer')[0] !== undefined, 'layer should exist');
test(stage.get('.myCircle')[0] !== undefined, 'circle should exist');
stage.remove(layer);
test(stage.children.length === 0, 'stage should have 0 children');
test(stage.get('.myLayer')[0] === undefined, 'layer should not exist');
test(stage.get('.myCircle')[0] === undefined, 'circle should not exist');
},
'STAGE - remove layer with no shapes': function(containerId) {
var stage = new Kinetic.Stage({
@@ -674,6 +680,8 @@ Test.prototype.tests = {
var layer = new Kinetic.Layer();
stage.add(layer);
stage.remove(layer);
test(stage.children.length === 0, 'stage should have 0 children');
},
'STAGE - remove shape multiple times': function(containerId) {
var stage = new Kinetic.Stage({