mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Simplify code for #insertAbove/Below(), by introducing a private helper.
Making overriding in Layer simpler.
This commit is contained in:
parent
5e210c583a
commit
16735e23af
2 changed files with 46 additions and 53 deletions
|
@ -1603,41 +1603,41 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
||||||
items = null;
|
items = null;
|
||||||
}
|
}
|
||||||
return items;
|
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# */{
|
// Private helper for #insertAbove() / #insertBelow()
|
||||||
/**
|
_insert: function(above, item, _preserve) {
|
||||||
* Inserts this item above the specified item.
|
if (!item._parent)
|
||||||
*
|
return null;
|
||||||
* @param {Item} item the item above which it should be inserted
|
var index = item._index + (above ? 1 : 0);
|
||||||
* @return {Item} the inserted item, or {@code null} if inserting was
|
// If the item is removed and inserted it again further above,
|
||||||
* not possible.
|
// the index needs to be adjusted accordingly.
|
||||||
*/
|
if (item._parent === this._parent && index > this._index)
|
||||||
insertAbove: insert(true),
|
index--;
|
||||||
|
return item._parent.insertChild(index, this, _preserve);
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts this item below the specified item.
|
* Inserts this item above the specified item.
|
||||||
*
|
*
|
||||||
* @param {Item} item the item above which it should be inserted
|
* @param {Item} item the item above which it should be inserted
|
||||||
* @return {Item} the inserted item, or {@code null} if inserting was
|
* @return {Item} the inserted item, or {@code null} if inserting was not
|
||||||
* not possible.
|
* possible.
|
||||||
*/
|
*/
|
||||||
insertBelow: insert(false)
|
insertAbove: function(item, _preserve) {
|
||||||
};
|
return this._insert(true, item, _preserve);
|
||||||
}, /** @lends Item# */{
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Sends this item to the back of all other items within the same parent.
|
||||||
|
|
|
@ -112,25 +112,18 @@ var Layer = Group.extend(/** @lends Layer# */{
|
||||||
*/
|
*/
|
||||||
activate: function() {
|
activate: function() {
|
||||||
this._project.activeLayer = this;
|
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 {
|
// Private helper for #insertAbove() / #insertBelow()
|
||||||
insertAbove: insert(true),
|
_insert: function _insert(above, item, _preserve) {
|
||||||
|
// If the item is a layer and contained within Project#layers, use
|
||||||
insertBelow: insert(false)
|
// 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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue