Implement #clone() for Item, Group and Path. Needs testing.

This commit is contained in:
Jürg Lehni 2011-05-19 21:56:23 +01:00
parent b52abbfbc1
commit 7401e42316
3 changed files with 40 additions and 17 deletions

View file

@ -26,6 +26,12 @@ var Group = this.Group = Item.extend({
|| typeof items[0] !== 'object' ? arguments : items);
},
clone: function() {
var copy = this.base();
copy._clipped = this._clipped;
return copy;
},
/**
* Specifies whether the group item is to be clipped.
* When setting to true, the first child in the group is automatically

View file

@ -23,6 +23,36 @@ var Item = this.Item = Base.extend({
this.setStyle(this._project.getCurrentStyle());
},
/**
* Clones the item within the same project.
*
* @return the newly cloned item
*/
clone: function() {
var copy = new this.constructor();
copy.setStyle(this._style);
if (this._children) {
for (var i = 0, l = this._children.length; i < l; i++)
copy.appendTop(this._children[i].clone());
}
// 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
// place
var keys = ['locked', 'visible', 'opacity', 'blendMode', '_clipMask'];
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (this.hasOwnProperty(key))
copy[key] = this[key];
}
copy.moveAbove(this);
// Only set name once the copy is moved, to avoid setting and unsettting
// name related structures.
if (this._name)
copy.setName(this._name);
return copy;
},
/**
* Private notifier that is called whenever a change occurs in this item or
* its sub-elements, such as Segments, Curves, PathStyles, etc.
@ -128,20 +158,6 @@ var Item = this.Item = Base.extend({
return copy;
},
/**
* Clones the item within the same project.
*
* @return the newly cloned item
*/
clone: function() {
var copy = new this.constructor();
// TODO: Copy children and other things.
if (this._parent) {
this._parent.appendTop(copy);
}
return copy;
},
setSelected: function(selected) {
if (this._children) {
for (var i = 0, l = this._children.length; i < l; i++) {

View file

@ -29,10 +29,11 @@ var Path = this.Path = PathItem.extend({
},
clone: function() {
var copy = new Path(this._segments);
copy.setStyle(this._style);
var copy = this.base();
copy.setSegments(this._segments);
copy._closed = this._closed;
copy.moveAbove(this);
if (this._clockwise !== undefined)
copy._clockwise = this._clockwise;
return copy;
},