AttributeCore.js revision fdaf522f78615dc3145b1771f6f8daba2df06bb6
/**
* The attribute module provides an augmentable Attribute implementation, which
* adds configurable attributes and attribute change events to the class being
* augmented. It also provides a State class, which is used internally by Attribute,
* store state.
*
* @module attribute
*/
/**
* The attribute-core submodule provides the lightest level of attribute handling support
* without Attribute change events, or lesser used methods such as reset(), modifyAttrs(),
* and removeAttr().
*
* @module attribute
* @submodule attribute-core
*/
var O = Y.Object,
DOT = ".",
// Externally configurable props
GETTER = "getter",
SETTER = "setter",
READ_ONLY = "readOnly",
WRITE_ONCE = "writeOnce",
INIT_ONLY = "initOnly",
VALIDATOR = "validator",
VALUE = "value",
VALUE_FN = "valueFn",
LAZY_ADD = "lazyAdd",
// Used for internal state management
ADDED = "added",
BYPASS_PROXY = "_bypassProxy",
INITIALIZING = "initializing",
INIT_VALUE = "initValue",
LAZY = "lazy",
IS_LAZY_ADD = "isLazyAdd",
/**
* <p>
* AttributeCore provides configurable attribute support. It is designed to be
* augmented on to a host class, and provides the host with the ability to configure
* attributes to store and retrieve state, without support for attribute change events.
* </p>
* <p>For example, attributes added to the host can be configured:</p>
* <ul>
* <li>As read only.</li>
* <li>As write once.</li>
* <li>With a setter function, which can be used to manipulate
* values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
* <li>With a getter function, which can be used to manipulate stored values,
* before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
* <li>With a validator function, to validate values before they are stored.</li>
* </ul>
*
* <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
* options available for attributes</p>.
*
* @class AttributeCore
* @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.
* @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.
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
*/
}
/**
* <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
*
* <p>You can return this value from your setter if you wish to combine validator and setter
* functionality into a single setter function, which either returns the massaged value to be stored or
* AttributeCore.INVALID_VALUE to prevent invalid values from being stored.</p>
*
* @property INVALID_VALUE
* @type Object
* @static
* @final
*/
AttributeCore.INVALID_VALUE = {};
/**
* The list of properties which can be configured for
* each attribute (e.g. setter, getter, writeOnce etc.).
*
* This property is used internally as a whitelist for faster
* Y.mix operations.
*
* @property _ATTR_CFG
* @type Array
* @static
* @protected
*/
AttributeCore._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BYPASS_PROXY];
/**
* Constructor logic for attributes. Initializes the host state, and sets up the inital attributes passed to the
* constructor.
*
* @method _initAttrHost
* @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.
* @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.
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
*/
},
/**
* <p>
* Adds an attribute with the provided configuration to the host object.
* </p>
* <p>
* The config argument object supports the following properties:
* </p>
*
* <dl>
* <dt>value <Any></dt>
* <dd>The initial value to set on the attribute</dd>
*
* <dt>valueFn <Function | String></dt>
* <dd>
* <p>A function, which will return the initial value to set on the attribute. This is useful
* for cases where the attribute configuration is defined statically, but needs to
* reference the host instance ("this") to obtain an initial value. If both the value and valueFn properties are defined,
* the value returned by the valueFn has precedence over the value property, unless it returns undefined, in which
* case the value property is used.</p>
*
* <p>valueFn can also be set to a string, representing the name of the instance method to be used to retrieve the value.</p>
* </dd>
*
* <dt>readOnly <boolean></dt>
* <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
* cannot be modified by invoking the set method.</dd>
*
* <dt>writeOnce <boolean> or <string></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.
* <p>The writeOnce attribute can also be set to the string "initOnly", in which case the attribute can only be set during initialization
* (when used with Base, this means it can only be set during construction)</p>
* </dd>
*
* <dt>setter <Function | String></dt>
* <dd>
* <p>The setter function used to massage or normalize the value passed to the set method for the attribute.
* The value returned by the setter will be the final stored value. Returning
* <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
* the value from being stored.
* </p>
*
* <p>setter can also be set to a string, representing the name of the instance method to be used as the setter function.</p>
* </dd>
*
* <dt>getter <Function | String></dt>
* <dd>
* <p>
* The getter function used to massage or normalize the value returned by the get method for the attribute.
* The value returned by the getter function is the value which will be returned to the user when they
* invoke get.
* </p>
*
* <p>getter can also be set to a string, representing the name of the instance method to be used as the getter function.</p>
* </dd>
*
* <dt>validator <Function | String></dt>
* <dd>
* <p>
* The validator function invoked prior to setting the stored value. Returning
* false from the validator function will prevent the value from being stored.
* </p>
*
* <p>validator can also be set to a string, representing the name of the instance method to be used as the validator function.</p>
* </dd>
*
* <dt>lazyAdd <boolean></dt>
* This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through
* the <a href="#method_addAttrs">addAttrs</a> method.</dd>
*
* </dl>
*
* <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
* the context ("this") set to the host object.</p>
*
* <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>
*
* @method addAttr
*
* @param {String} name The name of the attribute.
* @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
*
* <p>
* <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need
* to protect the original values, you will need to merge the object.
* </p>
*
* @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set).
*
* @return {Object} A reference to the host object.
*
* @chainable
*/
var host = this, // help compression
added : true
});
} else {
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) {
// We'll go through set, don't want to set value in config directly
}
config.initializing = true;
if (hasValue) {
// Go through set, so that raw values get normalized/validated
}
}
}
return host;
},
/**
* Checks 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, false if it hasn't. This method will return true for lazily added attributes.
*/
},
/**
* 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.
*
* @method get
*
* @param {String} name The name of the attribute. 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 value of the attribute
*/
},
/**
* Checks whether or not the attribute is one which has been
* added lazily and still requires initialization.
*
* @method _isLazyAttr
* @private
* @param {String} name The name of the attribute
* @return {boolean} true if it's a lazily added attribute, false otherwise.
*/
_isLazyAttr: function(name) {
},
/**
* Finishes initializing an attribute which has been lazily added.
*
* @method _addLazyAttr
* @private
* @param {Object} name The name of the attribute
*/
},
/**
* Sets the value of an attribute.
*
* @method set
* @chainable
*
* @param {String} name The name of the attribute. If the
* current 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 set the attribute to.
*
* @return {Object} A reference to the host object.
*/
},
/**
* Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
*
* @method _set
* @protected
* @chainable
*
* @param {String} name The name of the attribute.
* @param {Any} val The value to set the attribute to.
* @return {Object} A reference to the host object.
*/
},
/**
* Provides the common implementation for the public set and protected _set methods.
*
* See <a href="#method_set">set</a> for argument details.
*
* @method _setAttr
* @protected
* @chainable
*
* @param {String} name The name of the attribute.
* @param {Any} value The value to set the attribute to.
* @param {Object} opts (Optional) Optional event data to be mixed into
* the event facade passed to subscribers of the attribute's change event.
* This is currently a hack. There's no real need for the AttributeCore implementation
* to support this parameter, but breaking it out into AttributeEvents, results in
* additional function hops for the critical path. May change in 3.5.0 PR3.
* @param {boolean} force If true, allows the caller to set values for
* readOnly or writeOnce attributes which have already been set.
*
* @return {Object} A reference to the host object.
*/
// HACK - no real reason core needs to know about opts, but
// it adds fn hops if we want to break it out.
// Not sure it's worth it for this critical path
var allowSet = true,
stateProxy = this._stateProxy,
cfg,
path,
}
if (this._isLazyAttr(name)) {
this._addLazyAttr(name);
}
// 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?
initialSet = false;
}
if (!initialSet && !force) {
if (writeOnce) {
allowSet = false;
}
allowSet = false;
}
}
Y.log('Set attribute:' + name + ', aborted; Attribute is writeOnce: "initOnly"', 'warn', 'attribute');
allowSet = false;
}
if (allowSet) {
// Don't need currVal if initialSet (might fail in custom getter if it always expects a non-undefined/non-null value)
if (!initialSet) {
}
if (path) {
allowSet = false;
}
}
if (allowSet) {
if (!this._fireAttrChange || initializing) {
} else {
// HACK - no real reason core needs to know about _fireAttrChange, but
// it adds fn hops if we want to break it out. Not sure it's worth it for this critical path
}
}
}
return this;
},
/**
* Provides the common implementation for the public get method,
* allowing Attribute hosts to over-ride either method.
*
* See <a href="#method_get">get</a> for argument details.
*
* @method _getAttr
* @protected
* @chainable
*
* @param {String} name The name of the attribute.
* @return {Any} The value of the attribute.
*/
var host = this, // help compression
path,
val,
cfg;
}
// On Demand - Should be rare - handles out of order valueFn references
cfg = {};
}
// Lazy Init
}
}
return val;
},
/**
* Gets the stored value for the attribute, from either the
* internal state object, or the state proxy if it exits
*
* @method _getStateVal
* @private
* @param {String} name The name of the attribute
* @return {Any} The stored value of the attribute
*/
_getStateVal : function(name) {
var stateProxy = this._stateProxy;
return stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY) ? stateProxy[name] : this._state.get(name, VALUE);
},
/**
* Sets the stored value for the attribute, in either the
* internal state object, or the state proxy if it exits
*
* @method _setStateVal
* @private
* @param {String} name The name of the attribute
* @param {Any} value The value of the attribute
*/
var stateProxy = this._stateProxy;
} 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 host = this,
allowSet = true,
if (validator) {
// Assume string - trying to keep critical path tight, so avoiding Lang check
}
if (validator) {
if (!valid && initializing) {
valid = true; // Assume it's valid, for perf.
}
}
}
if (setter) {
// Assume string - trying to keep critical path tight, so avoiding Lang check
}
if (setter) {
if (retVal === INVALID_VALUE) {
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;
} else {
// Store value
if (!(INIT_VALUE in cfg)) {
}
}
}
} else {
allowSet = false;
}
return allowSet;
},
/**
* Sets multiple attribute values.
*
* @method setAttrs
* @return {Object} A reference to the host object.
* @chainable
*/
},
/**
* Implementation behind the public setAttrs method, to set multiple attribute values.
*
* @method _setAttrs
* @protected
* @return {Object} A reference to the host object.
* @chainable
*/
}
}
return this;
},
/**
* Gets multiple attribute values.
*
* @method getAttrs
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
* returned. If set to true, all attributes modified from their initial values are returned.
*/
},
/**
* Implementation behind the public getAttrs method, to get multiple attribute values.
*
* @method _getAttrs
* @protected
* @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
* returned. If set to true, all attributes modified from their initial values are returned.
*/
var host = this,
o = {},
modifiedOnly = (attrs === true);
// TODO - figure out how to get all "added"
// Go through get, to honor cloning/normalization
}
}
return o;
},
/**
* Configures a group of attributes, and sets initial values.
*
* <p>
* </p>
*
* @method addAttrs
* @chainable
*
* @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.
* See <a href="#method_addAttr">addAttr</a>.
*
* @return {Object} A reference to the host object.
*/
var host = this; // help compression
if (cfgs) {
}
return host;
},
/**
* Implementation behind the public addAttrs method.
*
* This method is invoked directly by get if it encounters a scenario
* in which an attribute's valueFn attempts to obtain the
* value an attribute in the same group of attributes, which has not yet
* been added (on demand initialization).
*
* @method _addAttrs
* @private
* @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.
* See <a href="#method_addAttr">addAttr</a>.
*/
var host = this, // help compression
attr,
// Not Merging. Caller is responsible for isolating configs
// Handle simple, complex and user values, accounting for read-only
}
}
}
}
},
/**
* Utility method to protect an attribute configuration
* hash, by merging the entire object and the individual
* attr config objects.
*
* @method _protectAttrs
* @protected
* @param {Object} attrs A hash of attribute to configuration object pairs.
* @return {Object} A protected version of the attrs argument.
*/
_protectAttrs : function(attrs) {
if (attrs) {
}
}
}
return attrs;
},
/**
* Utility method to normalize attribute values. The base implementation
* simply merges the hash to protect the original.
*
* @method _normAttrVals
*
* @return {Object}
*
* @private
*/
_normAttrVals : function(valueHash) {
},
/**
* Returns the initial value of the given attribute from
* either the default configuration provided, or the
* over-ridden value if it exists in the set of initValues
* provided and the attribute is not read-only.
*
* @param {String} attr The name of the attribute
* @param {Object} cfg The attribute configuration object
* @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
*
* @return {Any} The initial value of the attribute.
*
* @method _getAttrInitVal
* @private
*/
// init value is provided by the user if it exists, else, provided by the config
} else {
if (valFn) {
}
if (valFn) {
}
}
}
return val;
},
/**
* Utility method to set up initial attributes defined during construction, either through the constructor.ATTRS property, or explicitly passed in.
*
* @method _initAttrs
* @protected
* @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.
* @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.
* @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
*/
// ATTRS support for Node, which is not Base based
}
}
};