3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('base-build', function(Y) {
10
* The base-build submodule provides Base.build functionality, which
11
* can be used to create custom classes, by aggregating extensions onto
15
* @submodule base-build
20
INITIALIZER = "initializer",
21
DESTRUCTOR = "destructor",
24
Base._build = function(name, main, extensions, px, sx, cfg) {
26
var build = Base._build,
28
builtClass = build._ctor(main, cfg),
29
buildCfg = build._cfg(main, cfg),
31
_mixCust = build._mixCust,
33
aggregates = buildCfg.aggregates,
34
custom = buildCfg.custom,
36
dynamic = builtClass._yuibuild.dynamic,
38
i, l, val, extClass, extProto,
42
if (dynamic && aggregates) {
43
for (i = 0, l = aggregates.length; i < l; ++i) {
45
if (main.hasOwnProperty(val)) {
46
builtClass[val] = L.isArray(main[val]) ? [] : {};
52
for (i = 0, l = extensions.length; i < l; i++) {
53
extClass = extensions[i];
55
extProto = extClass.prototype;
57
initializer = extProto[INITIALIZER];
58
destructor = extProto[DESTRUCTOR];
59
delete extProto[INITIALIZER];
60
delete extProto[DESTRUCTOR];
62
// Prototype, old non-displacing augment
63
Y.mix(builtClass, extClass, true, null, 1);
66
_mixCust(builtClass, extClass, aggregates, custom);
69
extProto[INITIALIZER] = initializer;
73
extProto[DESTRUCTOR] = destructor;
76
builtClass._yuibuild.exts.push(extClass);
80
Y.mix(builtClass.prototype, px, true);
84
Y.mix(builtClass, build._clean(sx, aggregates, custom), true);
85
_mixCust(builtClass, sx, aggregates, custom);
88
builtClass.prototype.hasImpl = build._impl;
91
builtClass.NAME = name;
92
builtClass.prototype.constructor = builtClass;
102
_mixCust: function(r, s, aggregates, custom) {
105
Y.aggregate(r, s, true, aggregates);
109
for (var j in custom) {
110
if (custom.hasOwnProperty(j)) {
117
_tmpl: function(main) {
119
function BuiltClass() {
120
BuiltClass.superclass.constructor.apply(this, arguments);
122
Y.extend(BuiltClass, main);
127
_impl : function(extClass) {
128
var classes = this._getClasses(), i, l, cls, exts, ll, j;
129
for (i = 0, l = classes.length; i < l; i++) {
132
exts = cls._yuibuild.exts;
135
for (j = 0; j < ll; j++) {
136
if (exts[j] === extClass) {
145
_ctor : function(main, cfg) {
147
var dynamic = (cfg && false === cfg.dynamic) ? false : true,
148
builtClass = (dynamic) ? build._tmpl(main) : main,
149
buildCfg = builtClass._yuibuild;
152
buildCfg = builtClass._yuibuild = {};
155
buildCfg.id = buildCfg.id || null;
156
buildCfg.exts = buildCfg.exts || [];
157
buildCfg.dynamic = dynamic;
162
_cfg : function(main, cfg) {
166
cfgAggr = (cfg && cfg.aggregates),
167
cfgCustBuild = (cfg && cfg.custom),
170
while (c && c.prototype) {
171
buildCfg = c._buildCfg;
173
if (buildCfg.aggregates) {
174
aggr = aggr.concat(buildCfg.aggregates);
176
if (buildCfg.custom) {
177
Y.mix(cust, buildCfg.custom, true);
180
c = c.superclass ? c.superclass.constructor : null;
184
aggr = aggr.concat(cfgAggr);
187
Y.mix(cust, cfg.cfgBuild, true);
196
_clean : function(sx, aggregates, custom) {
197
var prop, i, l, sxclone = Y.merge(sx);
199
for (prop in custom) {
200
if (sxclone.hasOwnProperty(prop)) {
201
delete sxclone[prop];
205
for (i = 0, l = aggregates.length; i < l; i++) {
206
prop = aggregates[i];
207
if (sxclone.hasOwnProperty(prop)) {
208
delete sxclone[prop];
218
* Builds a custom constructor function (class) from the
219
* main function, and array of extension functions (classes)
220
* provided. The NAME field for the constructor function is
221
* defined by the first argument passed in.
224
* The cfg object supports the following properties
227
* <dt>dynamic <boolean></dt>
229
* <p>If true (default), a completely new class
230
* is created which extends the main class, and acts as the
231
* host on which the extension classes are augmented.</p>
232
* <p>If false, the extensions classes are augmented directly to
233
* the main class, modifying the main class' prototype.</p>
235
* <dt>aggregates <String[]></dt>
236
* <dd>An array of static property names, which will get aggregated
237
* on to the built class, in addition to the default properties build
238
* will always aggregate as defined by the main class' static _buildCfg
244
* @deprecated Use the more convenient Base.create and Base.mix methods instead
246
* @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
247
* @param {Function} main The main class on which to base the built class
248
* @param {Function[]} extensions The set of extension classes which will be
249
* augmented/aggregated to the built class.
250
* @param {Object} cfg Optional. Build configuration for the class (see description).
251
* @return {Function} A custom class, created from the provided main and extension classes
253
Base.build = function(name, main, extensions, cfg) {
254
return build(name, main, extensions, null, null, cfg);
258
* <p>Creates a new class (constructor function) which extends the base class passed in as the second argument,
259
* and mixes in the array of extensions provided.</p>
260
* <p>Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).</p>
261
* <p>Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).</p>
267
* @param {Function} name The name of the newly created class. Used to defined the NAME property for the new class.
268
* @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).
269
* @param {Function[]} extensions The list of extensions which will be mixed into the built class.
270
* @param {Object} px The set of prototype properties/methods to add to the built class.
271
* @param {Object} sx The set of static properties/methods to add to the built class.
272
* @return {Function} The newly created class.
274
Base.create = function(name, base, extensions, px, sx) {
275
return build(name, base, extensions, px, sx);
279
* <p>Mixes in a list of extensions to an existing class.</p>
282
* @param {Function} main The existing class into which the extensions should be mixed. The class needs to be Base or a class derived from Base (e.g. Widget)
283
* @param {Function[]} extensions The set of extension classes which will mixed into the existing main class.
284
* @return {Function} The modified main class, with extensions mixed in.
286
Base.mix = function(main, extensions) {
287
return build(null, main, extensions, null, null, {dynamic:false});
291
* The build configuration for the Base class.
293
* Defines the static fields which need to be aggregated
294
* when the Base class is used as the main class passed to
295
* the <a href="#method_Base.build">Base.build</a> method.
297
* @property _buildCfg
305
ATTRS : function(prop, r, s) {
307
r.ATTRS = r.ATTRS || {};
311
var sAttrs = s.ATTRS,
316
if (sAttrs.hasOwnProperty(a)) {
317
rAttrs[a] = rAttrs[a] || {};
318
Y.mix(rAttrs[a], sAttrs[a], true);
324
aggregates : ["_PLUG", "_UNPLUG"]
328
}, '3.4.1' ,{requires:['base-base']});