Implement Item#get/setDocument and Document#selectedItems.

This commit is contained in:
Jonathan Puckey 2011-04-21 15:47:00 +02:00
parent b2cd8cdec2
commit 517793c48a
3 changed files with 47 additions and 17 deletions

View file

@ -37,6 +37,7 @@ var Document = this.Document = Base.extend({
this.symbols = []; this.symbols = [];
this.views = [new DocumentView(this)]; this.views = [new DocumentView(this)];
this.activeView = this.views[0]; this.activeView = this.views[0];
this._selectedItems = [];
}, },
getCurrentStyle: function() { getCurrentStyle: function() {

View file

@ -19,7 +19,7 @@ var Item = this.Item = Base.extend({
initialize: function() { initialize: function() {
paper.document.activeLayer.appendTop(this); 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); child.setSelected(selected);
} }
} else { } 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() { getSelected: function() {
if (this._children) { if (this.children) {
for (var i = 0, l = this._children.length; i < l; i++) { for (var i = 0, l = this.children.length; i < l; i++) {
var child = this._children[i]; if (this.children[i].getSelected())
if (child.getSelected())
return true; return true;
} }
} else { } else {
return !!this._selected; 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 // TODO: isFullySelected / setFullySelected
@ -252,6 +279,8 @@ var Item = this.Item = Base.extend({
* Removes the item. * Removes the item.
*/ */
remove: function() { remove: function() {
if(this._selected)
this.setSelected(false);
return this.removeFromParent(); return this.removeFromParent();
}, },
@ -749,7 +778,7 @@ var Item = this.Item = Base.extend({
item.removeFromParent(); item.removeFromParent();
this.children.splice(top ? this.children.length : 0, 0, item); this.children.splice(top ? this.children.length : 0, 0, item);
item.parent = this; item.parent = this;
item.document = this.document; item.setDocument(this._document);
return true; return true;
} }
return false; return false;
@ -763,7 +792,7 @@ var Item = this.Item = Base.extend({
item.parent.children.splice(item.getIndex() item.parent.children.splice(item.getIndex()
+ (above ? 1 : -1), 0, this); + (above ? 1 : -1), 0, this);
this.parent = item.parent; this.parent = item.parent;
this.document = item.document; this.setDocument(item._document);
return true; return true;
} }
return false; return false;

View file

@ -19,13 +19,13 @@ 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); this._document.layers.push(this);
this.activate(); this.activate();
}, },
getIndex: function() { 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() { removeFromParent: function() {
if (!this.parent) { if (!this.parent) {
return !!this.document.layers.splice(this.getIndex(), 1).length; return !!this._document.layers.splice(this.getIndex(), 1).length;
} else { } else {
return this.base(); return this.base();
} }
@ -42,16 +42,16 @@ var Layer = this.Layer = Group.extend({
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.getIndex() + 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.getIndex() - 1] || null;
}, },
activate: function() { activate: function() {
this.document.activeLayer = this; this._document.activeLayer = this;
} }
}, new function () { }, new function () {
function move(above) { 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 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), item._document.layers.splice(item.getIndex() + (above ? 1 : -1),
0, this); 0, this);
this.document = item.document; this.setDocument(item._document);
return true; return true;
} else { } else {
return this.base(item); return this.base(item);