2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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 - It augments Base with the Plugin.Host interface which provides
13 * plugin support and also provides the Base.build method which provides a way to build custom classes using extensions.
19 * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
20 * and without the extension support provided by Base.build.
23 * @submodule base-base
30 INITIALIZED = "initialized",
31 DESTROYED = "destroyed",
32 INITIALIZER = "initializer",
33 BUBBLETARGETS = "bubbleTargets",
34 _BUBBLETARGETS = "_bubbleTargets",
35 OBJECT_CONSTRUCTOR = Object.prototype.constructor,
38 DESTRUCTOR = "destructor",
40 Attribute = Y.Attribute;
44 * A base class which objects requiring attributes and custom event support can
45 * extend. Base also handles the chaining of initializer and destructor methods across
46 * the hierarchy as part of object construction and destruction. Additionally, attributes configured
47 * through the static <a href="#property_Base.ATTRS">ATTRS</a> property for each class
48 * in the hierarchy will be initialized by Base.
52 * The static <a href="#property_Base.NAME">NAME</a> property of each class extending
53 * from Base will be used as the identifier for the class, and is used by Base to prefix
54 * all events fired by instances of that class.
62 * @param {Object} config Object with configuration property name/value pairs. The object can be
63 * used to provide default values for the objects published attributes.
66 * The config object can also contain the following non-attribute properties, providing a convenient
67 * way to configure events listeners and plugins for the instance, as part of the constructor call:
72 * <dd>An event name to listener function map, to register event listeners for the "on" moment of the event. A constructor convenience property for the <a href="Base.html#method_on">on</a> method.</dd>
74 * <dd>An event name to listener function map, to register event listeners for the "after" moment of the event. A constructor convenience property for the <a href="Base.html#method_after">after</a> method.</dd>
75 * <dt>bubbleTargets</dt>
76 * <dd>An object, or array of objects, to register as bubble targets for bubbled events fired by this instance. A constructor convenience property for the <a href="EventTarget.html#method_addTarget">addTarget</a> method.</dd>
78 * <dd>A plugin, or array of plugins to be plugged into the instance (see PluginHost's plug method for signature details). A constructor convenience property for the <a href="Plugin.Host.html#method_plug">plug</a> method.</dd>
83 // So the object can be used as a hash key (as DD does)
88 // If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
89 // initial state, but don't initialize Plugins yet. That's done after initialization.
90 var PluginHost = Y.Plugin && Y.Plugin.Host;
91 if (this._initPlugins && PluginHost) {
92 PluginHost.call(this);
95 if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
98 * The string used to identify the class of this object.
100 * @deprecated Use this.constructor.NAME
104 this.name = this.constructor.NAME;
105 this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
107 this.init.apply(this, arguments);
111 * The list of properties which can be configured for
112 * each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
114 * @property Base._ATTR_CFG
119 Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
123 * The string to be used to identify instances of
124 * this class, for example in prefixing events.
127 * Classes extending Base, should define their own
128 * static NAME property, which should be camelCase by
129 * convention (e.g. MyClass.NAME = "myClass";).
131 * @property Base.NAME
138 * The default set of attributes which will be available for instances of this class, and
139 * their configuration. In addition to the configuration properties listed by
140 * Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
141 * can also be configured with a "cloneDefaultValue" property, which defines how the statically
142 * defined value field should be protected ("shallow", "deep" and false are supported values).
144 * By default if the value is an object literal or an array it will be "shallow" cloned, to
145 * protect the default value.
147 * @property Base.ATTRS
153 * Flag indicating whether or not this object
154 * has been through the init lifecycle phase.
156 * @attribute initialized
167 * Flag indicating whether or not this object
168 * has been through the destroy lifecycle phase.
170 * @attribute destroyed
184 * Init lifecycle method, invoked during construction.
185 * Fires the init event prior to setting up attributes and
186 * invoking initializers for the class hierarchy.
191 * @param {Object} config Object with configuration property name/value pairs
192 * @return {Base} A reference to this object
194 init: function(config) {
196 this._yuievt.config.prefix = this._eventPrefix;
200 * Lifecycle event for the init phase, fired prior to initialization.
201 * Invoking the preventDefault() method on the event object provided
202 * to subscribers will prevent initialization from occuring.
205 * Subscribers to the "after" momemt of this event, will be notified
206 * after initialization of the object is complete (and therefore
207 * cannot prevent initialization).
211 * @preventable _defInitFn
212 * @param {EventFacade} e Event object, with a cfg property which
213 * refers to the configuration object passed to the constructor.
218 defaultTargetOnly:true,
219 defaultFn:this._defInitFn
222 this._preInitEventCfg(config);
224 this.fire(INIT, {cfg: config});
230 * Handles the special on, after and target properties which allow the user to
231 * easily configure on and after listeners as well as bubble targets during
232 * construction, prior to init.
235 * @method _preInitEventCfg
236 * @param {Object} config The user configuration object
238 _preInitEventCfg : function(config) {
244 this.after(config.after);
249 userTargets = (config && BUBBLETARGETS in config);
251 if (userTargets || _BUBBLETARGETS in this) {
252 target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
253 if (L.isArray(target)) {
254 for (i = 0, l = target.length; i < l; i++) {
255 this.addTarget(target[i]);
258 this.addTarget(target);
265 * Destroy lifecycle method. Fires the destroy
266 * event, prior to invoking destructors for the
270 * Subscribers to the destroy
271 * event can invoke preventDefault on the event object, to prevent destruction
275 * @return {Base} A reference to this object
279 destroy: function() {
283 * Lifecycle event for the destroy phase,
284 * fired prior to destruction. Invoking the preventDefault
285 * method on the event object provided to subscribers will
286 * prevent destruction from proceeding.
289 * Subscribers to the "after" moment of this event, will be notified
290 * after destruction is complete (and as a result cannot prevent
294 * @preventable _defDestroyFn
295 * @param {EventFacade} e Event object
297 this.publish(DESTROY, {
300 defaultTargetOnly:true,
301 defaultFn: this._defDestroyFn
310 * Default init event handler
313 * @param {EventFacade} e Event object, with a cfg property which
314 * refers to the configuration object passed to the constructor.
317 _defInitFn : function(e) {
318 this._initHierarchy(e.cfg);
319 if (this._initPlugins) {
320 // Need to initPlugins manually, to handle constructor parsing, static Plug parsing
321 this._initPlugins(e.cfg);
323 this._set(INITIALIZED, true);
327 * Default destroy event handler
329 * @method _defDestroyFn
330 * @param {EventFacade} e Event object
333 _defDestroyFn : function(e) {
334 this._destroyHierarchy();
335 if (this._destroyPlugins) {
336 this._destroyPlugins();
338 this._set(DESTROYED, true);
342 * Returns the class hierarchy for this object, with Base being the last class in the array.
344 * @method _getClasses
346 * @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
347 * This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
350 _getClasses : function() {
351 if (!this._classes) {
352 this._initHierarchyData();
354 return this._classes;
358 * Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
360 * @method _getAttrCfgs
362 * @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
363 * This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
366 _getAttrCfgs : function() {
368 this._initHierarchyData();
374 * A helper method used when processing ATTRS across the class hierarchy during
375 * initialization. Returns a disposable object with the attributes defined for
376 * the provided class, extracted from the set of all attributes passed in .
378 * @method _filterAttrCfs
381 * @param {Function} clazz The class for which the desired attributes are required.
382 * @param {Object} allCfgs The set of all attribute configurations for this instance.
383 * Attributes will be removed from this set, if they belong to the filtered class, so
384 * that by the time all classes are processed, allCfgs will be empty.
386 * @return {Object} The set of attributes belonging to the class passed in, in the form
387 * of an object with attribute name/configuration pairs.
389 _filterAttrCfgs : function(clazz, allCfgs) {
390 var cfgs = null, attr, attrs = clazz.ATTRS;
393 for (attr in attrs) {
394 if (attrs.hasOwnProperty(attr) && allCfgs[attr]) {
396 cfgs[attr] = allCfgs[attr];
397 delete allCfgs[attr];
406 * A helper method used by _getClasses and _getAttrCfgs, which determines both
407 * the array of classes and aggregate set of attribute configurations
408 * across the class hierarchy for the instance.
410 * @method _initHierarchyData
413 _initHierarchyData : function() {
414 var c = this.constructor,
420 classes[classes.length] = c;
424 attrs[attrs.length] = c.ATTRS;
426 c = c.superclass ? c.superclass.constructor : null;
429 this._classes = classes;
430 this._attrs = this._aggregateAttrs(attrs);
434 * A helper method, used by _initHierarchyData to aggregate
435 * attribute configuration across the instances class hierarchy.
437 * The method will potect the attribute configuration value to protect the statically defined
438 * default value in ATTRS if required (if the value is an object literal, array or the
439 * attribute configuration has cloneDefaultValue set to shallow or deep).
441 * @method _aggregateAttrs
443 * @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
444 * (subclass first, Base last)
445 * @return {Object} The aggregate set of ATTRS definitions for the instance
447 _aggregateAttrs : function(allAttrs) {
455 cfgProps = Base._ATTR_CFG,
459 for (i = allAttrs.length-1; i >= 0; --i) {
462 for (attr in attrs) {
463 if (attrs.hasOwnProperty(attr)) {
465 // Protect config passed in
466 cfg = Y.mix({}, attrs[attr], true, cfgProps);
469 clone = cfg.cloneDefaultValue;
472 if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
473 cfg.value = Y.clone(val);
474 } else if (clone === SHALLOW) {
475 cfg.value = Y.merge(val);
477 // else if (clone === false), don't clone the static default value.
478 // It's intended to be used by reference.
482 if (attr.indexOf(DOT) !== -1) {
483 path = attr.split(DOT);
487 if (path && aggAttrs[attr] && aggAttrs[attr].value) {
488 O.setValue(aggAttrs[attr].value, path, val);
490 if (!aggAttrs[attr]) {
491 aggAttrs[attr] = cfg;
493 Y.mix(aggAttrs[attr], cfg, true, cfgProps);
505 * Initializes the class hierarchy for the instance, which includes
506 * initializing attributes for each class defined in the class's
507 * static <a href="#property_Base.ATTRS">ATTRS</a> property and
508 * invoking the initializer method on the prototype of each class in the hierarchy.
510 * @method _initHierarchy
511 * @param {Object} userVals Object with configuration property name/value pairs
514 _initHierarchy : function(userVals) {
515 var lazy = this._lazyAddAttrs,
521 classes = this._getClasses(),
522 attrCfgs = this._getAttrCfgs();
524 for (ci = classes.length-1; ci >= 0; ci--) {
526 constr = classes[ci];
527 constrProto = constr.prototype;
529 if (constr._yuibuild && constr._yuibuild.exts) {
530 for (ei = 0, el = constr._yuibuild.exts.length; ei < el; ei++) {
531 constr._yuibuild.exts[ei].apply(this, arguments);
535 this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
537 // Using INITIALIZER in hasOwnProperty check, for performance reasons (helps IE6 avoid GC thresholds when
538 // referencing string literals). Not using it in apply, again, for performance "." is faster.
539 if (constrProto.hasOwnProperty(INITIALIZER)) {
540 constrProto.initializer.apply(this, arguments);
546 * Destroys the class hierarchy for this instance by invoking
547 * the descructor method on the prototype of each class in the hierarchy.
549 * @method _destroyHierarchy
552 _destroyHierarchy : function() {
556 classes = this._getClasses();
558 for (ci = 0, cl = classes.length; ci < cl; ci++) {
559 constr = classes[ci];
560 constrProto = constr.prototype;
561 if (constrProto.hasOwnProperty(DESTRUCTOR)) {
562 constrProto.destructor.apply(this, arguments);
568 * Default toString implementation. Provides the constructor NAME
569 * and the instance guid, if set.
572 * @return {String} String representation for this object
574 toString: function() {
575 return this.name + "[" + Y.stamp(this, true) + "]";
580 // Straightup augment, no wrapper functions
581 Y.mix(Base, Attribute, false, null, 1);
584 Base.prototype.constructor = Base;
589 }, '3.3.0' ,{requires:['attribute-base']});