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']});
532 YUI.add('base-pluginhost', function(Y) {
535 * The base-pluginhost submodule adds Plugin support to Base, by augmenting Base with
536 * Plugin.Host and setting up static (class level) Base.plug and Base.unplug methods.
539 * @submodule base-pluginhost
544 PluginHost = Y.Plugin.Host;
546 Y.mix(Base, PluginHost, false, null, 1);
549 * Alias for <a href="Plugin.Host.html#method_Plugin.Host.plug">Plugin.Host.plug</a>. See aliased
550 * method for argument and return value details.
555 Base.plug = PluginHost.plug;
558 * Alias for <a href="Plugin.Host.html#method_Plugin.Host.unplug">Plugin.Host.unplug</a>. See the
559 * aliased method for argument and return value details.
561 * @method Base.unplug
564 Base.unplug = PluginHost.unplug;
567 }, '3.0.0' ,{requires:['base-base', 'pluginhost']});
568 YUI.add('base-build', function(Y) {
571 * The base-build submodule provides Base.build functionality, which
572 * can be used to create custom classes, by aggregating extensions onto
576 * @submodule base-build
584 * The build configuration for the Base class.
586 * Defines the static fields which need to be aggregated
587 * when the Base class is used as the main class passed to
588 * the <a href="#method_Base.build">Base.build</a> method.
590 * @property Base._buildCfg
597 aggregates : ["ATTRS", "_PLUG", "_UNPLUG"]
602 * Builds a custom constructor function (class) from the
603 * main function, and array of extension functions (classes)
604 * provided. The NAME field for the constructor function is
605 * defined by the first argument passed in.
608 * The cfg object supports the following properties
611 * <dt>dynamic <boolean></dt>
613 * <p>If true (default), a completely new class
614 * is created which extends the main class, and acts as the
615 * host on which the extension classes are augmented.</p>
616 * <p>If false, the extensions classes are augmented directly to
617 * the main class, modifying the main class' prototype.</p>
619 * <dt>aggregates <String[]></dt>
620 * <dd>An array of static property names, which will get aggregated
621 * on to the built class, in addition to the default properties build
622 * will always aggregate as defined by the main class' static _buildCfg
629 * @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
630 * @param {Function} main The main class on which to base the built class
631 * @param {Function[]} extensions The set of extension classes which will be
632 * augmented/aggregated to the built class.
633 * @param {Object} cfg Optional. Build configuration for the class (see description).
634 * @return {Function} A custom class, created from the provided main and extension classes
636 Base.build = function(name, main, extensions, cfg) {
638 var build = Base.build,
639 builtClass = build._getClass(main, cfg),
640 aggregates = build._getAggregates(main, cfg),
641 dynamic = builtClass._yuibuild.dynamic,
644 // Shallow isolate aggregates
647 for (i = 0, l = aggregates.length; i < l; ++i) {
649 if (main.hasOwnProperty(val)) {
650 builtClass[val] = L.isArray(main[val]) ? [] : {};
653 Y.aggregate(builtClass, main, true, aggregates);
658 for (i = 0, l = extensions.length; i < l; i++) {
659 extClass = extensions[i];
662 Y.aggregate(builtClass, extClass, true, aggregates);
666 Y.mix(builtClass, extClass, true, null, 1);
668 builtClass._yuibuild.exts.push(extClass);
671 builtClass.prototype.hasImpl = build._hasImpl;
674 builtClass.NAME = name;
675 builtClass.prototype.constructor = builtClass;
683 _template: function(main) {
685 function BuiltClass() {
687 BuiltClass.superclass.constructor.apply(this, arguments);
689 var f = BuiltClass._yuibuild.exts,
693 for (i = 0; i < l; i++) {
694 f[i].apply(this, arguments);
699 Y.extend(BuiltClass, main);
704 _hasImpl : function(extClass) {
705 var classes = this._getClasses();
706 for (var i = 0, l = classes.length; i < l; i++) {
707 var cls = classes[i];
710 var exts = cls._yuibuild.exts,
714 for (j = 0; j < ll; j++) {
715 if (exts[j] === extClass) {
724 _getClass : function(main, cfg) {
726 var dynamic = (cfg && false === cfg.dynamic) ? false : true,
727 builtClass = (dynamic) ? Base.build._template(main) : main;
729 builtClass._yuibuild = {
738 _getAggregates : function(main, cfg) {
740 cfgAggr = (cfg && cfg.aggregates),
744 while (c && c.prototype) {
745 classAggr = c._buildCfg && c._buildCfg.aggregates;
747 aggr = aggr.concat(classAggr);
749 c = c.superclass ? c.superclass.constructor : null;
753 aggr = aggr.concat(cfgAggr);
761 }, '3.0.0' ,{requires:['base-base']});
764 YUI.add('base', function(Y){}, '3.0.0' ,{use:['base-base', 'base-pluginhost', 'base-build']});