Move and unify handling of Item#_set(props) to new Item#_initialize(), and add support for props.insert = false.

This commit is contained in:
Jürg Lehni 2013-07-21 15:45:22 -07:00
parent 5e2654b490
commit aa4d990c90
11 changed files with 44 additions and 46 deletions

View file

@ -89,11 +89,10 @@ var Group = Item.extend(/** @lends Group# */{
* }); * });
*/ */
initialize: function Group(arg) { initialize: function Group(arg) {
Item.call(this);
// Allow Group to have children and named children // Allow Group to have children and named children
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};
if (arg && !this._set(arg)) if (!this._initialize(arg))
this.addChildren(Array.isArray(arg) ? arg : arguments); this.addChildren(Array.isArray(arg) ? arg : arguments);
}, },

View file

@ -61,7 +61,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
data: {} data: {}
}, },
initialize: function Item(point) { initialize: function Item() {
// Do nothing.
},
_initialize: function(props, point) {
// Define this Item's unique id. // Define this Item's unique id.
this._id = Item._id = (Item._id || 0) + 1; this._id = Item._id = (Item._id || 0) + 1;
// If _project is already set, the item was already moved into the DOM // 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) { if (!this._project) {
var project = paper.project, var project = paper.project,
layer = project.activeLayer; 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); layer.addChild(this);
else } else {
this._setProject(project); this._setProject(project);
}
} }
this._style = new Style(this._project._currentStyle, this); this._style = new Style(this._project._currentStyle, this);
this._matrix = new Matrix(); this._matrix = new Matrix();
if (point) if (point)
this._matrix.translate(point); this._matrix.translate(point);
return props ? this._set(props, { insert: true }) : true;
}, },
_events: new function() { _events: new function() {

View file

@ -72,10 +72,10 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
// Support two forms of item initialization: Passing one object literal // Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or a symbol (arg0) // describing all the different properties to be set, or a symbol (arg0)
// and a point where it should be placed (arg1). // and a point where it should be placed (arg1).
Item.call(this, arg1 !== undefined && Point.read(arguments, 1)); // If _initialize can set properties through object literal, we're done.
// If we can handle setting properties through object literal, we're all // Otherwise we need to set symbol from arg0.
// set. Otherwise we need to set symbol. if (!this._initialize(arg0,
if (arg0 && !this._set(arg0)) arg1 !== undefined && Point.read(arguments, 1)))
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0)); this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
}, },

View file

