16
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),
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,
81
35
dynamic = builtClass._yuibuild.dynamic,
82
37
i, l, val, extClass;
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]) ? [] : {};
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]) ? [] : {};
93
Y.aggregate(builtClass, main, true, aggregates);
98
49
for (i = 0, l = extensions.length; i < l; i++) {
99
50
extClass = extensions[i];
102
Y.aggregate(builtClass, extClass, true, aggregates);
52
// Prototype, old non-displacing augment
106
53
Y.mix(builtClass, extClass, true, null, 1);
55
_mixCust(builtClass, extClass, aggregates, custom);
108
57
builtClass._yuibuild.exts.push(extClass);
111
builtClass.prototype.hasImpl = build._hasImpl;
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;
114
72
builtClass.NAME = name;
118
76
return builtClass;
123
_template: function(main) {
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) {
125
100
function BuiltClass() {
127
101
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
103
Y.extend(BuiltClass, main);
141
105
return BuiltClass;
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];
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++) {
149
112
if (cls._yuibuild) {
150
var exts = cls._yuibuild.exts,
113
exts = cls._yuibuild.exts;
154
116
for (j = 0; j < ll; j++) {
155
117
if (exts[j] === extClass) {
164
_getClass : function(main, cfg) {
126
_ctor : function(main, cfg) {
166
128
var dynamic = (cfg && false === cfg.dynamic) ? false : true,
167
builtClass = (dynamic) ? Base.build._template(main) : main;
129
builtClass = (dynamic) ? build._tmpl(main) : main;
169
131
builtClass._yuibuild = {
175
137
return builtClass;
178
_getAggregates : function(main, cfg) {
140
_cfg : function(main, cfg) {
180
144
cfgAggr = (cfg && cfg.aggregates),
145
cfgCustBuild = (cfg && cfg.custom),
184
148
while (c && c.prototype) {
185
classAggr = c._buildCfg && c._buildCfg.aggregates;
187
aggr = aggr.concat(classAggr);
149
buildCfg = c._buildCfg;
151
if (buildCfg.aggregates) {
152
aggr = aggr.concat(buildCfg.aggregates);
154
if (buildCfg.custom) {
155
Y.mix(cust, buildCfg.custom, true);
189
158
c = c.superclass ? c.superclass.constructor : null;
193
162
aggr = aggr.concat(cfgAggr);
165
Y.mix(cust, cfg.cfgBuild, true);
174
_clean : function(sx, aggregates, custom) {
175
var prop, i, l, sxclone = Y.merge(sx);
177
for (prop in custom) {
178
if (sxclone.hasOwnProperty(prop)) {
179
delete sxclone[prop];
183
for (i = 0, l = aggregates.length; i < l; i++) {
184
prop = aggregates[i];
185
if (sxclone.hasOwnProperty(prop)) {
186
delete sxclone[prop];
201
}, '3.0.0' ,{requires:['base-base']});
196
* Builds a custom constructor function (class) from the
197
* main function, and array of extension functions (classes)
198
* provided. The NAME field for the constructor function is
199
* defined by the first argument passed in.
202
* The cfg object supports the following properties
205
* <dt>dynamic <boolean></dt>
207
* <p>If true (default), a completely new class
208
* is created which extends the main class, and acts as the
209
* host on which the extension classes are augmented.</p>
210
* <p>If false, the extensions classes are augmented directly to
211
* the main class, modifying the main class' prototype.</p>
213
* <dt>aggregates <String[]></dt>
214
* <dd>An array of static property names, which will get aggregated
215
* on to the built class, in addition to the default properties build
216
* will always aggregate as defined by the main class' static _buildCfg
223
* @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
224
* @param {Function} main The main class on which to base the built class
225
* @param {Function[]} extensions The set of extension classes which will be
226
* augmented/aggregated to the built class.
227
* @param {Object} cfg Optional. Build configuration for the class (see description).
228
* @return {Function} A custom class, created from the provided main and extension classes
230
Base.build = function(name, main, extensions, cfg) {
231
return build(name, main, extensions, null, null, cfg);
235
* <p>Creates a new class (constructor function) which extends the base class passed in as the second argument,
236
* and mixes in the array of extensions provided.</p>
237
* <p>Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).</p>
238
* <p>Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).</p>
242
* @method Base.create
244
* @param {Function} name The name of the newly created class. Used to defined the NAME property for the new class.
245
* @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).
246
* @param {Function[]} extensions The list of extensions which will be mixed into the built class.
247
* @return {Function} The newly created class.
249
Base.create = function(name, base, extensions, px, sx) {
250
return build(name, base, extensions, px, sx);
254
* <p>Mixes in a list of extensions to an existing class.</p>
257
* @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)
258
* @param {Function[]} extensions The set of extension classes which will mixed into the existing main class.
259
* @return {Function} The modified main class, with extensions mixed in.
261
Base.mix = function(main, extensions) {
262
return build(null, main, extensions, null, null, {dynamic:false});
266
* The build configuration for the Base class.
268
* Defines the static fields which need to be aggregated
269
* when the Base class is used as the main class passed to
270
* the <a href="#method_Base.build">Base.build</a> method.
272
* @property Base._buildCfg
280
ATTRS : function(prop, r, s) {
281
r[prop] = r[prop] || {};
283
Y.aggregate(r[prop], s[prop], true);
287
aggregates : ["_PLUG", "_UNPLUG"]
291
}, '3.1.0' ,{requires:['base-base']});