fix #352 also did major remove and destroy refactoring, and added lots of unit and visual tests

This commit is contained in:
Eric Rowell
2013-06-08 15:57:36 -07:00
parent 0983531197
commit b86aa11d47
4 changed files with 76 additions and 21 deletions

View File

@@ -45,17 +45,10 @@
* @memberof Kinetic.Container.prototype
*/
destroyChildren: function() {
var children = this.children,
child;
var children = this.children;
while(children.length > 0) {
var child = children[0];
if (child.hasChildren()) {
child.destroyChildren();
children[0].destroy();
}
child.destroy();
}
return this;
},
/**
@@ -76,6 +69,14 @@
// chainable
return this;
},
destroy: function() {
// destroy children
if (this.hasChildren()) {
this.destroyChildren();
}
// then destroy self
Kinetic.Node.prototype.destroy.call(this);
},
/**
* return a {@link Kinetic.Collection} of nodes that match the selector. Use '#' for id selections
* and '.' for name selections. You can also select by type or class name

View File

@@ -167,8 +167,9 @@
if(parent && parent.children) {
parent.children.splice(this.index, 1);
parent._setChildrenIndices();
}
delete this.parent;
}
return this;
},
/**
@@ -179,20 +180,13 @@
* node.destroy();
*/
destroy: function() {
var children = this.children,
go = Kinetic.Global;
// destroy children
while(this.hasChildren() && children.length > 0) {
children[0].destroy();
}
var go = Kinetic.Global;
// remove from ids and names hashes
go._removeId(this.getId());
go._removeName(this.getName(), this._id);
this.remove();
return this;
},
/**
* get attr

View File

@@ -135,14 +135,13 @@
* @method
* @memberof Kinetic.Stage.prototype
*/
remove: function() {
destroy: function() {
var content = this.content;
Kinetic.Node.prototype.remove.call(this);
Kinetic.Container.prototype.destroy.call(this);
if(content && Kinetic.Util._isInDocument(content)) {
this.getContainer().removeChild(content);
}
return this;
},
/**
* get mouse position for desktop apps

View File

@@ -256,6 +256,67 @@ Test.Modules.STAGE = {
layer.add(circle);
stage.add(layer);
},
'remove stage': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4
});
layer.add(circle);
stage.add(layer);
// remove should have no effect, and should cause no JS error
stage.remove();
},
'destroy stage': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200,
id: 'stageFalconId',
name: 'stageFalconName'
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
id: 'circleFalconId',
name: 'circleFalconName'
});
layer.add(circle);
stage.add(layer);
test(Kinetic.Global.ids.stageFalconId._id === stage._id, 'stage id should be in global ids map');
test(Kinetic.Global.names.stageFalconName[0]._id === stage._id, 'stage name should be in global names map');
test(Kinetic.Global.ids.circleFalconId._id === circle._id, 'circle id should be in global ids map');
test(Kinetic.Global.names.circleFalconName[0]._id === circle._id, 'circle name should be in global names map');
stage.destroy();
test(Kinetic.Global.ids.stageFalconId === undefined, 'stage should no longer be in ids map');
test(Kinetic.Global.names.stageFalconName === undefined, 'stage should no longer be in names map');
test(Kinetic.Global.ids.circleFalconId === undefined, 'circle should no longer be in ids map');
test(Kinetic.Global.names.circleFalconName === undefined, 'circle should no longer be in names map');
},
'scale stage with no shapes': function(containerId) {
var stage = new Kinetic.Stage({