mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-06-08 11:14:15 -04:00
Move #_setProperties() and #set() from Item to Base, rename it to #_set and use it for Tool too.
This commit is contained in:
parent
9d708f2690
commit
119e5acbf9
9 changed files with 33 additions and 29 deletions
src
|
@ -50,6 +50,30 @@ this.Base = Base.inject(/** @lends Base# */{
|
||||||
return Base.toJson(this, options);
|
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 */{
|
statics: /** @lends Base */{
|
||||||
|
|
||||||
_types: {},
|
_types: {},
|
||||||
|
|
|
@ -69,7 +69,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
|
||||||
// Allow Group to have children and named children
|
// Allow Group to have children and named children
|
||||||
this._children = [];
|
this._children = [];
|
||||||
this._namedChildren = {};
|
this._namedChildren = {};
|
||||||
if (!this._setProperties(arg))
|
if (!this._set(arg))
|
||||||
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
_serialize: function(options, dictionary) {
|
||||||
var props = {},
|
var props = {},
|
||||||
that = this;
|
that = this;
|
||||||
|
|
|
@ -66,7 +66,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
|
||||||
this.base(arg1 !== undefined && Point.read(arguments, 1));
|
this.base(arg1 !== undefined && Point.read(arguments, 1));
|
||||||
// If we can handle setting properties through object literal, we're all
|
// If we can handle setting properties through object literal, we're all
|
||||||
// set. Otherwise we need to set symbol.
|
// set. Otherwise we need to set symbol.
|
||||||
if (!this._setProperties(arg0))
|
if (!this._set(arg0))
|
||||||
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
|
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
this.base(point !== undefined && Point.read(arguments, 1));
|
this.base(point !== undefined && Point.read(arguments, 1));
|
||||||
// If we can handle setting properties through object literal, we're all
|
// If we can handle setting properties through object literal, we're all
|
||||||
// set. Otherwise we need to check the type of object:
|
// set. Otherwise we need to check the type of object:
|
||||||
if (!this._setProperties(object)) {
|
if (!this._set(object)) {
|
||||||
if (object.getContext) {
|
if (object.getContext) {
|
||||||
this.setCanvas(object);
|
this.setCanvas(object);
|
||||||
} else if (typeof object === 'string') {
|
} else if (typeof object === 'string') {
|
||||||
|
|
|
@ -42,7 +42,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
|
||||||
// CompoundPath has children and supports named children.
|
// CompoundPath has children and supports named children.
|
||||||
this._children = [];
|
this._children = [];
|
||||||
this._namedChildren = {};
|
this._namedChildren = {};
|
||||||
if (!this._setProperties(arg))
|
if (!this._set(arg))
|
||||||
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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
|
// If it is an array, it can also be a description of a point, so
|
||||||
// check its first entry for object as well.
|
// check its first entry for object as well.
|
||||||
// But first see if segments are directly passed at all. If not, try
|
// But first see if segments are directly passed at all. If not, try
|
||||||
// _setProperties(arg).
|
// _set(arg).
|
||||||
var segments = Array.isArray(arg)
|
var segments = Array.isArray(arg)
|
||||||
? typeof arg[0] === 'object'
|
? typeof arg[0] === 'object'
|
||||||
? arg
|
? arg
|
||||||
|
@ -64,7 +64,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
|
||||||
: null;
|
: null;
|
||||||
this.setSegments(segments || []);
|
this.setSegments(segments || []);
|
||||||
if (!segments)
|
if (!segments)
|
||||||
this._setProperties(arg);
|
this._set(arg);
|
||||||
},
|
},
|
||||||
|
|
||||||
clone: function() {
|
clone: function() {
|
||||||
|
|
|
@ -49,7 +49,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
|
||||||
this._content = '';
|
this._content = '';
|
||||||
this._lines = [];
|
this._lines = [];
|
||||||
if (hasProperties) {
|
if (hasProperties) {
|
||||||
this._setProperties(arg);
|
this._set(arg);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -50,11 +50,12 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
'onKeyDown', 'onKeyUp' ],
|
'onKeyDown', 'onKeyUp' ],
|
||||||
|
|
||||||
// DOCS: rewrite Tool constructor explanation
|
// DOCS: rewrite Tool constructor explanation
|
||||||
initialize: function() {
|
initialize: function(props) {
|
||||||
this.base();
|
this.base();
|
||||||
this._firstMove = true;
|
this._firstMove = true;
|
||||||
this._count = 0;
|
this._count = 0;
|
||||||
this._downCount = 0;
|
this._downCount = 0;
|
||||||
|
this._set(props);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue