Finish ObservedRectangle, by solving observer notification for all setters.

This commit is contained in:
Jürg Lehni 2011-03-22 17:15:56 +00:00
parent 14010eb8b2
commit dbb947b7aa

View file

@ -97,7 +97,6 @@ var Rectangle = this.Rectangle = Base.extend({
},
setLeft: function(left) {
// right should not move
this.width -= left - this.x;
this.x = left;
return this;
@ -255,14 +254,17 @@ var Rectangle = this.Rectangle = Base.extend({
getX = 'get' + x,
getY = 'get' + y,
setX = 'set' + x,
setY = 'set' + y;
this['get' + part] = function() {
return ObservedPoint.create(this, 'set' + part,
setY = 'set' + y,
get = 'get' + part,
set = 'set' + part;
this[get] = function() {
return ObservedPoint.create(this, set,
this[getX](), this[getY]());
};
this['set' + part] = function(point) {
this[set] = function(point) {
point = Point.read(arguments);
return this[setX](point.x)[setY](point.y); // Note: call chaining!
// Note: call chaining happens here.
return this[setX](point.x)[setY](point.y);
};
}, { beans: true });
});
@ -280,50 +282,6 @@ var ObservedRectangle = Rectangle.extend({
return this;
},
// TODO: Use loop to create these?
getX: function() {
return this._x;
},
setX: function(x) {
this._x = x;
this._observer[this._set](this);
},
getY: function() {
return this._y;
},
setY: function(y) {
this._y = y;
this._observer[this._set](this);
},
getWidth: function() {
return this._width;
},
setWidth: function(width) {
this._width = width;
this._observer[this._set](this);
},
getHeight: function() {
return this._height;
},
setHeight: function(height) {
this._height = height;
this._observer[this._set](this);
},
// TODO: Implement for all properties on ObservedRectangle using loop
setCenter: function(center) {
Rectangle.prototype.setCenter.apply(this, center);
this._observer[this._set](this);
return this;
},
statics: {
/**
* Provide a faster creator for Points out of two coordinates that
@ -342,4 +300,39 @@ var ObservedRectangle = Rectangle.extend({
return rect;
}
}
}, new function() {
var proto = Rectangle.prototype;
return Base.each(['x', 'y', 'width', 'height'], function(key) {
var part = Base.capitalize(key);
var internal = '_' + key;
this['get' + part] = function() {
return this[internal];
}
this['set' + part] = function(value) {
this[internal] = value;
// Check if this setter is called from another one which sets
// _dontNotify, as it will notify itself
if (!this._dontNotify)
this._observer[this._set](this);
}
}, Base.each(['Point', 'Size', 'Center',
'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY',
'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight',
'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter'],
function(key) {
var name = 'set' + key;
this[name] = function(value) {
// Make sure the above setters of x, y, width, height do not
// each notify the observer, as we're going to take care of this
// afterwards here, only once per change.
this._dontNotify = true;
proto[name].apply(this, arguments);
delete this._dontNotify;
this._observer[this._set](this);
return this;
}
}, { beans: true })
);
});