1 |
efrain |
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:
|
|
|
48 |
* </p>
|
|
|
49 |
* <dl>
|
|
|
50 |
* <dt>readOnly;</dt>
|
|
|
51 |
* <dt>writeOnce;</dt>
|
|
|
52 |
* <dt>broadcast; and</dt>
|
|
|
53 |
* <dt>getter.</dt>
|
|
|
54 |
* </dl>
|
|
|
55 |
* <p>
|
|
|
56 |
* Note: New attributes cannot be added using this interface. New attributes must be
|
|
|
57 |
* added using {{#crossLink "AttributeCore/addAttr:method"}}addAttr{{/crossLink}}, or an
|
|
|
58 |
* appropriate manner for a class which utilises Attributes (e.g. the
|
|
|
59 |
* {{#crossLink "Base/ATTRS:property"}}ATTRS{{/crossLink}} property in
|
|
|
60 |
* {{#crossLink "Base"}}Base{{/crossLink}}).
|
|
|
61 |
* </p>
|
|
|
62 |
* @method modifyAttr
|
|
|
63 |
* @param {String} name The name of the attribute whose configuration is to be updated.
|
|
|
64 |
* @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
|
|
|
65 |
*/
|
|
|
66 |
modifyAttr: function(name, config) {
|
|
|
67 |
var host = this, // help compression
|
|
|
68 |
prop, state;
|
|
|
69 |
|
|
|
70 |
if (host.attrAdded(name)) {
|
|
|
71 |
|
|
|
72 |
if (host._isLazyAttr(name)) {
|
|
|
73 |
host._addLazyAttr(name);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
state = host._state;
|
|
|
77 |
for (prop in config) {
|
|
|
78 |
if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
|
|
|
79 |
state.add(name, prop, config[prop]);
|
|
|
80 |
|
|
|
81 |
// If we reconfigured broadcast, need to republish
|
|
|
82 |
if (prop === BROADCAST) {
|
|
|
83 |
state.remove(name, PUBLISHED);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
} else {
|
|
|
88 |
Y.log('Attribute modifyAttr:' + name + ' has not been added. Use addAttr to add the attribute', 'warn', 'attribute');
|
|
|
89 |
}
|
|
|
90 |
},
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Removes an attribute from the host object
|
|
|
94 |
*
|
|
|
95 |
* @method removeAttr
|
|
|
96 |
* @param {String} name The name of the attribute to be removed.
|
|
|
97 |
*/
|
|
|
98 |
removeAttr: function(name) {
|
|
|
99 |
this._state.removeAll(name);
|
|
|
100 |
},
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Resets the attribute (or all attributes) to its initial value, as long as
|
|
|
104 |
* the attribute is not readOnly, or writeOnce.
|
|
|
105 |
*
|
|
|
106 |
* @method reset
|
|
|
107 |
* @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
|
|
|
108 |
* @return {Object} A reference to the host object.
|
|
|
109 |
* @chainable
|
|
|
110 |
*/
|
|
|
111 |
reset : function(name) {
|
|
|
112 |
var host = this; // help compression
|
|
|
113 |
|
|
|
114 |
if (name) {
|
|
|
115 |
if (host._isLazyAttr(name)) {
|
|
|
116 |
host._addLazyAttr(name);
|
|
|
117 |
}
|
|
|
118 |
host.set(name, host._state.get(name, INIT_VALUE));
|
|
|
119 |
} else {
|
|
|
120 |
Y.Object.each(host._state.data, function(v, n) {
|
|
|
121 |
host.reset(n);
|
|
|
122 |
});
|
|
|
123 |
}
|
|
|
124 |
return host;
|
|
|
125 |
},
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Returns an object with the configuration properties (and value)
|
|
|
129 |
* for the given attribute. If attrName is not provided, returns the
|
|
|
130 |
* configuration properties for all attributes.
|
|
|
131 |
*
|
|
|
132 |
* @method _getAttrCfg
|
|
|
133 |
* @protected
|
|
|
134 |
* @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
|
|
|
135 |
* @return {Object} The configuration properties for the given attribute, or all attributes.
|
|
|
136 |
*/
|
|
|
137 |
_getAttrCfg : function(name) {
|
|
|
138 |
var o,
|
|
|
139 |
state = this._state;
|
|
|
140 |
|
|
|
141 |
if (name) {
|
|
|
142 |
o = state.getAll(name) || {};
|
|
|
143 |
} else {
|
|
|
144 |
o = {};
|
|
|
145 |
Y.each(state.data, function(v, n) {
|
|
|
146 |
o[n] = state.getAll(n);
|
|
|
147 |
});
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
return o;
|
|
|
151 |
}
|
|
|
152 |
};
|
|
|
153 |
|
|
|
154 |
Y.AttributeExtras = AttributeExtras;
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
}, '3.18.1', {"requires": ["oop"]});
|