Rename Item#children to private Item#_children and add #getChildren getter.

This commit is contained in:
Jürg Lehni 2011-05-14 18:07:10 +01:00
parent 185a7f15ac
commit 0bab694a01
5 changed files with 84 additions and 73 deletions

View file

@ -19,7 +19,7 @@ var Group = this.Group = Item.extend({
initialize: function(items) { initialize: function(items) {
this.base(); this.base();
this.children = []; this._children = [];
if (items) { if (items) {
for (var i = 0, l = items.length; i < l; i++) { for (var i = 0, l = items.length; i < l; i++) {
this.appendTop(items[i]); this.appendTop(items[i]);
@ -53,8 +53,8 @@ var Group = this.Group = Item.extend({
}, },
draw: function(ctx, param) { draw: function(ctx, param) {
for (var i = 0, l = this.children.length; i < l; i++) { for (var i = 0, l = this._children.length; i < l; i++) {
Item.draw(this.children[i], ctx, param); Item.draw(this._children[i], ctx, param);
if (this._clipped && i == 0) if (this._clipped && i == 0)
ctx.clip(); ctx.clip();
} }

View file

@ -67,9 +67,9 @@ var Item = this.Item = Base.extend({
}, },
setSelected: function(selected) { setSelected: function(selected) {
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++) {
this.children[i].setSelected(selected); this._children[i].setSelected(selected);
} }
} else { } else {
if ((selected = !!selected) != this._selected) { if ((selected = !!selected) != this._selected) {
@ -82,9 +82,9 @@ var Item = this.Item = Base.extend({
}, },
isSelected: function() { isSelected: 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++) {
if (this.children[i].isSelected()) { if (this._children[i].isSelected()) {
return true; return true;
} }
} }
@ -101,9 +101,9 @@ var Item = this.Item = Base.extend({
_setDocument: function(document) { _setDocument: function(document) {
if (this._document != document) { if (this._document != document) {
this._document = document; this._document = document;
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++) {
this.children[i]._setDocument(document); this._children[i]._setDocument(document);
} }
} }
} }
@ -219,49 +219,11 @@ var Item = this.Item = Base.extend({
} }
}, },
// TODO: getIsolated / setIsolated (print specific feature) // TODO: get/setIsolated (print specific feature)
// TODO: get/setKnockout (print specific feature) // TODO: get/setKnockout (print specific feature)
// TODO get/setAlphaIsShape // TODO get/setAlphaIsShape
// TODO: get/setData // TODO: get/setData
/**
* Reverses the order of this item's children
*/
reverseChildren: function() {
if (this.children) {
this.children.reverse();
// TODO: Reassign _index
}
},
/**
* The first item contained within this item.
*/
getFirstChild: function() {
return this.children && this.children[0] || null;
},
/**
* The last item contained within this item.
*/
getLastChild: function() {
return this.children && this.children[this.children.length - 1] || null;
},
/**
* The next item on the same level as this item.
*/
getNextSibling: function() {
return this._parent && this._parent.children[this._index + 1] || null;
},
/**
* The previous item on the same level as this item.
*/
getPreviousSibling: function() {
return this._parent && this._parent.children[this._index - 1] || null;
},
/** /**
* The item that this item is contained within. * The item that this item is contained within.
*/ */
@ -269,6 +231,8 @@ var Item = this.Item = Base.extend({
return this._parent; return this._parent;
}, },
// TODO: #getLayer()
/** /**
* The index of this item within the list of it's parent's children. * The index of this item within the list of it's parent's children.
*/ */
@ -276,12 +240,59 @@ var Item = this.Item = Base.extend({
return this._index; return this._index;
}, },
/**
* The children items contained within this item.
*/
getChildren: function() {
return this._children;
},
// TODO: #setChildren()
/**
* Reverses the order of this item's children
*/
reverseChildren: function() {
// TODO: Reassign _index
if (this._children) {
this._children.reverse();
}
},
/**
* The first item contained within this item.
*/
getFirstChild: function() {
return this._children && this._children[0] || null;
},
/**
* The last item contained within this item.
*/
getLastChild: function() {
return this._children && this._children[this._children.length - 1] || null;
},
/**
* The next item on the same level as this item.
*/
getNextSibling: function() {
return this._parent && this._parent._children[this._index + 1] || null;
},
/**
* The previous item on the same level as this item.
*/
getPreviousSibling: function() {
return this._parent && this._parent._children[this._index - 1] || null;
},
/** /**
* Removes the item from its parent's children list. * Removes the item from its parent's children list.
*/ */
_removeFromParent: function() { _removeFromParent: function() {
if (this._parent) { if (this._parent) {
var ok = !!Base.splice(this._parent.children, null, var ok = !!Base.splice(this._parent._children, null,
this._index, 1).length; this._index, 1).length;
this._parent = null; this._parent = null;
this._index = null; this._index = null;
@ -307,7 +318,7 @@ var Item = this.Item = Base.extend({
* @return {@true if it has one or more children} * @return {@true if it has one or more children}
*/ */
hasChildren: function() { hasChildren: function() {
return this.children && this.children.length > 0; return this._children && this._children.length > 0;
}, },
/** /**
@ -460,7 +471,7 @@ var Item = this.Item = Base.extend({
}, },
_getBounds: function(includeStroke) { _getBounds: function(includeStroke) {
var children = this.children; var children = this._children;
if (children && children.length) { if (children && children.length) {
var x1 = Infinity, var x1 = Infinity,
x2 = -Infinity, x2 = -Infinity,
@ -588,9 +599,9 @@ var Item = this.Item = Base.extend({
// Transform position as well // Transform position as well
if (this._position) if (this._position)
this._position = matrix._transformPoint(this._position); this._position = matrix._transformPoint(this._position);
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]; var child = this._children[i];
child.transform(matrix, flags); child.transform(matrix, flags);
} }
} }
@ -819,8 +830,8 @@ var Item = this.Item = Base.extend({
function append(top) { function append(top) {
return function(item) { return function(item) {
item._removeFromParent(); item._removeFromParent();
if (this.children) { if (this._children) {
Base.splice(this.children, [item], top ? undefined : 0, 0); Base.splice(this._children, [item], top ? undefined : 0, 0);
item._parent = this; item._parent = this;
item._setDocument(this._document); item._setDocument(this._document);
return true; return true;
@ -833,7 +844,7 @@ var Item = this.Item = Base.extend({
return function(item) { return function(item) {
// first remove the item from its parent's children list // first remove the item from its parent's children list
if (item._parent && this._removeFromParent()) { if (item._parent && this._removeFromParent()) {
Base.splice(item._parent.children, [this], Base.splice(item._parent._children, [this],
item._index + (above ? 1 : -1), 0); item._index + (above ? 1 : -1), 0);
this._parent = item._parent; this._parent = item._parent;
this._setDocument(item._document); this._setDocument(item._document);

View file

@ -18,7 +18,7 @@ var Layer = this.Layer = Group.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {
this.children = []; this._children = [];
this._document = paper.document; this._document = paper.document;
// Push it onto document.layers and set index: // Push it onto document.layers and set index:
this._index = this._document.layers.push(this) - 1; this._index = this._document.layers.push(this) - 1;

View file

@ -60,7 +60,7 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
get = 'get' + part; get = 'get' + part;
fields[set] = function(value) { fields[set] = function(value) {
var children = this._item && this._item.children; var children = this._item && this._item._children;
value = isColor ? Color.read(arguments) : value; value = isColor ? Color.read(arguments) : value;
if (children) { if (children) {
for (var i = 0, l = children.length; i < l; i++) for (var i = 0, l = children.length; i < l; i++)
@ -79,7 +79,7 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
}; };
fields[get] = function() { fields[get] = function() {
var children = this._item && this._item.children, var children = this._item && this._item._children,
style; style;
// If this item has children, walk through all of them and see if // If this item has children, walk through all of them and see if
// they all have the same style. // they all have the same style.

View file

@ -18,7 +18,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
// PORT: port the reversing of segments and keepDirection flag // PORT: port the reversing of segments and keepDirection flag
initialize: function(items, keepDirection) { initialize: function(items, keepDirection) {
this.base(); this.base();
this.children = []; this._children = [];
if (items) { if (items) {
for (var i = 0, l = items.length; i < l; i++) { for (var i = 0, l = items.length; i < l; i++) {
var item = items[i]; var item = items[i];
@ -40,8 +40,8 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
* @return the simplified compound path. * @return the simplified compound path.
*/ */
simplify: function() { simplify: function() {
if (this.children.length == 1) { if (this._children.length == 1) {
var child = this.children[0]; var child = this._children[0];
child.moveAbove(this); child.moveAbove(this);
this.remove(); this.remove();
return child; return child;
@ -50,16 +50,16 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
}, },
smooth: function() { smooth: function() {
for (var i = 0, l = this.children.length; i < l; i++) for (var i = 0, l = this._children.length; i < l; i++)
this.children[i].smooth(); this._children[i].smooth();
}, },
draw: function(ctx, param) { draw: function(ctx, param) {
var firstChild = this.children[0]; var firstChild = this._children[0];
ctx.beginPath(); ctx.beginPath();
param.compound = true; param.compound = true;
for (var i = 0, l = this.children.length; i < l; i++) for (var i = 0, l = this._children.length; i < l; i++)
Item.draw(this.children[i], ctx, param); Item.draw(this._children[i], ctx, param);
firstChild._setStyles(ctx); firstChild._setStyles(ctx);
var fillColor = firstChild.getFillColor(), var fillColor = firstChild.getFillColor(),
strokeColor = firstChild.getStrokeColor(); strokeColor = firstChild.getStrokeColor();
@ -74,8 +74,8 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
} }
}, new function() { // Injection scope for PostScript-like drawing functions }, new function() { // Injection scope for PostScript-like drawing functions
function getCurrentPath(that) { function getCurrentPath(that) {
if (that.children.length) { if (that._children.length) {
return that.children[that.children.length - 1]; return that._children[that._children.length - 1];
} else { } else {
throw new Error('Use a moveTo() command first'); throw new Error('Use a moveTo() command first');
} }