@ -76,10 +76,10 @@ var Raster = Item.extend(/** @lends Raster# */{
// Support two forms of item initialization: Passing one object literal // Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or an image // describing all the different properties to be set, or an image
// (object) and a point where it should be placed (point). // (object) and a point where it should be placed (point).
Item.call(this, position !== undefined && Point.read(arguments, 1)); // If _initialize can set properties through object literal, we're done.
// If we can handle setting properties through object literal, we're all // Otherwise we need to check the type of object:
// set. Otherwise we need to check the type of object: if (!this._initialize(object,
if (object && !this._set(object)) { position !== undefined && Point.read(arguments, 1))) {
if (object.getContext) { if (object.getContext) {
this.setCanvas(object); this.setCanvas(object);
} else if (typeof object === 'string') { } else if (typeof object === 'string') {
@ -98,7 +98,7 @@ var Raster = Item.extend(/** @lends Raster# */{
image = this._image; image = this._image;
if (image) { if (image) {
param.image = image; param.image = image;
} else { } else if (this._canvas) {
// If the Raster contains a Canvas object, we need to create // If the Raster contains a Canvas object, we need to create
// a new one and draw this raster's canvas on it. // a new one and draw this raster's canvas on it.
var canvas = param.canvas = CanvasProvider.getCanvas(this._size); var canvas = param.canvas = CanvasProvider.getCanvas(this._size);

View file

@ -21,8 +21,8 @@ var Shape = Item.extend(/** @lends Shape# */{
_class: 'Shape', _class: 'Shape',
_transformContent: false, _transformContent: false,
initialize: function Shape(type, point, size) { initialize: function Shape(type, point, size, props) {
Item.call(this, point); this._initialize(props, point);
this._type = type; this._type = type;
this._size = size; this._size = size;
}, },
@ -165,11 +165,7 @@ var Shape = Item.extend(/** @lends Shape# */{
statics: new function() { statics: new function() {
function createShape(type, point, size, args) { function createShape(type, point, size, args) {
var shape = new Shape(type, point, size), return new Shape(type, point, size, Base.getNamed(args));
named = Base.getNamed(args);
if (named)
shape._set(named);
return shape;
} }
return /** @lends Shape */{ return /** @lends Shape */{

View file

@ -50,11 +50,10 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
* compoundPath.children[1].position.x += 5; * compoundPath.children[1].position.x += 5;
*/ */
initialize: function CompoundPath(arg) { initialize: function CompoundPath(arg) {
PathItem.call(this);
// CompoundPath has children and supports named children. // CompoundPath has children and supports named children.
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};
if (arg && !this._set(arg)) if (!this._initialize(arg))
this.addChildren(Array.isArray(arg) ? arg : arguments); this.addChildren(Array.isArray(arg) ? arg : arguments);
}, },

View file

@ -13,11 +13,7 @@
Path.inject({ statics: new function() { Path.inject({ statics: new function() {
function createPath(args) { function createPath(args) {
var path = new Path(), return new Path(Base.getNamed(args));
named = Base.getNamed(args);
if (named)
path._set(named);
return path;
} }
function createRectangle(/* rectangle */) { function createRectangle(/* rectangle */) {

View file

@ -70,7 +70,6 @@ var Path = PathItem.extend(/** @lends Path# */{
initialize: function Path(arg) { initialize: function Path(arg) {
this._closed = false; this._closed = false;
this._segments = []; this._segments = [];
Item.call(this);
// arg can either be an object literal describing properties to be set // 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 // on the path, a list of segments to be set, or the first of multiple
// arguments describing separate segments. // arguments describing separate segments.
@ -82,12 +81,16 @@ var Path = PathItem.extend(/** @lends Path# */{
? typeof arg[0] === 'object' ? typeof arg[0] === 'object'
? arg ? arg
: arguments : 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 ? arguments
: null; : null;
// Always call setSegments() to initialize a few related variables.
this.setSegments(segments || []); this.setSegments(segments || []);
if (arg && !segments) // Only pass on arg as props if it wasn't consumed for segments already.
this._set(arg); this._initialize(!segments && arg);
}, },
clone: function(insert) { clone: function(insert) {
@ -102,9 +105,8 @@ var Path = PathItem.extend(/** @lends Path# */{
return copy; return copy;
}, },
_changed: function(flags) { _changed: function _changed(flags) {
// Don't use base() for reasons of performance. _changed.base.call(this, flags);
Item.prototype._changed.call(this, flags);
if (flags & /*#=*/ ChangeFlag.GEOMETRY) { if (flags & /*#=*/ ChangeFlag.GEOMETRY) {
delete this._length; delete this._length;
// Clockwise state becomes undefined as soon as geometry changes. // Clockwise state becomes undefined as soon as geometry changes.

View file

@ -23,7 +23,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
_class: 'PathItem', _class: 'PathItem',
initialize: function PathItem() { initialize: function PathItem() {
Item.apply(this, arguments); // Do nothing.
}, },
/** /**

View file

@ -46,8 +46,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
* Canvas element that should be wrapped in a newly created view. * Canvas element that should be wrapped in a newly created view.
*/ */
initialize: function Project(view) { initialize: function Project(view) {
// Activate straight away by passing true to base(), so paper.project is // Activate straight away by passing true to PaperScopeItem constructor,
// set, as required by Layer and DoumentView constructors. // so paper.project is set, as required by Layer and DoumentView
// constructors.
PaperScopeItem.call(this, true); PaperScopeItem.call(this, true);
this.layers = []; this.layers = [];
this.symbols = []; this.symbols = [];

View file

@ -32,18 +32,16 @@ var TextItem = Item.extend(/** @lends TextItem# */{
_boundsGetter: 'getBounds', _boundsGetter: 'getBounds',
initialize: function TextItem(arg) { initialize: function TextItem(arg) {
this._content = '';
this._lines = [];
// Support two forms of item initialization: Passing one object literal // Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or a point where // describing all the different properties to be set, or a point where
// it should be placed (arg). // it should be placed (arg).
// See if a point is passed, and if so, pass it on to base(). If not, it // See if a point is passed, and if so, pass it on to _initialize().
// might be a properties object literal for #setPropeties() at the end. // If not, it might be a properties object literal.
var hasProperties = arg && Base.isPlainObject(arg) var hasProps = arg && Base.isPlainObject(arg)
&& arg.x === undefined && arg.y === undefined; && arg.x === undefined && arg.y === undefined;
Item.call(this, hasProperties ? null : Point.read(arguments)); this._initialize(hasProps && arg, !hasProps && Point.read(arguments));
this._content = '';
this._lines = [];
if (hasProperties)
this._set(arg);
}, },
/** /**