diff --git a/src/core/Base.js b/src/core/Base.js index 4987b164..863ed6da 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -50,6 +50,30 @@ this.Base = Base.inject(/** @lends Base# */{ return Base.toJson(this, options); }, + /** + * Sets all the properties of the passed object literal to their values on + * the item it is called on, and returns the item itself. + */ + set: function(props) { + if (props) { + for (var key in props) + if (props.hasOwnProperty(key)) + this[key] = props[key]; + } + return this; + }, + + /** + * #_set() is part of the mechanism for constructors which take one object + * literal describing all the properties to be set on the created instance. + * It behaves the same as #set(), but only if the provided object is a plain + * object. It returns undefined otherwise. + */ + _set: function(props) { + if (Base.isPlainObject(props)) + return this.set(props); + }, + statics: /** @lends Base */{ _types: {}, diff --git a/src/item/Group.js b/src/item/Group.js index 6a031b2d..5d69022e 100644 --- a/src/item/Group.js +++ b/src/item/Group.js @@ -69,7 +69,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{ // Allow Group to have children and named children this._children = []; this._namedChildren = {}; - if (!this._setProperties(arg)) + if (!this._set(arg)) this.addChildren(Array.isArray(arg) ? arg : arguments); }, diff --git a/src/item/Item.js b/src/item/Item.js index 98fdc956..c2da084b 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -133,27 +133,6 @@ var Item = this.Item = Base.extend(Callback, { ); }, - // #_setProperties is part of the mechanism for Item constructors which take - // one object literal describing all the properties to be set on the created - // instance. - _setProperties: function(props) { - if (Base.isPlainObject(props)) - return this.set(props); - }, - - /** - * Sets all the properties of the passed object literal to their values on - * the item it is called on, and returns the item itself. - */ - set: function(props) { - if (props) { - for (var key in props) - if (props.hasOwnProperty(key)) - this[key] = props[key]; - } - return this; - }, - _serialize: function(options, dictionary) { var props = {}, that = this; diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index 7c60d915..0e825de5 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -66,7 +66,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol this.base(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 (!this._setProperties(arg0)) + if (!this._set(arg0)) this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0)); }, diff --git a/src/item/Raster.js b/src/item/Raster.js index def6e141..fc36d281 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -45,7 +45,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ this.base(point !== 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 (!this._setProperties(object)) { + if (!this._set(object)) { if (object.getContext) { this.setCanvas(object); } else if (typeof object === 'string') { diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index f2c05df9..e4955a75 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -42,7 +42,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath# // CompoundPath has children and supports named children. this._children = []; this._namedChildren = {}; - if (!this._setProperties(arg)) + if (!this._set(arg)) this.addChildren(Array.isArray(arg) ? arg : arguments); }, diff --git a/src/path/Path.js b/src/path/Path.js index 7c6d932b..fa705dad 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -54,7 +54,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ // If it is an array, it can also be a description of a point, so // check its first entry for object as well. // But first see if segments are directly passed at all. If not, try - // _setProperties(arg). + // _set(arg). var segments = Array.isArray(arg) ? typeof arg[0] === 'object' ? arg @@ -64,7 +64,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ : null; this.setSegments(segments || []); if (!segments) - this._setProperties(arg); + this._set(arg); }, clone: function() { diff --git a/src/text/TextItem.js b/src/text/TextItem.js index 88264836..525e9078 100644 --- a/src/text/TextItem.js +++ b/src/text/TextItem.js @@ -49,7 +49,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{ this._content = ''; this._lines = []; if (hasProperties) { - this._setProperties(arg); + this._set(arg); } }, diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 1d1fbd6b..f263ce7a 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -50,11 +50,12 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ 'onKeyDown', 'onKeyUp' ], // DOCS: rewrite Tool constructor explanation - initialize: function() { + initialize: function(props) { this.base(); this._firstMove = true; this._count = 0; this._downCount = 0; + this._set(props); }, /**