diff --git a/src/item/Item.js b/src/item/Item.js index e44b7a46..3d7ada6f 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1176,6 +1176,9 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * Clones the item within the same project and places the copy above the * item. * + * @param {Boolean} [insert=true] specifies wether the copy should be + * inserted into the DOM. When set to {@code true}, it is inserted above the + * original. * @return {Item} the newly cloned item * * @example {@paperscript} @@ -1194,11 +1197,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * copy.position.x += i * copy.bounds.width; * } */ - clone: function() { - return this._clone(new this.constructor()); + clone: function(insert) { + return this._clone(new this.constructor({ insert: false }), insert); }, - _clone: function(copy) { + _clone: function(copy, insert) { // Copy over style copy.setStyle(this._style); // If this item has children, clone and append each of them: @@ -1206,7 +1209,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ // Clone all children and add them to the copy. tell #addChild we're // cloning, as needed by CompoundPath#insertChild(). for (var i = 0, l = this._children.length; i < l; i++) - copy.addChild(this._children[i].clone(), true); + copy.addChild(this._children[i].clone(false), true); } // Only copy over these fields if they are actually defined in 'this' // TODO: Consider moving this to Base once it's useful in more than one @@ -1227,6 +1230,9 @@ var Item = Base.extend(Callback, /** @lends Item# */{ // in the same parent, by passing true for the unique parameter. if (this._name) copy.setName(this._name, true); + // Insert is true by default. + if (insert || insert === undefined) + copy.insertAbove(this); return copy; }, @@ -1586,8 +1592,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true it was inserted} */ insertAbove: function(item, _preserve) { + if (!item._parent) + return null; var index = item._index; - if (item._parent == this._parent && index < this._index) + if (item._parent === this._parent && index < this._index) index++; return item._parent.insertChild(index, this, _preserve); }, @@ -1599,8 +1607,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true it was inserted} */ insertBelow: function(item, _preserve) { + if (!item._parent) + return null; var index = item._index; - if (item._parent == this._parent && index > this._index) + if (item._parent === this._parent && index > this._index) index--; return item._parent.insertChild(index, this, _preserve); }, @@ -1831,7 +1841,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true if it is above the specified item} */ isAbove: function(item) { - return this._getOrder(item) == -1; + return this._getOrder(item) === -1; }, /** @@ -1842,7 +1852,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true if it is below the specified item} */ isBelow: function(item) { - return this._getOrder(item) == 1; + return this._getOrder(item) === 1; }, /** @@ -1852,7 +1862,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true if it is the parent of the item} */ isParent: function(item) { - return this._parent == item; + return this._parent === item; }, /** @@ -1862,7 +1872,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @return {Boolean} {@true it is a child of the item} */ isChild: function(item) { - return item && item._parent == this; + return item && item._parent === this; }, /** diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index 86f6d0ca..911016ad 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -98,8 +98,8 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{ symbol._instances[this._id] = this; }, - clone: function() { - return this._clone(new PlacedSymbol(this.symbol)); + clone: function(insert) { + return this._clone(new PlacedSymbol(this.symbol), insert); }, isEmpty: function() { diff --git a/src/item/Raster.js b/src/item/Raster.js index bf9232d6..0cbce496 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -93,7 +93,7 @@ var Raster = Item.extend(/** @lends Raster# */{ this._size = new Size(); }, - clone: function() { + clone: function(insert) { var element = this._image; if (!element) { // If the Raster contains a Canvas object, we need to create @@ -101,8 +101,7 @@ var Raster = Item.extend(/** @lends Raster# */{ element = CanvasProvider.getCanvas(this._size); element.getContext('2d').drawImage(this._canvas, 0, 0); } - var copy = new Raster(element); - return this._clone(copy); + return this._clone(new Raster(element), insert); }, /** diff --git a/src/path/Path.js b/src/path/Path.js index c51d037f..bf7dcc8d 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -90,8 +90,8 @@ var Path = PathItem.extend(/** @lends Path# */{ this._set(arg); }, - clone: function() { - var copy = this._clone(new Path(this._segments)); + clone: function(insert) { + var copy = this._clone(new Path(this._segments), insert); copy._closed = this._closed; if (this._clockwise !== undefined) copy._clockwise = this._clockwise; diff --git a/src/project/Symbol.js b/src/project/Symbol.js index 31446387..808b5587 100644 --- a/src/project/Symbol.js +++ b/src/project/Symbol.js @@ -146,6 +146,6 @@ var Symbol = Base.extend(/** @lends Symbol# */{ * @return {Symbol} */ clone: function() { - return new Symbol(this._definition.clone()); + return new Symbol(this._definition.clone(false)); } }); diff --git a/src/text/PointText.js b/src/text/PointText.js index 07257490..1b4561b9 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -47,8 +47,8 @@ var PointText = TextItem.extend(/** @lends PointText# */{ TextItem.apply(this, arguments); }, - clone: function() { - return this._clone(new PointText()); + clone: function(insert) { + return this._clone(new PointText(), insert); }, /** diff --git a/test/tests/Item_Cloning.js b/test/tests/Item_Cloning.js index f73850df..d9244b09 100644 --- a/test/tests/Item_Cloning.js +++ b/test/tests/Item_Cloning.js @@ -171,6 +171,9 @@ test('Item#clone() Hierarchy', function() { var path1 = new Path.Circle([150, 150], 60); var path2 = new Path.Circle([150, 150], 60); var clone = path1.clone(); + equals(function() { + return path2.isAbove(path1); + }, true); equals(function() { return clone.isAbove(path1); }, true);