mirror of
https://github.com/konvajs/konva.git
synced 2026-01-18 19:51:21 +08:00
added new destroyChildren() method
This commit is contained in:
@@ -39,6 +39,25 @@
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* destroy all children
|
||||||
|
* @method
|
||||||
|
* @memberof Kinetic.Container.prototype
|
||||||
|
*/
|
||||||
|
destroyChildren: function() {
|
||||||
|
var children = this.children,
|
||||||
|
child;
|
||||||
|
|
||||||
|
while(children.length > 0) {
|
||||||
|
var child = children[0];
|
||||||
|
if (child.hasChildren()) {
|
||||||
|
child.destroyChildren();
|
||||||
|
}
|
||||||
|
child.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* add node to container
|
* add node to container
|
||||||
* @method
|
* @method
|
||||||
|
|||||||
10
src/Node.js
10
src/Node.js
@@ -179,22 +179,18 @@
|
|||||||
* node.destroy();
|
* node.destroy();
|
||||||
*/
|
*/
|
||||||
destroy: function() {
|
destroy: function() {
|
||||||
var parent = this.getParent(),
|
var children = this.children,
|
||||||
stage = this.getStage(),
|
|
||||||
dd = Kinetic.DD,
|
|
||||||
go = Kinetic.Global;
|
go = Kinetic.Global;
|
||||||
|
|
||||||
// destroy children
|
// destroy children
|
||||||
while(this.children && this.children.length > 0) {
|
while(this.hasChildren() && children.length > 0) {
|
||||||
this.children[0].destroy();
|
children[0].destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove from ids and names hashes
|
// remove from ids and names hashes
|
||||||
go._removeId(this.getId());
|
go._removeId(this.getId());
|
||||||
go._removeName(this.getName(), this._id);
|
go._removeName(this.getName(), this._id);
|
||||||
|
|
||||||
// TODO: stop transitions
|
|
||||||
|
|
||||||
this.remove();
|
this.remove();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -350,6 +350,55 @@ Test.Modules.CONTAINER = {
|
|||||||
test(layer.children.length === 0, 'layer should have 0 children');
|
test(layer.children.length === 0, 'layer should have 0 children');
|
||||||
test(group.children.length === 0, 'group should have 0 children');
|
test(group.children.length === 0, 'group should have 0 children');
|
||||||
},
|
},
|
||||||
|
'destroy all children from layer': function(containerId) {
|
||||||
|
var stage = new Kinetic.Stage({
|
||||||
|
container: containerId,
|
||||||
|
width: 578,
|
||||||
|
height: 200
|
||||||
|
});
|
||||||
|
var layer = new Kinetic.Layer({
|
||||||
|
name: 'layerName',
|
||||||
|
id: 'layerId'
|
||||||
|
});
|
||||||
|
var group = new Kinetic.Group();
|
||||||
|
var circle1 = new Kinetic.Circle({
|
||||||
|
x: 100,
|
||||||
|
y: stage.getHeight() / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'green',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4,
|
||||||
|
name: 'circleName',
|
||||||
|
id: 'circleId'
|
||||||
|
});
|
||||||
|
|
||||||
|
var circle2 = new Kinetic.Circle({
|
||||||
|
x: 300,
|
||||||
|
y: stage.getHeight() / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'green',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4
|
||||||
|
});
|
||||||
|
|
||||||
|
group.add(circle1);
|
||||||
|
group.add(circle2);
|
||||||
|
layer.add(group);
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
test(layer.children.length === 1, 'layer should have 1 children');
|
||||||
|
test(group.children.length === 2, 'group should have 2 children');
|
||||||
|
test(Kinetic.Global.names.circleName.length > 0, 'circleName should be in names hash');
|
||||||
|
test(Kinetic.Global.ids.circleId.getId() === 'circleId', 'layerId should be in ids hash');
|
||||||
|
|
||||||
|
layer.destroyChildren();
|
||||||
|
layer.draw();
|
||||||
|
|
||||||
|
test(layer.children.length === 0, 'layer should have 0 children');
|
||||||
|
test(group.children.length === 0, 'group should have 0 children');
|
||||||
|
test(Kinetic.Global.names.circleName === undefined, 'circleName should not be in names hash');
|
||||||
|
test(Kinetic.Global.ids.circleId === undefined, 'layerId should not be in ids hash');
|
||||||
|
},
|
||||||
'add group': function(containerId) {
|
'add group': function(containerId) {
|
||||||
var stage = new Kinetic.Stage({
|
var stage = new Kinetic.Stage({
|
||||||
container: containerId,
|
container: containerId,
|
||||||
|
|||||||
Reference in New Issue
Block a user