diff --git a/src/item/Item.js b/src/item/Item.js index 32de56d6..fe1ed285 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -977,11 +977,43 @@ var Item = this.Item = Base.extend(Callback, { * * // Now the parent of the path has become the group: * console.log(path.parent == group); // true + * + * @example // Setting the parent of the item to another item + * var path = new Path(); + * + * // New items are placed in the active layer: + * console.log(path.parent == project.activeLayer); // true + * + * var group = new Group(); + * group.parent = path; + * + * // Now the parent of the path has become the group: + * console.log(path.parent == group); // true + * + * // The path is now contained in the children list of group: + * console.log(group.children[0] == path); // true + * + * @example // Setting the parent of an item in the constructor + * var group = new Group(); + * + * var path = new Path({ + * parent: group + * }); + * + * // The parent of the path is the group: + * console.log(path.parent == group); // true + * + * // The path is contained in the children list of group: + * console.log(group.children[0] == path); // true */ getParent: function() { return this._parent; }, + setParent: function(item) { + return item.addChild(this); + }, + /** * The children items contained within this item. Items that define a * {@link #name} can also be accessed by name. diff --git a/test/tests/Item.js b/test/tests/Item.js index 4aa462fa..ebd2cb48 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -63,6 +63,41 @@ test('addChild(item)', function() { }, 1); }); +test('setting item.parent', function() { + var layer1 = paper.project.activeLayer; + var layer2 = new Layer(); + layer1.activate(); + var group = new Group(); + + var path = new Path(); + equals(function() { + return path.parent === layer1; + }, true, 'Path is a child of layer1 because it is active'); + + path.parent = layer2; + equals(function() { + return path.parent === layer2; + }, true, 'The parent of path was set to layer2'); + + path.parent = group; + equals(function() { + return path.parent === group; + }, true, 'The parent of path was set to group'); + equals(function() { + return layer2.children.indexOf(path) === -1; + }, false, 'The path is no longer a child of layer2'); + + var path2 = new Path({ + parent: group + }); + equals(function() { + return path2.parent === group; + }, true, 'setting the parent in the constructor'); + equals(function() { + return group.children.indexOf(path2) == 1; + }, true, 'the index of path2 is 1, because group already contains path from before'); +}); + test('item.parent / item.isChild / item.isParent / item.layer', function() { var project = paper.project; var secondDoc = new Project();