Automate generation of style accessors through Style class.

Shaving off some byes by adding more magic.
This commit is contained in:
Jürg Lehni 2011-12-19 22:14:10 +01:00
parent 9e79514b54
commit 19429d9b6d
6 changed files with 26 additions and 32 deletions

View file

@ -215,6 +215,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
/**
* The path style of the item.
*
* @name Item#getStyle
* @type PathStyle
* @bean
*
@ -251,13 +252,6 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
* var path2 = new Path.Circle(new Point(150, 50), 20);
* path2.style = myStyle;
*/
getStyle: function() {
return this._style;
},
setStyle: function(style) {
this._style.initialize(style);
},
statics: {
_id: 0

View file

@ -45,7 +45,7 @@ var CharacterStyle = this.CharacterStyle = PathStyle.extend(/** @lends Character
font: 'sans-serif'
}),
_owner: TextItem,
_style: '_characterStyle'
_style: 'characterStyle'
/**
* CharacterStyle objects don't need to be created directly. Just pass an

View file

@ -35,7 +35,7 @@ var ParagraphStyle = this.ParagraphStyle = Style.extend(/** @lends ParagraphStyl
justification: 'left'
},
_owner: TextItem,
_style: '_paragraphStyle'
_style: 'paragraphStyle'
/**
* ParagraphStyle objects don't need to be created directly. Just pass an

View file

@ -58,7 +58,7 @@ var PathStyle = this.PathStyle = Style.extend(/** @lends PathStyle# */{
miterLimit: Change.STROKE
},
_owner: Item,
_style: '_style'
_style: 'style'
// DOCS: why isn't the example code showing up?
/**

View file

@ -47,9 +47,21 @@ var Style = Item.extend({
// Inject style getters and setters into the 'owning' class, which
// redirect calls to the linked style objects through their internal
// property on the instances of that class, as defined by _style.
var styleKey = src._style,
flags = src._flags || {};
src._owner.inject(Base.each(src._defaults, function(value, key) {
var styleKey = '_' + src._style,
stylePart = Base.capitalize(src._style),
flags = src._flags || {},
owner = {};
// Define accessor on owner class for this style:
owner['get' + stylePart] = function() {
return this[styleKey];
};
owner['set' + stylePart] = function(style) {
this[styleKey].initialize(style);
};
Base.each(src._defaults, function(value, key) {
var isColor = !!key.match(/Color$/),
part = Base.capitalize(key),
set = 'set' + part,
@ -105,16 +117,16 @@ var Style = Item.extend({
return style;
};
// Style-getters and setters for owner class:
// 'this' = the Base.each() side-car = the object that is
// returned from Base.each and injected into _owner above:
this[set] = function(value) {
owner[set] = function(value) {
this[styleKey][set](value);
return this;
};
this[get] = function() {
owner[get] = function() {
return this[styleKey][get]();
};
}, {}));
});
src._owner.inject(owner);
// Pass on to base()
return this.base.apply(this, arguments);
}
}

View file

@ -94,28 +94,16 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
*
* The character style of the text item.
*
* @name TextItem#getCharacterStyle
* @type CharacterStyle
* @bean
*/
getCharacterStyle: function() {
return this._characterStyle;
},
setCharacterStyle: function(style) {
this._characterStyle.initialize(style);
},
/**
* The paragraph style of the text item.
*
* @name TextItem#getParagraphStyle
* @type ParagraphStyle
* @bean
*/
getParagraphStyle: function() {
return this._paragraphStyle;
},
setParagraphStyle: function(style) {
this._paragraphStyle.initialize(style);
}
});