Skip to content

Commit

Permalink
add function takes care about parent existence. fix #426, #434
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrton committed Mar 1, 2014
1 parent cadcb91 commit 74060ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@
* @returns {Container}
*/
add: function(child) {
if (child.getParent()) {
child.moveTo(this);
return;
}
var children = this.children;

this._validateAdd(child);
child.index = children.length;
child.parent = this;
Expand Down
35 changes: 35 additions & 0 deletions test/unit/Container-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,41 @@ suite('Container', function() {
assert.equal(blueGroup.getZIndex(), 1, 'blue group should have zindex 1 after relayering');
assert.equal(greenGroup.getZIndex(), 0, 'green group should have zindex 0 after relayering');

layer.draw();
});

// ======================================================
test('add and moveTo should work same way (depend on parent)', function() {
var stage = addStage();
var layer = new Kinetic.Layer();
var greenGroup = new Kinetic.Group();
var blueGroup = new Kinetic.Group();

var bluecircle = new Kinetic.Circle({
x: 200,
y: stage.getHeight() / 2,
radius: 70,
fill: 'blue',
stroke: 'black',
strokeWidth: 4
});

bluecircle.moveTo(blueGroup);

layer.add(blueGroup);
layer.add(greenGroup);
stage.add(layer);

assert.equal(blueGroup.getChildren().length, 1, 'blue group should have only one children');
blueGroup.add(bluecircle);
assert.equal(blueGroup.getChildren().length, 1, 'blue group should have only one children after adding node twice');

greenGroup.add(bluecircle);
assert.equal(blueGroup.getChildren().length, 0, 'blue group should not have children');
assert.equal(greenGroup.getChildren().length, 1, 'green group should have only one children');



layer.draw();
});

Expand Down

0 comments on commit 74060ce

Please sign in to comment.