Implement item-level #equals() method and clean up other equals methods.

This commit is contained in:
Jürg Lehni 2013-10-17 13:08:54 +02:00
parent 83be3cb023
commit 73d2c34088
13 changed files with 71 additions and 16 deletions

View file

@ -112,9 +112,9 @@ var Matrix = Base.extend(/** @lends Matrix# */{
* @return {Boolean} {@true if the matrices are equal}
*/
equals: function(mx) {
return mx === this || mx && this._a == mx._a && this._b == mx._b
&& this._c == mx._c && this._d == mx._d && this._tx == mx._tx
&& this._ty == mx._ty
return mx === this || mx && this._a === mx._a && this._b === mx._b
&& this._c === mx._c && this._d === mx._d
&& this._tx === mx._tx && this._ty === mx._ty
|| false;
},

View file

@ -205,7 +205,7 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
rect = Rectangle.read(arguments);
return rect === this
|| rect && this.x === rect.x && this.y === rect.y
&& this.width === rect.width && this.height=== rect.height
&& this.width === rect.width && this.height === rect.height
|| false;
},

View file

@ -1233,6 +1233,30 @@ var Item = Base.extend(Callback, /** @lends Item# */{
return this._parent ? this._parent.isInserted() : false;
},
equals: function(item) {
// Note: We do not compare name and selected state.
return item === this || item && this._class === item._class
&& this._style.equals(item._style)
&& this._matrix.equals(item._matrix)
&& this._locked === item._locked
&& this._visible === item._visible
&& this._blendMode === item._blendMode
&& this._opacity === item._opacity
&& this._clipMask === item._clipMask
&& this._guide === item._guide
&& this._equals(item)
|| false;
},
/**
* A private helper for #equals(), to be overridden in sub-classes. When it
* is called, item is always defined, of the same class as `this` and has
* equal general state attributes such as matrix, style, opacity, etc.
*/
_equals: function(item) {
return Base.equals(this._children, item._children);
},
/**
* Clones the item within the same project and places the copy above the
* item.

View file

@ -79,6 +79,10 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
},
_equals: function(item) {
return this._symbol === item._symbol;
},
/**
* The symbol that the placed symbol refers to.
*

View file

@ -92,6 +92,10 @@ var Raster = Item.extend(/** @lends Raster# */{
this._size = new Size();
},
_equals: function(item) {
return this.getSource() === item.getSource();
},
clone: function(insert) {
var param = { insert: false },
image = this._image;

View file

@ -31,6 +31,13 @@ var Shape = Item.extend(/** @lends Shape# */{
this._initialize(props, center);
},
_equals: function(item) {
return this._shape === item._shape
&& this._size.equals(item._size)
// Radius can be a number or size:
&& Base.equals(this._radius, item._radius);
},
clone: function(insert) {
return this._clone(new Shape(this._shape, this.getPosition(true),
this._size.clone(),

View file

@ -96,6 +96,10 @@ var Path = PathItem.extend(/** @lends Path# */{
this._initialize(!segments && arg);
},
_equals: function(item) {
return Base.equals(this._segments, item._segments);
},
clone: function(insert) {
var copy = this._clone(new Path({
segments: this._segments,

View file

@ -478,7 +478,7 @@ var Segment = Base.extend(/** @lends Segment# */{
},
equals: function(segment) {
return segment === this || segment
return segment === this || segment && this._class === segment._class
&& this._point.equals(segment._point)
&& this._handleIn.equals(segment._handleIn)
&& this._handleOut.equals(segment._handleOut)

View file

@ -740,7 +740,8 @@ var Color = Base.extend(new function() {
equals: function(color) {
if (Base.isPlainValue(color))
color = Color.read(arguments);
return color === this || color && this._type === color._type
return color === this || color && this._class === color._class
&& this._type === color._type
&& this._alpha === color._alpha
&& Base.equals(this._components, color._components)
|| false;

View file

@ -180,8 +180,10 @@ var Gradient = Base.extend(/** @lends Gradient# */{
* @return {Boolean} {@true they are equal}
*/
equals: function(gradient) {
if (gradient && gradient.constructor == this.constructor
&& this._stops.length == gradient._stops.length) {
if (gradient === this)
return true;
if (gradient && this._class === gradient._class
&& this._stops.length === gradient._stops.length) {
for (var i = 0, l = this._stops.length; i < l; i++) {
if (!this._stops[i].equals(gradient._stops[i]))
return false;

View file

@ -174,7 +174,7 @@ var GradientStop = Base.extend(/** @lends GradientStop# */{
},
equals: function(stop) {
return stop === this || stop instanceof GradientStop
return stop === this || stop && this._class === stop._class
&& this._color.equals(stop._color)
&& this._rampPoint == stop._rampPoint
|| false;

View file

@ -235,6 +235,12 @@ var Style = Base.extend(new function() {
}
},
equals: function(style) {
return style === this || style && this._class === style._class
&& Base.equals(this._values, style._values)
|| false;
},
getLeading: function getLeading() {
// Override leading to return fontSize * 1.2 by default.
var leading = getLeading.base.call(this);

View file

@ -44,11 +44,20 @@ var TextItem = Item.extend(/** @lends TextItem# */{
this._initialize(hasProps && arg, !hasProps && Point.read(arguments));
},
_equals: function(item) {
return this._content === item._content;
},
_clone: function _clone(copy) {
copy.setContent(this._content);
return _clone.base.call(this, copy);
},
/**
* The text contents of the text item.
*
* @name TextItem#content
* @type String
* @bean
*
* @example {@paperscript}
* // Setting the content of a PointText item:
@ -75,12 +84,6 @@ var TextItem = Item.extend(/** @lends TextItem# */{
* text.content = 'Your position is: ' + event.point.toString();
* }
*/
_clone: function _clone(copy) {
copy.setContent(this._content);
return _clone.base.call(this, copy);
},
getContent: function() {
return this._content;
},