attribute-base-debug.js revision 7760b60cb5a901faeffead389cf312b2b4fbef0d
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The State class maintains state for a collection of named items, with
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * a varying number of properties defined.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * It avoids the need to create a separate class for the item, and separate instances
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * of these classes for each item, by storing the state in a 2 level hash table,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * improving performance when the number of items is likely to be large.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @constructor
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @class State
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Y.State = function() {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Hash of attributes
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @property data
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw this.data = {};
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States Y.State.prototype = {
12b65585e720714b31036daaa2b30eb76014048eGordon Ross * Adds a property to an item.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @method add
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param key {String} The name of the property.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param val {Any} The value of the property.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw var d = this.data;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Adds multiple properties to an item.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @method addAll
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * @param o {Object} A hash of property/value pairs.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (key in o) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Removes a property from an item.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @method remove
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param key {String} The property to remove.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw var d = this.data;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Removes multiple properties from an item, or remove the item completely.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @method removeAll
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param o {Object|Array} Collection of properties to delete. If not provided, the entire item is removed.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States var d = this.data;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Y.each(o || d, function(v, k) {
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States }
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * For a given item, returns the value of the property requested, or undefined if not found.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * @method get
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param key {String} Optional. The property value to retrieve.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * @return {Any} The value of the supplied property.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego var d = this.data;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States return (d[key] && name in d[key]) ? d[key][name] : undefined;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * For the given item, returns a disposable object with all of the
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * item's property/value pairs.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @method getAll
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @param name {String} The name of the item
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * @return {Object} An object with property/value pairs for the item.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego var d = this.data, o;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Y.each(d, function(v, k) {
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego if (name in d[k]) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw o = o || {};
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw o[k] = v[name];
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * The attribute module provides an augmentable Attribute implementation, which
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * adds configurable attributes and attribute change events to the class being
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * augmented. It also provides a State class, which is used internally by Attribute,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * but can also be used independently to provide a name/property/value data structure to
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * store state.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @module attribute
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw var O = Y.Object,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw // Externally configurable props
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw // Used for internal state management
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States IS_LAZY_ADD = "isLazyAdd",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw // Properties which can be changed after the attribute has been added.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States MODIFIABLE[READ_ONLY] = 1;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States MODIFIABLE[GETTER] = 1;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <p>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Attribute provides configurable attribute support along with attribute change events. It is designed to be
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * augmented onto a host class, and provides the host with the ability to configure attributes to store and retrieve state,
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * along with attribute change events.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * </p>
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * <p>For example, attributes added to the host can be configured:</p>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <ul>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <li>As read only.</li>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <li>As write once.</li>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <li>With a setter function, which can be used to manipulate
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <li>With a getter function, which can be used to manipulate stored values,
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <li>With a validator function, to validate values before they are stored.</li>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * </ul>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * options available for attributes</p>.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <p><strong>NOTE:</strong> Most implementations will be better off extending the <a href="Base.html">Base</a> class,
12b65585e720714b31036daaa2b30eb76014048eGordon Ross * instead of augmenting Attribute directly. Base augments Attribute and will handle the initial configuration
12b65585e720714b31036daaa2b30eb76014048eGordon Ross * of attributes for derived classes, accounting for values passed into the constructor.</p>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @class Attribute
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @uses EventTarget
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States function Attribute() {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Y.log('Attribute constructor called', 'info', 'attribute');
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw // Perf tweak - avoid creating event literals if not required.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States host._ATTR_E_FACADE = {};
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States EventTarget.call(host, {emitFacade:true});
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw // _conf maintained for backwards compat
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States host._requireAddAttr = host._requireAddAttr || false;
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas * <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2eas * <p>You can return this value from your setter if you wish to combine validator and setter
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * functionality into a single setter function, which either returns the massaged value to be stored or
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Attribute.INVALID_VALUE to prevent invalid values from being stored.</p>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @property Attribute.INVALID_VALUE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * @type Object
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States INVALID_VALUE = Attribute.INVALID_VALUE;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The list of properties which can be configured for
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * each attribute (e.g. setter, getter, writeOnce etc.).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This property is used internally as a whitelist for faster
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Y.mix operations.
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * @property Attribute._ATTR_CFG
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @type Array
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * @protected
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw Attribute._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BROADCAST];
7f667e74610492ddbce8ce60f52ece95d2401949jose borrego * Adds an attribute with the provided configuration to the host object.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * </p>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The config argument object supports the following properties:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>value <Any></dt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dd>The initial value to set on the attribute</dd>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>valueFn <Function></dt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dd>A function, which will return the initial value to set on the attribute. This is useful
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * for cases where the attribute configuration is defined statically, but needs to
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * reference the host instance ("this") to obtain an initial value.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If defined, this precedence over the value property.</dd>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>readOnly <boolean></dt>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * cannot be modified by invoking the set method.</dd>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>writeOnce <boolean></dt>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <dd>Whether or not the attribute is "write once". Attributes having writeOnce set to true,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * can only have their values set once, be it through the default configuration,
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * constructor configuration arguments, or by invoking set.</dd>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <dt>setter <Function></dt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dd>The setter function used to massage or normalize the value passed to the set method for the attribute.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The value returned by the setter will be the final stored value. Returning
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the value from being stored.</dd>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>getter <Function></dt>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dd>The getter function used to massage or normalize the value returned by the get method for the attribute.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The value returned by the getter function is the value which will be returned to the user when they
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * invoke get.</dd>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>validator <Function></dt>
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * <dd>The validator function invoked prior to setting the stored value. Returning
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * false from the validator function will prevent the value from being stored.</dd>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * <dt>broadcast <int></dt>
* <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
* This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through
* <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
* @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
* <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need
* @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set).
if (host._stateProxy && host._stateProxy[name]) { Y.log('addAttr: ' + name + ' exists on the _stateProxy object. The newly added attribute will override the use of _stateProxy for this attribute', 'warn', 'attribute'); }
if (host.attrAdded(name) && !state.get(name, IS_LAZY_ADD)) { Y.log('Attribute: ' + name + ' already exists. Cannot add it again without removing it first', 'warn', 'attribute'); }
if (config.readOnly && !hasValue) { Y.log('readOnly attribute: ' + name + ', added without an initial value. Value will be set on initial call to set', 'warn', 'attribute');}
if(hasValue) {
if (hasValue) {
// Go through set, so that raw values get normalized/validated
return host;
* @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.
* @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
if (!host.attrAdded(name)) {Y.log('Attribute modifyAttr:' + name + ' has not been added. Use addAttr to add the attribute', 'warn', 'attribute');}
* dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
* @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
if (name) {
}, host);
return host;
* Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
path,
val,
cfg;
cfg = {};
return val;
var allowSet = true,
path,
allowSet = false;
allowSet = false;
if (allowSet) {
if (path) {
allowSet = false;
if (allowSet) {
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
var host = this,
queuable:false,
silent:true,
_defAttrChangeFn : function(e) {
Y.log('State not updated and stopImmediatePropagation called for attribute: ' + e.attrName + ' , value:' + e.newVal, 'warn', 'attribute');
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
var host = this,
allowSet = true,
if (validator) {
if (setter) {
Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
allowSet = false;
Y.log('Attribute: ' + attrName + ', raw value: ' + newVal + ' modified by setter to:' + retVal, 'info', 'attribute');
if (allowSet) {
allowSet = false;
allowSet = false;
return allowSet;
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
var host = this,
// Go through get, to honor cloning/normalization
* @param {Object} cfgs An object with attribute name/configuration pairs.
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
if (cfgs) {
return host;
* @param {Object} cfgs An object with attribute name/configuration pairs.
* @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
* Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
* @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
* Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
attr,
* @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
return val;