2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('base-base', function(Y) {
11 * The base module provides the Base class, which objects requiring attribute and custom event support can extend.
12 * The module also provides two ways to reuse code - An augmentable Plugin.Host interface which provides plugin support
13 * (which is augmented to the Base class) and Base.build which provides a way to
14 * build custom classes using extensions.
20 * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
21 * and without the extension support provided by Base.build.
24 * @submodule base-base
31 INITIALIZED = "initialized",
32 DESTROYED = "destroyed",
33 INITIALIZER = "initializer",
34 OBJECT_CONSTRUCTOR = Object.prototype.constructor,
37 DESTRUCTOR = "destructor",
39 Attribute = Y.Attribute;
43 * A base class which objects requiring attributes and custom event support can
44 * extend. Base also handles the chaining of initializer and destructor methods across
45 * the hierarchy as part of object construction and destruction. Additionally, attributes configured
46 * through the static <a href="#property_Base.ATTRS">ATTRS</a> property for each class
47 * in the hierarchy will be initialized by Base.
51 * The static <a href="#property_Base.NAME">NAME</a> property of each class extending
52 * from Base will be used as the identifier for the class, and is used by Base to prefix
53 * all events fired by instances of that class.
60 * @param {Object} config Object with configuration property name/value pairs
66 // If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
67 // initial state, but don't initialize Plugins yet. That's done after initialization.
68 var PluginHost = Y.Plugin && Y.Plugin.Host;
69 if (this._initPlugins && PluginHost) {
70 PluginHost.call(this);
73 if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
75 this.init.apply(this, arguments);
79 * The list of properties which can be configured for
80 * each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
82 * @property Base._ATTR_CFG
87 Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
91 * The string to be used to identify instances of
92 * this class, for example in prefixing events.
95 * Classes extending Base, should define their own
96 * static NAME property, which should be camelCase by
97 * convention (e.g. MyClass.NAME = "myClass";).
106 * The default set of attributes which will be available for instances of this class, and
107 * their configuration. In addition to the configuration properties listed by
108 * Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
109 * can also be configured with a "cloneDefaultValue" property, which defines how the statically
110 * defined value field should be protected ("shallow", "deep" and false are supported values).
112 * By default if the value is an object literal or an array it will be "shallow" cloned, to
113 * protect the default value.
115 * @property Base.ATTRS
121 * Flag indicating whether or not this object
122 * has been through the init lifecycle phase.
124 * @attribute initialized
135 * Flag indicating whether or not this object
136 * has been through the destroy lifecycle phase.
138 * @attribute destroyed
152 * Init lifecycle method, invoked during construction.
153 * Fires the init event prior to setting up attributes and
154 * invoking initializers for the class hierarchy.
159 * @param {Object} config Object with configuration property name/value pairs
160 * @return {Base} A reference to this object
162 init: function(config) {
165 * The string used to identify the class of this object.
167 * @deprecated Use this.constructor.NAME
171 this._yuievt.config.prefix = this.name = this.constructor.NAME;
175 * Lifecycle event for the init phase, fired prior to initialization.
176 * Invoking the preventDefault() method on the event object provided
177 * to subscribers will prevent initialization from occuring.
180 * Subscribers to the "after" momemt of this event, will be notified
181 * after initialization of the object is complete (and therefore
182 * cannot prevent initialization).
186 * @preventable _defInitFn
187 * @param {EventFacade} e Event object, with a cfg property which
188 * refers to the configuration object passed to the constructor.
192 defaultFn:this._defInitFn
200 this.after(config.after);
204 this.fire(INIT, {cfg: config});
211 * Destroy lifecycle method. Fires the destroy
212 * event, prior to invoking destructors for the
216 * Subscribers to the destroy
217 * event can invoke preventDefault on the event object, to prevent destruction
221 * @return {Base} A reference to this object
225 destroy: function() {
229 * Lifecycle event for the destroy phase,
230 * fired prior to destruction. Invoking the preventDefault
231 * method on the event object provided to subscribers will
232 * prevent destruction from proceeding.
235 * Subscribers to the "after" moment of this event, will be notified
236 * after destruction is complete (and as a result cannot prevent
240 * @preventable _defDestroyFn
241 * @param {EventFacade} e Event object
243 this.publish(DESTROY, {
245 defaultFn: this._defDestroyFn
252 * Default init event handler
255 * @param {EventFacade} e Event object, with a cfg property which
256 * refers to the configuration object passed to the constructor.
259 _defInitFn : function(e) {
260 this._initHierarchy(e.cfg);
261 if (this._initPlugins) {
262 // Need to initPlugins manually, to handle constructor parsing, static Plug parsing
263 this._initPlugins(e.cfg);
265 this._set(INITIALIZED, true);
269 * Default destroy event handler
271 * @method _defDestroyFn
272 * @param {EventFacade} e Event object
275 _defDestroyFn : function(e) {
276 this._destroyHierarchy();
277 if (this._destroyPlugins) {
278 this._destroyPlugins();
280 this._set(DESTROYED, true);
284 * Returns the class hierarchy for this object, with Base being the last class in the array.
286 * @method _getClasses
288 * @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
289 * This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
292 _getClasses : function() {
293 if (!this._classes) {
294 this._initHierarchyData();
296 return this._classes;
300 * Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
302 * @method _getAttrCfgs
304 * @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
305 * This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
308 _getAttrCfgs : function() {
310 this._initHierarchyData();
316 * A helper method used when processing ATTRS across the class hierarchy during
317 * initialization. Returns a disposable object with the attributes defined for
318 * the provided class, extracted from the set of all attributes passed in .
320 * @method _filterAttrCfs
323 * @param {Function} clazz The class for which the desired attributes are required.
324 * @param {Object} allCfgs The set of all attribute configurations for this instance.
325 * Attributes will be removed from this set, if they belong to the filtered class, so
326 * that by the time all classes are processed, allCfgs will be empty.
328 * @return {Object} The set of attributes belonging to the class passed in, in the form
329 * of an object with attribute name/configuration pairs.
331 _filterAttrCfgs : function(clazz, allCfgs) {
332 var cfgs = null, attr, attrs = clazz.ATTRS;
335 for (attr in attrs) {
336 if (attrs.hasOwnProperty(attr) && allCfgs[attr]) {
338 cfgs[attr] = allCfgs[attr];
339 delete allCfgs[attr];
348 * A helper method used by _getClasses and _getAttrCfgs, which determines both
349 * the array of classes and aggregate set of attribute configurations
350 * across the class hierarchy for the instance.
352 * @method _initHierarchyData
355 _initHierarchyData : function() {
356 var c = this.constructor,
362 classes[classes.length] = c;
366 attrs[attrs.length] = c.ATTRS;
368 c = c.superclass ? c.superclass.constructor : null;
371 this._classes = classes;
372 this._attrs = this._aggregateAttrs(attrs);
376 * A helper method, used by _initHierarchyData to aggregate
377 * attribute configuration across the instances class hierarchy.
379 * The method will potect the attribute configuration value to protect the statically defined
380 * default value in ATTRS if required (if the value is an object literal, array or the
381 * attribute configuration has cloneDefaultValue set to shallow or deep).
383 * @method _aggregateAttrs
385 * @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
386 * (subclass first, Base last)
387 * @return {Object} The aggregate set of ATTRS definitions for the instance
389 _aggregateAttrs : function(allAttrs) {
397 cfgProps = Base._ATTR_CFG,
401 for (i = allAttrs.length-1; i >= 0; --i) {
404 for (attr in attrs) {
405 if (attrs.hasOwnProperty(attr)) {
407 // Protect config passed in
408 cfg = Y.mix({}, attrs[attr], true, cfgProps);
411 clone = cfg.cloneDefaultValue;
414 if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
415 cfg.value = Y.clone(val);
416 } else if (clone === SHALLOW) {
417 cfg.value = Y.merge(val);
419 // else if (clone === false), don't clone the static default value.
420 // It's intended to be used by reference.
424 if (attr.indexOf(DOT) !== -1) {
425 path = attr.split(DOT);
429 if (path && aggAttrs[attr] && aggAttrs[attr].value) {
430 O.setValue(aggAttrs[attr].value, path, val);
432 if (!aggAttrs[attr]) {
433 aggAttrs[attr] = cfg;
435 Y.mix(aggAttrs[attr], cfg, true, cfgProps);
447 * Initializes the class hierarchy for the instance, which includes
448 * initializing attributes for each class defined in the class's
449 * static <a href="#property_Base.ATTRS">ATTRS</a> property and
450 * invoking the initializer method on the prototype of each class in the hierarchy.
452 * @method _initHierarchy
453 * @param {Object} userVals Object with configuration property name/value pairs
456 _initHierarchy : function(userVals) {
457 var lazy = this._lazyAddAttrs,
463 classes = this._getClasses(),
464 attrCfgs = this._getAttrCfgs();
466 for (ci = classes.length-1; ci >= 0; ci--) {
468 constr = classes[ci];
469 constrProto = constr.prototype;
471 if (constr._yuibuild && constr._yuibuild.exts && !constr._yuibuild.dynamic) {
472 for (ei = 0, el = constr._yuibuild.exts.length; ei < el; ei++) {
473 constr._yuibuild.exts[ei].apply(this, arguments);
477 this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
479 if (constrProto.hasOwnProperty(INITIALIZER)) {
480 constrProto.initializer.apply(this, arguments);
486 * Destroys the class hierarchy for this instance by invoking
487 * the descructor method on the prototype of each class in the hierarchy.
489 * @method _destroyHierarchy
492 _destroyHierarchy : function() {
496 classes = this._getClasses();
498 for (ci = 0, cl = classes.length; ci < cl; ci++) {
499 constr = classes[ci];
500 constrProto = constr.prototype;
501 if (constrProto.hasOwnProperty(DESTRUCTOR)) {
502 constrProto.destructor.apply(this, arguments);
508 * Default toString implementation. Provides the constructor NAME
509 * and the instance ID.
512 * @return {String} String representation for this object
514 toString: function() {
515 return this.constructor.NAME + "[" + Y.stamp(this) + "]";
519 // Straightup augment, no wrapper functions
520 Y.mix(Base, Attribute, false, null, 1);
523 Base.prototype.constructor = Base;
528 Base.prototype.constructor = Base;
531 }, '3.0.0' ,{requires:['attribute-base']});