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-build', function(Y) {
11 * The base-build submodule provides Base.build functionality, which
12 * can be used to create custom classes, by aggregating extensions onto
16 * @submodule base-build
24 * The build configuration for the Base class.
26 * Defines the static fields which need to be aggregated
27 * when the Base class is used as the main class passed to
28 * the <a href="#method_Base.build">Base.build</a> method.
30 * @property Base._buildCfg
37 aggregates : ["ATTRS", "_PLUG", "_UNPLUG"]
42 * Builds a custom constructor function (class) from the
43 * main function, and array of extension functions (classes)
44 * provided. The NAME field for the constructor function is
45 * defined by the first argument passed in.
48 * The cfg object supports the following properties
51 * <dt>dynamic <boolean></dt>
53 * <p>If true (default), a completely new class
54 * is created which extends the main class, and acts as the
55 * host on which the extension classes are augmented.</p>
56 * <p>If false, the extensions classes are augmented directly to
57 * the main class, modifying the main class' prototype.</p>
59 * <dt>aggregates <String[]></dt>
60 * <dd>An array of static property names, which will get aggregated
61 * on to the built class, in addition to the default properties build
62 * will always aggregate as defined by the main class' static _buildCfg
69 * @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
70 * @param {Function} main The main class on which to base the built class
71 * @param {Function[]} extensions The set of extension classes which will be
72 * augmented/aggregated to the built class.
73 * @param {Object} cfg Optional. Build configuration for the class (see description).
74 * @return {Function} A custom class, created from the provided main and extension classes
76 Base.build = function(name, main, extensions, cfg) {
78 var build = Base.build,
79 builtClass = build._getClass(main, cfg),
80 aggregates = build._getAggregates(main, cfg),
81 dynamic = builtClass._yuibuild.dynamic,
84 // Shallow isolate aggregates
87 for (i = 0, l = aggregates.length; i < l; ++i) {
89 if (main.hasOwnProperty(val)) {
90 builtClass[val] = L.isArray(main[val]) ? [] : {};
93 Y.aggregate(builtClass, main, true, aggregates);
98 for (i = 0, l = extensions.length; i < l; i++) {
99 extClass = extensions[i];
102 Y.aggregate(builtClass, extClass, true, aggregates);
106 Y.mix(builtClass, extClass, true, null, 1);
108 builtClass._yuibuild.exts.push(extClass);
111 builtClass.prototype.hasImpl = build._hasImpl;
114 builtClass.NAME = name;
115 builtClass.prototype.constructor = builtClass;
123 _template: function(main) {
125 function BuiltClass() {
127 BuiltClass.superclass.constructor.apply(this, arguments);
129 var f = BuiltClass._yuibuild.exts,
133 for (i = 0; i < l; i++) {
134 f[i].apply(this, arguments);
139 Y.extend(BuiltClass, main);
144 _hasImpl : function(extClass) {
145 var classes = this._getClasses();
146 for (var i = 0, l = classes.length; i < l; i++) {
147 var cls = classes[i];
150 var exts = cls._yuibuild.exts,
154 for (j = 0; j < ll; j++) {
155 if (exts[j] === extClass) {
164 _getClass : function(main, cfg) {
166 var dynamic = (cfg && false === cfg.dynamic) ? false : true,
167 builtClass = (dynamic) ? Base.build._template(main) : main;
169 builtClass._yuibuild = {
178 _getAggregates : function(main, cfg) {
180 cfgAggr = (cfg && cfg.aggregates),
184 while (c && c.prototype) {
185 classAggr = c._buildCfg && c._buildCfg.aggregates;
187 aggr = aggr.concat(classAggr);
189 c = c.superclass ? c.superclass.constructor : null;
193 aggr = aggr.concat(cfgAggr);
201 }, '3.0.0' ,{requires:['base-base']});