paper.js/src/item/Layer.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
2011-03-07 20:41:50 -05:00
* http://paperjs.org/
2011-03-06 19:50:44 -05:00
* http://scriptographer.org/
*
2011-03-07 20:41:50 -05:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-06 19:50:44 -05:00
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
var Layer = this.Layer = Group.extend({
beans: true,
2011-02-11 12:37:36 -05:00
initialize: function() {
this.children = [];
this._document = paper.document;
// Push it onto document.layers and set index:
this._index = this._document.layers.push(this) - 1;
this.activate();
},
/**
* Removes the layer from its document's layers list
* or its parent's children list.
*/
_removeFromParent: function() {
2011-05-07 12:27:19 -04:00
return this.parent ? this.base()
: !!Base.splice(this._document.layers, null, this._index, 1).length;
},
getNextSibling: function() {
return this.parent ? this.base()
2011-05-07 12:27:19 -04:00
: this._document.layers[this._index + 1] || null;
},
getPreviousSibling: function() {
return this.parent ? this.base()
2011-05-07 12:27:19 -04:00
: this._document.layers[this._index - 1] || null;
},
activate: function() {
this._document.activeLayer = this;
2011-02-11 12:37:36 -05:00
}
}, new function () {
function move(above) {
return function(item) {
// if the item is a layer and contained within Document#layers
2011-05-07 09:34:57 -04:00
if (item instanceof Layer && !item.parent
2011-05-07 12:27:19 -04:00
&& this._removeFromParent()) {
Base.splice(item._document.layers, [this],
item._index + (above ? 1 : -1), 0);
this._setDocument(item._document);
return true;
}
2011-05-07 12:27:19 -04:00
return this.base(item);
2011-05-02 06:23:42 -04:00
};
}
return {
moveAbove: move(true),
moveBelow: move(false)
};
});