attribute.js revision c953f6562bc9790b8b04f97ffc6866583e1694f2
/**
* Managed Attribute Provider
* @module attribute
*/
/**
* Maintain state for a collection of items. Individual properties
* are stored in hash tables. This is instead of having state objects
* for each item in the collection. For large collections, especially
* changing ones, this approach may perform better.
*
* @constructor
* @class State
*/
Y.State = function() {
/**
* Hash of attributes
* @property data
*/
this.data = {};
};
/**
* Add an item with the property and value provided
*
* @method add
* @param name {string} identifier for this attribute
* @param key {string} property identifier
* @param val {Any} property value
*/
var d = this.data;
},
/**
* Add an item with all of the properties in the supplied object.
*
* @method addAll
* @param name {string} identifier for this attribute
* @param o hash of attributes
*/
var key;
for (key in o) {
if (o.hasOwnProperty(key)) {
}
}
},
/**
* Remove the given key for a specific item
*
* @method remove
* @param name {string} name of attribute
* @param o {string} The key to delete.
*/
var d = this.data;
}
},
/**
* Remove entire item, or optionally specified fields
*
* @method removeAll
* @param name {string} name of attribute
* @param o {object|array} Collection of keys to delete. If not provided, entire item is removed.
*/
var d = this.data;
Y.each(o || d, function(v, k) {
} else {
}
}, this);
},
/**
* For a given item, returns the value of the attribute requested, or undefined if not found.
*
* @method get
* @param name {string} name of attribute
* @param key {string} optional The attribute value to retrieve.
* @return The value of the supplied key.
*/
var d = this.data;
},
/**
* For a given item, returns a disposable object with all attribute
*
* @method getAll
* @param name {string} name of attribute
* @return An object withall data.
*/
var d = this.data, o;
Y.each(d, function(v, k) {
if (name in d[k]) {
o = o || {};
o[k] = v[name];
}
}, this);
return o;
}
};
/**
* Managed Attribute Provider
* @module attribute
*/
var O = Y.Object,
DOT = ".",
CHANGE = "Change",
GETTER = "getter",
SETTER = "setter",
VALUE = "value",
ADDED = "added",
INITIALIZING = "initializing",
INIT_VALUE = "initValue",
READ_ONLY = "readOnly",
WRITE_ONCE = "writeOnce",
VALIDATOR = "validator",
PUBLISHED = "published",
EventTarget = Y.EventTarget;
/**
* <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 setter 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 getter 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() {
this._ATTR_E_FACADE = {};
}
Attribute.INVALID_VALUE = {};
/**
* <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 setter method. The value returned by the
* setter 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 getter method for the attribute.
* The value returned by the getter 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(hasValue) {
// We'll go through set, don't want to set value in _conf directory
}
config[INITIALIZING] = true;
if (hasValue) {
// Go through set, so that raw values get normalized/validated
}
} else {
}
return this;
},
/**
* Tests if the given attribute has been added to the host
*
* @method attrAdded
* @param {String} name The name of the attribute to check.
* @return boolean, true if an attribute with the given name has been added.
*/
},
/**
* 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 'getter' function, this method will delegate
* to the 'getter' to obtain the value of the attribute.
* The 'getter' will be passed the current value of the attribute
* as the only argument.
*
* @method get
*
* @param {String} name 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>).
*
* @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
*/
var allowSet = true,
path,
}
} else {
if (!initialSet && !force) {
allowSet = false;
}
allowSet = false;
}
}
if (allowSet) {
if (path) {
allowSet = false;
}
}
if (allowSet) {
} else {
}
}
}
}
return this;
},
/**
* Utility method to help setup the event payload and
* fire the attribute change event.
*
* @method _fireAttrChange
* @private
* @param {String} attrName The name of the attribute
* @param {String} subAttrName The full path of the property being changed,
* if this is a sub-attribute value being change. Otherwise null.
* @param {Any} currVal The current value of the attribute
* @param {Any} newVal The new value of the attribute
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
*/
}
},
/**
* Default handler implementation for Attribute change events
*
* @private
* @method _defAttrChangeFn
* @param {Event.Facade} e The event object for the custom event
*/
_defAttrChangeFn : function(e) {
// Prevent "after" listeners from being invoked since nothing changed.
} else {
}
},
/**
* Updates the stored value of the attribute in the privately held State object,
* if validation and setter passes.
*
* @method _setAttrVal
* @private
* @param {String} attrName The attribute name.
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
* @param {Any} prevVal The currently stored value of the attribute.
* @param {Any} newVal The value which is going to be stored.
*
* @return {booolean} true if the new attribute value was stored, false if not.
*/
var allowSet = true,
if (setter) {
if (retVal === INVALID_VALUE) {
allowSet = false;
}
}
if (allowSet) {
allowSet = false;
} else {
// Store value
}
}
}
} else {
allowSet = false;
}
return allowSet;
},
/**
* 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. This method does not
*
* @method addAttrs
* @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.