mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Make sure Item#children is always checked for != null and simplify many of the DOM methods.
This commit is contained in:
parent
e66dd46b9c
commit
be6d60102c
2 changed files with 56 additions and 60 deletions
|
@ -156,45 +156,36 @@ var Item = this.Item = Base.extend({
|
||||||
* Reverses the order of this item's children
|
* Reverses the order of this item's children
|
||||||
*/
|
*/
|
||||||
reverseChildren: function() {
|
reverseChildren: function() {
|
||||||
this.children.reverse();
|
if (this.children)
|
||||||
|
this.children.reverse();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The first item contained within this item.
|
* The first item contained within this item.
|
||||||
*/
|
*/
|
||||||
getFirstChild: function() {
|
getFirstChild: function() {
|
||||||
if (this.children.length > 0)
|
return this.children && this.children[0] || null;
|
||||||
return this.children[0];
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The last item contained within this item.
|
* The last item contained within this item.
|
||||||
*/
|
*/
|
||||||
getLastChild: function() {
|
getLastChild: function() {
|
||||||
if (this.children.length > 0)
|
return this.children && this.children[this.children.length - 1] || null;
|
||||||
return this.children[this.children.length - 1];
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The next item on the same level as this item.
|
* The next item on the same level as this item.
|
||||||
*/
|
*/
|
||||||
getNextSibling: function() {
|
getNextSibling: function() {
|
||||||
if (this.parent) {
|
return this.parent && this.parent.children[this.getIndex() + 1] || null;
|
||||||
var index = this.index + 1;
|
|
||||||
if (index < this.parent.children.length)
|
|
||||||
return this.parent.children[index];
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The previous item on the same level as this item.
|
* The previous item on the same level as this item.
|
||||||
*/
|
*/
|
||||||
getPreviousSibling: function() {
|
getPreviousSibling: function() {
|
||||||
if (this.parent) {
|
return this.parent && this.parent.children[this.getIndex() - 1] || null;
|
||||||
var index = this.index - 1;
|
|
||||||
if (index <= 0)
|
|
||||||
return this.parent.children[index];
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -204,23 +195,26 @@ var Item = this.Item = Base.extend({
|
||||||
// TODO: Relying on indexOf() here is slow, especially since it is
|
// TODO: Relying on indexOf() here is slow, especially since it is
|
||||||
// used for getPrevious/NextSibling().
|
// used for getPrevious/NextSibling().
|
||||||
// We need linked lists instead.
|
// We need linked lists instead.
|
||||||
return this.parent.children.indexOf(this);
|
return this.parent ? this.parent.children.indexOf(this) : -1;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
||||||
this.parent.children.splice(this.index, 1);
|
var ok = !!this.parent.children.splice(this.getIndex(), 1).length;
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the item.
|
* Removes the item.
|
||||||
*/
|
*/
|
||||||
remove: function() {
|
remove: function() {
|
||||||
this.removeFromParent();
|
return this.removeFromParent();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,7 +225,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;
|
return this.children && this.children.length > 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -283,10 +277,14 @@ var Item = this.Item = Base.extend({
|
||||||
* @param item The item that will be appended as a child
|
* @param item The item that will be appended as a child
|
||||||
*/
|
*/
|
||||||
appendTop: function(item) {
|
appendTop: function(item) {
|
||||||
item.removeFromParent();
|
if (this.children) {
|
||||||
this.children.push(item);
|
item.removeFromParent();
|
||||||
item.parent = this;
|
this.children.push(item);
|
||||||
item.document = this.document;
|
item.parent = this;
|
||||||
|
item.document = this.document;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -305,10 +303,14 @@ var Item = this.Item = Base.extend({
|
||||||
* @param item The item that will be appended as a child
|
* @param item The item that will be appended as a child
|
||||||
*/
|
*/
|
||||||
appendBottom: function(item) {
|
appendBottom: function(item) {
|
||||||
item.removeFromParent();
|
if (this.children) {
|
||||||
this.children.splice(0, 0, item);
|
item.removeFromParent();
|
||||||
item.parent = this;
|
this.children.splice(0, 0, item);
|
||||||
item.document = this.document;
|
item.parent = this;
|
||||||
|
item.document = this.document;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -337,11 +339,13 @@ var Item = this.Item = Base.extend({
|
||||||
*/
|
*/
|
||||||
moveAbove: function(item) {
|
moveAbove: function(item) {
|
||||||
// first remove the item from its parent's children list
|
// first remove the item from its parent's children list
|
||||||
this.removeFromParent();
|
if (item.parent && this.removeFromParent()) {
|
||||||
item.parent.children.splice(item.index + 1, 0, this);
|
item.parent.children.splice(item.getIndex() + 1, 0, this);
|
||||||
this.parent = item.parent;
|
this.parent = item.parent;
|
||||||
this.document = item.document;
|
this.document = item.document;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -361,11 +365,13 @@ var Item = this.Item = Base.extend({
|
||||||
*/
|
*/
|
||||||
moveBelow: function(item) {
|
moveBelow: function(item) {
|
||||||
// first remove the item from its parent's children list
|
// first remove the item from its parent's children list
|
||||||
this.removeFromParent();
|
if (item.parent && this.removeFromParent()) {
|
||||||
item.parent.children.splice(item.index - 1, 0, this);
|
item.parent.children.splice(item.getIndex() - 1, 0, this);
|
||||||
this.parent = item.parent;
|
this.parent = item.parent;
|
||||||
this.document = item.document;
|
this.document = item.document;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,7 +9,7 @@ var Layer = this.Layer = Group.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getIndex: function() {
|
getIndex: function() {
|
||||||
return !this.parent ? this.document.layers.indexOf(this) : this.base();
|
return this.parent ? this.base() : this.document.layers.indexOf(this);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,9 +18,9 @@ var Layer = this.Layer = Group.extend({
|
||||||
*/
|
*/
|
||||||
removeFromParent: function() {
|
removeFromParent: function() {
|
||||||
if (!this.parent) {
|
if (!this.parent) {
|
||||||
this.document.layers.splice(this.index, 1);
|
return !!this.document.layers.splice(this.getIndex(), 1).length;
|
||||||
} else {
|
} else {
|
||||||
this.base();
|
return this.base();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -28,11 +28,11 @@ 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.index + 1, 0, this);
|
item.document.layers.splice(item.getIndex() + 1, 0, this);
|
||||||
this.document = item.document;
|
this.document = item.document;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
this.base(item);
|
return this.base(item);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -40,32 +40,22 @@ 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.index - 1, 0, this);
|
item.document.layers.splice(item.getIndex() - 1, 0, this);
|
||||||
this.document = item.document;
|
this.document = item.document;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
this.base(item);
|
return this.base(item);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getNextSibling: function() {
|
getNextSibling: function() {
|
||||||
if (!this.parent) {
|
return this.parent ? this.base()
|
||||||
var index = this.index + 1;
|
: this.document.layers[this.getIndex() + 1] || null;
|
||||||
if (index < this.document.layers.length)
|
|
||||||
return this.document.layers[index];
|
|
||||||
} else {
|
|
||||||
return this.base();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getPreviousSibling: function() {
|
getPreviousSibling: function() {
|
||||||
if (!this.parent) {
|
return this.parent ? this.base()
|
||||||
var index = this.index - 1;
|
: this.document.layers[this.getIndex() - 1] || null;
|
||||||
if (index <= 0)
|
|
||||||
return this.document.layers[index];
|
|
||||||
} else {
|
|
||||||
return this.base();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
activate: function() {
|
activate: function() {
|
||||||
|
|
Loading…
Reference in a new issue