|
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.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('attribute', 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 /** |
|
130 * The attribute module provides an augmentable Attribute implementation, which |
|
131 * adds configurable attributes and attribute change events to the class being |
|
132 * augmented. It also provides a State class, which is used internally by Attribute, |
|
133 * but can also be used independently to provide a name/property/value data structure to |
|
134 * store state. |
|
135 * |
|
136 * @module attribute |
|
137 */ |
|
138 |
|
139 var O = Y.Object, |
|
140 EventTarget = Y.EventTarget, |
|
141 |
|
142 DOT = ".", |
|
143 CHANGE = "Change", |
|
144 |
|
145 // Externally configurable props |
|
146 GETTER = "getter", |
|
147 SETTER = "setter", |
|
148 READ_ONLY = "readOnly", |
|
149 WRITE_ONCE = "writeOnce", |
|
150 VALIDATOR = "validator", |
|
151 VALUE = "value", |
|
152 VALUE_FN = "valueFn", |
|
153 BROADCAST = "broadcast", |
|
154 LAZY_ADD = "lazyAdd", |
|
155 |
|
156 // Used for internal state management |
|
157 ADDED = "added", |
|
158 INITIALIZING = "initializing", |
|
159 INIT_VALUE = "initValue", |
|
160 PUBLISHED = "published", |
|
161 DEF_VALUE = "defaultValue", |
|
162 LAZY = "lazy", |
|
163 IS_LAZY_ADD = "isLazyAdd", |
|
164 |
|
165 INVALID_VALUE, |
|
166 MODIFIABLE = {}; |
|
167 |
|
168 // Properties which can be changed after the attribute has been added. |
|
169 MODIFIABLE[READ_ONLY] = 1; |
|
170 MODIFIABLE[WRITE_ONCE] = 1; |
|
171 MODIFIABLE[GETTER] = 1; |
|
172 MODIFIABLE[BROADCAST] = 1; |
|
173 |
|
174 /** |
|
175 * <p> |
|
176 * Attribute provides configurable attribute support along with attribute change events. It is designed to be |
|
177 * augmented onto a host class, and provides the host with the ability to configure attributes to store and retrieve state, |
|
178 * along with attribute change events. |
|
179 * </p> |
|
180 * <p>For example, attributes added to the host can be configured:</p> |
|
181 * <ul> |
|
182 * <li>As read only.</li> |
|
183 * <li>As write once.</li> |
|
184 * <li>With a setter function, which can be used to manipulate |
|
185 * values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li> |
|
186 * <li>With a getter function, which can be used to manipulate stored values, |
|
187 * before they are returned by Attribute's <a href="#method_get">get</a> method.</li> |
|
188 * <li>With a validator function, to validate values before they are stored.</li> |
|
189 * </ul> |
|
190 * |
|
191 * <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration |
|
192 * options available for attributes</p>. |
|
193 * |
|
194 * <p><strong>NOTE:</strong> Most implementations will be better off extending the <a href="Base.html">Base</a> class, |
|
195 * instead of augmenting Attribute directly. Base augments Attribute and will handle the initial configuration |
|
196 * of attributes for derived classes, accounting for values passed into the constructor.</p> |
|
197 * |
|
198 * @class Attribute |
|
199 * @uses EventTarget |
|
200 */ |
|
201 function Attribute() { |
|
202 Y.log('Attribute constructor called', 'info', 'attribute'); |
|
203 |
|
204 // Perf tweak - avoid creating event literals if not required. |
|
205 this._ATTR_E_FACADE = {}; |
|
206 |
|
207 EventTarget.call(this, {emitFacade:true}); |
|
208 this._conf = new Y.State(); |
|
209 } |
|
210 |
|
211 /** |
|
212 * <p>The value to return from an attribute setter in order to prevent the set from going through.</p> |
|
213 * |
|
214 * <p>You can return this value from your setter if you wish to combine validator and setter |
|
215 * functionality into a single setter function, which either returns the massaged value to be stored or |
|
216 * Attribute.INVALID_VALUE to prevent invalid values from being stored.</p> |
|
217 * |
|
218 * @property Attribute.INVALID_VALUE |
|
219 * @type Object |
|
220 * @static |
|
221 * @final |
|
222 */ |
|
223 Attribute.INVALID_VALUE = {}; |
|
224 INVALID_VALUE = Attribute.INVALID_VALUE; |
|
225 |
|
226 /** |
|
227 * The list of properties which can be configured for |
|
228 * each attribute (e.g. setter, getter, writeOnce etc.). |
|
229 * |
|
230 * This property is used internally as a whitelist for faster |
|
231 * Y.mix operations. |
|
232 * |
|
233 * @property Attribute._ATTR_CFG |
|
234 * @type Array |
|
235 * @static |
|
236 * @protected |
|
237 */ |
|
238 Attribute._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BROADCAST]; |
|
239 |
|
240 Attribute.prototype = { |
|
241 /** |
|
242 * <p> |
|
243 * Adds an attribute with the provided configuration to the host object. |
|
244 * </p> |
|
245 * <p> |
|
246 * The config argument object supports the following properties: |
|
247 * </p> |
|
248 * |
|
249 * <dl> |
|
250 * <dt>value <Any></dt> |
|
251 * <dd>The initial value to set on the attribute</dd> |
|
252 * |
|
253 * <dt>valueFn <Function></dt> |
|
254 * <dd>A function, which will return the initial value to set on the attribute. This is useful |
|
255 * for cases where the attribute configuration is defined statically, but needs to |
|
256 * reference the host instance ("this") to obtain an initial value. |
|
257 * If defined, this precedence over the value property.</dd> |
|
258 * |
|
259 * <dt>readOnly <boolean></dt> |
|
260 * <dd>Whether or not the attribute is read only. Attributes having readOnly set to true |
|
261 * cannot be modified by invoking the set method.</dd> |
|
262 * |
|
263 * <dt>writeOnce <boolean></dt> |
|
264 * <dd>Whether or not the attribute is "write once". Attributes having writeOnce set to true, |
|
265 * can only have their values set once, be it through the default configuration, |
|
266 * constructor configuration arguments, or by invoking set.</dd> |
|
267 * |
|
268 * <dt>setter <Function></dt> |
|
269 * <dd>The setter function used to massage or normalize the value passed to the set method for the attribute. |
|
270 * The value returned by the setter will be the final stored value. Returning |
|
271 * <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent |
|
272 * the value from being stored.</dd> |
|
273 * |
|
274 * <dt>getter <Function></dt> |
|
275 * <dd>The getter function used to massage or normalize the value returned by the get method for the attribute. |
|
276 * The value returned by the getter function is the value which will be returned to the user when they |
|
277 * invoke get.</dd> |
|
278 * |
|
279 * <dt>validator <Function></dt> |
|
280 * <dd>The validator function invoked prior to setting the stored value. Returning |
|
281 * false from the validator function will prevent the value from being stored.</dd> |
|
282 * |
|
283 * <dt>broadcast <int></dt> |
|
284 * <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 |
|
285 * valid values. By default attribute change events are not broadcast.</dd> |
|
286 * |
|
287 * <dt>lazyAdd <boolean></dt> |
|
288 * <dd>Whether or not to delay initialization of the attribute until the first call to get/set it. |
|
289 * This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through |
|
290 * the <a href="#method_addAttrs">addAttrs</a> method.</dd> |
|
291 * |
|
292 * </dl> |
|
293 * |
|
294 * <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with |
|
295 * the context ("this") set to the host object.</p> |
|
296 * |
|
297 * @method addAttr |
|
298 * |
|
299 * @param {String} name The name of the attribute. |
|
300 * @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute. |
|
301 * |
|
302 * <p> |
|
303 * <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need |
|
304 * to protect the original values, you will need to merge the object. |
|
305 * </p> |
|
306 * |
|
307 * @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set). |
|
308 * |
|
309 * @return {Object} A reference to the host object. |
|
310 * |
|
311 * @chainable |
|
312 */ |
|
313 addAttr: function(name, config, lazy) { |
|
314 |
|
315 Y.log('Adding attribute: ' + name, 'info', 'attribute'); |
|
316 var conf = this._conf; |
|
317 |
|
318 lazy = (LAZY_ADD in config) ? config[LAZY_ADD] : lazy; |
|
319 |
|
320 if (lazy && !this.attrAdded(name)) { |
|
321 Y.log('Lazy Add: ' + name, 'info', 'attribute'); |
|
322 |
|
323 conf.add(name, LAZY, config || {}); |
|
324 conf.add(name, ADDED, true); |
|
325 } else { |
|
326 |
|
327 if (this.attrAdded(name) && !conf.get(name, IS_LAZY_ADD)) { Y.log('Attribute: ' + name + ' already exists. Cannot add it again without removing it first', 'warn', 'attribute'); } |
|
328 |
|
329 if (!this.attrAdded(name) || conf.get(name, IS_LAZY_ADD)) { |
|
330 Y.log('Non-Lazy Add: ' + name, 'info', 'attribute'); |
|
331 |
|
332 config = config || {}; |
|
333 |
|
334 var value, hasValue = (VALUE in config); |
|
335 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');} |
|
336 |
|
337 if(hasValue) { |
|
338 // We'll go through set, don't want to set value in _conf directory |
|
339 value = config.value; |
|
340 delete config.value; |
|
341 } |
|
342 |
|
343 config.added = true; |
|
344 config.initializing = true; |
|
345 |
|
346 conf.addAll(name, config); |
|
347 |
|
348 if (hasValue) { |
|
349 // Go through set, so that raw values get normalized/validated |
|
350 this.set(name, value); |
|
351 } |
|
352 |
|
353 conf.remove(name, INITIALIZING); |
|
354 } |
|
355 } |
|
356 |
|
357 return this; |
|
358 }, |
|
359 |
|
360 /** |
|
361 * Checks if the given attribute has been added to the host |
|
362 * |
|
363 * @method attrAdded |
|
364 * @param {String} name The name of the attribute to check. |
|
365 * @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. |
|
366 */ |
|
367 attrAdded: function(name) { |
|
368 return !!this._conf.get(name, ADDED); |
|
369 }, |
|
370 |
|
371 /** |
|
372 * Updates the configuration of an attribute which has already been added. |
|
373 * <p> |
|
374 * The properties which can be modified through this interface are limited |
|
375 * to the following subset of attributes, which can be safely modified |
|
376 * after a value has already been set on the attribute: readOnly, writeOnce, |
|
377 * broadcast and getter. |
|
378 * </p> |
|
379 * @method modifyAttr |
|
380 * @param {String} name The name of the attribute whose configuration is to be updated. |
|
381 * @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify. |
|
382 */ |
|
383 modifyAttr: function(name, config) { |
|
384 if (this.attrAdded(name)) { |
|
385 |
|
386 if (this._isLazyAttr(name)) { |
|
387 this._addLazyAttr(name); |
|
388 } |
|
389 |
|
390 var prop, conf = this._conf; |
|
391 for (prop in config) { |
|
392 if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) { |
|
393 conf.add(name, prop, config[prop]); |
|
394 |
|
395 // If we reconfigured broadcast, need to republish |
|
396 if (prop === BROADCAST) { |
|
397 conf.remove(name, PUBLISHED); |
|
398 } |
|
399 } |
|
400 } |
|
401 } |
|
402 |
|
403 if (!this.attrAdded(name)) {Y.log('Attribute modifyAttr:' + name + ' has not been added. Use addAttr to add the attribute', 'warn', 'attribute');} |
|
404 }, |
|
405 |
|
406 /** |
|
407 * Removes an attribute from the host object |
|
408 * |
|
409 * @method removeAttr |
|
410 * @param {String} name The name of the attribute to be removed. |
|
411 */ |
|
412 removeAttr: function(name) { |
|
413 this._conf.removeAll(name); |
|
414 }, |
|
415 |
|
416 /** |
|
417 * Returns the current value of the attribute. If the attribute |
|
418 * has been configured with a 'getter' function, this method will delegate |
|
419 * to the 'getter' to obtain the value of the attribute. |
|
420 * |
|
421 * @method get |
|
422 * |
|
423 * @param {String} name The name of the attribute. If the value of the attribute is an Object, |
|
424 * dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>) |
|
425 * |
|
426 * @return {Any} The value of the attribute |
|
427 */ |
|
428 get : function(name) { |
|
429 |
|
430 var fullName = name, |
|
431 conf = this._conf, |
|
432 path, |
|
433 getter, |
|
434 val; |
|
435 |
|
436 if (name.indexOf(DOT) !== -1) { |
|
437 path = name.split(DOT); |
|
438 name = path.shift(); |
|
439 } |
|
440 |
|
441 // On Demand - Should be rare - handles out of order valueFn references |
|
442 if (this._tCfgs && this._tCfgs[name]) { |
|
443 var cfg = {}; |
|
444 cfg[name] = this._tCfgs[name]; |
|
445 delete this._tCfgs[name]; |
|
446 this._addAttrs(cfg, this._tVals); |
|
447 } |
|
448 |
|
449 // Lazy Init |
|
450 if (this._isLazyAttr(name)) { |
|
451 this._addLazyAttr(name); |
|
452 } |
|
453 |
|
454 val = conf.get(name, VALUE); |
|
455 getter = conf.get(name, GETTER); |
|
456 |
|
457 val = (getter) ? getter.call(this, val, fullName) : val; |
|
458 val = (path) ? O.getValue(val, path) : val; |
|
459 |
|
460 return val; |
|
461 }, |
|
462 |
|
463 /** |
|
464 * Checks whether or not the attribute is one which has been |
|
465 * added lazily and still requires initialization. |
|
466 * |
|
467 * @method _isLazyAttr |
|
468 * @private |
|
469 * @param {String} name The name of the attribute |
|
470 * @return {boolean} true if it's a lazily added attribute, false otherwise. |
|
471 */ |
|
472 _isLazyAttr: function(name) { |
|
473 return this._conf.get(name, LAZY); |
|
474 }, |
|
475 |
|
476 /** |
|
477 * Finishes initializing an attribute which has been lazily added. |
|
478 * |
|
479 * @method _addLazyAttr |
|
480 * @private |
|
481 * @param {Object} name The name of the attribute |
|
482 */ |
|
483 _addLazyAttr: function(name) { |
|
484 var conf = this._conf; |
|
485 var lazyCfg = conf.get(name, LAZY); |
|
486 conf.add(name, IS_LAZY_ADD, true); |
|
487 conf.remove(name, LAZY); |
|
488 this.addAttr(name, lazyCfg); |
|
489 }, |
|
490 |
|
491 /** |
|
492 * Sets the value of an attribute. |
|
493 * |
|
494 * @method set |
|
495 * @chainable |
|
496 * |
|
497 * @param {String} name The name of the attribute. If the |
|
498 * current value of the attribute is an Object, dot notation can be used |
|
499 * to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>). |
|
500 * |
|
501 * @param {Any} value The value to set the attribute to. |
|
502 * |
|
503 * @param {Object} opts (Optional) Optional event data to be mixed into |
|
504 * the event facade passed to subscribers of the attribute's change event. This |
|
505 * can be used as a flexible way to identify the source of a call to set, allowing |
|
506 * the developer to distinguish between set called internally by the host, vs. |
|
507 * set called externally by the application developer. |
|
508 * |
|
509 * @return {Object} A reference to the host object. |
|
510 */ |
|
511 set : function(name, val, opts) { |
|
512 return this._setAttr(name, val, opts); |
|
513 }, |
|
514 |
|
515 /** |
|
516 * Resets the attribute (or all attributes) to its initial value, as long as |
|
517 * the attribute is not readOnly, or writeOnce. |
|
518 * |
|
519 * @method reset |
|
520 * @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset. |
|
521 * @return {Object} A reference to the host object. |
|
522 * @chainable |
|
523 */ |
|
524 reset : function(name) { |
|
525 if (name) { |
|
526 if (this._isLazyAttr(name)) { |
|
527 this._addLazyAttr(name); |
|
528 } |
|
529 this.set(name, this._conf.get(name, INIT_VALUE)); |
|
530 } else { |
|
531 var added = this._conf.data.added; |
|
532 Y.each(added, function(v, n) { |
|
533 this.reset(n); |
|
534 }, this); |
|
535 } |
|
536 return this; |
|
537 }, |
|
538 |
|
539 /** |
|
540 * Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details. |
|
541 * |
|
542 * @method _set |
|
543 * @protected |
|
544 * @chainable |
|
545 * |
|
546 * @param {String} name The name of the attribute. |
|
547 * @param {Any} val The value to set the attribute to. |
|
548 * @param {Object} opts (Optional) Optional event data to be mixed into |
|
549 * the event facade passed to subscribers of the attribute's change event. |
|
550 * @return {Object} A reference to the host object. |
|
551 */ |
|
552 _set : function(name, val, opts) { |
|
553 return this._setAttr(name, val, opts, true); |
|
554 }, |
|
555 |
|
556 /** |
|
557 * Provides the common implementation for the public set and protected _set methods. |
|
558 * |
|
559 * See <a href="#method_set">set</a> for argument details. |
|
560 * |
|
561 * @method _setAttr |
|
562 * @protected |
|
563 * @chainable |
|
564 * |
|
565 * @param {String} name The name of the attribute. |
|
566 * @param {Any} value The value to set the attribute to. |
|
567 * @param {Object} opts (Optional) Optional event data to be mixed into |
|
568 * the event facade passed to subscribers of the attribute's change event. |
|
569 * @param {boolean} force If true, allows the caller to set values for |
|
570 * readOnly or writeOnce attributes which have already been set. |
|
571 * |
|
572 * @return {Object} A reference to the host object. |
|
573 */ |
|
574 _setAttr : function(name, val, opts, force) { |
|
575 var allowSet = true, |
|
576 conf = this._conf, |
|
577 data = conf.data, |
|
578 initialSet, |
|
579 strPath, |
|
580 path, |
|
581 currVal; |
|
582 |
|
583 if (name.indexOf(DOT) !== -1) { |
|
584 strPath = name; |
|
585 path = name.split(DOT); |
|
586 name = path.shift(); |
|
587 } |
|
588 |
|
589 if (this._isLazyAttr(name)) { |
|
590 this._addLazyAttr(name); |
|
591 } |
|
592 |
|
593 initialSet = (!data.value || !(name in data.value)); |
|
594 |
|
595 if (!this.attrAdded(name)) { |
|
596 Y.log('Set attribute:' + name + ', aborted; Attribute is not configured', 'warn', 'attribute'); |
|
597 } else { |
|
598 |
|
599 if (!initialSet && !force) { |
|
600 |
|
601 if (conf.get(name, WRITE_ONCE)) { |
|
602 Y.log('Set attribute:' + name + ', aborted; Attribute is writeOnce', 'warn', 'attribute'); |
|
603 allowSet = false; |
|
604 } |
|
605 |
|
606 if (conf.get(name, READ_ONLY)) { |
|
607 Y.log('Set attribute:' + name + ', aborted; Attribute is readOnly', 'warn', 'attribute'); |
|
608 allowSet = false; |
|
609 } |
|
610 } |
|
611 |
|
612 if (allowSet) { |
|
613 currVal = this.get(name); |
|
614 |
|
615 if (path) { |
|
616 val = O.setValue(Y.clone(currVal), path, val); |
|
617 |
|
618 if (val === undefined) { |
|
619 Y.log('Set attribute path:' + strPath + ', aborted; Path is invalid', 'warn', 'attribute'); |
|
620 allowSet = false; |
|
621 } |
|
622 } |
|
623 |
|
624 if (allowSet) { |
|
625 if (conf.get(name, INITIALIZING)) { |
|
626 this._setAttrVal(name, strPath, currVal, val); |
|
627 } else { |
|
628 this._fireAttrChange(name, strPath, currVal, val, opts); |
|
629 } |
|
630 } |
|
631 } |
|
632 } |
|
633 |
|
634 return this; |
|
635 }, |
|
636 |
|
637 /** |
|
638 * Utility method to help setup the event payload and fire the attribute change event. |
|
639 * |
|
640 * @method _fireAttrChange |
|
641 * @private |
|
642 * @param {String} attrName The name of the attribute |
|
643 * @param {String} subAttrName The full path of the property being changed, |
|
644 * if this is a sub-attribute value being change. Otherwise null. |
|
645 * @param {Any} currVal The current value of the attribute |
|
646 * @param {Any} newVal The new value of the attribute |
|
647 * @param {Object} opts Any additional event data to mix into the attribute change event's event facade. |
|
648 */ |
|
649 _fireAttrChange : function(attrName, subAttrName, currVal, newVal, opts) { |
|
650 var eventName = attrName + CHANGE, |
|
651 conf = this._conf, |
|
652 facade; |
|
653 |
|
654 if (!conf.get(attrName, PUBLISHED)) { |
|
655 this.publish(eventName, { |
|
656 queuable:false, |
|
657 defaultFn:this._defAttrChangeFn, |
|
658 silent:true, |
|
659 broadcast : conf.get(attrName, BROADCAST) |
|
660 }); |
|
661 conf.add(attrName, PUBLISHED, true); |
|
662 } |
|
663 |
|
664 facade = (opts) ? Y.merge(opts) : this._ATTR_E_FACADE; |
|
665 |
|
666 facade.type = eventName; |
|
667 facade.attrName = attrName; |
|
668 facade.subAttrName = subAttrName; |
|
669 facade.prevVal = currVal; |
|
670 facade.newVal = newVal; |
|
671 |
|
672 this.fire(facade); |
|
673 }, |
|
674 |
|
675 /** |
|
676 * Default function for attribute change events. |
|
677 * |
|
678 * @private |
|
679 * @method _defAttrChangeFn |
|
680 * @param {EventFacade} e The event object for attribute change events. |
|
681 */ |
|
682 _defAttrChangeFn : function(e) { |
|
683 if (!this._setAttrVal(e.attrName, e.subAttrName, e.prevVal, e.newVal)) { |
|
684 Y.log('State not updated and stopImmediatePropagation called for attribute: ' + e.attrName + ' , value:' + e.newVal, 'warn', 'attribute'); |
|
685 // Prevent "after" listeners from being invoked since nothing changed. |
|
686 e.stopImmediatePropagation(); |
|
687 } else { |
|
688 e.newVal = this._conf.get(e.attrName, VALUE); |
|
689 } |
|
690 }, |
|
691 |
|
692 /** |
|
693 * Updates the stored value of the attribute in the privately held State object, |
|
694 * if validation and setter passes. |
|
695 * |
|
696 * @method _setAttrVal |
|
697 * @private |
|
698 * @param {String} attrName The attribute name. |
|
699 * @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z"). |
|
700 * @param {Any} prevVal The currently stored value of the attribute. |
|
701 * @param {Any} newVal The value which is going to be stored. |
|
702 * |
|
703 * @return {booolean} true if the new attribute value was stored, false if not. |
|
704 */ |
|
705 _setAttrVal : function(attrName, subAttrName, prevVal, newVal) { |
|
706 |
|
707 var allowSet = true, |
|
708 conf = this._conf, |
|
709 |
|
710 validator = conf.get(attrName, VALIDATOR), |
|
711 setter = conf.get(attrName, SETTER), |
|
712 initializing = conf.get(attrName, INITIALIZING), |
|
713 |
|
714 name = subAttrName || attrName, |
|
715 retVal; |
|
716 |
|
717 if (validator) { |
|
718 var valid = validator.call(this, newVal, name); |
|
719 |
|
720 if (!valid && initializing) { |
|
721 newVal = conf.get(attrName, DEF_VALUE); |
|
722 valid = true; // Assume it's valid, for perf. |
|
723 } |
|
724 } |
|
725 |
|
726 if (!validator || valid) { |
|
727 if (setter) { |
|
728 retVal = setter.call(this, newVal, name); |
|
729 |
|
730 if (retVal === INVALID_VALUE) { |
|
731 Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute'); |
|
732 allowSet = false; |
|
733 } else if (retVal !== undefined){ |
|
734 Y.log('Attribute: ' + attrName + ', raw value: ' + newVal + ' modified by setter to:' + retVal, 'info', 'attribute'); |
|
735 newVal = retVal; |
|
736 } |
|
737 } |
|
738 |
|
739 if (allowSet) { |
|
740 if(!subAttrName && newVal === prevVal) { |
|
741 Y.log('Attribute: ' + attrName + ', value unchanged:' + newVal, 'warn', 'attribute'); |
|
742 allowSet = false; |
|
743 } else { |
|
744 // Store value |
|
745 if (conf.get(attrName, INIT_VALUE) === undefined) { |
|
746 conf.add(attrName, INIT_VALUE, newVal); |
|
747 } |
|
748 conf.add(attrName, VALUE, newVal); |
|
749 } |
|
750 } |
|
751 |
|
752 } else { |
|
753 Y.log('Attribute:' + attrName + ', Validation failed for value:' + newVal, 'warn', 'attribute'); |
|
754 allowSet = false; |
|
755 } |
|
756 |
|
757 return allowSet; |
|
758 }, |
|
759 |
|
760 /** |
|
761 * Sets multiple attribute values. |
|
762 * |
|
763 * @method setAttrs |
|
764 * @param {Object} attrs An object with attributes name/value pairs. |
|
765 * @return {Object} A reference to the host object. |
|
766 * @chainable |
|
767 */ |
|
768 setAttrs : function(attrs) { |
|
769 for (var attr in attrs) { |
|
770 if ( attrs.hasOwnProperty(attr) ) { |
|
771 this.set(attr, attrs[attr]); |
|
772 } |
|
773 } |
|
774 return this; |
|
775 }, |
|
776 |
|
777 /** |
|
778 * Gets multiple attribute values. |
|
779 * |
|
780 * @method getAttrs |
|
781 * @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are |
|
782 * returned. If set to true, all attributes modified from their initial values are returned. |
|
783 * @return {Object} An object with attribute name/value pairs. |
|
784 */ |
|
785 getAttrs : function(attrs) { |
|
786 var o = {}, i, l, attr, val, |
|
787 modifiedOnly = (attrs === true); |
|
788 |
|
789 attrs = (attrs && !modifiedOnly) ? attrs : O.keys(this._conf.data.added); |
|
790 |
|
791 for (i = 0, l = attrs.length; i < l; i++) { |
|
792 // Go through get, to honor cloning/normalization |
|
793 attr = attrs[i]; |
|
794 val = this.get(attr); |
|
795 |
|
796 if (!modifiedOnly || this._conf.get(attr, VALUE) != this._conf.get(attr, INIT_VALUE)) { |
|
797 o[attr] = this.get(attr); |
|
798 } |
|
799 } |
|
800 |
|
801 return o; |
|
802 }, |
|
803 |
|
804 /** |
|
805 * Configures a group of attributes, and sets initial values. |
|
806 * |
|
807 * <p> |
|
808 * <strong>NOTE:</strong> This method does not isolate the configuration object by merging/cloning. |
|
809 * The caller is responsible for merging/cloning the configuration object if required. |
|
810 * </p> |
|
811 * |
|
812 * @method addAttrs |
|
813 * @chainable |
|
814 * |
|
815 * @param {Object} cfgs An object with attribute name/configuration pairs. |
|
816 * @param {Object} values An object with attribute name/value pairs, defining the initial values to apply. |
|
817 * Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only. |
|
818 * @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set. |
|
819 * Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration. |
|
820 * See <a href="#method_addAttr">addAttr</a>. |
|
821 * |
|
822 * @return {Object} A reference to the host object. |
|
823 */ |
|
824 addAttrs : function(cfgs, values, lazy) { |
|
825 if (cfgs) { |
|
826 this._tCfgs = cfgs; |
|
827 this._tVals = this._splitAttrVals(values); |
|
828 |
|
829 this._addAttrs(cfgs, this._tVals, lazy); |
|
830 |
|
831 this._tCfgs = this._tVals = null; |
|
832 } |
|
833 |
|
834 return this; |
|
835 }, |
|
836 |
|
837 /** |
|
838 * Implementation behind the public addAttrs method. |
|
839 * |
|
840 * This method is invoked directly by get if it encounters a scenario |
|
841 * in which an attribute's valueFn attempts to obtain the |
|
842 * value an attribute in the same group of attributes, which has not yet |
|
843 * been added (on demand initialization). |
|
844 * |
|
845 * @method _addAttrs |
|
846 * @private |
|
847 * @param {Object} cfgs An object with attribute name/configuration pairs. |
|
848 * @param {Object} values An object with attribute name/value pairs, defining the initial values to apply. |
|
849 * Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only. |
|
850 * @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set. |
|
851 * Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration. |
|
852 * See <a href="#method_addAttr">addAttr</a>. |
|
853 */ |
|
854 _addAttrs : function(cfgs, values, lazy) { |
|
855 var attr, |
|
856 attrCfg, |
|
857 value; |
|
858 |
|
859 for (attr in cfgs) { |
|
860 if (cfgs.hasOwnProperty(attr)) { |
|
861 |
|
862 // Not Merging. Caller is responsible for isolating configs |
|
863 attrCfg = cfgs[attr]; |
|
864 attrCfg.defaultValue = attrCfg.value; |
|
865 |
|
866 // Handle simple, complex and user values, accounting for read-only |
|
867 value = this._getAttrInitVal(attr, attrCfg, this._tVals); |
|
868 |
|
869 if (value !== undefined) { |
|
870 attrCfg.value = value; |
|
871 } |
|
872 |
|
873 if (this._tCfgs[attr]) { |
|
874 delete this._tCfgs[attr]; |
|
875 } |
|
876 |
|
877 this.addAttr(attr, attrCfg, lazy); |
|
878 } |
|
879 } |
|
880 }, |
|
881 |
|
882 /** |
|
883 * Utility method to split out simple attribute name/value pairs ("x") |
|
884 * from complex attribute name/value pairs ("x.y.z"), so that complex |
|
885 * attributes can be keyed by the top level attribute name. |
|
886 * |
|
887 * @method _splitAttrVals |
|
888 * @param {Object} valueHash An object with attribute name/value pairs |
|
889 * |
|
890 * @return {Object} An object literal with 2 properties - "simple" and "complex", |
|
891 * containing simple and complex attribute values respectively keyed |
|
892 * by the top level attribute name, or null, if valueHash is falsey. |
|
893 * |
|
894 * @private |
|
895 */ |
|
896 _splitAttrVals : function(valueHash) { |
|
897 var vals = {}, |
|
898 subvals = {}, |
|
899 path, |
|
900 attr, |
|
901 v, k; |
|
902 |
|
903 if (valueHash) { |
|
904 for (k in valueHash) { |
|
905 if (valueHash.hasOwnProperty(k)) { |
|
906 if (k.indexOf(DOT) !== -1) { |
|
907 path = k.split(DOT); |
|
908 attr = path.shift(); |
|
909 v = subvals[attr] = subvals[attr] || []; |
|
910 v[v.length] = { |
|
911 path : path, |
|
912 value: valueHash[k] |
|
913 }; |
|
914 } else { |
|
915 vals[k] = valueHash[k]; |
|
916 } |
|
917 } |
|
918 } |
|
919 return { simple:vals, complex:subvals }; |
|
920 } else { |
|
921 return null; |
|
922 } |
|
923 }, |
|
924 |
|
925 /** |
|
926 * Returns the initial value of the given attribute from |
|
927 * either the default configuration provided, or the |
|
928 * over-ridden value if it exists in the set of initValues |
|
929 * provided and the attribute is not read-only. |
|
930 * |
|
931 * @param {String} attr The name of the attribute |
|
932 * @param {Object} cfg The attribute configuration object |
|
933 * @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _splitAttrVals |
|
934 * |
|
935 * @return {Any} The initial value of the attribute. |
|
936 * |
|
937 * @method _getAttrInitVal |
|
938 * @private |
|
939 */ |
|
940 _getAttrInitVal : function(attr, cfg, initValues) { |
|
941 |
|
942 var val = (cfg.valueFn) ? cfg.valueFn.call(this) : cfg.value, |
|
943 simple, |
|
944 complex, |
|
945 i, |
|
946 l, |
|
947 path, |
|
948 subval, |
|
949 subvals; |
|
950 |
|
951 if (!cfg.readOnly && initValues) { |
|
952 |
|
953 Y.log('Checking initValues in _getAttrIniVal: ' + attr, 'info', 'attribute'); |
|
954 |
|
955 // Simple Attributes |
|
956 simple = initValues.simple; |
|
957 if (simple && simple.hasOwnProperty(attr)) { |
|
958 val = simple[attr]; |
|
959 } |
|
960 |
|
961 // Complex Attributes (complex values applied, after simple, incase both are set) |
|
962 complex = initValues.complex; |
|
963 if (complex && complex.hasOwnProperty(attr)) { |
|
964 subvals = complex[attr]; |
|
965 for (i = 0, l = subvals.length; i < l; ++i) { |
|
966 path = subvals[i].path; |
|
967 subval = subvals[i].value; |
|
968 O.setValue(val, path, subval); |
|
969 } |
|
970 } |
|
971 } |
|
972 |
|
973 Y.log('initValue for ' + attr + ':' + val, 'info', 'attribute'); |
|
974 |
|
975 return val; |
|
976 } |
|
977 }; |
|
978 |
|
979 // Basic prototype augment - no lazy constructor invocation. |
|
980 Y.mix(Attribute, EventTarget, false, null, 1); |
|
981 |
|
982 Y.Attribute = Attribute; |
|
983 |
|
984 |
|
985 |
|
986 }, '3.0.0b1' ,{requires:['event-custom']}); |