diff --git a/src/item/Group.js b/src/item/Group.js index 51fb029a..60bb6389 100644 --- a/src/item/Group.js +++ b/src/item/Group.js @@ -20,6 +20,7 @@ var Group = this.Group = Item.extend({ initialize: function(items) { this.base(); this._children = []; + this._namedChildren = {}; if (items) { for (var i = 0, l = items.length; i < l; i++) { this.appendTop(items[i]); diff --git a/src/item/Item.js b/src/item/Item.js index 3f945555..33f28764 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -43,6 +43,54 @@ var Item = this.Item = Base.extend({ } return this._id; }, + + _removeFromNamed: function() { + var children = this._parent._children; + var namedChildren = this._parent._namedChildren; + var name = this._name; + if (children[name] = this) + delete children[name]; + var namedArray = namedChildren[name]; + namedArray.splice(namedArray.indexOf(this), 1); + // If there are any items left in the named array, set + // the last of them to be this.parent.children[this.name] + if (namedArray.length) { + children[name] = namedArray[namedArray.length - 1]; + } else { + // Otherwise delete the empty array + delete namedChildren[name]; + } + }, + + /** + * The name of the item. + */ + getName: function() { + if (this._name) + return this._name; + }, + + setName: function(name) { + var children = this._parent.children; + var namedChildren = this._parent._namedChildren; + if (name != this.name) { + // If the item already had a name, + // remove its property from the parent's children object: + if (this._name) + this._removeFromNamed(); + this._name = name ? name : undefined; + } + if (name) { + if (!namedChildren[name]) { + namedChildren[name] = [this]; + } else { + namedChildren[name].push(this); + } + children[name] = this; + } else { + children[name] = undefined; + } + }, /** * When passed a document, copies the item to the document, @@ -243,6 +291,8 @@ var Item = this.Item = Base.extend({ */ _removeFromParent: function() { if (this._parent) { + if (this._name) + this._removeFromNamed(); var res = Base.splice(this._parent._children, null, this._index, 1); this._parent = this._index = null; return !!res.length; @@ -699,6 +749,8 @@ var Item = this.Item = Base.extend({ Base.splice(this._children, [item], top ? undefined : 0, 0); item._parent = this; item._setDocument(this._document); + if (item._name) + item.setName(item._name); return true; } return false; @@ -713,6 +765,8 @@ var Item = this.Item = Base.extend({ item._index + (above ? 1 : -1), 0); this._parent = item._parent; this._setDocument(item._document); + if (item._name) + item.setName(item._name); return true; } return false; diff --git a/src/item/Layer.js b/src/item/Layer.js index af81c172..d44c0207 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -19,6 +19,7 @@ var Layer = this.Layer = Group.extend({ initialize: function() { this._children = []; + this._namedChildren = {}; this._document = paper.document; // Push it onto document.layers and set index: this._index = this._document.layers.push(this) - 1;