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