Implement insert parameter in Item#clone() to control DOM insertion.

Defaults to true. Also insert in the right place now, above the original.
This commit is contained in:
Jürg Lehni 2013-07-19 18:27:00 -07:00
parent 14888bca50
commit 20a17f2939
7 changed files with 32 additions and 20 deletions

View file

@ -1176,6 +1176,9 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* Clones the item within the same project and places the copy above the * Clones the item within the same project and places the copy above the
* item. * 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 * @return {Item} the newly cloned item
* *
* @example {@paperscript} * @example {@paperscript}
@ -1194,11 +1197,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* copy.position.x += i * copy.bounds.width; * copy.position.x += i * copy.bounds.width;
* } * }
*/ */
clone: function() { clone: function(insert) {
return this._clone(new this.constructor()); return this._clone(new this.constructor({ insert: false }), insert);
}, },
_clone: function(copy) { _clone: function(copy, insert) {
// Copy over style // Copy over style
copy.setStyle(this._style); copy.setStyle(this._style);
// If this item has children, clone and append each of them: // 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 // Clone all children and add them to the copy. tell #addChild we're
// cloning, as needed by CompoundPath#insertChild(). // cloning, as needed by CompoundPath#insertChild().
for (var i = 0, l = this._children.length; i < l; i++) 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' // 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 // 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. // in the same parent, by passing true for the unique parameter.
if (this._name) if (this._name)
copy.setName(this._name, true); copy.setName(this._name, true);
// Insert is true by default.
if (insert || insert === undefined)
copy.insertAbove(this);
return copy; return copy;
}, },
@ -1586,8 +1592,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* @return {Boolean} {@true it was inserted} * @return {Boolean} {@true it was inserted}
*/ */
insertAbove: function(item, _preserve) { insertAbove: function(item, _preserve) {
if (!item._parent)
return null;
var index = item._index; var index = item._index;
if (item._parent == this._parent && index < this._index) if (item._parent === this._parent && index < this._index)
index++; index++;
return item._parent.insertChild(index, this, _preserve); return item._parent.insertChild(index, this, _preserve);
}, },
@ -1599,8 +1607,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* @return {Boolean} {@true it was inserted} * @return {Boolean} {@true it was inserted}
*/ */
insertBelow: function(item, _preserve) { insertBelow: function(item, _preserve) {
if (!item._parent)
return null;
var index = item._index; var index = item._index;
if (item._parent == this._parent && index > this._index) if (item._parent === this._parent && index > this._index)
index--; index--;
return item._parent.insertChild(index, this, _preserve); 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} * @return {Boolean} {@true if it is above the specified item}
*/ */
isAbove: function(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} * @return {Boolean} {@true if it is below the specified item}
*/ */
isBelow: function(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} * @return {Boolean} {@true if it is the parent of the item}
*/ */
isParent: function(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} * @return {Boolean} {@true it is a child of the item}
*/ */
isChild: function(item) { isChild: function(item) {
return item && item._parent == this; return item && item._parent === this;
}, },
/** /**

View file

@ -98,8 +98,8 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
symbol._instances[this._id] = this; symbol._instances[this._id] = this;
}, },
clone: function() { clone: function(insert) {
return this._clone(new PlacedSymbol(this.symbol)); return this._clone(new PlacedSymbol(this.symbol), insert);
}, },
isEmpty: function() { isEmpty: function() {

View file

@ -93,7 +93,7 @@ var Raster = Item.extend(/** @lends Raster# */{
this._size = new Size(); this._size = new Size();
}, },
clone: function() { clone: function(insert) {
var element = this._image; var element = this._image;
if (!element) { if (!element) {
// If the Raster contains a Canvas object, we need to create // 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 = CanvasProvider.getCanvas(this._size);
element.getContext('2d').drawImage(this._canvas, 0, 0); element.getContext('2d').drawImage(this._canvas, 0, 0);
} }
var copy = new Raster(element); return this._clone(new Raster(element), insert);
return this._clone(copy);
}, },
/** /**

View file

@ -90,8 +90,8 @@ var Path = PathItem.extend(/** @lends Path# */{
this._set(arg); this._set(arg);
}, },
clone: function() { clone: function(insert) {
var copy = this._clone(new Path(this._segments)); var copy = this._clone(new Path(this._segments), insert);
copy._closed = this._closed; copy._closed = this._closed;
if (this._clockwise !== undefined) if (this._clockwise !== undefined)
copy._clockwise = this._clockwise; copy._clockwise = this._clockwise;

View file

@ -146,6 +146,6 @@ var Symbol = Base.extend(/** @lends Symbol# */{
* @return {Symbol} * @return {Symbol}
*/ */
clone: function() { clone: function() {
return new Symbol(this._definition.clone()); return new Symbol(this._definition.clone(false));
} }
}); });

View file

@ -47,8 +47,8 @@ var PointText = TextItem.extend(/** @lends PointText# */{
TextItem.apply(this, arguments); TextItem.apply(this, arguments);
}, },
clone: function() { clone: function(insert) {
return this._clone(new PointText()); return this._clone(new PointText(), insert);
}, },
/** /**

View file

@ -171,6 +171,9 @@ test('Item#clone() Hierarchy', function() {
var path1 = new Path.Circle([150, 150], 60); var path1 = new Path.Circle([150, 150], 60);
var path2 = new Path.Circle([150, 150], 60); var path2 = new Path.Circle([150, 150], 60);
var clone = path1.clone(); var clone = path1.clone();
equals(function() {
return path2.isAbove(path1);
}, true);
equals(function() { equals(function() {
return clone.isAbove(path1); return clone.isAbove(path1);
}, true); }, true);