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.views = [new DocumentView(this)];
this.activeView = this.views[0];
this._selectedItems = [];
},
getCurrentStyle: function() {

View file

@ -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;

View file

@ -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);