Skip to content

Commit 3ac039b

Browse files
committed
Implement properties object literal initialization for all Item constructors.
1 parent 6b0b691 commit 3ac039b

7 files changed

Lines changed: 73 additions & 35 deletions

File tree

src/core/Base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ this.Base = Base.inject(/** @lends Base# */{
105105
* @param {Boolean} clone controls wether passed objects should be
106106
* cloned if they are already provided in the required type
107107
*/
108-
read: function(list, start, length, clone) {
108+
read: function(list, start, length, clone, readNull) {
109109
var proto = this.prototype,
110110
readIndex = proto._readIndex,
111111
index = start || readIndex && list._index || 0;
112112
if (!length)
113113
length = list.length - index;
114114
var obj = list[index];
115115
if (obj instanceof this
116-
// If the class defines _readNull, return null when nothing
117-
// was provided
118-
|| proto._readNull && obj == null && length <= 1) {
116+
// If the class defines _readNull, return null when nothing
117+
// was provided
118+
|| (proto._readNull || readNull) && obj == null && length <= 1) {
119119
if (readIndex)
120120
list._index = index + 1;
121121
return obj && clone ? obj.clone() : obj;

src/item/Group.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
6868
* group.rotate(1, view.center);
6969
* }
7070
*/
71-
initialize: function(items) {
71+
initialize: function(arg) {
7272
this.base();
7373
// Allow Group to have children and named children
7474
this._children = [];
7575
this._namedChildren = {};
76-
this.addChildren(Array.isArray(items) ? items : arguments);
76+
if (!this.setProperties(arg))
77+
this.addChildren(Array.isArray(arg) ? arg : arguments);
7778
},
7879

7980
_changed: function(flags) {

src/item/Item.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
111111
this._matrix.translate(point);
112112
},
113113

114+
setProperties: function(props) {
115+
if (Base.isObject(props)) {
116+
for (var key in props)
117+
if (props.hasOwnProperty(key))
118+
this[key] = props[key];
119+
return true;
120+
}
121+
},
122+
114123
/**
115124
* Private notifier that is called whenever a change occurs in this item or
116125
* its sub-elements, such as Segments, Curves, PathStyles, etc.

src/item/PlacedSymbol.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
6060
* instance.scale(0.25 + Math.random() * 0.75);
6161
* }
6262
*/
63-
initialize: function(symbol, point) {
64-
this.base(Point.read(arguments, 1));
65-
this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol));
63+
initialize: function(arg0, arg1) {
64+
// Support two forms of item initialization: Passing one object literal
65+
// describing all the different properties to be set, or a symbol (arg0)
66+
// and a point where it should be placed (arg1).
67+
this.base(arg1 !== undefined && Point.read(arguments, 1));
68+
// If we can handle setting properties through object literal, we're all
69+
// set. Otherwise we need to set symbol.
70+
if (!this.setProperties(arg0))
71+
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
6672
},
6773

6874
/**

src/item/Raster.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
3535
*
3636
* @param {HTMLImageElement|Canvas|string} [object]
3737
*/
38-
initialize: function(object, point) {
39-
this.base(Point.read(arguments, 1));
40-
if (object.getContext) {
41-
this.setCanvas(object);
42-
} else if (typeof object === 'string') {
43-
this.setSource(object);
44-
} else {
45-
this.setImage(object);
38+
initialize: function(arg0, arg1) {
39+
// Support two forms of item initialization: Passing one object literal
40+
// describing all the different properties to be set, or an image (arg0)
41+
// and a point where it should be placed (arg1).
42+
this.base(arg1 !== undefined && Point.read(arguments, 1));
43+
// If we can handle setting properties through object literal, we're all
44+
// set. Otherwise we need to check the type of arg0:
45+
if (!this.setProperties(arg0)) {
46+
if (arg0.getContext) {
47+
this.setCanvas(arg0);
48+
} else if (typeof arg0 === 'string') {
49+
this.setSource(arg0);
50+
} else {
51+
this.setImage(arg0);
52+
}
4653
}
4754
},
4855

src/path/Path.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,27 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
4444
* var path = new Path(segments);
4545
* path.strokeColor = 'black';
4646
*/
47-
initialize: function(segments) {
48-
this.base();
47+
initialize: function(arg) {
4948
this._closed = false;
50-
this._selectedSegmentState = 0;
51-
// Support both passing of segments as array or arguments
49+
this._segments = [];
50+
this.base();
51+
// arg can either be an object literal describing properties to be set
52+
// on the path, a list of segments to be set, or the first of multiple
53+
// arguments describing separate segments.
5254
// If it is an array, it can also be a description of a point, so
53-
// check its first entry for object as well
54-
this.setSegments(Array.isArray(segments)
55-
&& typeof segments[0] === 'object' ? segments : arguments);
55+
// check its first entry for object as well.
56+
// But first see if segments are directly passed at all. If not, try
57+
// #setProperties(arg).
58+
var segments = Array.isArray(arg)
59+
? typeof arg[0] === 'object'
60+
? arg
61+
: arguments
62+
: arg && (arg.point !== undefined || arg.x !== undefined)
63+
? arguments
64+
: null;
65+
this.setSegments(segments || []);
66+
if (!segments)
67+
this.setProperties(arg);
5668
},
5769

5870
clone: function() {
@@ -94,15 +106,10 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
94106
},
95107

96108
setSegments: function(segments) {
97-
if (!this._segments) {
98-
this._segments = [];
99-
} else {
100-
this._selectedSegmentState = 0;
101-
this._segments.length = 0;
102-
// Make sure new curves are calculated next time we call getCurves()
103-
if (this._curves)
104-
delete this._curves;
105-
}
109+
this._selectedSegmentState = 0;
110+
this._segments.length = 0;
111+
// Make sure new curves are calculated next time we call getCurves()
112+
delete this._curves;
106113
this._add(Segment.readAll(segments));
107114
},
108115

src/text/TextItem.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
3030
// so use the same name for all of them
3131
_boundsGetter: 'getBounds',
3232

33-
initialize: function(point) {
33+
initialize: function(arg) {
34+
// Support two forms of item initialization: Passing one object literal
35+
// describing all the different properties to be set, or a point where
36+
// it should be placed (arg).
3437
// Note that internally #characterStyle is the same as #style, but
3538
// defined as an instance of CharacterStyle. We need to define it before
3639
// calling this.base(), to override the default PathStyle instance.
3740
this._style = CharacterStyle.create(this);
3841
this._paragraphStyle = ParagraphStyle.create(this);
39-
this.base(Point.read(arguments));
42+
// See if a point is passed, and if so, pass it on to base(). If not, it
43+
// might be a properties object literal for #setPropeties() at the end.
44+
var point = Point.read(arguments, 0, 0, false, true); // readNull
45+
this.base(point);
4046
// No need to call setStyle(), since base() handles this already.
4147
// Call with no parameter to initalize defaults now.
4248
this.setParagraphStyle();
4349
this._content = '';
4450
this._lines = [];
51+
if (!point)
52+
this.setProperties(arg);
4553
},
4654

4755
/**

0 commit comments

Comments
 (0)