diff --git a/src/item/Group.js b/src/item/Group.js index 6037f99f..891ebe5b 100644 --- a/src/item/Group.js +++ b/src/item/Group.js @@ -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); }, diff --git a/src/item/Item.js b/src/item/Item.js index 079bb94b..e6364fe1 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -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() { diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index 85e1ae9a..e2304782 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -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)); }, diff --git a/src/item/Raster.js b/src/item/Raster.js index 94b8b007..c5dc5da6 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -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); diff --git a/src/item/Shape.js b/src/item/Shape.js index e2ab5331..3fb3653c 100644 --- a/src/item/Shape.js +++ b/src/item/Shape.js @@ -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 */{ diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index a9c03a95..713256cf 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -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); }, diff --git a/src/path/Path.Constructors.js b/src/path/Path.Constructors.js index b34330c2..c755240e 100644 --- a/src/path/Path.Constructors.js +++ b/src/path/Path.Constructors.js @@ -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 */) { diff --git a/src/path/Path.js b/src/path/Path.js index 7813cfca..48495d34 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -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. diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 6aefb168..ef4ddc92 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -23,7 +23,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{ _class: 'PathItem', initialize: function PathItem() { - Item.apply(this, arguments); + // Do nothing. }, /** diff --git a/src/project/Project.js b/src/project/Project.js index e57b9784..4e027167 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -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 = []; diff --git a/src/text/TextItem.js b/src/text/TextItem.js index 828d2603..ddab6695 100644 --- a/src/text/TextItem.js +++ b/src/text/TextItem.js @@ -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)); }, /**