1 |
efrain |
1 |
YUI.add('attribute-core', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* The State class maintains state for a collection of named items, with
|
|
|
5 |
* a varying number of properties defined.
|
|
|
6 |
*
|
|
|
7 |
* It avoids the need to create a separate class for the item, and separate instances
|
|
|
8 |
* of these classes for each item, by storing the state in a 2 level hash table,
|
|
|
9 |
* improving performance when the number of items is likely to be large.
|
|
|
10 |
*
|
|
|
11 |
* @constructor
|
|
|
12 |
* @class State
|
|
|
13 |
*/
|
|
|
14 |
Y.State = function() {
|
|
|
15 |
/**
|
|
|
16 |
* Hash of attributes
|
|
|
17 |
* @property data
|
|
|
18 |
*/
|
|
|
19 |
this.data = {};
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
Y.State.prototype = {
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Adds a property to an item.
|
|
|
26 |
*
|
|
|
27 |
* @method add
|
|
|
28 |
* @param name {String} The name of the item.
|
|
|
29 |
* @param key {String} The name of the property.
|
|
|
30 |
* @param val {Any} The value of the property.
|
|
|
31 |
*/
|
|
|
32 |
add: function(name, key, val) {
|
|
|
33 |
var item = this.data[name];
|
|
|
34 |
|
|
|
35 |
if (!item) {
|
|
|
36 |
item = this.data[name] = {};
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
item[key] = val;
|
|
|
40 |
},
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Adds multiple properties to an item.
|
|
|
44 |
*
|
|
|
45 |
* @method addAll
|
|
|
46 |
* @param name {String} The name of the item.
|
|
|
47 |
* @param obj {Object} A hash of property/value pairs.
|
|
|
48 |
*/
|
|
|
49 |
addAll: function(name, obj) {
|
|
|
50 |
var item = this.data[name],
|
|
|
51 |
key;
|
|
|
52 |
|
|
|
53 |
if (!item) {
|
|
|
54 |
item = this.data[name] = {};
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
for (key in obj) {
|
|
|
58 |
if (obj.hasOwnProperty(key)) {
|
|
|
59 |
item[key] = obj[key];
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
},
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Removes a property from an item.
|
|
|
66 |
*
|
|
|
67 |
* @method remove
|
|
|
68 |
* @param name {String} The name of the item.
|
|
|
69 |
* @param key {String} The property to remove.
|
|
|
70 |
*/
|
|
|
71 |
remove: function(name, key) {
|
|
|
72 |
var item = this.data[name];
|
|
|
73 |
|
|
|
74 |
if (item) {
|
|
|
75 |
delete item[key];
|
|
|
76 |
}
|
|
|
77 |
},
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Removes multiple properties from an item, or removes the item completely.
|
|
|
81 |
*
|
|
|
82 |
* @method removeAll
|
|
|
83 |
* @param name {String} The name of the item.
|
|
|
84 |
* @param obj {Object|Array} Collection of properties to delete. If not provided, the entire item is removed.
|
|
|
85 |
*/
|
|
|
86 |
removeAll: function(name, obj) {
|
|
|
87 |
var data;
|
|
|
88 |
|
|
|
89 |
if (!obj) {
|
|
|
90 |
data = this.data;
|
|
|
91 |
|
|
|
92 |
if (name in data) {
|
|
|
93 |
delete data[name];
|
|
|
94 |
}
|
|
|
95 |
} else {
|
|
|
96 |
Y.each(obj, function(value, key) {
|
|
|
97 |
this.remove(name, typeof key === 'string' ? key : value);
|
|
|
98 |
}, this);
|
|
|
99 |
}
|
|
|
100 |
},
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* For a given item, returns the value of the property requested, or undefined if not found.
|
|
|
104 |
*
|
|
|
105 |
* @method get
|
|
|
106 |
* @param name {String} The name of the item
|
|
|
107 |
* @param key {String} Optional. The property value to retrieve.
|
|
|
108 |
* @return {Any} The value of the supplied property.
|
|
|
109 |
*/
|
|
|
110 |
get: function(name, key) {
|
|
|
111 |
var item = this.data[name];
|
|
|
112 |
|
|
|
113 |
if (item) {
|
|
|
114 |
return item[key];
|
|
|
115 |
}
|
|
|
116 |
},
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* For the given item, returns an object with all of the
|
|
|
120 |
* item's property/value pairs. By default the object returned
|
|
|
121 |
* is a shallow copy of the stored data, but passing in true
|
|
|
122 |
* as the second parameter will return a reference to the stored
|
|
|
123 |
* data.
|
|
|
124 |
*
|
|
|
125 |
* @method getAll
|
|
|
126 |
* @param name {String} The name of the item
|
|
|
127 |
* @param reference {boolean} true, if you want a reference to the stored
|
|
|
128 |
* object
|
|
|
129 |
* @return {Object} An object with property/value pairs for the item.
|
|
|
130 |
*/
|
|
|
131 |
getAll : function(name, reference) {
|
|
|
132 |
var item = this.data[name],
|
|
|
133 |
key, obj;
|
|
|
134 |
|
|
|
135 |
if (reference) {
|
|
|
136 |
obj = item;
|
|
|
137 |
} else if (item) {
|
|
|
138 |
obj = {};
|
|
|
139 |
|
|
|
140 |
for (key in item) {
|
|
|
141 |
if (item.hasOwnProperty(key)) {
|
|
|
142 |
obj[key] = item[key];
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
return obj;
|
|
|
148 |
}
|
|
|
149 |
};
|
|
|
150 |
/*For log lines*/
|
|
|
151 |
/*jshint maxlen:200*/
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* The attribute module provides an augmentable Attribute implementation, which
|
|
|
155 |
* adds configurable attributes and attribute change events to the class being
|
|
|
156 |
* augmented. It also provides a State class, which is used internally by Attribute,
|
|
|
157 |
* but can also be used independently to provide a name/property/value data structure to
|
|
|
158 |
* store state.
|
|
|
159 |
*
|
|
|
160 |
* @module attribute
|
|
|
161 |
*/
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* The attribute-core submodule provides the lightest level of attribute handling support
|
|
|
165 |
* without Attribute change events, or lesser used methods such as reset(), modifyAttrs(),
|
|
|
166 |
* and removeAttr().
|
|
|
167 |
*
|
|
|
168 |
* @module attribute
|
|
|
169 |
* @submodule attribute-core
|
|
|
170 |
*/
|
|
|
171 |
var O = Y.Object,
|
|
|
172 |
Lang = Y.Lang,
|
|
|
173 |
|
|
|
174 |
DOT = ".",
|
|
|
175 |
|
|
|
176 |
// Externally configurable props
|
|
|
177 |
GETTER = "getter",
|
|
|
178 |
SETTER = "setter",
|
|
|
179 |
READ_ONLY = "readOnly",
|
|
|
180 |
WRITE_ONCE = "writeOnce",
|
|
|
181 |
INIT_ONLY = "initOnly",
|
|
|
182 |
VALIDATOR = "validator",
|
|
|
183 |
VALUE = "value",
|
|
|
184 |
VALUE_FN = "valueFn",
|
|
|
185 |
LAZY_ADD = "lazyAdd",
|
|
|
186 |
|
|
|
187 |
// Used for internal state management
|
|
|
188 |
ADDED = "added",
|
|
|
189 |
BYPASS_PROXY = "_bypassProxy",
|
|
|
190 |
INIT_VALUE = "initValue",
|
|
|
191 |
LAZY = "lazy",
|
|
|
192 |
|
|
|
193 |
INVALID_VALUE;
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* <p>
|
|
|
197 |
* AttributeCore provides the lightest level of configurable attribute support. It is designed to be
|
|
|
198 |
* augmented on to a host class, and provides the host with the ability to configure
|
|
|
199 |
* attributes to store and retrieve state, <strong>but without support for attribute change events</strong>.
|
|
|
200 |
* </p>
|
|
|
201 |
* <p>For example, attributes added to the host can be configured:</p>
|
|
|
202 |
* <ul>
|
|
|
203 |
* <li>As read only.</li>
|
|
|
204 |
* <li>As write once.</li>
|
|
|
205 |
* <li>With a setter function, which can be used to manipulate
|
|
|
206 |
* values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
|
|
|
207 |
* <li>With a getter function, which can be used to manipulate stored values,
|
|
|
208 |
* before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
|
|
|
209 |
* <li>With a validator function, to validate values before they are stored.</li>
|
|
|
210 |
* </ul>
|
|
|
211 |
*
|
|
|
212 |
* <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
|
|
|
213 |
* options available for attributes.</p>
|
|
|
214 |
*
|
|
|
215 |
* <p>Object/Classes based on AttributeCore can augment <a href="AttributeObservable.html">AttributeObservable</a>
|
|
|
216 |
* (with true for overwrite) and <a href="AttributeExtras.html">AttributeExtras</a> to add attribute event and
|
|
|
217 |
* additional, less commonly used attribute methods, such as `modifyAttr`, `removeAttr` and `reset`.</p>
|
|
|
218 |
*
|
|
|
219 |
* @class AttributeCore
|
|
|
220 |
* @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
221 |
* These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
|
|
|
222 |
* @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
223 |
* These are not merged/cloned. The caller is responsible for isolating user provided values if required.
|
|
|
224 |
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
225 |
*/
|
|
|
226 |
function AttributeCore(attrs, values, lazy) {
|
|
|
227 |
// HACK: Fix #2531929
|
|
|
228 |
// Complete hack, to make sure the first clone of a node value in IE doesn't doesn't hurt state - maintains 3.4.1 behavior.
|
|
|
229 |
// Too late in the release cycle to do anything about the core problem.
|
|
|
230 |
// The root issue is that cloning a Y.Node instance results in an object which barfs in IE, when you access it's properties (since 3.3.0).
|
|
|
231 |
this._yuievt = null;
|
|
|
232 |
|
|
|
233 |
this._initAttrHost(attrs, values, lazy);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
|
|
|
238 |
*
|
|
|
239 |
* <p>You can return this value from your setter if you wish to combine validator and setter
|
|
|
240 |
* functionality into a single setter function, which either returns the massaged value to be stored or
|
|
|
241 |
* AttributeCore.INVALID_VALUE to prevent invalid values from being stored.</p>
|
|
|
242 |
*
|
|
|
243 |
* @property INVALID_VALUE
|
|
|
244 |
* @type Object
|
|
|
245 |
* @static
|
|
|
246 |
* @final
|
|
|
247 |
*/
|
|
|
248 |
AttributeCore.INVALID_VALUE = {};
|
|
|
249 |
INVALID_VALUE = AttributeCore.INVALID_VALUE;
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* The list of properties which can be configured for
|
|
|
253 |
* each attribute (e.g. setter, getter, writeOnce etc.).
|
|
|
254 |
*
|
|
|
255 |
* This property is used internally as a whitelist for faster
|
|
|
256 |
* Y.mix operations.
|
|
|
257 |
*
|
|
|
258 |
* @property _ATTR_CFG
|
|
|
259 |
* @type Array
|
|
|
260 |
* @static
|
|
|
261 |
* @protected
|
|
|
262 |
*/
|
|
|
263 |
AttributeCore._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BYPASS_PROXY];
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Utility method to protect an attribute configuration hash, by merging the
|
|
|
267 |
* entire object and the individual attr config objects.
|
|
|
268 |
*
|
|
|
269 |
* @method protectAttrs
|
|
|
270 |
* @static
|
|
|
271 |
* @param {Object} attrs A hash of attribute to configuration object pairs.
|
|
|
272 |
* @return {Object} A protected version of the `attrs` argument.
|
|
|
273 |
*/
|
|
|
274 |
AttributeCore.protectAttrs = function (attrs) {
|
|
|
275 |
if (attrs) {
|
|
|
276 |
attrs = Y.merge(attrs);
|
|
|
277 |
for (var attr in attrs) {
|
|
|
278 |
if (attrs.hasOwnProperty(attr)) {
|
|
|
279 |
attrs[attr] = Y.merge(attrs[attr]);
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
return attrs;
|
|
|
285 |
};
|
|
|
286 |
|
|
|
287 |
AttributeCore.prototype = {
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Constructor logic for attributes. Initializes the host state, and sets up the inital attributes passed to the
|
|
|
291 |
* constructor.
|
|
|
292 |
*
|
|
|
293 |
* @method _initAttrHost
|
|
|
294 |
* @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
295 |
* These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
|
|
|
296 |
* @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
297 |
* These are not merged/cloned. The caller is responsible for isolating user provided values if required.
|
|
|
298 |
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
299 |
* @private
|
|
|
300 |
*/
|
|
|
301 |
_initAttrHost : function(attrs, values, lazy) {
|
|
|
302 |
this._state = new Y.State();
|
|
|
303 |
this._initAttrs(attrs, values, lazy);
|
|
|
304 |
},
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* <p>
|
|
|
308 |
* Adds an attribute with the provided configuration to the host object.
|
|
|
309 |
* </p>
|
|
|
310 |
* <p>
|
|
|
311 |
* The config argument object supports the following properties:
|
|
|
312 |
* </p>
|
|
|
313 |
*
|
|
|
314 |
* <dl>
|
|
|
315 |
* <dt>value <Any></dt>
|
|
|
316 |
* <dd>The initial value to set on the attribute</dd>
|
|
|
317 |
*
|
|
|
318 |
* <dt>valueFn <Function | String></dt>
|
|
|
319 |
* <dd>
|
|
|
320 |
* <p>A function, which will return the initial value to set on the attribute. This is useful
|
|
|
321 |
* for cases where the attribute configuration is defined statically, but needs to
|
|
|
322 |
* reference the host instance ("this") to obtain an initial value. If both the value and valueFn properties are defined,
|
|
|
323 |
* the value returned by the valueFn has precedence over the value property, unless it returns undefined, in which
|
|
|
324 |
* case the value property is used.</p>
|
|
|
325 |
*
|
|
|
326 |
* <p>valueFn can also be set to a string, representing the name of the instance method to be used to retrieve the value.</p>
|
|
|
327 |
* </dd>
|
|
|
328 |
*
|
|
|
329 |
* <dt>readOnly <boolean></dt>
|
|
|
330 |
* <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
|
|
|
331 |
* cannot be modified by invoking the set method.</dd>
|
|
|
332 |
*
|
|
|
333 |
* <dt>writeOnce <boolean> or <string></dt>
|
|
|
334 |
* <dd>
|
|
|
335 |
* Whether or not the attribute is "write once". Attributes having writeOnce set to true,
|
|
|
336 |
* can only have their values set once, be it through the default configuration,
|
|
|
337 |
* constructor configuration arguments, or by invoking set.
|
|
|
338 |
* <p>The writeOnce attribute can also be set to the string "initOnly",
|
|
|
339 |
* in which case the attribute can only be set during initialization
|
|
|
340 |
* (when used with Base, this means it can only be set during construction)</p>
|
|
|
341 |
* </dd>
|
|
|
342 |
*
|
|
|
343 |
* <dt>setter <Function | String></dt>
|
|
|
344 |
* <dd>
|
|
|
345 |
* <p>The setter function used to massage or normalize the value passed to the set method for the attribute.
|
|
|
346 |
* The value returned by the setter will be the final stored value. Returning
|
|
|
347 |
* <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
|
|
|
348 |
* the value from being stored.
|
|
|
349 |
* </p>
|
|
|
350 |
*
|
|
|
351 |
* <p>setter can also be set to a string, representing the name of the instance method to be used as the setter function.</p>
|
|
|
352 |
* </dd>
|
|
|
353 |
*
|
|
|
354 |
* <dt>getter <Function | String></dt>
|
|
|
355 |
* <dd>
|
|
|
356 |
* <p>
|
|
|
357 |
* The getter function used to massage or normalize the value returned by the get method for the attribute.
|
|
|
358 |
* The value returned by the getter function is the value which will be returned to the user when they
|
|
|
359 |
* invoke get.
|
|
|
360 |
* </p>
|
|
|
361 |
*
|
|
|
362 |
* <p>getter can also be set to a string, representing the name of the instance method to be used as the getter function.</p>
|
|
|
363 |
* </dd>
|
|
|
364 |
*
|
|
|
365 |
* <dt>validator <Function | String></dt>
|
|
|
366 |
* <dd>
|
|
|
367 |
* <p>
|
|
|
368 |
* The validator function invoked prior to setting the stored value. Returning
|
|
|
369 |
* false from the validator function will prevent the value from being stored.
|
|
|
370 |
* </p>
|
|
|
371 |
*
|
|
|
372 |
* <p>validator can also be set to a string, representing the name of the instance method to be used as the validator function.</p>
|
|
|
373 |
* </dd>
|
|
|
374 |
*
|
|
|
375 |
* <dt>lazyAdd <boolean></dt>
|
|
|
376 |
* <dd>Whether or not to delay initialization of the attribute until the first call to get/set it.
|
|
|
377 |
* This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through
|
|
|
378 |
* the <a href="#method_addAttrs">addAttrs</a> method.</dd>
|
|
|
379 |
*
|
|
|
380 |
* </dl>
|
|
|
381 |
*
|
|
|
382 |
* <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
|
|
|
383 |
* the context ("this") set to the host object.</p>
|
|
|
384 |
*
|
|
|
385 |
* <p>Configuration properties outside of the list mentioned above are considered private properties used internally by attribute,
|
|
|
386 |
* and are not intended for public use.</p>
|
|
|
387 |
*
|
|
|
388 |
* @method addAttr
|
|
|
389 |
*
|
|
|
390 |
* @param {String} name The name of the attribute.
|
|
|
391 |
* @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
|
|
|
392 |
*
|
|
|
393 |
* <p>
|
|
|
394 |
* <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need
|
|
|
395 |
* to protect the original values, you will need to merge the object.
|
|
|
396 |
* </p>
|
|
|
397 |
*
|
|
|
398 |
* @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set).
|
|
|
399 |
*
|
|
|
400 |
* @return {Object} A reference to the host object.
|
|
|
401 |
*
|
|
|
402 |
* @chainable
|
|
|
403 |
*/
|
|
|
404 |
addAttr : function(name, config, lazy) {
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
var host = this, // help compression
|
|
|
408 |
state = host._state,
|
|
|
409 |
data = state.data,
|
|
|
410 |
value,
|
|
|
411 |
added,
|
|
|
412 |
hasValue;
|
|
|
413 |
|
|
|
414 |
config = config || {};
|
|
|
415 |
|
|
|
416 |
if (LAZY_ADD in config) {
|
|
|
417 |
lazy = config[LAZY_ADD];
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
added = state.get(name, ADDED);
|
|
|
421 |
|
|
|
422 |
if (lazy && !added) {
|
|
|
423 |
state.data[name] = {
|
|
|
424 |
lazy : config,
|
|
|
425 |
added : true
|
|
|
426 |
};
|
|
|
427 |
} else {
|
|
|
428 |
|
|
|
429 |
|
|
|
430 |
if (!added || config.isLazyAdd) {
|
|
|
431 |
|
|
|
432 |
hasValue = (VALUE in config);
|
|
|
433 |
|
|
|
434 |
|
|
|
435 |
if (hasValue) {
|
|
|
436 |
|
|
|
437 |
// We'll go through set, don't want to set value in config directly
|
|
|
438 |
|
|
|
439 |
// PERF TODO: VALIDATE: See if setting this to undefined is sufficient. We use to delete before.
|
|
|
440 |
// In certain code paths/use cases, undefined may not be the same as not present.
|
|
|
441 |
// If not, we can set it to some known fixed value (like INVALID_VALUE, say INITIALIZING_VALUE) for performance,
|
|
|
442 |
// to avoid a delete which seems to help a lot.
|
|
|
443 |
|
|
|
444 |
value = config.value;
|
|
|
445 |
config.value = undefined;
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
config.added = true;
|
|
|
449 |
config.initializing = true;
|
|
|
450 |
|
|
|
451 |
data[name] = config;
|
|
|
452 |
|
|
|
453 |
if (hasValue) {
|
|
|
454 |
// Go through set, so that raw values get normalized/validated
|
|
|
455 |
host.set(name, value);
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
config.initializing = false;
|
|
|
459 |
}
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
return host;
|
|
|
463 |
},
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* Checks if the given attribute has been added to the host
|
|
|
467 |
*
|
|
|
468 |
* @method attrAdded
|
|
|
469 |
* @param {String} name The name of the attribute to check.
|
|
|
470 |
* @return {boolean} true if an attribute with the given name has been added, false if it hasn't.
|
|
|
471 |
* This method will return true for lazily added attributes.
|
|
|
472 |
*/
|
|
|
473 |
attrAdded: function(name) {
|
|
|
474 |
return !!(this._state.get(name, ADDED));
|
|
|
475 |
},
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* Returns the current value of the attribute. If the attribute
|
|
|
479 |
* has been configured with a 'getter' function, this method will delegate
|
|
|
480 |
* to the 'getter' to obtain the value of the attribute.
|
|
|
481 |
*
|
|
|
482 |
* @method get
|
|
|
483 |
*
|
|
|
484 |
* @param {String} name The name of the attribute. If the value of the attribute is an Object,
|
|
|
485 |
* dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
|
|
|
486 |
*
|
|
|
487 |
* @return {Any} The value of the attribute
|
|
|
488 |
*/
|
|
|
489 |
get : function(name) {
|
|
|
490 |
return this._getAttr(name);
|
|
|
491 |
},
|
|
|
492 |
|
|
|
493 |
/**
|
|
|
494 |
* Checks whether or not the attribute is one which has been
|
|
|
495 |
* added lazily and still requires initialization.
|
|
|
496 |
*
|
|
|
497 |
* @method _isLazyAttr
|
|
|
498 |
* @private
|
|
|
499 |
* @param {String} name The name of the attribute
|
|
|
500 |
* @return {boolean} true if it's a lazily added attribute, false otherwise.
|
|
|
501 |
*/
|
|
|
502 |
_isLazyAttr: function(name) {
|
|
|
503 |
return this._state.get(name, LAZY);
|
|
|
504 |
},
|
|
|
505 |
|
|
|
506 |
/**
|
|
|
507 |
* Finishes initializing an attribute which has been lazily added.
|
|
|
508 |
*
|
|
|
509 |
* @method _addLazyAttr
|
|
|
510 |
* @private
|
|
|
511 |
* @param {Object} name The name of the attribute
|
|
|
512 |
* @param {Object} [lazyCfg] Optional config hash for the attribute. This is added for performance
|
|
|
513 |
* along the critical path, where the calling method has already obtained lazy config from state.
|
|
|
514 |
*/
|
|
|
515 |
_addLazyAttr: function(name, lazyCfg) {
|
|
|
516 |
var state = this._state;
|
|
|
517 |
|
|
|
518 |
lazyCfg = lazyCfg || state.get(name, LAZY);
|
|
|
519 |
|
|
|
520 |
if (lazyCfg) {
|
|
|
521 |
|
|
|
522 |
// PERF TODO: For App's id override, otherwise wouldn't be
|
|
|
523 |
// needed. It expects to find it in the cfg for it's
|
|
|
524 |
// addAttr override. Would like to remove, once App override is
|
|
|
525 |
// removed.
|
|
|
526 |
state.data[name].lazy = undefined;
|
|
|
527 |
|
|
|
528 |
lazyCfg.isLazyAdd = true;
|
|
|
529 |
|
|
|
530 |
this.addAttr(name, lazyCfg);
|
|
|
531 |
}
|
|
|
532 |
},
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* Sets the value of an attribute.
|
|
|
536 |
*
|
|
|
537 |
* @method set
|
|
|
538 |
* @chainable
|
|
|
539 |
*
|
|
|
540 |
* @param {String} name The name of the attribute. If the
|
|
|
541 |
* current value of the attribute is an Object, dot notation can be used
|
|
|
542 |
* to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
|
|
|
543 |
* @param {Any} value The value to set the attribute to.
|
|
|
544 |
* @param {Object} [opts] Optional data providing the circumstances for the change.
|
|
|
545 |
* @return {Object} A reference to the host object.
|
|
|
546 |
*/
|
|
|
547 |
set : function(name, val, opts) {
|
|
|
548 |
return this._setAttr(name, val, opts);
|
|
|
549 |
},
|
|
|
550 |
|
|
|
551 |
/**
|
|
|
552 |
* Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
|
|
|
553 |
*
|
|
|
554 |
* @method _set
|
|
|
555 |
* @protected
|
|
|
556 |
* @chainable
|
|
|
557 |
*
|
|
|
558 |
* @param {String} name The name of the attribute.
|
|
|
559 |
* @param {Any} val The value to set the attribute to.
|
|
|
560 |
* @param {Object} [opts] Optional data providing the circumstances for the change.
|
|
|
561 |
* @return {Object} A reference to the host object.
|
|
|
562 |
*/
|
|
|
563 |
_set : function(name, val, opts) {
|
|
|
564 |
return this._setAttr(name, val, opts, true);
|
|
|
565 |
},
|
|
|
566 |
|
|
|
567 |
/**
|
|
|
568 |
* Provides the common implementation for the public set and protected _set methods.
|
|
|
569 |
*
|
|
|
570 |
* See <a href="#method_set">set</a> for argument details.
|
|
|
571 |
*
|
|
|
572 |
* @method _setAttr
|
|
|
573 |
* @protected
|
|
|
574 |
* @chainable
|
|
|
575 |
*
|
|
|
576 |
* @param {String} name The name of the attribute.
|
|
|
577 |
* @param {Any} value The value to set the attribute to.
|
|
|
578 |
* @param {Object} [opts] Optional data providing the circumstances for the change.
|
|
|
579 |
* @param {boolean} force If true, allows the caller to set values for
|
|
|
580 |
* readOnly or writeOnce attributes which have already been set.
|
|
|
581 |
*
|
|
|
582 |
* @return {Object} A reference to the host object.
|
|
|
583 |
*/
|
|
|
584 |
_setAttr : function(name, val, opts, force) {
|
|
|
585 |
var allowSet = true,
|
|
|
586 |
state = this._state,
|
|
|
587 |
stateProxy = this._stateProxy,
|
|
|
588 |
tCfgs = this._tCfgs,
|
|
|
589 |
cfg,
|
|
|
590 |
initialSet,
|
|
|
591 |
strPath,
|
|
|
592 |
path,
|
|
|
593 |
currVal,
|
|
|
594 |
writeOnce,
|
|
|
595 |
initializing;
|
|
|
596 |
|
|
|
597 |
if (name.indexOf(DOT) !== -1) {
|
|
|
598 |
strPath = name;
|
|
|
599 |
|
|
|
600 |
path = name.split(DOT);
|
|
|
601 |
name = path.shift();
|
|
|
602 |
}
|
|
|
603 |
|
|
|
604 |
// On Demand - Should be rare - handles out of order valueFn, setter, getter references
|
|
|
605 |
if (tCfgs && tCfgs[name]) {
|
|
|
606 |
this._addOutOfOrder(name, tCfgs[name]);
|
|
|
607 |
}
|
|
|
608 |
|
|
|
609 |
cfg = state.data[name] || {};
|
|
|
610 |
|
|
|
611 |
if (cfg.lazy) {
|
|
|
612 |
cfg = cfg.lazy;
|
|
|
613 |
this._addLazyAttr(name, cfg);
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
initialSet = (cfg.value === undefined);
|
|
|
617 |
|
|
|
618 |
if (stateProxy && name in stateProxy && !cfg._bypassProxy) {
|
|
|
619 |
// TODO: Value is always set for proxy. Can we do any better? Maybe take a snapshot as the initial value for the first call to set?
|
|
|
620 |
initialSet = false;
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
writeOnce = cfg.writeOnce;
|
|
|
624 |
initializing = cfg.initializing;
|
|
|
625 |
|
|
|
626 |
if (!initialSet && !force) {
|
|
|
627 |
|
|
|
628 |
if (writeOnce) {
|
|
|
629 |
allowSet = false;
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
if (cfg.readOnly) {
|
|
|
633 |
allowSet = false;
|
|
|
634 |
}
|
|
|
635 |
}
|
|
|
636 |
|
|
|
637 |
if (!initializing && !force && writeOnce === INIT_ONLY) {
|
|
|
638 |
allowSet = false;
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
if (allowSet) {
|
|
|
642 |
// Don't need currVal if initialSet (might fail in custom getter if it always expects a non-undefined/non-null value)
|
|
|
643 |
if (!initialSet) {
|
|
|
644 |
currVal = this.get(name);
|
|
|
645 |
}
|
|
|
646 |
|
|
|
647 |
if (path) {
|
|
|
648 |
val = O.setValue(Y.clone(currVal), path, val);
|
|
|
649 |
|
|
|
650 |
if (val === undefined) {
|
|
|
651 |
allowSet = false;
|
|
|
652 |
}
|
|
|
653 |
}
|
|
|
654 |
|
|
|
655 |
if (allowSet) {
|
|
|
656 |
if (!this._fireAttrChange || initializing) {
|
|
|
657 |
this._setAttrVal(name, strPath, currVal, val, opts, cfg);
|
|
|
658 |
} else {
|
|
|
659 |
// HACK - no real reason core needs to know about _fireAttrChange, but
|
|
|
660 |
// it adds fn hops if we want to break it out. Not sure it's worth it for this critical path
|
|
|
661 |
this._fireAttrChange(name, strPath, currVal, val, opts, cfg);
|
|
|
662 |
}
|
|
|
663 |
}
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
return this;
|
|
|
667 |
},
|
|
|
668 |
|
|
|
669 |
/**
|
|
|
670 |
* Utility method used by get/set to add attributes
|
|
|
671 |
* encountered out of order when calling addAttrs().
|
|
|
672 |
*
|
|
|
673 |
* For example, if:
|
|
|
674 |
*
|
|
|
675 |
* this.addAttrs({
|
|
|
676 |
* foo: {
|
|
|
677 |
* setter: function() {
|
|
|
678 |
* // make sure this bar is available when foo is added
|
|
|
679 |
* this.get("bar");
|
|
|
680 |
* }
|
|
|
681 |
* },
|
|
|
682 |
* bar: {
|
|
|
683 |
* value: ...
|
|
|
684 |
* }
|
|
|
685 |
* });
|
|
|
686 |
*
|
|
|
687 |
* @method _addOutOfOrder
|
|
|
688 |
* @private
|
|
|
689 |
* @param name {String} attribute name
|
|
|
690 |
* @param cfg {Object} attribute configuration
|
|
|
691 |
*/
|
|
|
692 |
_addOutOfOrder : function(name, cfg) {
|
|
|
693 |
|
|
|
694 |
var attrs = {};
|
|
|
695 |
attrs[name] = cfg;
|
|
|
696 |
|
|
|
697 |
delete this._tCfgs[name];
|
|
|
698 |
|
|
|
699 |
// TODO: The original code went through addAttrs, so
|
|
|
700 |
// sticking with it for this pass. Seems like we could
|
|
|
701 |
// just jump straight to _addAttr() and get some perf
|
|
|
702 |
// improvement.
|
|
|
703 |
this._addAttrs(attrs, this._tVals);
|
|
|
704 |
},
|
|
|
705 |
|
|
|
706 |
/**
|
|
|
707 |
* Provides the common implementation for the public get method,
|
|
|
708 |
* allowing Attribute hosts to over-ride either method.
|
|
|
709 |
*
|
|
|
710 |
* See <a href="#method_get">get</a> for argument details.
|
|
|
711 |
*
|
|
|
712 |
* @method _getAttr
|
|
|
713 |
* @protected
|
|
|
714 |
* @chainable
|
|
|
715 |
*
|
|
|
716 |
* @param {String} name The name of the attribute.
|
|
|
717 |
* @return {Any} The value of the attribute.
|
|
|
718 |
*/
|
|
|
719 |
_getAttr : function(name) {
|
|
|
720 |
var fullName = name,
|
|
|
721 |
tCfgs = this._tCfgs,
|
|
|
722 |
path,
|
|
|
723 |
getter,
|
|
|
724 |
val,
|
|
|
725 |
attrCfg;
|
|
|
726 |
|
|
|
727 |
if (name.indexOf(DOT) !== -1) {
|
|
|
728 |
path = name.split(DOT);
|
|
|
729 |
name = path.shift();
|
|
|
730 |
}
|
|
|
731 |
|
|
|
732 |
// On Demand - Should be rare - handles out of
|
|
|
733 |
// order valueFn, setter, getter references
|
|
|
734 |
if (tCfgs && tCfgs[name]) {
|
|
|
735 |
this._addOutOfOrder(name, tCfgs[name]);
|
|
|
736 |
}
|
|
|
737 |
|
|
|
738 |
attrCfg = this._state.data[name] || {};
|
|
|
739 |
|
|
|
740 |
// Lazy Init
|
|
|
741 |
if (attrCfg.lazy) {
|
|
|
742 |
attrCfg = attrCfg.lazy;
|
|
|
743 |
this._addLazyAttr(name, attrCfg);
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
val = this._getStateVal(name, attrCfg);
|
|
|
747 |
|
|
|
748 |
getter = attrCfg.getter;
|
|
|
749 |
|
|
|
750 |
if (getter && !getter.call) {
|
|
|
751 |
getter = this[getter];
|
|
|
752 |
}
|
|
|
753 |
|
|
|
754 |
val = (getter) ? getter.call(this, val, fullName) : val;
|
|
|
755 |
val = (path) ? O.getValue(val, path) : val;
|
|
|
756 |
|
|
|
757 |
return val;
|
|
|
758 |
},
|
|
|
759 |
|
|
|
760 |
/**
|
|
|
761 |
* Gets the stored value for the attribute, from either the
|
|
|
762 |
* internal state object, or the state proxy if it exits
|
|
|
763 |
*
|
|
|
764 |
* @method _getStateVal
|
|
|
765 |
* @private
|
|
|
766 |
* @param {String} name The name of the attribute
|
|
|
767 |
* @param {Object} [cfg] Optional config hash for the attribute. This is added for performance along the critical path,
|
|
|
768 |
* where the calling method has already obtained the config from state.
|
|
|
769 |
*
|
|
|
770 |
* @return {Any} The stored value of the attribute
|
|
|
771 |
*/
|
|
|
772 |
_getStateVal : function(name, cfg) {
|
|
|
773 |
var stateProxy = this._stateProxy;
|
|
|
774 |
|
|
|
775 |
if (!cfg) {
|
|
|
776 |
cfg = this._state.getAll(name) || {};
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
return (stateProxy && (name in stateProxy) && !(cfg._bypassProxy)) ? stateProxy[name] : cfg.value;
|
|
|
780 |
},
|
|
|
781 |
|
|
|
782 |
/**
|
|
|
783 |
* Sets the stored value for the attribute, in either the
|
|
|
784 |
* internal state object, or the state proxy if it exits
|
|
|
785 |
*
|
|
|
786 |
* @method _setStateVal
|
|
|
787 |
* @private
|
|
|
788 |
* @param {String} name The name of the attribute
|
|
|
789 |
* @param {Any} value The value of the attribute
|
|
|
790 |
*/
|
|
|
791 |
_setStateVal : function(name, value) {
|
|
|
792 |
var stateProxy = this._stateProxy;
|
|
|
793 |
if (stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY)) {
|
|
|
794 |
stateProxy[name] = value;
|
|
|
795 |
} else {
|
|
|
796 |
this._state.add(name, VALUE, value);
|
|
|
797 |
}
|
|
|
798 |
},
|
|
|
799 |
|
|
|
800 |
/**
|
|
|
801 |
* Updates the stored value of the attribute in the privately held State object,
|
|
|
802 |
* if validation and setter passes.
|
|
|
803 |
*
|
|
|
804 |
* @method _setAttrVal
|
|
|
805 |
* @private
|
|
|
806 |
* @param {String} attrName The attribute name.
|
|
|
807 |
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
|
|
|
808 |
* @param {Any} prevVal The currently stored value of the attribute.
|
|
|
809 |
* @param {Any} newVal The value which is going to be stored.
|
|
|
810 |
* @param {Object} [opts] Optional data providing the circumstances for the change.
|
|
|
811 |
* @param {Object} [attrCfg] Optional config hash for the attribute. This is added for performance along the critical path,
|
|
|
812 |
* where the calling method has already obtained the config from state.
|
|
|
813 |
*
|
|
|
814 |
* @return {Boolean} true if the new attribute value was stored, false if not.
|
|
|
815 |
*/
|
|
|
816 |
_setAttrVal : function(attrName, subAttrName, prevVal, newVal, opts, attrCfg) {
|
|
|
817 |
|
|
|
818 |
var host = this,
|
|
|
819 |
allowSet = true,
|
|
|
820 |
cfg = attrCfg || this._state.data[attrName] || {},
|
|
|
821 |
validator = cfg.validator,
|
|
|
822 |
setter = cfg.setter,
|
|
|
823 |
initializing = cfg.initializing,
|
|
|
824 |
prevRawVal = this._getStateVal(attrName, cfg),
|
|
|
825 |
name = subAttrName || attrName,
|
|
|
826 |
retVal,
|
|
|
827 |
valid;
|
|
|
828 |
|
|
|
829 |
if (validator) {
|
|
|
830 |
if (!validator.call) {
|
|
|
831 |
// Assume string - trying to keep critical path tight, so avoiding Lang check
|
|
|
832 |
validator = this[validator];
|
|
|
833 |
}
|
|
|
834 |
if (validator) {
|
|
|
835 |
valid = validator.call(host, newVal, name, opts);
|
|
|
836 |
|
|
|
837 |
if (!valid && initializing) {
|
|
|
838 |
newVal = cfg.defaultValue;
|
|
|
839 |
valid = true; // Assume it's valid, for perf.
|
|
|
840 |
}
|
|
|
841 |
}
|
|
|
842 |
}
|
|
|
843 |
|
|
|
844 |
if (!validator || valid) {
|
|
|
845 |
if (setter) {
|
|
|
846 |
if (!setter.call) {
|
|
|
847 |
// Assume string - trying to keep critical path tight, so avoiding Lang check
|
|
|
848 |
setter = this[setter];
|
|
|
849 |
}
|
|
|
850 |
if (setter) {
|
|
|
851 |
retVal = setter.call(host, newVal, name, opts);
|
|
|
852 |
|
|
|
853 |
if (retVal === INVALID_VALUE) {
|
|
|
854 |
if (initializing) {
|
|
|
855 |
newVal = cfg.defaultValue;
|
|
|
856 |
} else {
|
|
|
857 |
allowSet = false;
|
|
|
858 |
}
|
|
|
859 |
} else if (retVal !== undefined){
|
|
|
860 |
newVal = retVal;
|
|
|
861 |
}
|
|
|
862 |
}
|
|
|
863 |
}
|
|
|
864 |
|
|
|
865 |
if (allowSet) {
|
|
|
866 |
if(!subAttrName && (newVal === prevRawVal) && !Lang.isObject(newVal)) {
|
|
|
867 |
allowSet = false;
|
|
|
868 |
} else {
|
|
|
869 |
// Store value
|
|
|
870 |
if (!(INIT_VALUE in cfg)) {
|
|
|
871 |
cfg.initValue = newVal;
|
|
|
872 |
}
|
|
|
873 |
host._setStateVal(attrName, newVal);
|
|
|
874 |
}
|
|
|
875 |
}
|
|
|
876 |
|
|
|
877 |
} else {
|
|
|
878 |
allowSet = false;
|
|
|
879 |
}
|
|
|
880 |
|
|
|
881 |
return allowSet;
|
|
|
882 |
},
|
|
|
883 |
|
|
|
884 |
/**
|
|
|
885 |
* Sets multiple attribute values.
|
|
|
886 |
*
|
|
|
887 |
* @method setAttrs
|
|
|
888 |
* @param {Object} attrs An object with attributes name/value pairs.
|
|
|
889 |
* @param {Object} [opts] Optional data providing the circumstances for the change.
|
|
|
890 |
* @return {Object} A reference to the host object.
|
|
|
891 |
* @chainable
|
|
|
892 |
*/
|
|
|
893 |
setAttrs : function(attrs, opts) {
|
|
|
894 |
return this._setAttrs(attrs, opts);
|
|
|
895 |
},
|
|
|
896 |
|
|
|
897 |
/**
|
|
|
898 |
* Implementation behind the public setAttrs method, to set multiple attribute values.
|
|
|
899 |
*
|
|
|
900 |
* @method _setAttrs
|
|
|
901 |
* @protected
|
|
|
902 |
* @param {Object} attrs An object with attributes name/value pairs.
|
|
|
903 |
* @param {Object} [opts] Optional data providing the circumstances for the change
|
|
|
904 |
* @return {Object} A reference to the host object.
|
|
|
905 |
* @chainable
|
|
|
906 |
*/
|
|
|
907 |
_setAttrs : function(attrs, opts) {
|
|
|
908 |
var attr;
|
|
|
909 |
for (attr in attrs) {
|
|
|
910 |
if ( attrs.hasOwnProperty(attr) ) {
|
|
|
911 |
this.set(attr, attrs[attr], opts);
|
|
|
912 |
}
|
|
|
913 |
}
|
|
|
914 |
return this;
|
|
|
915 |
},
|
|
|
916 |
|
|
|
917 |
/**
|
|
|
918 |
* Gets multiple attribute values.
|
|
|
919 |
*
|
|
|
920 |
* @method getAttrs
|
|
|
921 |
* @param {String[]|Boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
|
|
|
922 |
* returned. If set to true, all attributes modified from their initial values are returned.
|
|
|
923 |
* @return {Object} An object with attribute name/value pairs.
|
|
|
924 |
*/
|
|
|
925 |
getAttrs : function(attrs) {
|
|
|
926 |
return this._getAttrs(attrs);
|
|
|
927 |
},
|
|
|
928 |
|
|
|
929 |
/**
|
|
|
930 |
* Implementation behind the public getAttrs method, to get multiple attribute values.
|
|
|
931 |
*
|
|
|
932 |
* @method _getAttrs
|
|
|
933 |
* @protected
|
|
|
934 |
* @param {String[]|Boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
|
|
|
935 |
* returned. If set to true, all attributes modified from their initial values are returned.
|
|
|
936 |
* @return {Object} An object with attribute name/value pairs.
|
|
|
937 |
*/
|
|
|
938 |
_getAttrs : function(attrs) {
|
|
|
939 |
var obj = {},
|
|
|
940 |
attr, i, len,
|
|
|
941 |
modifiedOnly = (attrs === true);
|
|
|
942 |
|
|
|
943 |
// TODO - figure out how to get all "added"
|
|
|
944 |
if (!attrs || modifiedOnly) {
|
|
|
945 |
attrs = O.keys(this._state.data);
|
|
|
946 |
}
|
|
|
947 |
|
|
|
948 |
for (i = 0, len = attrs.length; i < len; i++) {
|
|
|
949 |
attr = attrs[i];
|
|
|
950 |
|
|
|
951 |
if (!modifiedOnly || this._getStateVal(attr) != this._state.get(attr, INIT_VALUE)) {
|
|
|
952 |
// Go through get, to honor cloning/normalization
|
|
|
953 |
obj[attr] = this.get(attr);
|
|
|
954 |
}
|
|
|
955 |
}
|
|
|
956 |
|
|
|
957 |
return obj;
|
|
|
958 |
},
|
|
|
959 |
|
|
|
960 |
/**
|
|
|
961 |
* Configures a group of attributes, and sets initial values.
|
|
|
962 |
*
|
|
|
963 |
* <p>
|
|
|
964 |
* <strong>NOTE:</strong> This method does not isolate the configuration object by merging/cloning.
|
|
|
965 |
* The caller is responsible for merging/cloning the configuration object if required.
|
|
|
966 |
* </p>
|
|
|
967 |
*
|
|
|
968 |
* @method addAttrs
|
|
|
969 |
* @chainable
|
|
|
970 |
*
|
|
|
971 |
* @param {Object} cfgs An object with attribute name/configuration pairs.
|
|
|
972 |
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
|
|
|
973 |
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
|
|
|
974 |
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
|
|
|
975 |
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
|
|
|
976 |
* See <a href="#method_addAttr">addAttr</a>.
|
|
|
977 |
*
|
|
|
978 |
* @return {Object} A reference to the host object.
|
|
|
979 |
*/
|
|
|
980 |
addAttrs : function(cfgs, values, lazy) {
|
|
|
981 |
if (cfgs) {
|
|
|
982 |
this._tCfgs = cfgs;
|
|
|
983 |
this._tVals = (values) ? this._normAttrVals(values) : null;
|
|
|
984 |
this._addAttrs(cfgs, this._tVals, lazy);
|
|
|
985 |
this._tCfgs = this._tVals = null;
|
|
|
986 |
}
|
|
|
987 |
|
|
|
988 |
return this;
|
|
|
989 |
},
|
|
|
990 |
|
|
|
991 |
/**
|
|
|
992 |
* Implementation behind the public addAttrs method.
|
|
|
993 |
*
|
|
|
994 |
* This method is invoked directly by get if it encounters a scenario
|
|
|
995 |
* in which an attribute's valueFn attempts to obtain the
|
|
|
996 |
* value an attribute in the same group of attributes, which has not yet
|
|
|
997 |
* been added (on demand initialization).
|
|
|
998 |
*
|
|
|
999 |
* @method _addAttrs
|
|
|
1000 |
* @private
|
|
|
1001 |
* @param {Object} cfgs An object with attribute name/configuration pairs.
|
|
|
1002 |
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
|
|
|
1003 |
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
|
|
|
1004 |
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
|
|
|
1005 |
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
|
|
|
1006 |
* See <a href="#method_addAttr">addAttr</a>.
|
|
|
1007 |
*/
|
|
|
1008 |
_addAttrs : function(cfgs, values, lazy) {
|
|
|
1009 |
var tCfgs = this._tCfgs,
|
|
|
1010 |
tVals = this._tVals,
|
|
|
1011 |
attr,
|
|
|
1012 |
attrCfg,
|
|
|
1013 |
value;
|
|
|
1014 |
|
|
|
1015 |
for (attr in cfgs) {
|
|
|
1016 |
if (cfgs.hasOwnProperty(attr)) {
|
|
|
1017 |
|
|
|
1018 |
// Not Merging. Caller is responsible for isolating configs
|
|
|
1019 |
attrCfg = cfgs[attr];
|
|
|
1020 |
attrCfg.defaultValue = attrCfg.value;
|
|
|
1021 |
|
|
|
1022 |
// Handle simple, complex and user values, accounting for read-only
|
|
|
1023 |
value = this._getAttrInitVal(attr, attrCfg, tVals);
|
|
|
1024 |
|
|
|
1025 |
if (value !== undefined) {
|
|
|
1026 |
attrCfg.value = value;
|
|
|
1027 |
}
|
|
|
1028 |
|
|
|
1029 |
if (tCfgs[attr]) {
|
|
|
1030 |
tCfgs[attr] = undefined;
|
|
|
1031 |
}
|
|
|
1032 |
|
|
|
1033 |
this.addAttr(attr, attrCfg, lazy);
|
|
|
1034 |
}
|
|
|
1035 |
}
|
|
|
1036 |
},
|
|
|
1037 |
|
|
|
1038 |
/**
|
|
|
1039 |
* Utility method to protect an attribute configuration
|
|
|
1040 |
* hash, by merging the entire object and the individual
|
|
|
1041 |
* attr config objects.
|
|
|
1042 |
*
|
|
|
1043 |
* @method _protectAttrs
|
|
|
1044 |
* @protected
|
|
|
1045 |
* @param {Object} attrs A hash of attribute to configuration object pairs.
|
|
|
1046 |
* @return {Object} A protected version of the attrs argument.
|
|
|
1047 |
* @deprecated Use `AttributeCore.protectAttrs()` or
|
|
|
1048 |
* `Attribute.protectAttrs()` which are the same static utility method.
|
|
|
1049 |
*/
|
|
|
1050 |
_protectAttrs : AttributeCore.protectAttrs,
|
|
|
1051 |
|
|
|
1052 |
/**
|
|
|
1053 |
* Utility method to normalize attribute values. The base implementation
|
|
|
1054 |
* simply merges the hash to protect the original.
|
|
|
1055 |
*
|
|
|
1056 |
* @method _normAttrVals
|
|
|
1057 |
* @param {Object} valueHash An object with attribute name/value pairs
|
|
|
1058 |
*
|
|
|
1059 |
* @return {Object} An object literal with 2 properties - "simple" and "complex",
|
|
|
1060 |
* containing simple and complex attribute values respectively keyed
|
|
|
1061 |
* by the top level attribute name, or null, if valueHash is falsey.
|
|
|
1062 |
*
|
|
|
1063 |
* @private
|
|
|
1064 |
*/
|
|
|
1065 |
_normAttrVals : function(valueHash) {
|
|
|
1066 |
var vals,
|
|
|
1067 |
subvals,
|
|
|
1068 |
path,
|
|
|
1069 |
attr,
|
|
|
1070 |
v, k;
|
|
|
1071 |
|
|
|
1072 |
if (!valueHash) {
|
|
|
1073 |
return null;
|
|
|
1074 |
}
|
|
|
1075 |
|
|
|
1076 |
vals = {};
|
|
|
1077 |
|
|
|
1078 |
for (k in valueHash) {
|
|
|
1079 |
if (valueHash.hasOwnProperty(k)) {
|
|
|
1080 |
if (k.indexOf(DOT) !== -1) {
|
|
|
1081 |
path = k.split(DOT);
|
|
|
1082 |
attr = path.shift();
|
|
|
1083 |
|
|
|
1084 |
subvals = subvals || {};
|
|
|
1085 |
|
|
|
1086 |
v = subvals[attr] = subvals[attr] || [];
|
|
|
1087 |
v[v.length] = {
|
|
|
1088 |
path : path,
|
|
|
1089 |
value: valueHash[k]
|
|
|
1090 |
};
|
|
|
1091 |
} else {
|
|
|
1092 |
vals[k] = valueHash[k];
|
|
|
1093 |
}
|
|
|
1094 |
}
|
|
|
1095 |
}
|
|
|
1096 |
|
|
|
1097 |
return { simple:vals, complex:subvals };
|
|
|
1098 |
},
|
|
|
1099 |
|
|
|
1100 |
/**
|
|
|
1101 |
* Returns the initial value of the given attribute from
|
|
|
1102 |
* either the default configuration provided, or the
|
|
|
1103 |
* over-ridden value if it exists in the set of initValues
|
|
|
1104 |
* provided and the attribute is not read-only.
|
|
|
1105 |
*
|
|
|
1106 |
* @param {String} attr The name of the attribute
|
|
|
1107 |
* @param {Object} cfg The attribute configuration object
|
|
|
1108 |
* @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
|
|
|
1109 |
*
|
|
|
1110 |
* @return {Any} The initial value of the attribute.
|
|
|
1111 |
*
|
|
|
1112 |
* @method _getAttrInitVal
|
|
|
1113 |
* @private
|
|
|
1114 |
*/
|
|
|
1115 |
_getAttrInitVal : function(attr, cfg, initValues) {
|
|
|
1116 |
var val = cfg.value,
|
|
|
1117 |
valFn = cfg.valueFn,
|
|
|
1118 |
tmpVal,
|
|
|
1119 |
initValSet = false,
|
|
|
1120 |
readOnly = cfg.readOnly,
|
|
|
1121 |
simple,
|
|
|
1122 |
complex,
|
|
|
1123 |
i,
|
|
|
1124 |
l,
|
|
|
1125 |
path,
|
|
|
1126 |
subval,
|
|
|
1127 |
subvals;
|
|
|
1128 |
|
|
|
1129 |
if (!readOnly && initValues) {
|
|
|
1130 |
// Simple Attributes
|
|
|
1131 |
simple = initValues.simple;
|
|
|
1132 |
if (simple && simple.hasOwnProperty(attr)) {
|
|
|
1133 |
val = simple[attr];
|
|
|
1134 |
initValSet = true;
|
|
|
1135 |
}
|
|
|
1136 |
}
|
|
|
1137 |
|
|
|
1138 |
if (valFn && !initValSet) {
|
|
|
1139 |
if (!valFn.call) {
|
|
|
1140 |
valFn = this[valFn];
|
|
|
1141 |
}
|
|
|
1142 |
if (valFn) {
|
|
|
1143 |
tmpVal = valFn.call(this, attr);
|
|
|
1144 |
val = tmpVal;
|
|
|
1145 |
}
|
|
|
1146 |
}
|
|
|
1147 |
|
|
|
1148 |
if (!readOnly && initValues) {
|
|
|
1149 |
|
|
|
1150 |
// Complex Attributes (complex values applied, after simple, in case both are set)
|
|
|
1151 |
complex = initValues.complex;
|
|
|
1152 |
|
|
|
1153 |
if (complex && complex.hasOwnProperty(attr) && (val !== undefined) && (val !== null)) {
|
|
|
1154 |
subvals = complex[attr];
|
|
|
1155 |
for (i = 0, l = subvals.length; i < l; ++i) {
|
|
|
1156 |
path = subvals[i].path;
|
|
|
1157 |
subval = subvals[i].value;
|
|
|
1158 |
O.setValue(val, path, subval);
|
|
|
1159 |
}
|
|
|
1160 |
}
|
|
|
1161 |
}
|
|
|
1162 |
|
|
|
1163 |
return val;
|
|
|
1164 |
},
|
|
|
1165 |
|
|
|
1166 |
/**
|
|
|
1167 |
* Utility method to set up initial attributes defined during construction,
|
|
|
1168 |
* either through the constructor.ATTRS property, or explicitly passed in.
|
|
|
1169 |
*
|
|
|
1170 |
* @method _initAttrs
|
|
|
1171 |
* @protected
|
|
|
1172 |
* @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
1173 |
* These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
|
|
|
1174 |
* @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
1175 |
* These are not merged/cloned. The caller is responsible for isolating user provided values if required.
|
|
|
1176 |
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
|
|
|
1177 |
*/
|
|
|
1178 |
_initAttrs : function(attrs, values, lazy) {
|
|
|
1179 |
// ATTRS support for Node, which is not Base based
|
|
|
1180 |
attrs = attrs || this.constructor.ATTRS;
|
|
|
1181 |
|
|
|
1182 |
var Base = Y.Base,
|
|
|
1183 |
BaseCore = Y.BaseCore,
|
|
|
1184 |
baseInst = (Base && Y.instanceOf(this, Base)),
|
|
|
1185 |
baseCoreInst = (!baseInst && BaseCore && Y.instanceOf(this, BaseCore));
|
|
|
1186 |
|
|
|
1187 |
if (attrs && !baseInst && !baseCoreInst) {
|
|
|
1188 |
this.addAttrs(Y.AttributeCore.protectAttrs(attrs), values, lazy);
|
|
|
1189 |
}
|
|
|
1190 |
}
|
|
|
1191 |
};
|
|
|
1192 |
|
|
|
1193 |
Y.AttributeCore = AttributeCore;
|
|
|
1194 |
|
|
|
1195 |
|
|
|
1196 |
}, '3.18.1', {"requires": ["oop"]});
|