mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Move and unify handling of Item#_set(props) to new Item#_initialize(), and add support for props.insert = false.
This commit is contained in:
parent
5e2654b490
commit
aa4d990c90
11 changed files with 44 additions and 46 deletions
|
@ -89,11 +89,10 @@ var Group = Item.extend(/** @lends Group# */{
|
|||
* });
|
||||
*/
|
||||
initialize: function Group(arg) {
|
||||
Item.call(this);
|
||||
// Allow Group to have children and named children
|
||||
this._children = [];
|
||||
this._namedChildren = {};
|
||||
if (arg && !this._set(arg))
|
||||
if (!this._initialize(arg))
|
||||
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
||||
},
|
||||
|
||||
|
|
|
@ -61,7 +61,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
data: {}
|
||||
},
|
||||
|
||||
initialize: function Item(point) {
|
||||
initialize: function Item() {
|
||||
// Do nothing.
|
||||
},
|
||||
|
||||
_initialize: function(props, point) {
|
||||
// Define this Item's unique id.
|
||||
this._id = Item._id = (Item._id || 0) + 1;
|
||||
// If _project is already set, the item was already moved into the DOM
|
||||
|
@ -69,15 +73,18 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
if (!this._project) {
|
||||
var project = paper.project,
|
||||
layer = project.activeLayer;
|
||||
if (layer)
|
||||
// Do not insert into DOM if insert: false is provided in props.
|
||||
if (layer && !(props && props.insert === false)) {
|
||||
layer.addChild(this);
|
||||
else
|
||||
} else {
|
||||
this._setProject(project);
|
||||
}
|
||||
}
|
||||
this._style = new Style(this._project._currentStyle, this);
|
||||
this._matrix = new Matrix();
|
||||
if (point)
|
||||
this._matrix.translate(point);
|
||||
return props ? this._set(props, { insert: true }) : true;
|
||||
},
|
||||
|
||||
_events: new function() {
|
||||
|
|
|
@ -72,10 +72,10 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
|
|||
// Support two forms of item initialization: Passing one object literal
|
||||
// describing all the different properties to be set, or a symbol (arg0)
|
||||
// and a point where it should be placed (arg1).
|
||||
Item.call(this, arg1 !== undefined && Point.read(arguments, 1));
|
||||
// If we can handle setting properties through object literal, we're all
|
||||
// set. Otherwise we need to set symbol.
|
||||
if (arg0 && !this._set(arg0))
|
||||
// If _initialize can set properties through object literal, we're done.
|
||||
// Otherwise we need to set symbol from arg0.
|
||||
if (!this._initialize(arg0,
|
||||
arg1 !== undefined && Point.read(arguments, 1)))
|
||||
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
|
||||
},
|
||||
|
||||
|
|
|
@ -76,10 +76,10 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
// Support two forms of item initialization: Passing one object literal
|
||||
// describing all the different properties to be set, or an image
|
||||
// (object) and a point where it should be placed (point).
|
||||
Item.call(this, position !== undefined && Point.read(arguments, 1));
|
||||
// If we can handle setting properties through object literal, we're all
|
||||
// set. Otherwise we need to check the type of object:
|
||||
if (object && !this._set(object)) {
|
||||
// If _initialize can set properties through object literal, we're done.
|
||||
// Otherwise we need to check the type of object:
|
||||
if (!this._initialize(object,
|
||||
position !== undefined && Point.read(arguments, 1))) {
|
||||
if (object.getContext) {
|
||||
this.setCanvas(object);
|
||||
} else if (typeof object === 'string') {
|
||||
|
@ -98,7 +98,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
image = this._image;
|
||||
if (image) {
|
||||
param.image = image;
|
||||
} else {
|
||||
} else if (this._canvas) {
|
||||
// If the Raster contains a Canvas object, we need to create
|
||||
// a new one and draw this raster's canvas on it.
|
||||
var canvas = param.canvas = CanvasProvider.getCanvas(this._size);
|
||||
|
|
|
@ -21,8 +21,8 @@ var Shape = Item.extend(/** @lends Shape# */{
|
|||
_class: 'Shape',
|
||||
_transformContent: false,
|
||||
|
||||
initialize: function Shape(type, point, size) {
|
||||
Item.call(this, point);
|
||||
initialize: function Shape(type, point, size, props) {
|
||||
this._initialize(props, point);
|
||||
this._type = type;
|
||||
this._size = size;
|
||||
},
|
||||
|
@ -165,11 +165,7 @@ var Shape = Item.extend(/** @lends Shape# */{
|
|||
|
||||
statics: new function() {
|
||||
function createShape(type, point, size, args) {
|
||||
var shape = new Shape(type, point, size),
|
||||
named = Base.getNamed(args);
|
||||
if (named)
|
||||
shape._set(named);
|
||||
return shape;
|
||||
return new Shape(type, point, size, Base.getNamed(args));
|
||||
}
|
||||
|
||||
return /** @lends Shape */{
|
||||
|
|
|
@ -50,11 +50,10 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
|||
* compoundPath.children[1].position.x += 5;
|
||||
*/
|
||||
initialize: function CompoundPath(arg) {
|
||||
PathItem.call(this);
|
||||
// CompoundPath has children and supports named children.
|
||||
this._children = [];
|
||||
this._namedChildren = {};
|
||||
if (arg && !this._set(arg))
|
||||
if (!this._initialize(arg))
|
||||
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
||||
},
|
||||
|
||||
|
|
|
@ -13,11 +13,7 @@
|
|||
Path.inject({ statics: new function() {
|
||||
|
||||
function createPath(args) {
|
||||
var path = new Path(),
|
||||
named = Base.getNamed(args);
|
||||
if (named)
|
||||
path._set(named);
|
||||
return path;
|
||||
return new Path(Base.getNamed(args));
|
||||
}
|
||||
|
||||
function createRectangle(/* rectangle */) {
|
||||
|
|
|
@ -70,7 +70,6 @@ var Path = PathItem.extend(/** @lends Path# */{
|
|||
initialize: function Path(arg) {
|
||||
this._closed = false;
|
||||
this._segments = [];
|
||||
Item.call(this);
|
||||
// arg can either be an object literal describing properties to be set
|
||||
// on the path, a list of segments to be set, or the first of multiple
|
||||
// arguments describing separate segments.
|
||||
|
@ -82,12 +81,16 @@ var Path = PathItem.extend(/** @lends Path# */{
|
|||
? typeof arg[0] === 'object'
|
||||
? arg
|
||||
: arguments
|
||||
: arg && (arg.point !== undefined || arg.x !== undefined)
|
||||
// See if it behaves like a segment or a point, but filter out
|
||||
// rectangles, as accepted by some Path.Constructor constructors.
|
||||
: arg && (arg.point !== undefined && arg.size === undefined
|
||||
|| arg.x !== undefined)
|
||||
? arguments
|
||||
: null;
|
||||
// Always call setSegments() to initialize a few related variables.
|
||||
this.setSegments(segments || []);
|
||||
if (arg && !segments)
|
||||
this._set(arg);
|
||||
// Only pass on arg as props if it wasn't consumed for segments already.
|
||||
this._initialize(!segments && arg);
|
||||
},
|
||||
|
||||
clone: function(insert) {
|
||||
|
@ -102,9 +105,8 @@ var Path = PathItem.extend(/** @lends Path# */{
|
|||
return copy;
|
||||
},
|
||||
|
||||
_changed: function(flags) {
|
||||
// Don't use base() for reasons of performance.
|
||||
Item.prototype._changed.call(this, flags);
|
||||
_changed: function _changed(flags) {
|
||||
_changed.base.call(this, flags);
|
||||
if (flags & /*#=*/ ChangeFlag.GEOMETRY) {
|
||||
delete this._length;
|
||||
// Clockwise state becomes undefined as soon as geometry changes.
|
||||
|
|
|
@ -23,7 +23,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
|
|||
_class: 'PathItem',
|
||||
|
||||
initialize: function PathItem() {
|
||||
Item.apply(this, arguments);
|
||||
// Do nothing.
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -46,8 +46,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
* Canvas element that should be wrapped in a newly created view.
|
||||
*/
|
||||
initialize: function Project(view) {
|
||||
// Activate straight away by passing true to base(), so paper.project is
|
||||
// set, as required by Layer and DoumentView constructors.
|
||||
// Activate straight away by passing true to PaperScopeItem constructor,
|
||||
// so paper.project is set, as required by Layer and DoumentView
|
||||
// constructors.
|
||||
PaperScopeItem.call(this, true);
|
||||
this.layers = [];
|
||||
this.symbols = [];
|
||||
|
|
|
@ -32,18 +32,16 @@ var TextItem = Item.extend(/** @lends TextItem# */{
|
|||
_boundsGetter: 'getBounds',
|
||||
|
||||
initialize: function TextItem(arg) {
|
||||
this._content = '';
|
||||
this._lines = [];
|
||||
// Support two forms of item initialization: Passing one object literal
|
||||
// describing all the different properties to be set, or a point where
|
||||
// it should be placed (arg).
|
||||
// See if a point is passed, and if so, pass it on to base(). If not, it
|
||||
// might be a properties object literal for #setPropeties() at the end.
|
||||
var hasProperties = arg && Base.isPlainObject(arg)
|
||||
// See if a point is passed, and if so, pass it on to _initialize().
|
||||
// If not, it might be a properties object literal.
|
||||
var hasProps = arg && Base.isPlainObject(arg)
|
||||
&& arg.x === undefined && arg.y === undefined;
|
||||
Item.call(this, hasProperties ? null : Point.read(arguments));
|
||||
this._content = '';
|
||||
this._lines = [];
|
||||
if (hasProperties)
|
||||
this._set(arg);
|
||||
this._initialize(hasProps && arg, !hasProps && Point.read(arguments));
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue