diff --git a/src/document/Document.js b/src/document/Document.js index 74285f2b..42c405d8 100644 --- a/src/document/Document.js +++ b/src/document/Document.js @@ -37,6 +37,7 @@ var Document = this.Document = Base.extend({ this.symbols = []; this.views = [new DocumentView(this)]; this.activeView = this.views[0]; + this._selectedItems = []; }, getCurrentStyle: function() { diff --git a/src/item/Item.js b/src/item/Item.js index f1b069db..ea71eebd 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -19,7 +19,7 @@ var Item = this.Item = Base.extend({ initialize: function() { paper.document.activeLayer.appendTop(this); - this.setStyle(this.document.getCurrentStyle()); + this.setStyle(this._document.getCurrentStyle()); }, /** @@ -60,20 +60,47 @@ var Item = this.Item = Base.extend({ child.setSelected(selected); } } else { - this._selected = selected; + if (selected != this._selected) { + // TODO: when an item is removed or moved to another + // document, it needs to be removed from _selectedItems + this._selected = selected; + var selectedItems = this._document._selectedItems; + if (selected) { + selectedItems.push(this); + } else { + // TODO: is there a faster way? + var index = selectedItems.indexOf(this); + if (index != -1) + selectedItems.splice(index, 1); + } + } } }, getSelected: function() { - if (this._children) { - for (var i = 0, l = this._children.length; i < l; i++) { - var child = this._children[i]; - if (child.getSelected()) + if (this.children) { + for (var i = 0, l = this.children.length; i < l; i++) { + if (this.children[i].getSelected()) return true; } } else { return !!this._selected; } + return false; + }, + + getDocument: function() { + return this._document; + }, + + setDocument: function(document) { + if (document != this._document) { + this._document = document; + if (this.children) { + for (var i = 0, l = this.children.length; i < l; i++) + this.children[i].setDocument(document); + } + } }, // TODO: isFullySelected / setFullySelected @@ -252,6 +279,8 @@ var Item = this.Item = Base.extend({ * Removes the item. */ remove: function() { + if(this._selected) + this.setSelected(false); return this.removeFromParent(); }, @@ -749,7 +778,7 @@ var Item = this.Item = Base.extend({ item.removeFromParent(); this.children.splice(top ? this.children.length : 0, 0, item); item.parent = this; - item.document = this.document; + item.setDocument(this._document); return true; } return false; @@ -763,7 +792,7 @@ var Item = this.Item = Base.extend({ item.parent.children.splice(item.getIndex() + (above ? 1 : -1), 0, this); this.parent = item.parent; - this.document = item.document; + this.setDocument(item._document); return true; } return false; diff --git a/src/item/Layer.js b/src/item/Layer.js index 1958d0bb..6a41c2a3 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -19,13 +19,13 @@ var Layer = this.Layer = Group.extend({ initialize: function() { this.children = []; - this.document = paper.document; - this.document.layers.push(this); + this._document = paper.document; + this._document.layers.push(this); this.activate(); }, getIndex: function() { - return this.parent ? this.base() : this.document.layers.indexOf(this); + return this.parent ? this.base() : this._document.layers.indexOf(this); }, /** @@ -34,7 +34,7 @@ var Layer = this.Layer = Group.extend({ */ removeFromParent: function() { if (!this.parent) { - return !!this.document.layers.splice(this.getIndex(), 1).length; + return !!this._document.layers.splice(this.getIndex(), 1).length; } else { return this.base(); } @@ -42,16 +42,16 @@ var Layer = this.Layer = Group.extend({ getNextSibling: function() { return this.parent ? this.base() - : this.document.layers[this.getIndex() + 1] || null; + : this._document.layers[this.getIndex() + 1] || null; }, getPreviousSibling: function() { return this.parent ? this.base() - : this.document.layers[this.getIndex() - 1] || null; + : this._document.layers[this.getIndex() - 1] || null; }, activate: function() { - this.document.activeLayer = this; + this._document.activeLayer = this; } }, new function () { function move(above) { @@ -59,9 +59,9 @@ var Layer = this.Layer = Group.extend({ // 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), + item._document.layers.splice(item.getIndex() + (above ? 1 : -1), 0, this); - this.document = item.document; + this.setDocument(item._document); return true; } else { return this.base(item);