Simplify code for #insertAbove/Below(), by introducing a private helper.

Making overriding in Layer simpler.
This commit is contained in:
Jürg Lehni 2013-07-21 16:41:45 -07:00
parent 5e210c583a
commit 16735e23af
2 changed files with 46 additions and 53 deletions

View file

@ -1603,41 +1603,41 @@ var Item = Base.extend(Callback, /** @lends Item# */{
items = null;
}
return items;
}
}, new function () { // Scope for insertAbove / insertBelow
function insert(above) {
return function(item, _preserve) {
if (!item._parent)
return null;
var index = item._index + (above ? 1 : 0);
// If the item is removed and inserted it again further above,
// the index needs to be adjusted accordingly.
if (item._parent === this._parent && index > this._index)
index--;
return item._parent.insertChild(index, this, _preserve);
};
}
},
return /** @lends Item# */{
/**
* Inserts this item above the specified item.
*
* @param {Item} item the item above which it should be inserted
* @return {Item} the inserted item, or {@code null} if inserting was
* not possible.
*/
insertAbove: insert(true),
// Private helper for #insertAbove() / #insertBelow()
_insert: function(above, item, _preserve) {
if (!item._parent)
return null;
var index = item._index + (above ? 1 : 0);
// If the item is removed and inserted it again further above,
// the index needs to be adjusted accordingly.
if (item._parent === this._parent && index > this._index)
index--;
return item._parent.insertChild(index, this, _preserve);
},
/**
* Inserts this item below the specified item.
*
* @param {Item} item the item above which it should be inserted
* @return {Item} the inserted item, or {@code null} if inserting was
* not possible.
*/
insertBelow: insert(false)
};
}, /** @lends Item# */{
/**
* Inserts this item above the specified item.
*
* @param {Item} item the item above which it should be inserted
* @return {Item} the inserted item, or {@code null} if inserting was not
* possible.
*/
insertAbove: function(item, _preserve) {
return this._insert(true, item, _preserve);
},
/**
* Inserts this item below the specified item.
*
* @param {Item} item the item above which it should be inserted
* @return {Item} the inserted item, or {@code null} if inserting was not
* possible.
*/
insertBelow: function(item, _preserve) {
return this._insert(false, item, _preserve);
},
/**
* Sends this item to the back of all other items within the same parent.

View file

@ -112,25 +112,18 @@ var Layer = Group.extend(/** @lends Layer# */{
*/
activate: function() {
this._project.activeLayer = this;
}
}, new function () {
function insert(above) {
return function insert(item) {
// If the item is a layer and contained within Project#layers, use
// our own version of move().
if (item instanceof Layer && !item._parent && this._remove(true)) {
Base.splice(item._project.layers, [this],
item._index + (above ? 1 : 0), 0);
this._setProject(item._project);
return this;
}
return insert.base.call(this, item);
};
}
},
return {
insertAbove: insert(true),
insertBelow: insert(false)
};
// Private helper for #insertAbove() / #insertBelow()
_insert: function _insert(above, item, _preserve) {
// If the item is a layer and contained within Project#layers, use
// our own version of move().
if (item instanceof Layer && !item._parent && this._remove(true)) {
Base.splice(item._project.layers, [this],
item._index + (above ? 1 : 0), 0);
this._setProject(item._project);
return this;
}
return _insert.base.call(this, above, item, _preserve);
}
});