mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 06:00:56 -05:00
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:
parent
b2b4924c9e
commit
e3518c29b1
2 changed files with 23 additions and 2 deletions
|
@ -1143,8 +1143,7 @@ function(name) {
|
||||||
* @param {item[]} items The items to be added as children
|
* @param {item[]} items The items to be added as children
|
||||||
*/
|
*/
|
||||||
addChildren: function(items) {
|
addChildren: function(items) {
|
||||||
for (var i = 0, l = items && items.length; i < l; i++)
|
this.insertChildren(this._children.length, items);
|
||||||
this.insertChild(undefined, items[i]);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1156,6 +1155,10 @@ function(name) {
|
||||||
* @param {Item[]} items The items to be appended as children
|
* @param {Item[]} items The items to be appended as children
|
||||||
*/
|
*/
|
||||||
insertChildren: function(index, items) {
|
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++) {
|
for (var i = 0, l = items && items.length; i < l; i++) {
|
||||||
if (this.insertChild(index, items[i]))
|
if (this.insertChild(index, items[i]))
|
||||||
index++;
|
index++;
|
||||||
|
|
|
@ -70,4 +70,22 @@ test('group.addChildren(otherGroup.children)', function() {
|
||||||
equals(function() {
|
equals(function() {
|
||||||
return group.children.length;
|
return group.children.length;
|
||||||
}, 0);
|
}, 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);
|
||||||
});
|
});
|
Loading…
Reference in a new issue