3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('base-core', 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 BaseCore.build method which provides a way to build custom classes using extensions.
18
* <p>The base-core module provides the BaseCore class, the lightest version of Base,
19
* which provides Base's basic lifecycle management and ATTRS construction support,
20
* but doesn't fire init/destroy or attribute change events.</p>
22
* <p>It mixes in AttributeCore, which is the lightest version of Attribute</p>
25
* @submodule base-core
30
INITIALIZED = "initialized",
31
DESTROYED = "destroyed",
32
INITIALIZER = "initializer",
33
OBJECT_CONSTRUCTOR = Object.prototype.constructor,
36
DESTRUCTOR = "destructor",
38
AttributeCore = Y.AttributeCore,
40
_wlmix = function(r, s, wlhash) {
51
* The BaseCore class, is the lightest version of Base, and provides Base's
52
* basic lifecycle management and ATTRS construction support, but doesn't
53
* fire init/destroy or attribute change events.
55
* BaseCore also handles the chaining of initializer and destructor methods across
56
* the hierarchy as part of object construction and destruction. Additionally, attributes
57
* configured through the static <a href="#property_BaseCore.ATTRS">ATTRS</a>
58
* property for each class in the hierarchy will be initialized by BaseCore.
60
* Classes which require attribute support, but don't intend to use/expose attribute
61
* change events can extend BaseCore instead of Base for optimal kweight and
62
* runtime performance.
67
* @param {Object} cfg Object with configuration property name/value pairs.
68
* The object can be used to provide initial values for the objects published
71
function BaseCore(cfg) {
72
if (!this._BaseInvoked) {
73
this._BaseInvoked = true;
75
Y.log('constructor called', 'life', 'base');
78
else { Y.log('Based constructor called more than once. Ignoring duplicate calls', 'life', 'base'); }
82
* The list of properties which can be configured for each attribute
83
* (e.g. setter, getter, writeOnce, readOnly etc.)
90
BaseCore._ATTR_CFG = AttributeCore._ATTR_CFG.concat("cloneDefaultValue");
91
BaseCore._ATTR_CFG_HASH = Y.Array.hash(BaseCore._ATTR_CFG);
94
* The array of non-attribute configuration properties supported by this class.
96
* For example `BaseCore` defines a "plugins" configuration property which
97
* should not be set up as an attribute. This property is primarily required so
98
* that when <a href="#property__allowAdHocAttrs">`_allowAdHocAttrs`</a> is enabled by a class,
99
* non-attribute configuration properties don't get added as ad-hoc attributes.
101
* @property _NON_ATTRS_CFG
106
BaseCore._NON_ATTRS_CFG = ["plugins"];
109
* This property controls whether or not instances of this class should
110
* allow users to add ad-hoc attributes through the constructor configuration
113
* AdHoc attributes are attributes which are not defined by the class, and are
114
* not handled by the MyClass._NON_ATTRS_CFG
116
* @property _allowAdHocAttrs
118
* @default undefined (false)
123
* The string to be used to identify instances of this class.
125
* Classes extending BaseCore, should define their own
126
* static NAME property, which should be camelCase by
127
* convention (e.g. MyClass.NAME = "myClass";).
133
BaseCore.NAME = "baseCore";
136
* The default set of attributes which will be available for instances of this class, and
137
* their configuration. In addition to the configuration properties listed by
138
* AttributeCore's <a href="AttributeCore.html#method_addAttr">addAttr</a> method,
139
* the attribute can also be configured with a "cloneDefaultValue" property, which
140
* defines how the statically defined value field should be protected
141
* ("shallow", "deep" and false are supported values).
143
* By default if the value is an object literal or an array it will be "shallow"
144
* cloned, to protect the default value.
152
* Flag indicating whether or not this object
153
* has been through the init lifecycle phase.
155
* @attribute initialized
166
* Flag indicating whether or not this object
167
* has been through the destroy lifecycle phase.
169
* @attribute destroyed
180
BaseCore.prototype = {
183
* Internal construction logic for BaseCore.
186
* @param {Object} config The constructor configuration object
189
_initBase : function(config) {
193
this._initAttribute(config);
195
// If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
196
// initial state, but don't initialize Plugins yet. That's done after initialization.
197
var PluginHost = Y.Plugin && Y.Plugin.Host;
198
if (this._initPlugins && PluginHost) {
199
PluginHost.call(this);
202
if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
205
* The string used to identify the class of this object.
207
* @deprecated Use this.constructor.NAME
211
this.name = this.constructor.NAME;
213
this.init.apply(this, arguments);
217
* Initializes AttributeCore
219
* @method _initAttribute
222
_initAttribute: function() {
223
AttributeCore.apply(this);
227
* Init lifecycle method, invoked during construction. Sets up attributes
228
* and invokes initializers for the class hierarchy.
232
* @param {Object} cfg Object with configuration property name/value pairs
233
* @return {BaseCore} A reference to this object
235
init: function(cfg) {
236
Y.log('init called', 'life', 'base');
244
* Internal initialization implementation for BaseCore
249
_baseInit: function(cfg) {
250
this._initHierarchy(cfg);
252
if (this._initPlugins) {
253
// Need to initPlugins manually, to handle constructor parsing, static Plug parsing
254
this._initPlugins(cfg);
256
this._set(INITIALIZED, true);
260
* Destroy lifecycle method. Invokes destructors for the class hierarchy.
263
* @return {BaseCore} A reference to this object
266
destroy: function() {
272
* Internal destroy implementation for BaseCore
274
* @method _baseDestroy
277
_baseDestroy : function() {
278
if (this._destroyPlugins) {
279
this._destroyPlugins();
281
this._destroyHierarchy();
282
this._set(DESTROYED, true);
286
* Returns the class hierarchy for this object, with BaseCore being the last class in the array.
288
* @method _getClasses
290
* @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
291
* This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
294
_getClasses : function() {
295
if (!this._classes) {
296
this._initHierarchyData();
298
return this._classes;
302
* Returns an aggregated set of attribute configurations, by traversing
303
* the class hierarchy.
305
* @method _getAttrCfgs
307
* @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
308
* This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
311
_getAttrCfgs : function() {
313
this._initHierarchyData();
319
* A helper method used when processing ATTRS across the class hierarchy during
320
* initialization. Returns a disposable object with the attributes defined for
321
* the provided class, extracted from the set of all attributes passed in.
323
* @method _filterAttrCfs
326
* @param {Function} clazz The class for which the desired attributes are required.
327
* @param {Object} allCfgs The set of all attribute configurations for this instance.
328
* Attributes will be removed from this set, if they belong to the filtered class, so
329
* that by the time all classes are processed, allCfgs will be empty.
331
* @return {Object} The set of attributes belonging to the class passed in, in the form
332
* of an object with attribute name/configuration pairs.
334
_filterAttrCfgs : function(clazz, allCfgs) {
335
var cfgs = null, attr, attrs = clazz.ATTRS;
338
for (attr in attrs) {
341
cfgs[attr] = allCfgs[attr];
342
allCfgs[attr] = null;
351
* @method _filterAdHocAttrs
354
* @param {Object} allAttrs The set of all attribute configurations for this instance.
355
* Attributes will be removed from this set, if they belong to the filtered class, so
356
* that by the time all classes are processed, allCfgs will be empty.
357
* @param {Object} userVals The config object passed in by the user, from which adhoc attrs are to be filtered.
358
* @return {Object} The set of adhoc attributes passed in, in the form
359
* of an object with attribute name/configuration pairs.
361
_filterAdHocAttrs : function(allAttrs, userVals) {
363
nonAttrs = this._nonAttrs,
368
for (attr in userVals) {
369
if (!allAttrs[attr] && !nonAttrs[attr] && userVals.hasOwnProperty(attr)) {
381
* A helper method used by _getClasses and _getAttrCfgs, which determines both
382
* the array of classes and aggregate set of attribute configurations
383
* across the class hierarchy for the instance.
385
* @method _initHierarchyData
388
_initHierarchyData : function() {
389
var c = this.constructor,
393
nonAttrs = (this._allowAdHocAttrs) ? {} : null,
399
classes[classes.length] = c;
403
attrs[attrs.length] = c.ATTRS;
406
if (this._allowAdHocAttrs) {
407
nonAttrsCfg = c._NON_ATTRS_CFG;
409
for (i = 0, l = nonAttrsCfg.length; i < l; i++) {
410
nonAttrs[nonAttrsCfg[i]] = true;
415
c = c.superclass ? c.superclass.constructor : null;
418
this._classes = classes;
419
this._nonAttrs = nonAttrs;
420
this._attrs = this._aggregateAttrs(attrs);
424
* Utility method to define the attribute hash used to filter/whitelist property mixes for
427
* @method _attrCfgHash
430
_attrCfgHash: function() {
431
return BaseCore._ATTR_CFG_HASH;
435
* A helper method, used by _initHierarchyData to aggregate
436
* attribute configuration across the instances class hierarchy.
438
* The method will protect the attribute configuration value to protect the statically defined
439
* default value in ATTRS if required (if the value is an object literal, array or the
440
* attribute configuration has cloneDefaultValue set to shallow or deep).
442
* @method _aggregateAttrs
444
* @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
445
* (subclass first, Base last)
446
* @return {Object} The aggregate set of ATTRS definitions for the instance
448
_aggregateAttrs : function(allAttrs) {
456
cfgPropsHash = this._attrCfgHash(),
460
for (i = allAttrs.length-1; i >= 0; --i) {
463
for (attr in attrs) {
464
if (attrs.hasOwnProperty(attr)) {
466
// Protect config passed in
467
//cfg = Y.mix({}, attrs[attr], true, cfgProps);
468
//cfg = Y.Object(attrs[attr]);
469
cfg = _wlmix({}, attrs[attr], cfgPropsHash);
472
clone = cfg.cloneDefaultValue;
475
if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
476
Y.log('Cloning default value for attribute:' + attr, 'info', 'base');
477
cfg.value = Y.clone(val);
478
} else if (clone === SHALLOW) {
479
Y.log('Merging default value for attribute:' + attr, 'info', 'base');
480
cfg.value = Y.merge(val);
482
// else if (clone === false), don't clone the static default value.
483
// It's intended to be used by reference.
487
if (attr.indexOf(DOT) !== -1) {
488
path = attr.split(DOT);
492
if (path && aggAttrs[attr] && aggAttrs[attr].value) {
493
O.setValue(aggAttrs[attr].value, path, val);
495
if (!aggAttrs[attr]) {
496
aggAttrs[attr] = cfg;
498
_wlmix(aggAttrs[attr], cfg, cfgPropsHash);
510
* Initializes the class hierarchy for the instance, which includes
511
* initializing attributes for each class defined in the class's
512
* static <a href="#property_BaseCore.ATTRS">ATTRS</a> property and
513
* invoking the initializer method on the prototype of each class in the hierarchy.
515
* @method _initHierarchy
516
* @param {Object} userVals Object with configuration property name/value pairs
519
_initHierarchy : function(userVals) {
520
var lazy = this._lazyAddAttrs,
528
classes = this._getClasses(),
529
attrCfgs = this._getAttrCfgs(),
530
cl = classes.length - 1;
532
for (ci = cl; ci >= 0; ci--) {
534
constr = classes[ci];
535
constrProto = constr.prototype;
536
exts = constr._yuibuild && constr._yuibuild.exts;
539
for (ei = 0, el = exts.length; ei < el; ei++) {
540
exts[ei].apply(this, arguments);
544
this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
546
if (this._allowAdHocAttrs && ci === cl) {
547
this.addAttrs(this._filterAdHocAttrs(attrCfgs, userVals), userVals, lazy);
550
// Using INITIALIZER in hasOwnProperty check, for performance reasons (helps IE6 avoid GC thresholds when
551
// referencing string literals). Not using it in apply, again, for performance "." is faster.
552
if (constrProto.hasOwnProperty(INITIALIZER)) {
553
constrProto.initializer.apply(this, arguments);
557
for (ei = 0; ei < el; ei++) {
558
extProto = exts[ei].prototype;
559
if (extProto.hasOwnProperty(INITIALIZER)) {
560
extProto.initializer.apply(this, arguments);
568
* Destroys the class hierarchy for this instance by invoking
569
* the destructor method on the prototype of each class in the hierarchy.
571
* @method _destroyHierarchy
574
_destroyHierarchy : function() {
577
ci, cl, ei, el, exts, extProto,
578
classes = this._getClasses();
580
for (ci = 0, cl = classes.length; ci < cl; ci++) {
581
constr = classes[ci];
582
constrProto = constr.prototype;
583
exts = constr._yuibuild && constr._yuibuild.exts;
586
for (ei = 0, el = exts.length; ei < el; ei++) {
587
extProto = exts[ei].prototype;
588
if (extProto.hasOwnProperty(DESTRUCTOR)) {
589
extProto.destructor.apply(this, arguments);
594
if (constrProto.hasOwnProperty(DESTRUCTOR)) {
595
constrProto.destructor.apply(this, arguments);
601
* Default toString implementation. Provides the constructor NAME
602
* and the instance guid, if set.
605
* @return {String} String representation for this object
607
toString: function() {
608
return this.name + "[" + Y.stamp(this, true) + "]";
612
// Straightup augment, no wrapper functions
613
Y.mix(BaseCore, AttributeCore, false, null, 1);
616
BaseCore.prototype.constructor = BaseCore;
618
Y.BaseCore = BaseCore;
621
}, '3.5.1' ,{requires:['attribute-core']});