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