|
1 YUI.add('attribute-extras', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * The attribute module provides an augmentable Attribute implementation, which |
|
5 * adds configurable attributes and attribute change events to the class being |
|
6 * augmented. It also provides a State class, which is used internally by Attribute, |
|
7 * but can also be used independently to provide a name/property/value data structure to |
|
8 * store state. |
|
9 * |
|
10 * @module attribute |
|
11 */ |
|
12 |
|
13 /** |
|
14 * The attribute-extras submodule provides less commonly used attribute methods, and can |
|
15 * be augmented/mixed into an implemention which used attribute-core. |
|
16 * |
|
17 * @module attribute |
|
18 * @submodule attribute-extras |
|
19 */ |
|
20 var BROADCAST = "broadcast", |
|
21 PUBLISHED = "published", |
|
22 INIT_VALUE = "initValue", |
|
23 |
|
24 MODIFIABLE = { |
|
25 readOnly:1, |
|
26 writeOnce:1, |
|
27 getter:1, |
|
28 broadcast:1 |
|
29 }; |
|
30 |
|
31 /** |
|
32 * A augmentable implementation for AttributeCore, providing less frequently used |
|
33 * methods for Attribute management such as modifyAttrs(), removeAttr and reset() |
|
34 * |
|
35 * @class AttributeExtras |
|
36 * @extensionfor AttributeCore |
|
37 */ |
|
38 function AttributeExtras() {} |
|
39 |
|
40 AttributeExtras.prototype = { |
|
41 |
|
42 /** |
|
43 * Updates the configuration of an attribute which has already been added. |
|
44 * <p> |
|
45 * The properties which can be modified through this interface are limited |
|
46 * to the following subset of attributes, which can be safely modified |
|
47 * after a value has already been set on the attribute: readOnly, writeOnce, |
|
48 * broadcast and getter. |
|
49 * </p> |
|
50 * @method modifyAttr |
|
51 * @param {String} name The name of the attribute whose configuration is to be updated. |
|
52 * @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify. |
|
53 */ |
|
54 modifyAttr: function(name, config) { |
|
55 var host = this, // help compression |
|
56 prop, state; |
|
57 |
|
58 if (host.attrAdded(name)) { |
|
59 |
|
60 if (host._isLazyAttr(name)) { |
|
61 host._addLazyAttr(name); |
|
62 } |
|
63 |
|
64 state = host._state; |
|
65 for (prop in config) { |
|
66 if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) { |
|
67 state.add(name, prop, config[prop]); |
|
68 |
|
69 // If we reconfigured broadcast, need to republish |
|
70 if (prop === BROADCAST) { |
|
71 state.remove(name, PUBLISHED); |
|
72 } |
|
73 } |
|
74 } |
|
75 } |
|
76 /*jshint maxlen:200*/ |
|
77 /*jshint maxlen:150 */ |
|
78 }, |
|
79 |
|
80 /** |
|
81 * Removes an attribute from the host object |
|
82 * |
|
83 * @method removeAttr |
|
84 * @param {String} name The name of the attribute to be removed. |
|
85 */ |
|
86 removeAttr: function(name) { |
|
87 this._state.removeAll(name); |
|
88 }, |
|
89 |
|
90 /** |
|
91 * Resets the attribute (or all attributes) to its initial value, as long as |
|
92 * the attribute is not readOnly, or writeOnce. |
|
93 * |
|
94 * @method reset |
|
95 * @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset. |
|
96 * @return {Object} A reference to the host object. |
|
97 * @chainable |
|
98 */ |
|
99 reset : function(name) { |
|
100 var host = this; // help compression |
|
101 |
|
102 if (name) { |
|
103 if (host._isLazyAttr(name)) { |
|
104 host._addLazyAttr(name); |
|
105 } |
|
106 host.set(name, host._state.get(name, INIT_VALUE)); |
|
107 } else { |
|
108 Y.Object.each(host._state.data, function(v, n) { |
|
109 host.reset(n); |
|
110 }); |
|
111 } |
|
112 return host; |
|
113 }, |
|
114 |
|
115 /** |
|
116 * Returns an object with the configuration properties (and value) |
|
117 * for the given attribute. If attrName is not provided, returns the |
|
118 * configuration properties for all attributes. |
|
119 * |
|
120 * @method _getAttrCfg |
|
121 * @protected |
|
122 * @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes. |
|
123 * @return {Object} The configuration properties for the given attribute, or all attributes. |
|
124 */ |
|
125 _getAttrCfg : function(name) { |
|
126 var o, |
|
127 state = this._state; |
|
128 |
|
129 if (name) { |
|
130 o = state.getAll(name) || {}; |
|
131 } else { |
|
132 o = {}; |
|
133 Y.each(state.data, function(v, n) { |
|
134 o[n] = state.getAll(n); |
|
135 }); |
|
136 } |
|
137 |
|
138 return o; |
|
139 } |
|
140 }; |
|
141 |
|
142 Y.AttributeExtras = AttributeExtras; |
|
143 |
|
144 |
|
145 }, '@VERSION@', {"requires": ["oop"]}); |