3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('base-base', function(Y) {
10
* The base module provides the Base class, which objects requiring attribute and custom event support can extend.
11
* The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides
12
* plugin support and also provides the Base.build method which provides a way to build custom classes using extensions.
18
* The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
19
* and without the extension support provided by Base.build.
22
* @submodule base-base
29
INITIALIZED = "initialized",
30
DESTROYED = "destroyed",
31
INITIALIZER = "initializer",
32
BUBBLETARGETS = "bubbleTargets",
33
_BUBBLETARGETS = "_bubbleTargets",
34
OBJECT_CONSTRUCTOR = Object.prototype.constructor,
37
DESTRUCTOR = "destructor",
39
Attribute = Y.Attribute,
41
_wlmix = function(r, s, wlhash) {
53
* A base class which objects requiring attributes and custom event support can
54
* extend. Base also handles the chaining of initializer and destructor methods across
55
* the hierarchy as part of object construction and destruction. Additionally, attributes configured
56
* through the static <a href="#property_Base.ATTRS">ATTRS</a> property for each class
57
* in the hierarchy will be initialized by Base.
61
* The static <a href="#property_Base.NAME">NAME</a> property of each class extending
62
* from Base will be used as the identifier for the class, and is used by Base to prefix
63
* all events fired by instances of that class.
71
* @param {Object} config Object with configuration property name/value pairs. The object can be
72
* used to provide default values for the objects published attributes.
75
* The config object can also contain the following non-attribute properties, providing a convenient
76
* way to configure events listeners and plugins for the instance, as part of the constructor call:
81
* <dd>An event name to listener function map, to register event listeners for the "on" moment of the event. A constructor convenience property for the <a href="Base.html#method_on">on</a> method.</dd>
83
* <dd>An event name to listener function map, to register event listeners for the "after" moment of the event. A constructor convenience property for the <a href="Base.html#method_after">after</a> method.</dd>
84
* <dt>bubbleTargets</dt>
85
* <dd>An object, or array of objects, to register as bubble targets for bubbled events fired by this instance. A constructor convenience property for the <a href="EventTarget.html#method_addTarget">addTarget</a> method.</dd>
87
* <dd>A plugin, or array of plugins to be plugged into the instance (see PluginHost's plug method for signature details). A constructor convenience property for the <a href="Plugin.Host.html#method_plug">plug</a> method.</dd>
91
Y.log('constructor called', 'life', 'base');
93
// So the object can be used as a hash key (as DD does)
98
// If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
99
// initial state, but don't initialize Plugins yet. That's done after initialization.
100
var PluginHost = Y.Plugin && Y.Plugin.Host;
101
if (this._initPlugins && PluginHost) {
102
PluginHost.call(this);
105
if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
108
* The string used to identify the class of this object.
110
* @deprecated Use this.constructor.NAME
114
this.name = this.constructor.NAME;
115
this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
117
this.init.apply(this, arguments);
121
* The list of properties which can be configured for
122
* each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
124
* @property _ATTR_CFG
129
Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
130
Base._ATTR_CFG_HASH = Y.Array.hash(Base._ATTR_CFG);
134
* The string to be used to identify instances of
135
* this class, for example in prefixing events.
138
* Classes extending Base, should define their own
139
* static NAME property, which should be camelCase by
140
* convention (e.g. MyClass.NAME = "myClass";).
149
* The default set of attributes which will be available for instances of this class, and
150
* their configuration. In addition to the configuration properties listed by
151
* Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
152
* can also be configured with a "cloneDefaultValue" property, which defines how the statically
153
* defined value field should be protected ("shallow", "deep" and false are supported values).
155
* By default if the value is an object literal or an array it will be "shallow" cloned, to
156
* protect the default value.
164
* Flag indicating whether or not this object
165
* has been through the init lifecycle phase.
167
* @attribute initialized
178
* Flag indicating whether or not this object
179
* has been through the destroy lifecycle phase.
181
* @attribute destroyed
195
* Init lifecycle method, invoked during construction.
196
* Fires the init event prior to setting up attributes and
197
* invoking initializers for the class hierarchy.
201
* @param {Object} config Object with configuration property name/value pairs
202
* @return {Base} A reference to this object
204
init: function(config) {
205
Y.log('init called', 'life', 'base');
207
this._yuievt.config.prefix = this._eventPrefix;
211
* Lifecycle event for the init phase, fired prior to initialization.
212
* Invoking the preventDefault() method on the event object provided
213
* to subscribers will prevent initialization from occuring.
216
* Subscribers to the "after" momemt of this event, will be notified
217
* after initialization of the object is complete (and therefore
218
* cannot prevent initialization).
222
* @preventable _defInitFn
223
* @param {EventFacade} e Event object, with a cfg property which
224
* refers to the configuration object passed to the constructor.
229
defaultTargetOnly:true,
230
defaultFn:this._defInitFn
233
this._preInitEventCfg(config);
235
this.fire(INIT, {cfg: config});
241
* Handles the special on, after and target properties which allow the user to
242
* easily configure on and after listeners as well as bubble targets during
243
* construction, prior to init.
246
* @method _preInitEventCfg
247
* @param {Object} config The user configuration object
249
_preInitEventCfg : function(config) {
255
this.after(config.after);
260
userTargets = (config && BUBBLETARGETS in config);
262
if (userTargets || _BUBBLETARGETS in this) {
263
target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
264
if (L.isArray(target)) {
265
for (i = 0, l = target.length; i < l; i++) {
266
this.addTarget(target[i]);
269
this.addTarget(target);
276
* Destroy lifecycle method. Fires the destroy
277
* event, prior to invoking destructors for the
281
* Subscribers to the destroy
282
* event can invoke preventDefault on the event object, to prevent destruction
286
* @return {Base} A reference to this object
289
destroy: function() {
290
Y.log('destroy called', 'life', 'base');
294
* Lifecycle event for the destroy phase,
295
* fired prior to destruction. Invoking the preventDefault
296
* method on the event object provided to subscribers will
297
* prevent destruction from proceeding.
300
* Subscribers to the "after" moment of this event, will be notified
301
* after destruction is complete (and as a result cannot prevent
305
* @preventable _defDestroyFn
306
* @param {EventFacade} e Event object
308
this.publish(DESTROY, {
311
defaultTargetOnly:true,
312
defaultFn: this._defDestroyFn
321
* Default init event handler
324
* @param {EventFacade} e Event object, with a cfg property which
325
* refers to the configuration object passed to the constructor.
328
_defInitFn : function(e) {
329
this._initHierarchy(e.cfg);
330
if (this._initPlugins) {
331
// Need to initPlugins manually, to handle constructor parsing, static Plug parsing
332
this._initPlugins(e.cfg);
334
this._set(INITIALIZED, true);
338
* Default destroy event handler
340
* @method _defDestroyFn
341
* @param {EventFacade} e Event object
344
_defDestroyFn : function(e) {
345
if (this._destroyPlugins) {
346
this._destroyPlugins();
348
this._destroyHierarchy();
349
this._set(DESTROYED, true);
353
* Returns the class hierarchy for this object, with Base being the last class in the array.
355
* @method _getClasses
357
* @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
358
* This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
361
_getClasses : function() {
362
if (!this._classes) {
363
this._initHierarchyData();
365
return this._classes;
369
* Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
371
* @method _getAttrCfgs
373
* @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
374
* This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
377
_getAttrCfgs : function() {
379
this._initHierarchyData();
385
* A helper method used when processing ATTRS across the class hierarchy during
386
* initialization. Returns a disposable object with the attributes defined for
387
* the provided class, extracted from the set of all attributes passed in .
389
* @method _filterAttrCfs
392
* @param {Function} clazz The class for which the desired attributes are required.
393
* @param {Object} allCfgs The set of all attribute configurations for this instance.
394
* Attributes will be removed from this set, if they belong to the filtered class, so
395
* that by the time all classes are processed, allCfgs will be empty.
397
* @return {Object} The set of attributes belonging to the class passed in, in the form
398
* of an object with attribute name/configuration pairs.
400
_filterAttrCfgs : function(clazz, allCfgs) {
401
var cfgs = null, attr, attrs = clazz.ATTRS;
404
for (attr in attrs) {
407
cfgs[attr] = allCfgs[attr];
408
allCfgs[attr] = null;
417
* A helper method used by _getClasses and _getAttrCfgs, which determines both
418
* the array of classes and aggregate set of attribute configurations
419
* across the class hierarchy for the instance.
421
* @method _initHierarchyData
424
_initHierarchyData : function() {
425
var c = this.constructor,
431
classes[classes.length] = c;
435
attrs[attrs.length] = c.ATTRS;
437
c = c.superclass ? c.superclass.constructor : null;
440
this._classes = classes;
441
this._attrs = this._aggregateAttrs(attrs);
445
* A helper method, used by _initHierarchyData to aggregate
446
* attribute configuration across the instances class hierarchy.
448
* The method will protect the attribute configuration value to protect the statically defined
449
* default value in ATTRS if required (if the value is an object literal, array or the
450
* attribute configuration has cloneDefaultValue set to shallow or deep).
452
* @method _aggregateAttrs
454
* @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
455
* (subclass first, Base last)
456
* @return {Object} The aggregate set of ATTRS definitions for the instance
458
_aggregateAttrs : function(allAttrs) {
466
cfgPropsHash = Base._ATTR_CFG_HASH,
470
for (i = allAttrs.length-1; i >= 0; --i) {
473
for (attr in attrs) {
474
if (attrs.hasOwnProperty(attr)) {
476
// Protect config passed in
477
//cfg = Y.mix({}, attrs[attr], true, cfgProps);
478
//cfg = Y.Object(attrs[attr]);
479
cfg = _wlmix({}, attrs[attr], cfgPropsHash);
482
clone = cfg.cloneDefaultValue;
485
if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
486
Y.log('Cloning default value for attribute:' + attr, 'info', 'base');
487
cfg.value = Y.clone(val);
488
} else if (clone === SHALLOW) {
489
Y.log('Merging default value for attribute:' + attr, 'info', 'base');
490
cfg.value = Y.merge(val);
492
// else if (clone === false), don't clone the static default value.
493
// It's intended to be used by reference.
497
if (attr.indexOf(DOT) !== -1) {
498
path = attr.split(DOT);
502
if (path && aggAttrs[attr] && aggAttrs[attr].value) {
503
O.setValue(aggAttrs[attr].value, path, val);
505
if (!aggAttrs[attr]) {
506
aggAttrs[attr] = cfg;
508
_wlmix(aggAttrs[attr], cfg, cfgPropsHash);
520
* Initializes the class hierarchy for the instance, which includes
521
* initializing attributes for each class defined in the class's
522
* static <a href="#property_Base.ATTRS">ATTRS</a> property and
523
* invoking the initializer method on the prototype of each class in the hierarchy.
525
* @method _initHierarchy
526
* @param {Object} userVals Object with configuration property name/value pairs
529
_initHierarchy : function(userVals) {
530
var lazy = this._lazyAddAttrs,
538
classes = this._getClasses(),
539
attrCfgs = this._getAttrCfgs();
541
for (ci = classes.length-1; ci >= 0; ci--) {
543
constr = classes[ci];
544
constrProto = constr.prototype;
545
exts = constr._yuibuild && constr._yuibuild.exts;
548
for (ei = 0, el = exts.length; ei < el; ei++) {
549
exts[ei].apply(this, arguments);
553
this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
555
// Using INITIALIZER in hasOwnProperty check, for performance reasons (helps IE6 avoid GC thresholds when
556
// referencing string literals). Not using it in apply, again, for performance "." is faster.
557
if (constrProto.hasOwnProperty(INITIALIZER)) {
558
constrProto.initializer.apply(this, arguments);
562
for (ei = 0; ei < el; ei++) {
563
extProto = exts[ei].prototype;
564
if (extProto.hasOwnProperty(INITIALIZER)) {
565
extProto.initializer.apply(this, arguments);
573
* Destroys the class hierarchy for this instance by invoking
574
* the destructor method on the prototype of each class in the hierarchy.
576
* @method _destroyHierarchy
579
_destroyHierarchy : function() {
582
ci, cl, ei, el, exts, extProto,
583
classes = this._getClasses();
585
for (ci = 0, cl = classes.length; ci < cl; ci++) {
586
constr = classes[ci];
587
constrProto = constr.prototype;
588
exts = constr._yuibuild && constr._yuibuild.exts;
591
for (ei = 0, el = exts.length; ei < el; ei++) {
592
extProto = exts[ei].prototype;
593
if (extProto.hasOwnProperty(DESTRUCTOR)) {
594
extProto.destructor.apply(this, arguments);
599
if (constrProto.hasOwnProperty(DESTRUCTOR)) {
600
constrProto.destructor.apply(this, arguments);
606
* Default toString implementation. Provides the constructor NAME
607
* and the instance guid, if set.
610
* @return {String} String representation for this object
612
toString: function() {
613
return this.name + "[" + Y.stamp(this, true) + "]";
618
// Straightup augment, no wrapper functions
619
Y.mix(Base, Attribute, false, null, 1);
622
Base.prototype.constructor = Base;
627
}, '3.4.1' ,{requires:['attribute-base']});