From 3d5ae373a82c91954c3eb1e66eeba74a8278de3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 25 Jun 2013 09:41:55 -0700 Subject: [PATCH] Remove static create() methods from LinkedPoint/Size/Rectangle, and give them initialize() methods instead that also name the classes in debugging. --- src/basic/Point.js | 26 +++++++++----------------- src/basic/Rectangle.js | 40 +++++++++++++++------------------------- src/basic/Size.js | 24 +++++++++--------------- src/item/Item.js | 12 +++++------- src/text/PointText.js | 2 +- src/ui/View.js | 4 ++-- 6 files changed, 41 insertions(+), 67 deletions(-) diff --git a/src/basic/Point.js b/src/basic/Point.js index fdd82d84..2dc0e306 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -917,12 +917,20 @@ var Point = Base.extend(/** @lends Point# */{ * * @class An internal version of Point that notifies its owner of each change * through setting itself again on the setter that corresponds to the getter - * that produced this LinkedPoint. See uses of LinkedPoint.create() + * that produced this LinkedPoint. * Note: This prototype is not exported. * * @ignore */ var LinkedPoint = Point.extend({ + // Have LinkedPoint appear as a normal Point in debugging + initialize: function Point(x, y, owner, setter) { + this._x = x; + this._y = y; + this._owner = owner; + this._setter = setter; + }, + set: function(x, y, dontNotify) { this._x = x; this._y = y; @@ -947,21 +955,5 @@ var LinkedPoint = Point.extend({ setY: function(y) { this._y = y; this._owner[this._setter](this); - }, - - statics: { - create: function(owner, setter, x, y, dontLink) { - // Support creation of normal Points rather than LinkedPoints - // through an optional parameter that can be passed to the getters. - // See e.g. Rectangle#getPoint(true). - if (dontLink) - return new Point(x, y); - var point = Base.create(LinkedPoint); - point._x = x; - point._y = y; - point._owner = owner; - point._setter = setter; - return point; - } } }); diff --git a/src/basic/Rectangle.js b/src/basic/Rectangle.js index c85cc732..9a03acb2 100644 --- a/src/basic/Rectangle.js +++ b/src/basic/Rectangle.js @@ -238,9 +238,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{ * @bean */ getPoint: function(/* dontLink */) { - // Pass on the optional argument _dontLink which tells LinkedPoint to - // produce a normal point instead. Used internally for speed reasons. - return LinkedPoint.create(this, 'setPoint', this.x, this.y, arguments[0]); + return new (arguments[0] ? Point : LinkedPoint) + (this.x, this.y, this, 'setPoint'); }, setPoint: function(point) { @@ -257,8 +256,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{ * @bean */ getSize: function(/* dontLink */) { - return LinkedSize.create(this, 'setSize', this.width, this.height, - arguments[0]); + return new (arguments[0] ? Size : LinkedSize) + (this.width, this.height, this, 'setSize'); }, setSize: function(size) { @@ -403,8 +402,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{ * @bean */ getCenter: function(/* dontLink */) { - return LinkedPoint.create(this, 'setCenter', - this.getCenterX(), this.getCenterY(), arguments[0]); + return new (arguments[0] ? Point : LinkedPoint) + (this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, setCenter: function(point) { @@ -801,8 +800,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{ get = 'get' + part, set = 'set' + part; this[get] = function(/* dontLink */) { - return LinkedPoint.create(this, set, - this[getX](), this[getY](), arguments[0]); + return new (arguments[0] ? Point : LinkedPoint) + (this[getX](), this[getY](), this, set); }; this[set] = function(point) { point = Point.read(arguments); @@ -824,6 +823,13 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{ * @private */ var LinkedRectangle = Rectangle.extend({ + // Have LinkedRectangle appear as a normal Rectangle in debugging + initialize: function Rectangle(x, y, width, height, owner, setter) { + this.set(x, y, width, height, true); + this._owner = owner; + this._setter = setter; + }, + set: function(x, y, width, height, dontNotify) { this._x = x; this._y = y; @@ -832,22 +838,6 @@ var LinkedRectangle = Rectangle.extend({ if (!dontNotify) this._owner[this._setter](this); return this; - }, - - statics: { - /** - * Provide a faster creator for Points out of two coordinates that - * does not rely on Point#initialize at all. This speeds up all math - * operations a lot. - * - * @ignore - */ - create: function(owner, setter, x, y, width, height) { - var rect = Base.create(LinkedRectangle).set(x, y, width, height, true); - rect._owner = owner; - rect._setter = setter; - return rect; - } } }, new function() { var proto = Rectangle.prototype; diff --git a/src/basic/Size.js b/src/basic/Size.js index ed57f56e..4793e1c5 100644 --- a/src/basic/Size.js +++ b/src/basic/Size.js @@ -512,12 +512,20 @@ var Size = Base.extend(/** @lends Size# */{ * * @class An internal version of Size that notifies its owner of each change * through setting itself again on the setter that corresponds to the getter - * that produced this LinkedSize. See uses of LinkedSize.create() + * that produced this LinkedSize. * Note: This prototype is not exported. * * @private */ var LinkedSize = Size.extend({ + // Have LinkedSize appear as a normal Size in debugging + initialize: function Size(width, height, owner, setter) { + this._width = width; + this._height = height; + this._owner = owner; + this._setter = setter; + }, + set: function(width, height, dontNotify) { this._width = width; this._height = height; @@ -542,19 +550,5 @@ var LinkedSize = Size.extend({ setHeight: function(height) { this._height = height; this._owner[this._setter](this); - }, - - statics: { - create: function(owner, setter, width, height, dontLink) { - // See LinkedPoint.create() for an explanation about dontLink. - if (dontLink) - return new Size(width, height); - var size = Base.create(LinkedSize); - size._width = width; - size._height = height; - size._owner = owner; - size._setter = setter; - return size; - } } }); diff --git a/src/item/Item.js b/src/item/Item.js index 749d25b1..091ad499 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -719,8 +719,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{ // use them to calculate the difference in #setPosition, as when it is // modified, it would hold new values already and only then cause the // calling of #setPosition. - return arguments[0] ? pos - : LinkedPoint.create(this, 'setPosition', pos.x, pos.y); + return new (arguments[0] ? Point : LinkedPoint) + (pos.x, pos.y, this, 'setPosition'); }, setPosition: function(/* point */) { @@ -773,11 +773,9 @@ var Item = Base.extend(Callback, /** @lends Item# */{ // If we're returning 'bounds', create a LinkedRectangle that uses // the setBounds() setter to update the Item whenever the bounds are // changed: - return name == 'getBounds' - ? LinkedRectangle.create(this, 'setBounds', - bounds.x, bounds.y, bounds.width, bounds.height) - // Return a clone of the cahce, so modifications won't - // affect it. + return name === 'getBounds' + ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, + bounds.height, this, 'setBounds') : bounds; }; }, diff --git a/src/text/PointText.js b/src/text/PointText.js index c957d97c..07257490 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -61,7 +61,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{ // Se Item#getPosition for an explanation why we create new LinkedPoint // objects each time. var point = this._matrix.getTranslation(); - return LinkedPoint.create(this, 'setPoint', point.x, point.y); + return new LinkedPoint(point.x, point.y, this, 'setPoint'); }, setPoint: function(point) { diff --git a/src/ui/View.js b/src/ui/View.js index 551e8bf3..289df0dc 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -94,8 +94,8 @@ var View = Base.extend(Callback, /** @lends View# */{ View._views.push(this); // Link this id to our view View._viewsById[this._id] = this; - this._viewSize = LinkedSize.create(this, 'setViewSize', - size.width, size.height); + this._viewSize = new LinkedSize(size.width, size.height, + this, 'setViewSize'); this._matrix = new Matrix(); this._zoom = 1; // Make sure the first view is focused for keyboard input straight away