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-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
23 Base._build = function(name, main, extensions, px, sx, cfg) {
25 var build = Base._build,
27 builtClass = build._ctor(main, cfg),
28 buildCfg = build._cfg(main, cfg),
30 _mixCust = build._mixCust,
32 aggregates = buildCfg.aggregates,
33 custom = buildCfg.custom,
35 dynamic = builtClass._yuibuild.dynamic,
39 if (dynamic && aggregates) {
40 for (i = 0, l = aggregates.length; i < l; ++i) {
42 if (main.hasOwnProperty(val)) {
43 builtClass[val] = L.isArray(main[val]) ? [] : {};
49 for (i = 0, l = extensions.length; i < l; i++) {
50 extClass = extensions[i];
52 // Prototype, old non-displacing augment
53 Y.mix(builtClass, extClass, true, null, 1);
55 _mixCust(builtClass, extClass, aggregates, custom);
57 builtClass._yuibuild.exts.push(extClass);
61 Y.mix(builtClass.prototype, px, true);
65 Y.mix(builtClass, build._clean(sx, aggregates, custom), true);
66 _mixCust(builtClass, sx, aggregates, custom);
69 builtClass.prototype.hasImpl = build._impl;
72 builtClass.NAME = name;
73 builtClass.prototype.constructor = builtClass;
83 _mixCust: function(r, s, aggregates, custom) {
86 Y.aggregate(r, s, true, aggregates);
90 for (var j in custom) {
91 if (custom.hasOwnProperty(j)) {
98 _tmpl: function(main) {
100 function BuiltClass() {
101 BuiltClass.superclass.constructor.apply(this, arguments);
103 Y.extend(BuiltClass, main);
108 _impl : function(extClass) {
109 var classes = this._getClasses(), i, l, cls, exts, ll, j;
110 for (i = 0, l = classes.length; i < l; i++) {
113 exts = cls._yuibuild.exts;
116 for (j = 0; j < ll; j++) {
117 if (exts[j] === extClass) {
126 _ctor : function(main, cfg) {
128 var dynamic = (cfg && false === cfg.dynamic) ? false : true,
129 builtClass = (dynamic) ? build._tmpl(main) : main,
130 buildCfg = builtClass._yuibuild;
133 buildCfg = builtClass._yuibuild = {};
136 buildCfg.id = buildCfg.id || null;
137 buildCfg.exts = buildCfg.exts || [];
138 buildCfg.dynamic = dynamic;
143 _cfg : function(main, cfg) {
147 cfgAggr = (cfg && cfg.aggregates),
148 cfgCustBuild = (cfg && cfg.custom),
151 while (c && c.prototype) {
152 buildCfg = c._buildCfg;
154 if (buildCfg.aggregates) {
155 aggr = aggr.concat(buildCfg.aggregates);
157 if (buildCfg.custom) {
158 Y.mix(cust, buildCfg.custom, true);
161 c = c.superclass ? c.superclass.constructor : null;
165 aggr = aggr.concat(cfgAggr);
168 Y.mix(cust, cfg.cfgBuild, true);
177 _clean : function(sx, aggregates, custom) {
178 var prop, i, l, sxclone = Y.merge(sx);
180 for (prop in custom) {
181 if (sxclone.hasOwnProperty(prop)) {
182 delete sxclone[prop];
186 for (i = 0, l = aggregates.length; i < l; i++) {
187 prop = aggregates[i];
188 if (sxclone.hasOwnProperty(prop)) {
189 delete sxclone[prop];
199 * Builds a custom constructor function (class) from the
200 * main function, and array of extension functions (classes)
201 * provided. The NAME field for the constructor function is
202 * defined by the first argument passed in.
205 * The cfg object supports the following properties
208 * <dt>dynamic <boolean></dt>
210 * <p>If true (default), a completely new class
211 * is created which extends the main class, and acts as the
212 * host on which the extension classes are augmented.</p>
213 * <p>If false, the extensions classes are augmented directly to
214 * the main class, modifying the main class' prototype.</p>
216 * <dt>aggregates <String[]></dt>
217 * <dd>An array of static property names, which will get aggregated
218 * on to the built class, in addition to the default properties build
219 * will always aggregate as defined by the main class' static _buildCfg
225 * @deprecated Use the more convenient Base.create and Base.mix methods instead
227 * @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
228 * @param {Function} main The main class on which to base the built class
229 * @param {Function[]} extensions The set of extension classes which will be
230 * augmented/aggregated to the built class.
231 * @param {Object} cfg Optional. Build configuration for the class (see description).
232 * @return {Function} A custom class, created from the provided main and extension classes
234 Base.build = function(name, main, extensions, cfg) {
235 return build(name, main, extensions, null, null, cfg);
239 * <p>Creates a new class (constructor function) which extends the base class passed in as the second argument,
240 * and mixes in the array of extensions provided.</p>
241 * <p>Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).</p>
242 * <p>Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).</p>
246 * @method Base.create
248 * @param {Function} name The name of the newly created class. Used to defined the NAME property for the new class.
249 * @param {Function} main The base class which the new class should extend. This class needs to be Base or a class derived from base (e.g. Widget).
250 * @param {Function[]} extensions The list of extensions which will be mixed into the built class.
251 * @param {Object} px The set of prototype properties/methods to add to the built class.
252 * @param {Object} sx The set of static properties/methods to add to the built class.
253 * @return {Function} The newly created class.
255 Base.create = function(name, base, extensions, px, sx) {
256 return build(name, base, extensions, px, sx);
260 * <p>Mixes in a list of extensions to an existing class.</p>
263 * @param {Function} main The existing class into which the extensions should be mixed. The class needs to be Base or class derived from base (e.g. Widget)
264 * @param {Function[]} extensions The set of extension classes which will mixed into the existing main class.
265 * @return {Function} The modified main class, with extensions mixed in.
267 Base.mix = function(main, extensions) {
268 return build(null, main, extensions, null, null, {dynamic:false});
272 * The build configuration for the Base class.
274 * Defines the static fields which need to be aggregated
275 * when the Base class is used as the main class passed to
276 * the <a href="#method_Base.build">Base.build</a> method.
278 * @property Base._buildCfg
286 ATTRS : function(prop, r, s) {
288 r.ATTRS = r.ATTRS || {};
292 var sAttrs = s.ATTRS,
297 if (sAttrs.hasOwnProperty(a)) {
298 rAttrs[a] = rAttrs[a] || {};
299 Y.mix(rAttrs[a], sAttrs[a], true);
305 aggregates : ["_PLUG", "_UNPLUG"]
309 }, '3.3.0' ,{requires:['base-base']});