Skip to content

Commit 119e5ac

Browse files
committed
Move #_setProperties() and #set() from Item to Base, rename it to #_set and use it for Tool too.
1 parent 9d708f2 commit 119e5ac

9 files changed

Lines changed: 33 additions & 29 deletions

File tree

src/core/Base.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ this.Base = Base.inject(/** @lends Base# */{
5050
return Base.toJson(this, options);
5151
},
5252

53+
/**
54+
* Sets all the properties of the passed object literal to their values on
55+
* the item it is called on, and returns the item itself.
56+
*/
57+
set: function(props) {
58+
if (props) {
59+
for (var key in props)
60+
if (props.hasOwnProperty(key))
61+
this[key] = props[key];
62+
}
63+
return this;
64+
},
65+
66+
/**
67+
* #_set() is part of the mechanism for constructors which take one object
68+
* literal describing all the properties to be set on the created instance.
69+
* It behaves the same as #set(), but only if the provided object is a plain
70+
* object. It returns undefined otherwise.
71+
*/
72+
_set: function(props) {
73+
if (Base.isPlainObject(props))
74+
return this.set(props);
75+
},
76+
5377
statics: /** @lends Base */{
5478

5579
_types: {},

src/item/Group.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
6969
// Allow Group to have children and named children
7070
this._children = [];
7171
this._namedChildren = {};
72-
if (!this._setProperties(arg))
72+
if (!this._set(arg))
7373
this.addChildren(Array.isArray(arg) ? arg : arguments);
7474
},
7575

src/item/Item.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,6 @@ var Item = this.Item = Base.extend(Callback, {
133133
);
134134
},
135135

136-
// #_setProperties is part of the mechanism for Item constructors which take
137-
// one object literal describing all the properties to be set on the created
138-
// instance.
139-
_setProperties: function(props) {
140-
if (Base.isPlainObject(props))
141-
return this.set(props);
142-
},
143-
144-
/**
145-
* Sets all the properties of the passed object literal to their values on
146-
* the item it is called on, and returns the item itself.
147-
*/
148-
set: function(props) {
149-
if (props) {
150-
for (var key in props)
151-
if (props.hasOwnProperty(key))
152-
this[key] = props[key];
153-
}
154-
return this;
155-
},
156-
157136
_serialize: function(options, dictionary) {
158137
var props = {},
159138
that = this;

src/item/PlacedSymbol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
6666
this.base(arg1 !== undefined && Point.read(arguments, 1));
6767
// If we can handle setting properties through object literal, we're all
6868
// set. Otherwise we need to set symbol.
69-
if (!this._setProperties(arg0))
69+
if (!this._set(arg0))
7070
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
7171
},
7272

src/item/Raster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
4545
this.base(point !== undefined && Point.read(arguments, 1));
4646
// If we can handle setting properties through object literal, we're all
4747
// set. Otherwise we need to check the type of object:
48-
if (!this._setProperties(object)) {
48+
if (!this._set(object)) {
4949
if (object.getContext) {
5050
this.setCanvas(object);
5151
} else if (typeof object === 'string') {

src/path/CompoundPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
4242
// CompoundPath has children and supports named children.
4343
this._children = [];
4444
this._namedChildren = {};
45-
if (!this._setProperties(arg))
45+
if (!this._set(arg))
4646
this.addChildren(Array.isArray(arg) ? arg : arguments);
4747
},
4848

src/path/Path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
5454
// If it is an array, it can also be a description of a point, so
5555
// check its first entry for object as well.
5656
// But first see if segments are directly passed at all. If not, try
57-
// _setProperties(arg).
57+
// _set(arg).
5858
var segments = Array.isArray(arg)
5959
? typeof arg[0] === 'object'
6060
? arg
@@ -64,7 +64,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
6464
: null;
6565
this.setSegments(segments || []);
6666
if (!segments)
67-
this._setProperties(arg);
67+
this._set(arg);
6868
},
6969

7070
clone: function() {

src/text/TextItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
4949
this._content = '';
5050
this._lines = [];
5151
if (hasProperties) {
52-
this._setProperties(arg);
52+
this._set(arg);
5353
}
5454
},
5555

src/tool/Tool.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
5050
'onKeyDown', 'onKeyUp' ],
5151

5252
// DOCS: rewrite Tool constructor explanation
53-
initialize: function() {
53+
initialize: function(props) {
5454
this.base();
5555
this._firstMove = true;
5656
this._count = 0;
5757
this._downCount = 0;
58+
this._set(props);
5859
},
5960

6061
/**

0 commit comments

Comments
 (0)