Base.js revision 793e508bd277c48d6a09b91bb99064b4046eda7a
/**
* The base module provides the Base class, which objects requiring managed attributes
* and custom event support can extend. The base module also provides two ways to reuse
* code - An augmentable PluginHost interface which provides plugin support
* (which is augmented to the Base class) and Base.build which provides a way to
* build custom classes using extensions.
*
* @module base
*/
/**
* The base-base submodule provides the Base class and augmentable PluginHost implementation,
* without the extension support provided by Base.build.
*
* @module base
* @submodule base-base
*/
var O = Y.Object,
DOT = ".",
DESTROY = "destroy",
INIT = "init",
INITIALIZED = "initialized",
DESTROYED = "destroyed",
INITIALIZER = "initializer",
DEEP = "deep",
SHALLOW = "shallow",
VALUE = "value",
DESTRUCTOR = "destructor";
/**
* <p>
* Provides a base class for which objects requiring managed attributes and
* custom event support can extend. The Base class also handles the chaining of
* initializer and destructor methods across the hierarchy as part of object construction
* and destruction. Additionally, attributes configured through the static
* <a href="#property_Base.ATTRS">ATTRS</a> property for each class in the hierarchy will be
* initialized by Base.
* </p>
*
* <p>The static <a href="#property_Base.NAME">NAME</a> property of each class extending
* from Base will be used as the identifier for the class, and is used by Base to prefix
* all events fired by instances of that class.</p>
*
* @class Base
* @constructor
* @uses Attribute, Plugin.Host
*
*/
function Base() {
this._silentInit = this._silentInit || false;
if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
}
/**
* The list of properties which can be configured for
* each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
*
* @property Base._ATTR_CFG
* @type Array
* @static
* @private
*/
/**
* <p>
* The string to be used to identify instances of
* this class, for example in prefixing events.
* </p>
* <p>
* Classes extending Base, should define their own
* static NAME property, which should be camelCase by
* convention (e.g. MyClass.NAME = "myClass";).
* </p>
* @property Base.NAME
* @type String
* @static
*/
/**
* Object literal defining the set of attributes which
* will be available for instances of this class, and
* how they are configured. See Attribute's <a href="Attribute.html#method_addAttr">addAttr</a>
* method for a description of configuration options available
* for each attribute.
*
* @property Base.ATTRS
* @type Object
* @static
*/
/**
* Flag indicating whether or not this object
* has been through the init lifecycle phase.
*
* @attribute initialized
* @readonly
* @default false
* @type boolean
*/
initialized: {
readOnly:true,
value:false
},
/**
* Flag indicating whether or not this object
* has been through the destroy lifecycle phase.
*
* @attribute destroyed
* @readonly
* @default false
* @type boolean
*/
destroyed: {
readOnly:true,
value:false
}
};
/**
* Init lifecycle method, invoked during construction.
* Fires the init event prior to setting up attributes and
* invoking initializers for the class hierarchy.
*
* @method init
* @final
* @chainable
* @return {Base} A reference to this object
*/
/**
* The string used to identify the class of this object.
*
* @deprecated Use this.constructor.NAME
* @property name
* @type String
*/
/**
* <p>
* Lifecycle event for the init phase, fired prior to initialization.
* Invoking the preventDefault() method on the event object provided
* to subscribers will prevent initialization from occuring.
* </p>
* <p>
* Subscribers to the "after" momemt of this event, will be notified
* after initialization of the object is complete (and therefore
* cannot prevent initialization).
* </p>
*
* @event init
* @preventable _defInitFn
* @param {Event.Facade} e Event object, with a cfg property which
* refers to the configuration object literal passed to the constructor.
*/
if (!this._silentInit) {
queuable:false,
defaultFn:this._defInitFn
});
}
if (config) {
}
}
}
if (!this._silentInit) {
} else {
}
return this;
},
/**
* <p>
* Destroy lifecycle method. Fires the destroy
* event, prior to invoking destructors for the
* class hierarchy.
* </p>
* <p>
* Subscribers to the destroy
* event can invoke preventDefault on the event object, to prevent destruction
* from proceeding.
* </p>
* @method destroy
* @return {Base} A reference to this object
* @final
* @chainable
*/
destroy: function() {
/**
* <p>
* Lifecycle event for the destroy phase,
* fired prior to destruction. Invoking the preventDefault
* method on the event object provided to subscribers will
* prevent destruction from proceeding.
* </p>
* <p>
* Subscribers to the "after" moment of this event, will be notified
* after destruction is complete (and as a result cannot prevent
* destruction).
* </p>
* @event destroy
* @preventable _defDestroyFn
* @param {Event.Facade} e Event object
*/
queuable:false,
defaultFn: this._defDestroyFn
});
return this;
},
/**
* Default init event handler
*
* @method _defInitFn
* @param {Event.Facade} e Event object, with a cfg property which
* refers to the configuration object literal passed to the constructor.
* @protected
*/
_defInitFn : function(e) {
this._initHierarchy(e.cfg);
this._initPlugins(e.cfg);
if (!this._silentInit) {
this._set(INITIALIZED, true);
} else {
}
},
/**
* Default destroy event handler
*
* @method _defDestroyFn
* @param {Event.Facade} e Event object
* @protected
*/
_defDestroyFn : function(e) {
this._destroyHierarchy();
this._destroyPlugins();
},
/**
* Returns the class hierarchy for this object, with Base being the last class in the array.
*
* @method _getClasses
* @protected
* @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
* This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
* cached value.
*/
_getClasses : function() {
if (!this._classes) {
this._initHierarchyData();
}
return this._classes;
},
/**
* Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
*
* @method _getAttrCfgs
* @protected
* @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
* This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
* the cached value.
*/
_getAttrCfgs : function() {
if (!this._attrs) {
this._initHierarchyData();
}
return this._attrs;
},
/**
* A helper method used when processing ATTRS across the class hierarchy during
* initialization. Returns a disposable object with the attributes defined for
* the provided class, extracted from the set of all attributes passed in .
*
* @method _filterAttrCfs
* @private
*
* @param {Function} clazz The class for which the desired attributes are required.
* @param {Object} allCfgs The set of all attribute configurations for this instance.
* Attributes will be removed from this set, if they belong to the filtered class, so
* that by the time all classes are processed, allCfgs will be empty.
*
* @return {Object} The set of attributes belonging to the class passed in, in the form
*/
if (attrs) {
}
}
}
return cfgs;
},
/**
* A helper method used by _getClasses and _getAttrCfgs, which determines both
* the array of classes and aggregate set of attribute configurations
* across the class hierarchy for the instance.
*
* @method _initHierarchyData
* @private
*/
_initHierarchyData : function() {
var c = this.constructor,
classes = [],
attrs = [];
while (c) {
// Add to classes
// Add to attributes
if (c.ATTRS) {
}
}
},
/**
* A helper method, used by _initHierarchyData to aggregate
* attribute configuration across the instances class hierarchy.
*
* The method will potect the attribute configuration value to protect the statically defined
* default value in ATTRS if required (value is an object literal or array or the
* attribute configuration has clone set to shallow or deep).
*
* @method _aggregateAttrs
* @private
* @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
* (subclass first, Base last)
* @return {Object} The aggregate set of ATTRS definitions for the instance
*/
_aggregateAttrs : function(allAttrs) {
var attr,
cfg,
val,
path,
i,
aggAttrs = {};
if (allAttrs) {
// Protect config passed in
if (val) {
if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
}
// else if (clone === false), don't clone the static default value.
// It's intended to be used by reference.
}
path = null;
}
} else if (!path){
} else {
}
}
}
}
}
}
return aggAttrs;
},
/**
* Initializes the class hierarchy for the instance, which includes
* initializing attributes for each class defined in the class's
* static <a href="#property_Base.ATTRS">ATTRS</a> property and
* invoking the initializer method on the prototype of each class in the hierarchy.
*
* @method _initHierarchy
* @private
*/
_initHierarchy : function(userVals) {
var lazy = this._lazyAddAttrs,
ci,
ei,
el,
classes = this._getClasses(),
attrCfgs = this._getAttrCfgs();
}
}
}
}
},
/**
* Destroys the class hierarchy for this instance by invoking
* the descructor method on the prototype of each class in the hierarchy.
*
* @method _destroyHierarchy
* @private
*/
_destroyHierarchy : function() {
var constr,
classes = this._getClasses();
}
}
},
/**
* Default toString implementation. Provides the constructor NAME
* and the instance ID.
*
* @method toString
* @return {String} String representation for this object
*/
toString: function() {
}
};
// Straightup augment, no wrapper functions
/**
* Alias for <a href="PluginHost.html#method_PluginHost.plug">PluginHost.plug</a>.
*
* @method Base.plug
* @static
*/
/**
* Alias for <a href="PluginHost.html#method_PluginHost.unplug">PluginHost.unplug</a>.
*
* @method Base.unplug
* @static
*/
// Fix constructor