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

View file

@ -45,7 +45,7 @@ var CharacterStyle = this.CharacterStyle = PathStyle.extend(/** @lends Character
font: 'sans-serif' font: 'sans-serif'
}), }),
_owner: TextItem, _owner: TextItem,
_style: '_characterStyle' _style: 'characterStyle'
/** /**
* CharacterStyle objects don't need to be created directly. Just pass an * 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' justification: 'left'
}, },
_owner: TextItem, _owner: TextItem,
_style: '_paragraphStyle' _style: 'paragraphStyle'
/** /**
* ParagraphStyle objects don't need to be created directly. Just pass an * 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 miterLimit: Change.STROKE
}, },
_owner: Item, _owner: Item,
_style: '_style' _style: 'style'
// DOCS: why isn't the example code showing up? // 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 // Inject style getters and setters into the 'owning' class, which
// redirect calls to the linked style objects through their internal // redirect calls to the linked style objects through their internal
// property on the instances of that class, as defined by _style. // property on the instances of that class, as defined by _style.
var styleKey = src._style, var styleKey = '_' + src._style,
flags = src._flags || {}; stylePart = Base.capitalize(src._style),
src._owner.inject(Base.each(src._defaults, function(value, key) { 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$/), var isColor = !!key.match(/Color$/),
part = Base.capitalize(key), part = Base.capitalize(key),
set = 'set' + part, set = 'set' + part,
@ -105,16 +117,16 @@ var Style = Item.extend({
return style; return style;
}; };
// Style-getters and setters for owner class: // Style-getters and setters for owner class:
// 'this' = the Base.each() side-car = the object that is owner[set] = function(value) {
// returned from Base.each and injected into _owner above:
this[set] = function(value) {
this[styleKey][set](value); this[styleKey][set](value);
return this; return this;
}; };
this[get] = function() { owner[get] = function() {
return this[styleKey][get](); return this[styleKey][get]();
}; };
}, {})); });
src._owner.inject(owner);
// Pass on to base()
return this.base.apply(this, arguments); 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. * The character style of the text item.
* *
* @name TextItem#getCharacterStyle
* @type CharacterStyle * @type CharacterStyle
* @bean * @bean
*/ */
getCharacterStyle: function() {
return this._characterStyle;
},
setCharacterStyle: function(style) {
this._characterStyle.initialize(style);
},
/** /**
* The paragraph style of the text item. * The paragraph style of the text item.
* *
* @name TextItem#getParagraphStyle
* @type ParagraphStyle * @type ParagraphStyle
* @bean * @bean
*/ */
getParagraphStyle: function() {
return this._paragraphStyle;
},
setParagraphStyle: function(style) {
this._paragraphStyle.initialize(style);
}
}); });