Fix issue where adding the children of one group to another using Item#insertChildren / Item#addChildren does not work.

This fixes #119. To avoid duplication I chose to call
Item#insertChildren from Item#addChildren.
This commit is contained in:
Jonathan Puckey 2012-10-06 13:08:07 +02:00
parent b2b4924c9e
commit e3518c29b1
2 changed files with 23 additions and 2 deletions

View file

@ -1143,8 +1143,7 @@ function(name) {
* @param {item[]} items The items to be added as children
*/
addChildren: function(items) {
for (var i = 0, l = items && items.length; i < l; i++)
this.insertChild(undefined, items[i]);
this.insertChildren(this._children.length, items);
},
/**
@ -1156,6 +1155,10 @@ function(name) {
* @param {Item[]} items The items to be appended as children
*/
insertChildren: function(index, items) {
// We need to clone items because it might be
// an Item#children array. Use Array.prototype.slice because
// in certain cases items is an arguments object
items = items && Array.prototype.slice.apply(items);
for (var i = 0, l = items && items.length; i < l; i++) {
if (this.insertChild(index, items[i]))
index++;

View file

@ -70,4 +70,22 @@ test('group.addChildren(otherGroup.children)', function() {
equals(function() {
return group.children.length;
}, 0);
});
test('group.insertChildren(0, otherGroup.children)', function() {
var group = new Group();
group.addChild(new Path());
group.addChild(new Path());
equals(function() {
return group.children.length;
}, 2);
var secondGroup = new Group();
secondGroup.insertChildren(0, group.children);
equals(function() {
return secondGroup.children.length;
}, 2);
equals(function() {
return group.children.length;
}, 0);
});