mirror of
https://github.com/konvajs/konva.git
synced 2025-09-18 18:27:58 +08:00
remove() method now correctly removes node descendants
This commit is contained in:
12
dist/kinetic-core.js
vendored
12
dist/kinetic-core.js
vendored
@@ -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;
|
||||
|
29
dist/kinetic-core.min.js
vendored
29
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -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;
|
||||
|
@@ -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({
|
||||
|
Reference in New Issue
Block a user