2
2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
3
Code licensed under the BSD License:
4
4
http://developer.yahoo.net/yui/license.txt
7
7
YUI.add('base', function(Y) {
108
* The build configuration for the Base class.
109
* Defines the static fields which need to be aggregated,
110
* when this class is used as the main class passed to
111
* the <a href="#method_build">Base.build</a> method.
113
* @property _buildCfg
120
aggregates : ["ATTRS"]
108
123
var _instances = {};
120
135
* <dt>dynamic <boolean></dt>
122
* <p>If true, a completely new class
137
* <p>If true (default), a completely new class
123
138
* is created which extends the main class, and acts as the
124
139
* host on which the extension classes are augmented.</p>
125
140
* <p>If false, the extensions classes are augmented directly to
128
143
* <dt>aggregates <String[]></dt>
129
144
* <dd>An array of static property names, which will get aggregated
130
* on to the built class in addition to the default properties build
131
* will always aggregate - "ATTRS" and "PLUGINS", as defined by
132
* Base.build.AGGREGATES</dd>
145
* on to the built class, in addition to the default properties build
146
* will always aggregate as defined by the main class' _buildCfg
143
159
Base.build = function(main, extensions, cfg) {
145
161
var build = Base.build,
153
aggregates = cfg.aggregates;
154
dynamic = cfg.dynamic;
157
// Create dynamic class or just modify main class
158
builtClass = (dynamic) ? build._template(main) : main;
160
builtClass._yuibuild = {
166
aggregates = (aggregates) ? build.AGGREGATES.concat(aggregates) : build.AGGREGATES;
168
var el = extensions.length,
169
al = aggregates.length,
163
builtClass = build._getClass(main, cfg),
164
aggregates = build._getAggregates(main, cfg),
165
dynamic = builtClass._yuibuild.dynamic,
172
171
// Shallow isolate aggregates
173
if (dynamic && aggregates) {
174
for (i = 0; i < al; i++) {
175
var val = aggregates[i];
176
if (O.owns(main, val)) {
177
builtClass[val] = L.isArray(main[val]) ? [] : {};
174
for (i = 0, l = aggregates.length; i < l; ++i) {
175
var val = aggregates[i];
176
if (O.owns(main, val)) {
177
builtClass[val] = L.isArray(main[val]) ? [] : {};
180
Y.aggregate(builtClass, main, true, aggregates);
180
Y.aggregate(builtClass, main, true, aggregates);
183
184
// Augment/Aggregate
184
for (i = 0; i < el; i++) {
185
extClass = extensions[i];
185
for (i = 0, l = extensions.length; i < l; i++) {
186
var extClass = extensions[i];
187
188
if (aggregates) {
188
189
Y.aggregate(builtClass, extClass, true, aggregates);
206
207
return builtClass;
209
210
Y.mix(Base.build, {
211
AGGREGATES : ["ATTRS", "PLUGINS"],
213
212
_template: function(main) {
215
214
function BuiltClass() {
216
216
BuiltClass.superclass.constructor.apply(this, arguments);
218
218
var f = BuiltClass._yuibuild.exts,
247
_getClass : function(main, cfg) {
249
// Create dynamic class or just modify main class
250
var dynamic = (cfg && false === cfg.dynamic) ? false : true,
251
builtClass = (dynamic) ? Base.build._template(main) : main;
253
builtClass._yuibuild = {
262
_getAggregates : function(main, cfg) {
264
cfgAggr = (cfg && cfg.aggregates),
267
while (c && c.prototype) {
268
var classAggr = c._buildCfg && c._buildCfg.aggregates;
270
aggr = aggr.concat(classAggr);
272
c = c.superclass ? c.superclass.constructor : null;
276
aggr = aggr.concat(cfgAggr);
252
286
* The custom class is created from the main class passed in as the first parameter
253
287
* along with the list of extension classes passed in
254
288
* as the second parameter using <a href="#method_build">Base.build</a>
255
* with "dynamic" set to true. See the documentation for this method
289
* with "dynamic" set to true. See the documentation for Base.build method
256
290
* to see how the main class and extension classes are used.
259
293
* <p>Any arguments following the 2nd argument are passed as arguments to the
260
294
* constructor of the newly created class used to create the instance.</p>
271
305
* @return {Object} An instance of the custom class
273
307
Base.create = function(main, extensions, args) {
274
var c = Base.build(main, extensions, {dynamic:true}),
308
var c = Base.build(main, extensions),
275
309
cArgs = Y.Array(arguments, 2, true);
308
* Init event, fired prior to initialization. Invoking
309
* the preventDefault method on the event object provided
342
* Lifecycle event for the init phase, fired prior to initialization.
343
* Invoking the preventDefault method on the event object provided
310
344
* to subscribers will prevent initialization from occuring.
352
* Destroy event, fired prior to destruction. Invoking
353
* the preventDefault method on the event object provided
354
* to subscribers will prevent destruction from proceeding.
386
* Lifecycle event for the destroy phase,
387
* fired prior to destruction. Invoking the preventDefault
388
* method on the event object provided to subscribers will
389
* prevent destruction from proceeding.
357
392
* Subscribers to the "after" moment of this event, will be notified
374
409
* Default init event handler
376
411
* @method _defInitFn
412
* @param {Event.Facade} e Event object
377
413
* @param {Object} config Object literal of configuration property name/value pairs
380
_defInitFn : function(config) {
416
_defInitFn : function(e, config) {
381
417
_instances[Y.stamp(this)] = this;
382
418
this._initHierarchy(config);
384
this._conf.remove(INITIALIZED, VALUE);
385
this.set(INITIALIZED, true);
420
this._set(INITIALIZED, true);
389
424
* Default destroy event handler
391
426
* @method _defDestroyFn
427
* @param {Event.Facade} e Event object
394
_defDestroyFn : function() {
430
_defDestroyFn : function(e) {
395
431
this._destroyHierarchy();
396
432
delete _instances[this._yuid];
398
this._conf.remove(DESTROYED, VALUE);
399
this.set(DESTROYED, true);
434
this._set(DESTROYED, true);
660
695
Y.mix(Base, Y.Attribute, false, null, 1);
697
Base.prototype.constructor = Base;
666
}, '3.0.0pr1' ,{requires:['attribute']});
702
}, '3.0.0pr2' ,{requires:['attribute']});