Attribute.js revision 8ab6c5a43c971d3f5b9f12ea6e5794a394a9a19c
/**
* Managed Attribute Provider
* @module attribute
*/
var O = Y.Object,
DOT = ".",
CHANGE = "Change",
GET = "get",
SET = "set",
GETTER = "getter",
SETTER = "setter",
VALUE = "value",
INIT_VALUE = "initValue",
READ_ONLY = "readOnly",
WRITE_ONCE = "writeOnce",
VALIDATOR = "validator",
/**
* <p>
* Attribute provides managed attribute support.
* </p>
* <p>
* The class is designed to be augmented onto a host class,
* initial configuration support and attribute change events.
* </p>
* <p>Attributes added to the host can:</p>
* <ul>
* <li>Be defined as read-only.</li>
* <li>Be defined as write-once.</li>
* <li>Be defined with a set function, used to manipulate
* values passed to Attribute's set method, before they are stored.</li>
* <li>Be defined with a validator function, to validate values before they are stored.</li>
* <li>Be defined with a get function, which can be used to manipulate stored values,
* before they are returned by Attribute's get method.</li>
* </ul>
*
* <p>See the <a href="#method_addAtt">addAttr</a> method, for details about how to add attributes with
* a specific configuration</p>
*
* @class Attribute
* @uses Event.Target
*/
function Attribute() {
}
/**
* <p>
* Adds an attribute, with the provided configuration to the host object. Intended
* to be used by the host object to setup it's set of available attributes.
* </p>
* <p>
* The config argument object literal supports the following optional properties:
* </p>
* <dl>
* <dt>value <Any></dt>
* <dd>The initial value to set on the attribute</dd>
* <dt>readOnly <Boolean></dt>
* <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
* cannot be set by invoking the set method.</dd>
* <dt>writeOnce <Boolean></dt>
* <dd>Whether or not the attribute is "write once". Attributes having writeOnce set to true,
* can only have their values set once, be it through the default configuration,
* constructor configuration arguments, or by invoking set.</dd>
* <dt>setter <Function></dt>
* <dd>The setter function to be invoked (within the context of the host object) before
* the attribute is stored by a call to the set method. The value returned by the
* set function will be the finally stored value.</dd>
* <dt>getter <Function></dt>
* <dd>The getter function to be invoked (within the context of the host object) before
* the stored values is returned to a user invoking the get method for the attribute.
* The value returned by the get function is the final value which will be returned to the
* user when they invoke get.</dd>
* <dt>validator <Function></dt>
* <dd>The validator function which is invoked prior to setting the stored value. Returning
* false from the validator function will prevent the value from being stored</dd>
* </dl>
*
* @method addAttr
*
* @param {String} name The attribute key
* @param {Object} config (optional) An object literal specifying the configuration for the attribute.
* <strong>NOTE:</strong> The config object is modified when adding an attribute,
* so if you need to protect the original values, you will need to merge or clone the object.
*
* @chainable
*/
var value,
if (config[READ_ONLY] && !hasValue) { Y.log('readOnly attribute: ' + name + ', added without an initial value. Value will be set on intial call to set', 'warn', 'attribute');}
if(hasValue) {
// We'll go through set, don't want to set value in _conf directory
}
if (hasValue) {
// Go through set, so that raw values get normalized/validated
}
return this;
},
/**
* Removes an attribute.
*
* @method removeAttr
* @param {String} name The attribute key
*/
removeAttr: function(name) {
},
/**
* Returns the current value of the attribute. If the attribute
* has been configured with a 'get' handler, this method will delegate
* to the 'get' handler to obtain the value of the attribute.
* The 'get' handler will be passed the current value of the attribute
* as the only argument.
*
* @method get
*
* @param {String} key The attribute whose value will be returned. If
* the value of the attribute is an Object, dot notation can be used to
* obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
*
* @return {Any} The current value of the attribute
*/
path,
val;
}
return val;
},
/**
* Sets the value of an attribute.
*
* @method set
* @chainable
*
* @param {String} name The name of the attribute. Note, if the
* value of the attribute is an Object, dot notation can be used
* to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
*
* @param {Any} value The value to apply to the attribute
*
* @param {Object} opts Optional event data. This object will be mixed into
* the event facade passed as the first argument to subscribers
* of attribute change events
*
* @return {Object} Reference to the host object
*/
},
/**
* Resets the given attribute or all attributes to the initial value.
*
* @method reset
* @param {String} name optional An attribute to reset. If omitted, all attributes are reset.
* @chainable
*/
if (name) {
} else {
this._set(n, v);
}, this);
}
return this;
},
/**
*
* @method _set
* @protected
* @chainable
*
* @return {Object} Reference to the host object
*/
},
/**
* Internal set implementation
*
* @method _setAttr
* @protected
* @chainable
*
* @param {String} name The name of the attribute. Note, if the
* value of the attribute is an Object, dot notation can be used
* to set the value of a property within the object
* (e.g. <code>set("x.y.z", 5)</code>), if the attribute has not
* been declared as an immutable attribute (see <a href="#property_CLONE">Attribute.CLONE</a>).
*
* @param {Any} value The value to apply to the attribute
*
* @param {Object} opts Optional event data. This object will be mixed into
* the event facade passed as the first argument to subscribers
* of attribute change events
*
* @param {boolean} force If true, allows the caller to set values for
* readOnly or writeOnce attributes which have already been set.
*
* @return {Object} Reference to the host object
*/
allowSet = true,
path,
}
if (!initialSet && !force) {
allowSet = false;
}
allowSet = false;
}
}
allowSet = false;
}
if (path) {
allowSet = false;
}
}
if (allowSet) {
}
return this;
},
/**
* <p>
* Alias for the Event.Target <a href="Event.Target.html#method_subscribe">subscribe</a> method.
* </p>
*
* <p>Subscribers using this method to listen for attribute change events will be notified just
* <strong>before</strong> the state of the attribute has been modified, and before the default handler has been
* invoked.</p>
*
* <p>The <a href="Event.Target.html#method_after">after</a> method, inherited from Event Target, can be used by subscribers
* who wish to be notified <strong>after</strong> the attribute's value has changed.</p>
*
* @param {String} type The event type. For attribute change events, the event type is "[Attribute Name]Change", e.g.
* for the attribute "enabled", the event type will be "enabledChange".
* @param {Function} fn The subscribed function to invoke
* @param {Object} context Optional execution context
* @param {Any*} args* 0..n additional arguments to append to supply to the subscribed function when the event fires.
* @method on
* @return {Event.Handle} The handle object for unsubscribing the subscriber from the event.
*/
on : function() {
},
/**
* Utility method to help setup the event payload and
* fire the attribute change event.
*
* @method _fireAttrChange
* @private
* @param {String} type The event name
* @param {Any} currVal The current value of the attribute
* @param {Any} newVal The new value of the attribute
* @param {String} attrName The name of the attribute
* @param {String} strFullPath The full path of the property being changed,
* if this is a sub-attribute value being change
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
*/
var eData = {
};
if (opts) {
}
},
/**
* Default handler implementation for set events
*
* @private
* @method _defAttrSet
* @param {Event.Facade} e The event object for the custom event
*/
_defAttrSet : function(e) {
if (setFn) {
}
}
// Store value
}
// Honor set normalization
} else {
Y.log('Validation failed. State not updated and stopImmediatePropagation called for attribute: ' + name + ' , value:' + val, 'warn', 'attribute');
// Prevent "after" listeners from being
// invoked since nothing changed.
}
},
/**
* Retrieves the sub value at the provided path,
* from the value object provided.
*
* @method _getSubAttrVal
* @private
*
* @param {Array} path A path array, specifying the object traversal path
* from which to obtain the sub value.
* @param {Object} val The object from which to extract the property value
* @return {Any} The value stored in the path or undefined if not found.
*/
i;
if (pl > 0) {
}
}
return val;
},
/**
* Sets the sub value at the provided path on the value object.
* Returns the modified value object, or undefined if the path is invalid.
*
*
*
* @method _setSubAttrVal
* @private
*
* @param {Array} path A path array, specifying the object traversal path
* at which to set the sub value.
* @param {Object} val The object on which to set the sub value.
* @param {Any} subval The sub value to set.
* @return {Object} The modified object, with the new sub value set, or
* undefined, if the path was invalid.
*/
i,
o;
if (leafIdx >= 0) {
o = val;
o = o[path[i]];
}
if (o !== undefined) {
} else {
}
}
return val;
},
/**
* Sets multiple attribute values.
*
* @method setAttrs
* @chainable
*/
}
}
return this;
},
/**
* Gets multiple attribute values.
*
* @method getAttrs
* @param {Array | Boolean} attrs Optional. An array of attribute names, whose values are required. If omitted, all attribute values are
* returned. If set to true, all attributes modified from their original values are returned.
*/
modifiedOnly = (attrs === true);
// Go through get, to honor cloning/normalization
}
}
return o;
},
/**
* Configures attributes, and sets initial values
*
* @method _initAttrs
* @protected
* @chainable
*
* @param {Object} values Name/value hash of initial values to apply. Values defined in the configuration hash will be over-written by the initial values hash unless read-only.
*/
if (cfgs) {
var attr,
// Not Merging. Caller is responsible for isolating configs
// Handle simple, complex and user values, accounting for read-only
}
}
}
}
return this;
},
/**
* Utility to split out regular attribute values
* from complex attribute values, so that complex
* attributes can be keyed by top level attribute name.
*
* @method _splitAttrValues
*
* @return {Object} Object literal with 2 properties - "simple" and "complex",
* containing simple and complex attribute values respectively keyed
* by attribute the top level attribute name.
* @private
*/
_splitAttrVals : function(valueHash) {
var vals = {},
subvals = {},
path,
attr,
v, k;
for (k in valueHash) {
if (valueHash.hasOwnProperty(k)) {
v[v.length] = {
};
} else {
}
}
}
},
/**
* Returns the initial value of the given attribute from
* either the default configuration provided, or the
* over-ridden value if it exists in the initValues
* hash provided, if the attribute is not read-only.
*
* @param {String} attr Attribute name
* @param {Object} cfg Default attribute configuration object literal
*
* @return {Any} Initial value of the attribute.
*
* @method _getAttrInitVal
* @private
*/
i,
l,
path,
// Simple Attributes
}
// Complex Attributes (complex values applied, after simple, incase both are set)
}
}
}
return val;
}
};
// Basic prototype augment - no lazy constructor invocation.