From 20409f308425c385033c562e2195537a237b83d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 7 May 2011 17:27:19 +0100 Subject: [PATCH] Use Base.splice() for Layers too. --- src/document/Document.js | 1 + src/item/Layer.js | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/document/Document.js b/src/document/Document.js index c7f9ae6e..83f998d7 100644 --- a/src/document/Document.js +++ b/src/document/Document.js @@ -55,6 +55,7 @@ var Document = this.Document = Base.extend({ this.bounds = Rectangle.create(0, 0, this._size.width, this._size.height); this.context = this.canvas.getContext('2d'); + // Push it onto paper.documents and adjust index in one: Base.splice(paper.documents, [this]); this.activate(); this.layers = []; diff --git a/src/item/Layer.js b/src/item/Layer.js index 3172db33..faaa7a52 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -20,34 +20,28 @@ var Layer = this.Layer = Group.extend({ initialize: function() { this.children = []; 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(); }, - getIndex: function() { - return this.parent ? this.base() : this._document.layers.indexOf(this); - }, - /** * Removes the layer from its document's layers list * or its parent's children list. */ _removeFromParent: function() { - if (!this.parent) { - return !!this._document.layers.splice(this.getIndex(), 1).length; - } else { - return this.base(); - } + return this.parent ? this.base() + : !!Base.splice(this._document.layers, null, this._index, 1).length; }, getNextSibling: function() { return this.parent ? this.base() - : this._document.layers[this.getIndex() + 1] || null; + : this._document.layers[this._index + 1] || null; }, getPreviousSibling: function() { return this.parent ? this.base() - : this._document.layers[this.getIndex() - 1] || null; + : this._document.layers[this._index - 1] || null; }, activate: function() { @@ -58,14 +52,13 @@ var Layer = this.Layer = Group.extend({ return function(item) { // if the item is a layer and contained within Document#layers if (item instanceof Layer && !item.parent - && this._removeFromParent()) { - item._document.layers.splice(item.getIndex() + (above ? 1 : -1), - 0, this); + && this._removeFromParent()) { + Base.splice(item._document.layers, [this], + item._index + (above ? 1 : -1), 0); this._setDocument(item._document); return true; - } else { - return this.base(item); } + return this.base(item); }; }