3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('attribute-base', function(Y) {
10
* The State class maintains state for a collection of named items, with
11
* a varying number of properties defined.
13
* It avoids the need to create a separate class for the item, and separate instances
14
* of these classes for each item, by storing the state in a 2 level hash table,
15
* improving performance when the number of items is likely to be large.
20
Y.State = function() {
31
* Adds a property to an item.
34
* @param name {String} The name of the item.
35
* @param key {String} The name of the property.
36
* @param val {Any} The value of the property.
38
add : function(name, key, val) {
40
d[key] = d[key] || {};
45
* Adds multiple properties to an item.
48
* @param name {String} The name of the item.
49
* @param o {Object} A hash of property/value pairs.
51
addAll: function(name, o) {
54
if (o.hasOwnProperty(key)) {
55
this.add(name, key, o[key]);
61
* Removes a property from an item.
64
* @param name {String} The name of the item.
65
* @param key {String} The property to remove.
67
remove: function(name, key) {
69
if (d[key] && (name in d[key])) {
75
* Removes multiple properties from an item, or remove the item completely.
78
* @param name {String} The name of the item.
79
* @param o {Object|Array} Collection of properties to delete. If not provided, the entire item is removed.
81
removeAll: function(name, o) {
84
Y.each(o || d, function(v, k) {
85
if(Y.Lang.isString(k)) {
94
* For a given item, returns the value of the property requested, or undefined if not found.
97
* @param name {String} The name of the item
98
* @param key {String} Optional. The property value to retrieve.
99
* @return {Any} The value of the supplied property.
101
get: function(name, key) {
103
return (d[key] && name in d[key]) ? d[key][name] : undefined;
107
* For the given item, returns a disposable object with all of the
108
* item's property/value pairs.
111
* @param name {String} The name of the item
112
* @return {Object} An object with property/value pairs for the item.
114
getAll : function(name) {
115
var d = this.data, o;
117
Y.each(d, function(v, k) {
128
* The attribute module provides an augmentable Attribute implementation, which
129
* adds configurable attributes and attribute change events to the class being
130
* augmented. It also provides a State class, which is used internally by Attribute,
131
* but can also be used independently to provide a name/property/value data structure to
138
* The attribute-base submodule provides core attribute handling support, with everything
139
* aside from complex attribute handling in the provider's constructor.
142
* @submodule attribute-base
146
EventTarget = Y.EventTarget,
151
// Externally configurable props
154
READ_ONLY = "readOnly",
155
WRITE_ONCE = "writeOnce",
156
INIT_ONLY = "initOnly",
157
VALIDATOR = "validator",
159
VALUE_FN = "valueFn",
160
BROADCAST = "broadcast",
161
LAZY_ADD = "lazyAdd",
162
BYPASS_PROXY = "_bypassProxy",
164
// Used for internal state management
166
INITIALIZING = "initializing",
167
INIT_VALUE = "initValue",
168
PUBLISHED = "published",
169
DEF_VALUE = "defaultValue",
171
IS_LAZY_ADD = "isLazyAdd",
177
// Properties which can be changed after the attribute has been added.
178
MODIFIABLE[READ_ONLY] = 1;
179
MODIFIABLE[WRITE_ONCE] = 1;
180
MODIFIABLE[GETTER] = 1;
181
MODIFIABLE[BROADCAST] = 1;
185
* Attribute provides configurable attribute support along with attribute change events. It is designed to be
186
* augmented on to a host class, and provides the host with the ability to configure attributes to store and retrieve state,
187
* along with attribute change events.
189
* <p>For example, attributes added to the host can be configured:</p>
191
* <li>As read only.</li>
192
* <li>As write once.</li>
193
* <li>With a setter function, which can be used to manipulate
194
* values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
195
* <li>With a getter function, which can be used to manipulate stored values,
196
* before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
197
* <li>With a validator function, to validate values before they are stored.</li>
200
* <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
201
* options available for attributes</p>.
203
* <p><strong>NOTE:</strong> Most implementations will be better off extending the <a href="Base.html">Base</a> class,
204
* instead of augmenting Attribute directly. Base augments Attribute and will handle the initial configuration
205
* of attributes for derived classes, accounting for values passed into the constructor.</p>
208
* @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>). These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
209
* @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>). These are not merged/cloned. The caller is responsible for isolating user provided values if required.
210
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
213
function Attribute(attrs, values, lazy) {
215
var host = this; // help compression
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
this._initAttrs(attrs, values, lazy);
232
* <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
234
* <p>You can return this value from your setter if you wish to combine validator and setter
235
* functionality into a single setter function, which either returns the massaged value to be stored or
236
* Attribute.INVALID_VALUE to prevent invalid values from being stored.</p>
238
* @property INVALID_VALUE
243
Attribute.INVALID_VALUE = {};
244
INVALID_VALUE = Attribute.INVALID_VALUE;
247
* The list of properties which can be configured for
248
* each attribute (e.g. setter, getter, writeOnce etc.).
250
* This property is used internally as a whitelist for faster
253
* @property _ATTR_CFG
258
Attribute._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BROADCAST, BYPASS_PROXY];
260
Attribute.prototype = {
263
* Adds an attribute with the provided configuration to the host object.
266
* The config argument object supports the following properties:
270
* <dt>value <Any></dt>
271
* <dd>The initial value to set on the attribute</dd>
273
* <dt>valueFn <Function | String></dt>
275
* <p>A function, which will return the initial value to set on the attribute. This is useful
276
* for cases where the attribute configuration is defined statically, but needs to
277
* reference the host instance ("this") to obtain an initial value. If both the value and valueFn properties are defined,
278
* the value returned by the valueFn has precedence over the value property, unless it returns undefined, in which
279
* case the value property is used.</p>
281
* <p>valueFn can also be set to a string, representing the name of the instance method to be used to retrieve the value.</p>
284
* <dt>readOnly <boolean></dt>
285
* <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
286
* cannot be modified by invoking the set method.</dd>
288
* <dt>writeOnce <boolean> or <string></dt>
290
* Whether or not the attribute is "write once". Attributes having writeOnce set to true,
291
* can only have their values set once, be it through the default configuration,
292
* constructor configuration arguments, or by invoking set.
293
* <p>The writeOnce attribute can also be set to the string "initOnly", in which case the attribute can only be set during initialization
294
* (when used with Base, this means it can only be set during construction)</p>
297
* <dt>setter <Function | String></dt>
299
* <p>The setter function used to massage or normalize the value passed to the set method for the attribute.
300
* The value returned by the setter will be the final stored value. Returning
301
* <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
302
* the value from being stored.
305
* <p>setter can also be set to a string, representing the name of the instance method to be used as the setter function.</p>
308
* <dt>getter <Function | String></dt>
311
* The getter function used to massage or normalize the value returned by the get method for the attribute.
312
* The value returned by the getter function is the value which will be returned to the user when they
316
* <p>getter can also be set to a string, representing the name of the instance method to be used as the getter function.</p>
319
* <dt>validator <Function | String></dt>
322
* The validator function invoked prior to setting the stored value. Returning
323
* false from the validator function will prevent the value from being stored.
326
* <p>validator can also be set to a string, representing the name of the instance method to be used as the validator function.</p>
329
* <dt>broadcast <int></dt>
330
* <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
331
* valid values. By default attribute change events are not broadcast.</dd>
333
* <dt>lazyAdd <boolean></dt>
334
* <dd>Whether or not to delay initialization of the attribute until the first call to get/set it.
335
* This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through
336
* the <a href="#method_addAttrs">addAttrs</a> method.</dd>
340
* <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
341
* the context ("this") set to the host object.</p>
343
* <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>
347
* @param {String} name The name of the attribute.
348
* @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
351
* <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need
352
* to protect the original values, you will need to merge the object.
355
* @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set).
357
* @return {Object} A reference to the host object.
361
addAttr: function(name, config, lazy) {
364
var host = this, // help compression
369
lazy = (LAZY_ADD in config) ? config[LAZY_ADD] : lazy;
371
if (lazy && !host.attrAdded(name)) {
372
state.add(name, LAZY, config || {});
373
state.add(name, ADDED, true);
377
if (!host.attrAdded(name) || state.get(name, IS_LAZY_ADD)) {
379
config = config || {};
381
hasValue = (VALUE in config);
385
// We'll go through set, don't want to set value in config directly
386
value = config.value;
391
config.initializing = true;
393
state.addAll(name, config);
396
// Go through set, so that raw values get normalized/validated
397
host.set(name, value);
400
state.remove(name, INITIALIZING);
408
* Checks if the given attribute has been added to the host
411
* @param {String} name The name of the attribute to check.
412
* @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.
414
attrAdded: function(name) {
415
return !!this._state.get(name, ADDED);
419
* Updates the configuration of an attribute which has already been added.
421
* The properties which can be modified through this interface are limited
422
* to the following subset of attributes, which can be safely modified
423
* after a value has already been set on the attribute: readOnly, writeOnce,
424
* broadcast and getter.
427
* @param {String} name The name of the attribute whose configuration is to be updated.
428
* @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
430
modifyAttr: function(name, config) {
431
var host = this, // help compression
434
if (host.attrAdded(name)) {
436
if (host._isLazyAttr(name)) {
437
host._addLazyAttr(name);
441
for (prop in config) {
442
if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
443
state.add(name, prop, config[prop]);
445
// If we reconfigured broadcast, need to republish
446
if (prop === BROADCAST) {
447
state.remove(name, PUBLISHED);
456
* Removes an attribute from the host object
459
* @param {String} name The name of the attribute to be removed.
461
removeAttr: function(name) {
462
this._state.removeAll(name);
466
* Returns the current value of the attribute. If the attribute
467
* has been configured with a 'getter' function, this method will delegate
468
* to the 'getter' to obtain the value of the attribute.
472
* @param {String} name The name of the attribute. If the value of the attribute is an Object,
473
* dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
475
* @return {Any} The value of the attribute
477
get : function(name) {
478
return this._getAttr(name);
482
* Checks whether or not the attribute is one which has been
483
* added lazily and still requires initialization.
485
* @method _isLazyAttr
487
* @param {String} name The name of the attribute
488
* @return {boolean} true if it's a lazily added attribute, false otherwise.
490
_isLazyAttr: function(name) {
491
return this._state.get(name, LAZY);
495
* Finishes initializing an attribute which has been lazily added.
497
* @method _addLazyAttr
499
* @param {Object} name The name of the attribute
501
_addLazyAttr: function(name) {
502
var state = this._state,
503
lazyCfg = state.get(name, LAZY);
505
state.add(name, IS_LAZY_ADD, true);
506
state.remove(name, LAZY);
507
this.addAttr(name, lazyCfg);
511
* Sets the value of an attribute.
516
* @param {String} name The name of the attribute. If the
517
* current value of the attribute is an Object, dot notation can be used
518
* to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
520
* @param {Any} value The value to set the attribute to.
522
* @param {Object} opts (Optional) Optional event data to be mixed into
523
* the event facade passed to subscribers of the attribute's change event. This
524
* can be used as a flexible way to identify the source of a call to set, allowing
525
* the developer to distinguish between set called internally by the host, vs.
526
* set called externally by the application developer.
528
* @return {Object} A reference to the host object.
530
set : function(name, val, opts) {
531
return this._setAttr(name, val, opts);
535
* Resets the attribute (or all attributes) to its initial value, as long as
536
* the attribute is not readOnly, or writeOnce.
539
* @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
540
* @return {Object} A reference to the host object.
543
reset : function(name) {
544
var host = this, // help compression
548
if (host._isLazyAttr(name)) {
549
host._addLazyAttr(name);
551
host.set(name, host._state.get(name, INIT_VALUE));
553
added = host._state.data.added;
554
Y.each(added, function(v, n) {
562
* Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
568
* @param {String} name The name of the attribute.
569
* @param {Any} val The value to set the attribute to.
570
* @param {Object} opts (Optional) Optional event data to be mixed into
571
* the event facade passed to subscribers of the attribute's change event.
572
* @return {Object} A reference to the host object.
574
_set : function(name, val, opts) {
575
return this._setAttr(name, val, opts, true);
579
* Provides the common implementation for the public get method,
580
* allowing Attribute hosts to over-ride either method.
582
* See <a href="#method_get">get</a> for argument details.
588
* @param {String} name The name of the attribute.
589
* @return {Any} The value of the attribute.
591
_getAttr : function(name) {
592
var host = this, // help compression
600
if (name.indexOf(DOT) !== -1) {
601
path = name.split(DOT);
605
// On Demand - Should be rare - handles out of order valueFn references
606
if (host._tCfgs && host._tCfgs[name]) {
608
cfg[name] = host._tCfgs[name];
609
delete host._tCfgs[name];
610
host._addAttrs(cfg, host._tVals);
614
if (host._isLazyAttr(name)) {
615
host._addLazyAttr(name);
618
val = host._getStateVal(name);
619
getter = state.get(name, GETTER);
621
if (getter && !getter.call) {
622
getter = this[getter];
625
val = (getter) ? getter.call(host, val, fullName) : val;
626
val = (path) ? O.getValue(val, path) : val;
632
* Provides the common implementation for the public set and protected _set methods.
634
* See <a href="#method_set">set</a> for argument details.
640
* @param {String} name The name of the attribute.
641
* @param {Any} value The value to set the attribute to.
642
* @param {Object} opts (Optional) Optional event data to be mixed into
643
* the event facade passed to subscribers of the attribute's change event.
644
* @param {boolean} force If true, allows the caller to set values for
645
* readOnly or writeOnce attributes which have already been set.
647
* @return {Object} A reference to the host object.
649
_setAttr : function(name, val, opts, force) {
652
stateProxy = this._stateProxy,
661
if (name.indexOf(DOT) !== -1) {
663
path = name.split(DOT);
667
if (this._isLazyAttr(name)) {
668
this._addLazyAttr(name);
671
initialSet = (!data.value || !(name in data.value));
673
if (stateProxy && name in stateProxy && !this._state.get(name, BYPASS_PROXY)) {
674
// 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?
678
if (this._requireAddAttr && !this.attrAdded(name)) {
681
writeOnce = state.get(name, WRITE_ONCE);
682
initializing = state.get(name, INITIALIZING);
684
if (!initialSet && !force) {
690
if (state.get(name, READ_ONLY)) {
695
if (!initializing && !force && writeOnce === INIT_ONLY) {
700
// Don't need currVal if initialSet (might fail in custom getter if it always expects a non-undefined/non-null value)
702
currVal = this.get(name);
706
val = O.setValue(Y.clone(currVal), path, val);
708
if (val === undefined) {
715
this._setAttrVal(name, strPath, currVal, val);
717
this._fireAttrChange(name, strPath, currVal, val, opts);
727
* Utility method to help setup the event payload and fire the attribute change event.
729
* @method _fireAttrChange
731
* @param {String} attrName The name of the attribute
732
* @param {String} subAttrName The full path of the property being changed,
733
* if this is a sub-attribute value being change. Otherwise null.
734
* @param {Any} currVal The current value of the attribute
735
* @param {Any} newVal The new value of the attribute
736
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
738
_fireAttrChange : function(attrName, subAttrName, currVal, newVal, opts) {
740
eventName = attrName + CHANGE,
744
if (!state.get(attrName, PUBLISHED)) {
745
host.publish(eventName, {
747
defaultTargetOnly: true,
748
defaultFn:host._defAttrChangeFn,
750
broadcast : state.get(attrName, BROADCAST)
752
state.add(attrName, PUBLISHED, true);
755
facade = (opts) ? Y.merge(opts) : host._ATTR_E_FACADE;
757
// Not using the single object signature for fire({type:..., newVal:...}), since
758
// we don't want to override type. Changed to the fire(type, {newVal:...}) signature.
760
// facade.type = eventName;
761
facade.attrName = attrName;
762
facade.subAttrName = subAttrName;
763
facade.prevVal = currVal;
764
facade.newVal = newVal;
766
// host.fire(facade);
767
host.fire(eventName, facade);
771
* Default function for attribute change events.
774
* @method _defAttrChangeFn
775
* @param {EventFacade} e The event object for attribute change events.
777
_defAttrChangeFn : function(e) {
778
if (!this._setAttrVal(e.attrName, e.subAttrName, e.prevVal, e.newVal)) {
779
// Prevent "after" listeners from being invoked since nothing changed.
780
e.stopImmediatePropagation();
782
e.newVal = this.get(e.attrName);
787
* Gets the stored value for the attribute, from either the
788
* internal state object, or the state proxy if it exits
790
* @method _getStateVal
792
* @param {String} name The name of the attribute
793
* @return {Any} The stored value of the attribute
795
_getStateVal : function(name) {
796
var stateProxy = this._stateProxy;
797
return stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY) ? stateProxy[name] : this._state.get(name, VALUE);
801
* Sets the stored value for the attribute, in either the
802
* internal state object, or the state proxy if it exits
804
* @method _setStateVal
806
* @param {String} name The name of the attribute
807
* @param {Any} value The value of the attribute
809
_setStateVal : function(name, value) {
810
var stateProxy = this._stateProxy;
811
if (stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY)) {
812
stateProxy[name] = value;
814
this._state.add(name, VALUE, value);
819
* Updates the stored value of the attribute in the privately held State object,
820
* if validation and setter passes.
822
* @method _setAttrVal
824
* @param {String} attrName The attribute name.
825
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
826
* @param {Any} prevVal The currently stored value of the attribute.
827
* @param {Any} newVal The value which is going to be stored.
829
* @return {booolean} true if the new attribute value was stored, false if not.
831
_setAttrVal : function(attrName, subAttrName, prevVal, newVal) {
837
validator = state.get(attrName, VALIDATOR),
838
setter = state.get(attrName, SETTER),
839
initializing = state.get(attrName, INITIALIZING),
840
prevValRaw = this._getStateVal(attrName),
842
name = subAttrName || attrName,
847
if (!validator.call) {
848
// Assume string - trying to keep critical path tight, so avoiding Lang check
849
validator = this[validator];
852
valid = validator.call(host, newVal, name);
854
if (!valid && initializing) {
855
newVal = state.get(attrName, DEF_VALUE);
856
valid = true; // Assume it's valid, for perf.
861
if (!validator || valid) {
864
// Assume string - trying to keep critical path tight, so avoiding Lang check
865
setter = this[setter];
868
retVal = setter.call(host, newVal, name);
870
if (retVal === INVALID_VALUE) {
872
} else if (retVal !== undefined){
879
if(!subAttrName && (newVal === prevValRaw) && !Lang.isObject(newVal)) {
883
if (state.get(attrName, INIT_VALUE) === undefined) {
884
state.add(attrName, INIT_VALUE, newVal);
886
host._setStateVal(attrName, newVal);
898
* Sets multiple attribute values.
901
* @param {Object} attrs An object with attributes name/value pairs.
902
* @return {Object} A reference to the host object.
905
setAttrs : function(attrs, opts) {
906
return this._setAttrs(attrs, opts);
910
* Implementation behind the public setAttrs method, to set multiple attribute values.
914
* @param {Object} attrs An object with attributes name/value pairs.
915
* @return {Object} A reference to the host object.
918
_setAttrs : function(attrs, opts) {
919
for (var attr in attrs) {
920
if ( attrs.hasOwnProperty(attr) ) {
921
this.set(attr, attrs[attr]);
928
* Gets multiple attribute values.
931
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
932
* returned. If set to true, all attributes modified from their initial values are returned.
933
* @return {Object} An object with attribute name/value pairs.
935
getAttrs : function(attrs) {
936
return this._getAttrs(attrs);
940
* Implementation behind the public getAttrs method, to get multiple attribute values.
944
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
945
* returned. If set to true, all attributes modified from their initial values are returned.
946
* @return {Object} An object with attribute name/value pairs.
948
_getAttrs : function(attrs) {
952
modifiedOnly = (attrs === true);
954
attrs = (attrs && !modifiedOnly) ? attrs : O.keys(host._state.data.added);
956
for (i = 0, l = attrs.length; i < l; i++) {
957
// Go through get, to honor cloning/normalization
959
val = host.get(attr);
961
if (!modifiedOnly || host._getStateVal(attr) != host._state.get(attr, INIT_VALUE)) {
962
o[attr] = host.get(attr);
970
* Configures a group of attributes, and sets initial values.
973
* <strong>NOTE:</strong> This method does not isolate the configuration object by merging/cloning.
974
* The caller is responsible for merging/cloning the configuration object if required.
980
* @param {Object} cfgs An object with attribute name/configuration pairs.
981
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
982
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
983
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
984
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
985
* See <a href="#method_addAttr">addAttr</a>.
987
* @return {Object} A reference to the host object.
989
addAttrs : function(cfgs, values, lazy) {
990
var host = this; // help compression
993
host._tVals = host._normAttrVals(values);
994
host._addAttrs(cfgs, host._tVals, lazy);
995
host._tCfgs = host._tVals = null;
1002
* Implementation behind the public addAttrs method.
1004
* This method is invoked directly by get if it encounters a scenario
1005
* in which an attribute's valueFn attempts to obtain the
1006
* value an attribute in the same group of attributes, which has not yet
1007
* been added (on demand initialization).
1011
* @param {Object} cfgs An object with attribute name/configuration pairs.
1012
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
1013
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
1014
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
1015
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
1016
* See <a href="#method_addAttr">addAttr</a>.
1018
_addAttrs : function(cfgs, values, lazy) {
1019
var host = this, // help compression
1024
for (attr in cfgs) {
1025
if (cfgs.hasOwnProperty(attr)) {
1027
// Not Merging. Caller is responsible for isolating configs
1028
attrCfg = cfgs[attr];
1029
attrCfg.defaultValue = attrCfg.value;
1031
// Handle simple, complex and user values, accounting for read-only
1032
value = host._getAttrInitVal(attr, attrCfg, host._tVals);
1034
if (value !== undefined) {
1035
attrCfg.value = value;
1038
if (host._tCfgs[attr]) {
1039
delete host._tCfgs[attr];
1042
host.addAttr(attr, attrCfg, lazy);
1048
* Utility method to protect an attribute configuration
1049
* hash, by merging the entire object and the individual
1050
* attr config objects.
1052
* @method _protectAttrs
1054
* @param {Object} attrs A hash of attribute to configuration object pairs.
1055
* @return {Object} A protected version of the attrs argument.
1057
_protectAttrs : function(attrs) {
1059
attrs = Y.merge(attrs);
1060
for (var attr in attrs) {
1061
if (attrs.hasOwnProperty(attr)) {
1062
attrs[attr] = Y.merge(attrs[attr]);
1070
* Utility method to normalize attribute values. The base implementation
1071
* simply merges the hash to protect the original.
1073
* @method _normAttrVals
1074
* @param {Object} valueHash An object with attribute name/value pairs
1080
_normAttrVals : function(valueHash) {
1081
return (valueHash) ? Y.merge(valueHash) : null;
1085
* Returns the initial value of the given attribute from
1086
* either the default configuration provided, or the
1087
* over-ridden value if it exists in the set of initValues
1088
* provided and the attribute is not read-only.
1090
* @param {String} attr The name of the attribute
1091
* @param {Object} cfg The attribute configuration object
1092
* @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
1094
* @return {Any} The initial value of the attribute.
1096
* @method _getAttrInitVal
1099
_getAttrInitVal : function(attr, cfg, initValues) {
1101
// init value is provided by the user if it exists, else, provided by the config
1102
if (!cfg[READ_ONLY] && initValues && initValues.hasOwnProperty(attr)) {
1103
val = initValues[attr];
1106
valFn = cfg[VALUE_FN];
1110
valFn = this[valFn];
1113
val = valFn.call(this);
1123
* Returns an object with the configuration properties (and value)
1124
* for the given attrubute. If attrName is not provided, returns the
1125
* configuration properties for all attributes.
1127
* @method _getAttrCfg
1129
* @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
1130
* @return {Object} The configuration properties for the given attribute, or all attributes.
1132
_getAttrCfg : function(name) {
1134
data = this._state.data;
1139
Y.each(data, function(cfg, cfgProp) {
1142
o[cfgProp] = cfg[name];
1145
Y.each(cfg, function(attrCfg, attr) {
1146
o[attr] = o[attr] || {};
1147
o[attr][cfgProp] = attrCfg;
1157
* Utility method to set up initial attributes defined during construction, either through the constructor.ATTRS property, or explicitly passed in.
1159
* @method _initAttrs
1161
* @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>). These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
1162
* @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>). These are not merged/cloned. The caller is responsible for isolating user provided values if required.
1163
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
1165
_initAttrs : function(attrs, values, lazy) {
1166
// ATTRS support for Node, which is not Base based
1167
attrs = attrs || this.constructor.ATTRS;
1170
if ( attrs && !(Base && Y.instanceOf(this, Base))) {
1171
this.addAttrs(this._protectAttrs(attrs), values, lazy);
1176
// Basic prototype augment - no lazy constructor invocation.
1177
Y.mix(Attribute, EventTarget, false, null, 1);
1179
Y.Attribute = Attribute;
1182
}, '3.4.1' ,{requires:['event-custom']});