Move #_setProperties() and #set() from Item to Base, rename it to #_set and use it for Tool too.

This commit is contained in:
Jürg Lehni 2013-02-15 18:28:49 -08:00
parent 9d708f2690
commit 119e5acbf9
9 changed files with 33 additions and 29 deletions

View file

@ -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: {},

View file

@ -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);
},

View file

@ -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;

View file

@ -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));
},

View file

@ -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') {

View file

@ -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);
},

View file

@ -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() {

View file

@ -49,7 +49,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
this._content = '';
this._lines = [];
if (hasProperties) {
this._setProperties(arg);
this._set(arg);
}
},

View file

@ -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);
},
/**