2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('attribute-base', function(Y) {
11
* The State class maintains state for a collection of named items, with
12
* a varying number of properties defined.
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.
21
Y.State = function() {
32
* Adds a property to an item.
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.
39
add : function(name, key, val) {
41
d[key] = d[key] || {};
46
* Adds multiple properties to an item.
49
* @param name {String} The name of the item.
50
* @param o {Object} A hash of property/value pairs.
52
addAll: function(name, o) {
55
if (o.hasOwnProperty(key)) {
56
this.add(name, key, o[key]);
62
* Removes a property from an item.
65
* @param name {String} The name of the item.
66
* @param key {String} The property to remove.
68
remove: function(name, key) {
70
if (d[key] && (name in d[key])) {
76
* Removes multiple properties from an item, or remove the item completely.
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.
82
removeAll: function(name, o) {
85
Y.each(o || d, function(v, k) {
86
if(Y.Lang.isString(k)) {
95
* For a given item, returns the value of the property requested, or undefined if not found.
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.
102
get: function(name, key) {
104
return (d[key] && name in d[key]) ? d[key][name] : undefined;
108
* For the given item, returns a disposable object with all of the
109
* item's property/value pairs.
112
* @param name {String} The name of the item
113
* @return {Object} An object with property/value pairs for the item.
115
getAll : function(name) {
116
var d = this.data, o;
118
Y.each(d, function(v, k) {
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
139
* The attribute-base submodule provides core attribute handling support, with everything
140
* aside from complex attribute handling in the provider's constructor.
143
* @submodule attribute-base
147
EventTarget = Y.EventTarget,
152
// Externally configurable props
155
READ_ONLY = "readOnly",
156
WRITE_ONCE = "writeOnce",
157
INIT_ONLY = "initOnly",
158
VALIDATOR = "validator",
160
VALUE_FN = "valueFn",
161
BROADCAST = "broadcast",
162
LAZY_ADD = "lazyAdd",
163
BYPASS_PROXY = "_bypassProxy",
165
// Used for internal state management
167
INITIALIZING = "initializing",
168
INIT_VALUE = "initValue",
169
PUBLISHED = "published",
170
DEF_VALUE = "defaultValue",
172
IS_LAZY_ADD = "isLazyAdd",
178
// Properties which can be changed after the attribute has been added.
179
MODIFIABLE[READ_ONLY] = 1;
180
MODIFIABLE[WRITE_ONCE] = 1;
181
MODIFIABLE[GETTER] = 1;
182
MODIFIABLE[BROADCAST] = 1;
186
* Attribute provides configurable attribute support along with attribute change events. It is designed to be
187
* augmented on to a host class, and provides the host with the ability to configure attributes to store and retrieve state,
188
* along with attribute change events.
190
* <p>For example, attributes added to the host can be configured:</p>
192
* <li>As read only.</li>
193
* <li>As write once.</li>
194
* <li>With a setter function, which can be used to manipulate
195
* values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
196
* <li>With a getter function, which can be used to manipulate stored values,
197
* before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
198
* <li>With a validator function, to validate values before they are stored.</li>
201
* <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
202
* options available for attributes</p>.
204
* <p><strong>NOTE:</strong> Most implementations will be better off extending the <a href="Base.html">Base</a> class,
205
* instead of augmenting Attribute directly. Base augments Attribute and will handle the initial configuration
206
* of attributes for derived classes, accounting for values passed into the constructor.</p>
211
function Attribute() {
213
var host = this, // help compression
214
attrs = this.constructor.ATTRS,
217
// Perf tweak - avoid creating event literals if not required.
218
host._ATTR_E_FACADE = {};
220
EventTarget.call(host, {emitFacade:true});
222
// _conf maintained for backwards compat
223
host._conf = host._state = new Y.State();
225
host._stateProxy = host._stateProxy || null;
226
host._requireAddAttr = host._requireAddAttr || false;
228
// ATTRS support for Node, which is not Base based
229
if ( attrs && !(Base && host instanceof Base)) {
230
host.addAttrs(this._protectAttrs(attrs));
235
* <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
237
* <p>You can return this value from your setter if you wish to combine validator and setter
238
* functionality into a single setter function, which either returns the massaged value to be stored or
239
* Attribute.INVALID_VALUE to prevent invalid values from being stored.</p>
241
* @property Attribute.INVALID_VALUE
246
Attribute.INVALID_VALUE = {};
247
INVALID_VALUE = Attribute.INVALID_VALUE;
250
* The list of properties which can be configured for
251
* each attribute (e.g. setter, getter, writeOnce etc.).
253
* This property is used internally as a whitelist for faster
256
* @property Attribute._ATTR_CFG
261
Attribute._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BROADCAST, BYPASS_PROXY];
263
Attribute.prototype = {
266
* Adds an attribute with the provided configuration to the host object.
269
* The config argument object supports the following properties:
273
* <dt>value <Any></dt>
274
* <dd>The initial value to set on the attribute</dd>
276
* <dt>valueFn <Function | String></dt>
278
* <p>A function, which will return the initial value to set on the attribute. This is useful
279
* for cases where the attribute configuration is defined statically, but needs to
280
* reference the host instance ("this") to obtain an initial value. If both the value and valueFn properties are defined,
281
* the value returned by the valueFn has precedence over the value property, unless it returns undefined, in which
282
* case the value property is used.</p>
284
* <p>valueFn can also be set to a string, representing the name of the instance method to be used to retrieve the value.</p>
287
* <dt>readOnly <boolean></dt>
288
* <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
289
* cannot be modified by invoking the set method.</dd>
291
* <dt>writeOnce <boolean> or <string></dt>
293
* Whether or not the attribute is "write once". Attributes having writeOnce set to true,
294
* can only have their values set once, be it through the default configuration,
295
* constructor configuration arguments, or by invoking set.
296
* <p>The writeOnce attribute can also be set to the string "initOnly", in which case the attribute can only be set during initialization
297
* (when used with Base, this means it can only be set during construction)</p>
300
* <dt>setter <Function | String></dt>
302
* <p>The setter function used to massage or normalize the value passed to the set method for the attribute.
303
* The value returned by the setter will be the final stored value. Returning
304
* <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
305
* the value from being stored.
308
* <p>setter can also be set to a string, representing the name of the instance method to be used as the setter function.</p>
311
* <dt>getter <Function | String></dt>
314
* The getter function used to massage or normalize the value returned by the get method for the attribute.
315
* The value returned by the getter function is the value which will be returned to the user when they
319
* <p>getter can also be set to a string, representing the name of the instance method to be used as the getter function.</p>
322
* <dt>validator <Function | String></dt>
325
* The validator function invoked prior to setting the stored value. Returning
326
* false from the validator function will prevent the value from being stored.
329
* <p>validator can also be set to a string, representing the name of the instance method to be used as the validator function.</p>
332
* <dt>broadcast <int></dt>
333
* <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
334
* valid values. By default attribute change events are not broadcast.</dd>
336
* <dt>lazyAdd <boolean></dt>
337
* <dd>Whether or not to delay initialization of the attribute until the first call to get/set it.
338
* This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through
339
* the <a href="#method_addAttrs">addAttrs</a> method.</dd>
343
* <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
344
* the context ("this") set to the host object.</p>
346
* <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>
350
* @param {String} name The name of the attribute.
351
* @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
354
* <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need
355
* to protect the original values, you will need to merge the object.
358
* @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set).
360
* @return {Object} A reference to the host object.
364
addAttr: function(name, config, lazy) {
367
var host = this, // help compression
372
lazy = (LAZY_ADD in config) ? config[LAZY_ADD] : lazy;
374
if (lazy && !host.attrAdded(name)) {
375
state.add(name, LAZY, config || {});
376
state.add(name, ADDED, true);
380
if (!host.attrAdded(name) || state.get(name, IS_LAZY_ADD)) {
382
config = config || {};
384
hasValue = (VALUE in config);
388
// We'll go through set, don't want to set value in config directly
389
value = config.value;
394
config.initializing = true;
396
state.addAll(name, config);
399
// Go through set, so that raw values get normalized/validated
400
host.set(name, value);
403
state.remove(name, INITIALIZING);
411
* Checks if the given attribute has been added to the host
414
* @param {String} name The name of the attribute to check.
415
* @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.
417
attrAdded: function(name) {
418
return !!this._state.get(name, ADDED);
422
* Updates the configuration of an attribute which has already been added.
424
* The properties which can be modified through this interface are limited
425
* to the following subset of attributes, which can be safely modified
426
* after a value has already been set on the attribute: readOnly, writeOnce,
427
* broadcast and getter.
430
* @param {String} name The name of the attribute whose configuration is to be updated.
431
* @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
433
modifyAttr: function(name, config) {
434
var host = this, // help compression
437
if (host.attrAdded(name)) {
439
if (host._isLazyAttr(name)) {
440
host._addLazyAttr(name);
444
for (prop in config) {
445
if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
446
state.add(name, prop, config[prop]);
448
// If we reconfigured broadcast, need to republish
449
if (prop === BROADCAST) {
450
state.remove(name, PUBLISHED);
459
* Removes an attribute from the host object
462
* @param {String} name The name of the attribute to be removed.
464
removeAttr: function(name) {
465
this._state.removeAll(name);
469
* Returns the current value of the attribute. If the attribute
470
* has been configured with a 'getter' function, this method will delegate
471
* to the 'getter' to obtain the value of the attribute.
475
* @param {String} name The name of the attribute. If the value of the attribute is an Object,
476
* dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
478
* @return {Any} The value of the attribute
480
get : function(name) {
481
return this._getAttr(name);
485
* Checks whether or not the attribute is one which has been
486
* added lazily and still requires initialization.
488
* @method _isLazyAttr
490
* @param {String} name The name of the attribute
491
* @return {boolean} true if it's a lazily added attribute, false otherwise.
493
_isLazyAttr: function(name) {
494
return this._state.get(name, LAZY);
498
* Finishes initializing an attribute which has been lazily added.
500
* @method _addLazyAttr
502
* @param {Object} name The name of the attribute
504
_addLazyAttr: function(name) {
505
var state = this._state,
506
lazyCfg = state.get(name, LAZY);
508
state.add(name, IS_LAZY_ADD, true);
509
state.remove(name, LAZY);
510
this.addAttr(name, lazyCfg);
514
* Sets the value of an attribute.
519
* @param {String} name The name of the attribute. If the
520
* current value of the attribute is an Object, dot notation can be used
521
* to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
523
* @param {Any} value The value to set the attribute to.
525
* @param {Object} opts (Optional) Optional event data to be mixed into
526
* the event facade passed to subscribers of the attribute's change event. This
527
* can be used as a flexible way to identify the source of a call to set, allowing
528
* the developer to distinguish between set called internally by the host, vs.
529
* set called externally by the application developer.
531
* @return {Object} A reference to the host object.
533
set : function(name, val, opts) {
534
return this._setAttr(name, val, opts);
538
* Resets the attribute (or all attributes) to its initial value, as long as
539
* the attribute is not readOnly, or writeOnce.
542
* @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
543
* @return {Object} A reference to the host object.
546
reset : function(name) {
547
var host = this, // help compression
551
if (host._isLazyAttr(name)) {
552
host._addLazyAttr(name);
554
host.set(name, host._state.get(name, INIT_VALUE));
556
added = host._state.data.added;
557
Y.each(added, function(v, n) {
565
* Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
571
* @param {String} name The name of the attribute.
572
* @param {Any} val The value to set the attribute to.
573
* @param {Object} opts (Optional) Optional event data to be mixed into
574
* the event facade passed to subscribers of the attribute's change event.
575
* @return {Object} A reference to the host object.
577
_set : function(name, val, opts) {
578
return this._setAttr(name, val, opts, true);
582
* Provides the common implementation for the public get method,
583
* allowing Attribute hosts to over-ride either method.
585
* See <a href="#method_get">get</a> for argument details.
591
* @param {String} name The name of the attribute.
592
* @return {Any} The value of the attribute.
594
_getAttr : function(name) {
595
var host = this, // help compression
603
if (name.indexOf(DOT) !== -1) {
604
path = name.split(DOT);
608
// On Demand - Should be rare - handles out of order valueFn references
609
if (host._tCfgs && host._tCfgs[name]) {
611
cfg[name] = host._tCfgs[name];
612
delete host._tCfgs[name];
613
host._addAttrs(cfg, host._tVals);
617
if (host._isLazyAttr(name)) {
618
host._addLazyAttr(name);
621
val = host._getStateVal(name);
622
getter = state.get(name, GETTER);
624
if (getter && !getter.call) {
625
getter = this[getter];
628
val = (getter) ? getter.call(host, val, fullName) : val;
629
val = (path) ? O.getValue(val, path) : val;
635
* Provides the common implementation for the public set and protected _set methods.
637
* See <a href="#method_set">set</a> for argument details.
643
* @param {String} name The name of the attribute.
644
* @param {Any} value The value to set the attribute to.
645
* @param {Object} opts (Optional) Optional event data to be mixed into
646
* the event facade passed to subscribers of the attribute's change event.
647
* @param {boolean} force If true, allows the caller to set values for
648
* readOnly or writeOnce attributes which have already been set.
650
* @return {Object} A reference to the host object.
652
_setAttr : function(name, val, opts, force) {
655
stateProxy = this._stateProxy,
664
if (name.indexOf(DOT) !== -1) {
666
path = name.split(DOT);
670
if (this._isLazyAttr(name)) {
671
this._addLazyAttr(name);
674
initialSet = (!data.value || !(name in data.value));
676
if (stateProxy && name in stateProxy && !this._state.get(name, BYPASS_PROXY)) {
677
// 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?
681
if (this._requireAddAttr && !this.attrAdded(name)) {
684
writeOnce = state.get(name, WRITE_ONCE);
685
initializing = state.get(name, INITIALIZING);
687
if (!initialSet && !force) {
693
if (state.get(name, READ_ONLY)) {
698
if (!initializing && !force && writeOnce === INIT_ONLY) {
703
// Don't need currVal if initialSet (might fail in custom getter if it always expects a non-undefined/non-null value)
705
currVal = this.get(name);
709
val = O.setValue(Y.clone(currVal), path, val);
711
if (val === undefined) {
718
this._setAttrVal(name, strPath, currVal, val);
720
this._fireAttrChange(name, strPath, currVal, val, opts);
730
* Utility method to help setup the event payload and fire the attribute change event.
732
* @method _fireAttrChange
734
* @param {String} attrName The name of the attribute
735
* @param {String} subAttrName The full path of the property being changed,
736
* if this is a sub-attribute value being change. Otherwise null.
737
* @param {Any} currVal The current value of the attribute
738
* @param {Any} newVal The new value of the attribute
739
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
741
_fireAttrChange : function(attrName, subAttrName, currVal, newVal, opts) {
743
eventName = attrName + CHANGE,
747
if (!state.get(attrName, PUBLISHED)) {
748
host.publish(eventName, {
750
defaultTargetOnly: true,
751
defaultFn:host._defAttrChangeFn,
753
broadcast : state.get(attrName, BROADCAST)
755
state.add(attrName, PUBLISHED, true);
758
facade = (opts) ? Y.merge(opts) : host._ATTR_E_FACADE;
760
facade.type = eventName;
761
facade.attrName = attrName;
762
facade.subAttrName = subAttrName;
763
facade.prevVal = currVal;
764
facade.newVal = newVal;
770
* Default function for attribute change events.
773
* @method _defAttrChangeFn
774
* @param {EventFacade} e The event object for attribute change events.
776
_defAttrChangeFn : function(e) {
777
if (!this._setAttrVal(e.attrName, e.subAttrName, e.prevVal, e.newVal)) {
778
// Prevent "after" listeners from being invoked since nothing changed.
779
e.stopImmediatePropagation();
781
e.newVal = this.get(e.attrName);
786
* Gets the stored value for the attribute, from either the
787
* internal state object, or the state proxy if it exits
789
* @method _getStateVal
791
* @param {String} name The name of the attribute
792
* @return {Any} The stored value of the attribute
794
_getStateVal : function(name) {
795
var stateProxy = this._stateProxy;
796
return stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY) ? stateProxy[name] : this._state.get(name, VALUE);
800
* Sets the stored value for the attribute, in either the
801
* internal state object, or the state proxy if it exits
803
* @method _setStateVal
805
* @param {String} name The name of the attribute
806
* @param {Any} value The value of the attribute
808
_setStateVal : function(name, value) {
809
var stateProxy = this._stateProxy;
810
if (stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY)) {
811
stateProxy[name] = value;
813
this._state.add(name, VALUE, value);
818
* Updates the stored value of the attribute in the privately held State object,
819
* if validation and setter passes.
821
* @method _setAttrVal
823
* @param {String} attrName The attribute name.
824
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
825
* @param {Any} prevVal The currently stored value of the attribute.
826
* @param {Any} newVal The value which is going to be stored.
828
* @return {booolean} true if the new attribute value was stored, false if not.
830
_setAttrVal : function(attrName, subAttrName, prevVal, newVal) {
836
validator = state.get(attrName, VALIDATOR),
837
setter = state.get(attrName, SETTER),
838
initializing = state.get(attrName, INITIALIZING),
839
prevValRaw = this._getStateVal(attrName),
841
name = subAttrName || attrName,
846
if (!validator.call) {
847
// Assume string - trying to keep critical path tight, so avoiding Lang check
848
validator = this[validator];
851
valid = validator.call(host, newVal, name);
853
if (!valid && initializing) {
854
newVal = state.get(attrName, DEF_VALUE);
855
valid = true; // Assume it's valid, for perf.
860
if (!validator || valid) {
863
// Assume string - trying to keep critical path tight, so avoiding Lang check
864
setter = this[setter];
867
retVal = setter.call(host, newVal, name);
869
if (retVal === INVALID_VALUE) {
871
} else if (retVal !== undefined){
878
if(!subAttrName && (newVal === prevValRaw) && !Lang.isObject(newVal)) {
882
if (state.get(attrName, INIT_VALUE) === undefined) {
883
state.add(attrName, INIT_VALUE, newVal);
885
host._setStateVal(attrName, newVal);
897
* Sets multiple attribute values.
900
* @param {Object} attrs An object with attributes name/value pairs.
901
* @return {Object} A reference to the host object.
904
setAttrs : function(attrs, opts) {
905
return this._setAttrs(attrs, opts);
909
* Implementation behind the public setAttrs method, to set multiple attribute values.
913
* @param {Object} attrs An object with attributes name/value pairs.
914
* @return {Object} A reference to the host object.
917
_setAttrs : function(attrs, opts) {
918
for (var attr in attrs) {
919
if ( attrs.hasOwnProperty(attr) ) {
920
this.set(attr, attrs[attr]);
927
* Gets multiple attribute values.
930
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
931
* returned. If set to true, all attributes modified from their initial values are returned.
932
* @return {Object} An object with attribute name/value pairs.
934
getAttrs : function(attrs) {
935
return this._getAttrs(attrs);
939
* Implementation behind the public getAttrs method, to get multiple attribute values.
943
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
944
* returned. If set to true, all attributes modified from their initial values are returned.
945
* @return {Object} An object with attribute name/value pairs.
947
_getAttrs : function(attrs) {
951
modifiedOnly = (attrs === true);
953
attrs = (attrs && !modifiedOnly) ? attrs : O.keys(host._state.data.added);
955
for (i = 0, l = attrs.length; i < l; i++) {
956
// Go through get, to honor cloning/normalization
958
val = host.get(attr);
960
if (!modifiedOnly || host._getStateVal(attr) != host._state.get(attr, INIT_VALUE)) {
961
o[attr] = host.get(attr);
969
* Configures a group of attributes, and sets initial values.
972
* <strong>NOTE:</strong> This method does not isolate the configuration object by merging/cloning.
973
* The caller is responsible for merging/cloning the configuration object if required.
979
* @param {Object} cfgs An object with attribute name/configuration pairs.
980
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
981
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
982
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
983
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
984
* See <a href="#method_addAttr">addAttr</a>.
986
* @return {Object} A reference to the host object.
988
addAttrs : function(cfgs, values, lazy) {
989
var host = this; // help compression
992
host._tVals = host._normAttrVals(values);
993
host._addAttrs(cfgs, host._tVals, lazy);
994
host._tCfgs = host._tVals = null;
1001
* Implementation behind the public addAttrs method.
1003
* This method is invoked directly by get if it encounters a scenario
1004
* in which an attribute's valueFn attempts to obtain the
1005
* value an attribute in the same group of attributes, which has not yet
1006
* been added (on demand initialization).
1010
* @param {Object} cfgs An object with attribute name/configuration pairs.
1011
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
1012
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
1013
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
1014
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
1015
* See <a href="#method_addAttr">addAttr</a>.
1017
_addAttrs : function(cfgs, values, lazy) {
1018
var host = this, // help compression
1023
for (attr in cfgs) {
1024
if (cfgs.hasOwnProperty(attr)) {
1026
// Not Merging. Caller is responsible for isolating configs
1027
attrCfg = cfgs[attr];
1028
attrCfg.defaultValue = attrCfg.value;
1030
// Handle simple, complex and user values, accounting for read-only
1031
value = host._getAttrInitVal(attr, attrCfg, host._tVals);
1033
if (value !== undefined) {
1034
attrCfg.value = value;
1037
if (host._tCfgs[attr]) {
1038
delete host._tCfgs[attr];
1041
host.addAttr(attr, attrCfg, lazy);
1047
* Utility method to protect an attribute configuration
1048
* hash, by merging the entire object and the individual
1049
* attr config objects.
1051
* @method _protectAttrs
1053
* @param {Object} attrs A hash of attribute to configuration object pairs.
1054
* @return {Object} A protected version of the attrs argument.
1056
_protectAttrs : function(attrs) {
1058
attrs = Y.merge(attrs);
1059
for (var attr in attrs) {
1060
if (attrs.hasOwnProperty(attr)) {
1061
attrs[attr] = Y.merge(attrs[attr]);
1069
* Utility method to normalize attribute values. The base implementation
1070
* simply merges the hash to protect the original.
1072
* @method _normAttrVals
1073
* @param {Object} valueHash An object with attribute name/value pairs
1079
_normAttrVals : function(valueHash) {
1080
return (valueHash) ? Y.merge(valueHash) : null;
1084
* Returns the initial value of the given attribute from
1085
* either the default configuration provided, or the
1086
* over-ridden value if it exists in the set of initValues
1087
* provided and the attribute is not read-only.
1089
* @param {String} attr The name of the attribute
1090
* @param {Object} cfg The attribute configuration object
1091
* @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
1093
* @return {Any} The initial value of the attribute.
1095
* @method _getAttrInitVal
1098
_getAttrInitVal : function(attr, cfg, initValues) {
1100
// init value is provided by the user if it exists, else, provided by the config
1101
if (!cfg[READ_ONLY] && initValues && initValues.hasOwnProperty(attr)) {
1102
val = initValues[attr];
1105
valFn = cfg[VALUE_FN];
1109
valFn = this[valFn];
1112
val = valFn.call(this);
1122
* Returns an object with the configuration properties (and value)
1123
* for the given attrubute. If attrName is not provided, returns the
1124
* configuration properties for all attributes.
1126
* @method _getAttrCfg
1128
* @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
1129
* @return {Object} The configuration properties for the given attribute, or all attributes.
1131
_getAttrCfg : function(name) {
1133
data = this._state.data;
1138
Y.each(data, function(cfg, cfgProp) {
1141
o[cfgProp] = cfg[name];
1144
Y.each(cfg, function(attrCfg, attr) {
1145
o[attr] = o[attr] || {};
1146
o[attr][cfgProp] = attrCfg;
1156
// Basic prototype augment - no lazy constructor invocation.
1157
Y.mix(Attribute, EventTarget, false, null, 1);
1159
Y.Attribute = Attribute;
1162
}, '3.2.0' ,{requires:['event-custom']});