Use Base.splice() for Layers too.

This commit is contained in:
Jürg Lehni 2011-05-07 17:27:19 +01:00
parent 66824b780a
commit 20409f3084
2 changed files with 11 additions and 17 deletions

View file

@ -55,6 +55,7 @@ var Document = this.Document = Base.extend({
this.bounds = Rectangle.create(0, 0, this._size.width, this.bounds = Rectangle.create(0, 0, this._size.width,
this._size.height); this._size.height);
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d');
// Push it onto paper.documents and adjust index in one:
Base.splice(paper.documents, [this]); Base.splice(paper.documents, [this]);
this.activate(); this.activate();
this.layers = []; this.layers = [];

View file

@ -20,34 +20,28 @@ var Layer = this.Layer = Group.extend({
initialize: function() { initialize: function() {
this.children = []; this.children = [];
this._document = paper.document; this._document = paper.document;
this._document.layers.push(this); // Push it onto document.layers and adjust index in one:
Base.splice(this._document.layers, [this]);
this.activate(); this.activate();
}, },
getIndex: function() {
return this.parent ? this.base() : this._document.layers.indexOf(this);
},
/** /**
* Removes the layer from its document's layers list * Removes the layer from its document's layers list
* or its parent's children list. * or its parent's children list.
*/ */
_removeFromParent: function() { _removeFromParent: function() {
if (!this.parent) { return this.parent ? this.base()
return !!this._document.layers.splice(this.getIndex(), 1).length; : !!Base.splice(this._document.layers, null, this._index, 1).length;
} else {
return this.base();
}
}, },
getNextSibling: function() { getNextSibling: function() {
return this.parent ? this.base() return this.parent ? this.base()
: this._document.layers[this.getIndex() + 1] || null; : this._document.layers[this._index + 1] || null;
}, },
getPreviousSibling: function() { getPreviousSibling: function() {
return this.parent ? this.base() return this.parent ? this.base()
: this._document.layers[this.getIndex() - 1] || null; : this._document.layers[this._index - 1] || null;
}, },
activate: function() { activate: function() {
@ -58,14 +52,13 @@ var Layer = this.Layer = Group.extend({
return function(item) { return function(item) {
// if the item is a layer and contained within Document#layers // if the item is a layer and contained within Document#layers
if (item instanceof Layer && !item.parent if (item instanceof Layer && !item.parent
&& this._removeFromParent()) { && this._removeFromParent()) {
item._document.layers.splice(item.getIndex() + (above ? 1 : -1), Base.splice(item._document.layers, [this],
0, this); item._index + (above ? 1 : -1), 0);
this._setDocument(item._document); this._setDocument(item._document);
return true; return true;
} else {
return this.base(item);
} }
return this.base(item);
}; };
} }