Base.js revision 3a81ce564b8d21870eb054300f28209035ce88ce
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Base class support for objects requiring managed attributes and acting as event targets.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * The base module also provides an augmentable PluginHost interface.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @module base
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * The base-base sub-module provides the Base class, without Base.build functionality
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @module base
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @sub-module base-base
b2640405e06105d868b5fc8f7b676bb680884380vboxsync var O = Y.Object,
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Provides a base class for managed attribute based
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * objects, which handles the chaining of initializer and destructor methods
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * across the hierarchy during init and destroy lifecycle methods and
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * handles automatic configuration of registered Attributes, through
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * the static <a href="#property_ATTRS">ATTRS</a> property.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * <p>The Base class also handles prefixing of event types with the static <a href="#property_NAME">NAME</a>
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * property for all events fired from instances of classes derived from Base.</p>
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @constructor
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @class Base
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @uses Attribute, Plugin.Host
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Object} config Object literal of configuration property name/value pairs
b2640405e06105d868b5fc8f7b676bb680884380vboxsync function Base() {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync if (this._lazyAttrInit !== false) { this._lazyAttrInit = true; }
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Name string to be used to identify instances of
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * this class, for example in prefixing events.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Classes extending Base, should define their own
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * static NAME property.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @property NAME
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @type String
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Object literal defining the set of attributes which
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * will be available for instances of this class, and
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * how they are configured. See Attributes addAtt method
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * for a description of configuration options available
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * for each attribute.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @property ATTRS
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @type Object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Flag indicating whether or not this object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * has been through the init lifecycle phase.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @attribute initialized
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @readOnly
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @default false
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @type boolean
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Flag indicating whether or not this object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * has been through the destroy lifecycle phase.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @attribute destroyed
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @readOnly
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @default false
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @type boolean
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Init lifecycle method, invoked during construction.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Fires the init event prior to invoking initializers on
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * the class hierarchy.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method init
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @chainable
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Object} config Object literal of configuration property name/value pairs
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @return {Base} A reference to this object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * The name string to be used to identify
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * this instance of object.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @property name
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @type String
b2640405e06105d868b5fc8f7b676bb680884380vboxsync this._yuievt.config.prefix = this.name = this.constructor.NAME;
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Lifecycle event for the init phase, fired prior to initialization.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Invoking the preventDefault method on the event object provided
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * to subscribers will prevent initialization from occuring.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Subscribers to the "after" momemt of this event, will be notified
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * after initialization of the object is complete (and therefore
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * cannot prevent initialization).
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @event init
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @preventable _defInitFn
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Event.Facade} e Event object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param config Object literal of configuration name/value pairs
b2640405e06105d868b5fc8f7b676bb680884380vboxsync return this;
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Destroy lifecycle method. Fires the destroy
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * event, prior to invoking destructors for the
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * class hierarchy.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Subscribers to the destroy
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * event can invoke preventDefault on the event object, to prevent destruction
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * from proceeding.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method destroy
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @return {Base} A reference to this object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @chainable
b2640405e06105d868b5fc8f7b676bb680884380vboxsync destroy: function() {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Lifecycle event for the destroy phase,
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * fired prior to destruction. Invoking the preventDefault
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * method on the event object provided to subscribers will
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * prevent destruction from proceeding.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Subscribers to the "after" moment of this event, will be notified
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * after destruction is complete (and as a result cannot prevent
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * destruction).
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @event destroy
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @preventable _defDestroyFn
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Event.Facade} e Event object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync return this;
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Default init event handler
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method _defInitFn
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Event.Facade} e Event object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @protected
b2640405e06105d868b5fc8f7b676bb680884380vboxsync _defInitFn : function(e) {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Default destroy event handler
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method _defDestroyFn
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @param {Event.Facade} e Event object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @protected
b2640405e06105d868b5fc8f7b676bb680884380vboxsync _defDestroyFn : function(e) {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Returns the class hierarchy for this object, with Base being the last class in the array.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method _getClasses
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @protected
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @return {Function[]} An Array of classes (constructor functions), making up the class hierarchy for this object
b2640405e06105d868b5fc8f7b676bb680884380vboxsync _getClasses : function() {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync if (!this._classes) {
b2640405e06105d868b5fc8f7b676bb680884380vboxsync return this._classes;
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @method _getAttrCfgs
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @protected
b2640405e06105d868b5fc8f7b676bb680884380vboxsync * @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync _getAttrCfgs : function() {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync if (!this._attrs) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync return this._attrs;
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method _filterAttrCfs
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @param {Function} clazz
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @param {Objects} allCfgs
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method _initHierarchyData
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync var c = this.constructor,
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync while (c) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // Add to classes
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // Add to attributes
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync c = c.superclass ? c.superclass.constructor : null;
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method _aggregateAttrs
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @param {Object} allAttrs
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync Y.log('Cloning default value for attribute:' + attr, 'info', 'base');
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync Y.log('Merging default value for attribute:' + attr, 'info', 'base');
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // else if (clone === false), don't clone the static default value.
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // It's intended to be used by reference.
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync if (path && aggAttrs[attr] && aggAttrs[attr].value) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync } else if (!path){
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * Initializes the class hierarchy rooted at this base class,
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * which includes initializing attributes for each class defined
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * in the class's static <a href="#property_ATTRS">ATTRS</a> property and invoking the initializer
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * method on the prototype of each class in the hierarchy.
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method _initHierarchy
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @param {Object} userVals Object literal containing attribute name/value pairs
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync if (constr._yuibuild && constr._yuibuild.exts && !constr._yuibuild.dynamic) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync for (ei = 0, el = constr._yuibuild.exts.length; ei < el; ei++) {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * Destroys the class hierarchy rooted at this base class by invoking
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * the descructor method on the prototype of each class in the hierarchy.
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method _destroyHierarchy
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * Default toString implementation. Provides the constructor NAME
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * and the instance ID.
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @method toString
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync * @return {String} String representation for this object
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync toString: function() {
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync return this.constructor.NAME + "[" + Y.stamp(this) + "]";
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // Straightup augment, no wrapper functions
eb4f1fa4c357485330370c0eaba27e5a2af7d9c4vboxsync // Fix constructor