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;
80
* The list of properties which can be configured for each attribute
81
* (e.g. setter, getter, writeOnce, readOnly etc.)
88
BaseCore._ATTR_CFG = AttributeCore._ATTR_CFG.concat("cloneDefaultValue");
89
BaseCore._ATTR_CFG_HASH = Y.Array.hash(BaseCore._ATTR_CFG);
92
* The array of non-attribute configuration properties supported by this class.
94
* For example `BaseCore` defines a "plugins" configuration property which
95
* should not be set up as an attribute. This property is primarily required so
96
* that when <a href="#property__allowAdHocAttrs">`_allowAdHocAttrs`</a> is enabled by a class,
97
* non-attribute configuration properties don't get added as ad-hoc attributes.
99
* @property _NON_ATTRS_CFG
104
BaseCore._NON_ATTRS_CFG = ["plugins"];
107
* This property controls whether or not instances of this class should
108
* allow users to add ad-hoc attributes through the constructor configuration
111
* AdHoc attributes are attributes which are not defined by the class, and are
112
* not handled by the MyClass._NON_ATTRS_CFG
114
* @property _allowAdHocAttrs
116
* @default undefined (false)
121
* The string to be used to identify instances of this class.
123
* Classes extending BaseCore, should define their own
124
* static NAME property, which should be camelCase by
125
* convention (e.g. MyClass.NAME = "myClass";).
131
BaseCore.NAME = "baseCore";
134
* The default set of attributes which will be available for instances of this class, and
135
* their configuration. In addition to the configuration properties listed by
136
* AttributeCore's <a href="AttributeCore.html#method_addAttr">addAttr</a> method,
137
* the attribute can also be configured with a "cloneDefaultValue" property, which
138
* defines how the statically defined value field should be protected
139
* ("shallow", "deep" and false are supported values).
141
* By default if the value is an object literal or an array it will be "shallow"
142
* cloned, to protect the default value.
150
* Flag indicating whether or not this object
151
* has been through the init lifecycle phase.
153
* @attribute initialized
164
* Flag indicating whether or not this object
165
* has been through the destroy lifecycle phase.
167
* @attribute destroyed
178
BaseCore.prototype = {
181
* Internal construction logic for BaseCore.
184
* @param {Object} config The constructor configuration object
187
_initBase : function(config) {
191
this._initAttribute(config);
193
// If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
194
// initial state, but don't initialize Plugins yet. That's done after initialization.
195
var PluginHost = Y.Plugin && Y.Plugin.Host;
196
if (this._initPlugins && PluginHost) {
197
PluginHost.call(this);
200
if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
203
* The string used to identify the class of this object.
205
* @deprecated Use this.constructor.NAME
209
this.name = this.constructor.NAME;
211
this.init.apply(this, arguments);
215
* Initializes AttributeCore
217
* @method _initAttribute
220
_initAttribute: function() {
221
AttributeCore.apply(this);
225
* Init lifecycle method, invoked during construction. Sets up attributes
226
* and invokes initializers for the class hierarchy.
230
* @param {Object} cfg Object with configuration property name/value pairs
231
* @return {BaseCore} A reference to this object
233
init: function(cfg) {
241
* Internal initialization implementation for BaseCore
246
_baseInit: function(cfg) {
247
this._initHierarchy(cfg);
249
if (this._initPlugins) {
250
// Need to initPlugins manually, to handle constructor parsing, static Plug parsing
251
this._initPlugins(cfg);
253
this._set(INITIALIZED, true);
257
* Destroy lifecycle method. Invokes destructors for the class hierarchy.
260
* @return {BaseCore} A reference to this object
263
destroy: function() {
269
* Internal destroy implementation for BaseCore
271
* @method _baseDestroy
274
_baseDestroy : function() {
275
if (this._destroyPlugins) {
276
this._destroyPlugins();
278
this._destroyHierarchy();
279
this._set(DESTROYED, true);
283
* Returns the class hierarchy for this object, with BaseCore being the last class in the array.
285
* @method _getClasses
287
* @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
288
* This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the
291
_getClasses : function() {
292
if (!this._classes) {
293
this._initHierarchyData();
295
return this._classes;
299
* Returns an aggregated set of attribute configurations, by traversing
300
* the class hierarchy.
302
* @method _getAttrCfgs
304
* @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
305
* This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
308
_getAttrCfgs : function() {
310
this._initHierarchyData();
316
* A helper method used when processing ATTRS across the class hierarchy during
317
* initialization. Returns a disposable object with the attributes defined for
318
* the provided class, extracted from the set of all attributes passed in.
320
* @method _filterAttrCfs
323
* @param {Function} clazz The class for which the desired attributes are required.
324
* @param {Object} allCfgs The set of all attribute configurations for this instance.
325
* Attributes will be removed from this set, if they belong to the filtered class, so
326
* that by the time all classes are processed, allCfgs will be empty.
328
* @return {Object} The set of attributes belonging to the class passed in, in the form
329
* of an object with attribute name/configuration pairs.
331
_filterAttrCfgs : function(clazz, allCfgs) {
332
var cfgs = null, attr, attrs = clazz.ATTRS;
335
for (attr in attrs) {
338
cfgs[attr] = allCfgs[attr];
339
allCfgs[attr] = null;
348
* @method _filterAdHocAttrs
351
* @param {Object} allAttrs The set of all attribute configurations for this instance.
352
* Attributes will be removed from this set, if they belong to the filtered class, so
353
* that by the time all classes are processed, allCfgs will be empty.
354
* @param {Object} userVals The config object passed in by the user, from which adhoc attrs are to be filtered.
355
* @return {Object} The set of adhoc attributes passed in, in the form
356
* of an object with attribute name/configuration pairs.
358
_filterAdHocAttrs : function(allAttrs, userVals) {
360
nonAttrs = this._nonAttrs,
365
for (attr in userVals) {
366
if (!allAttrs[attr] && !nonAttrs[attr] && userVals.hasOwnProperty(attr)) {
378
* A helper method used by _getClasses and _getAttrCfgs, which determines both
379
* the array of classes and aggregate set of attribute configurations
380
* across the class hierarchy for the instance.
382
* @method _initHierarchyData
385
_initHierarchyData : function() {
386
var c = this.constructor,
390
nonAttrs = (this._allowAdHocAttrs) ? {} : null,
396
classes[classes.length] = c;
400
attrs[attrs.length] = c.ATTRS;
403
if (this._allowAdHocAttrs) {
404
nonAttrsCfg = c._NON_ATTRS_CFG;
406
for (i = 0, l = nonAttrsCfg.length; i < l; i++) {
407
nonAttrs[nonAttrsCfg[i]] = true;
412
c = c.superclass ? c.superclass.constructor : null;
415
this._classes = classes;
416
this._nonAttrs = nonAttrs;
417
this._attrs = this._aggregateAttrs(attrs);
421
* Utility method to define the attribute hash used to filter/whitelist property mixes for
424
* @method _attrCfgHash
427
_attrCfgHash: function() {
428
return BaseCore._ATTR_CFG_HASH;
432
* A helper method, used by _initHierarchyData to aggregate
433
* attribute configuration across the instances class hierarchy.
435
* The method will protect the attribute configuration value to protect the statically defined
436
* default value in ATTRS if required (if the value is an object literal, array or the
437
* attribute configuration has cloneDefaultValue set to shallow or deep).
439
* @method _aggregateAttrs
441
* @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy
442
* (subclass first, Base last)
443
* @return {Object} The aggregate set of ATTRS definitions for the instance
445
_aggregateAttrs : function(allAttrs) {
453
cfgPropsHash = this._attrCfgHash(),
457
for (i = allAttrs.length-1; i >= 0; --i) {
460
for (attr in attrs) {
461
if (attrs.hasOwnProperty(attr)) {
463
// Protect config passed in
464
//cfg = Y.mix({}, attrs[attr], true, cfgProps);
465
//cfg = Y.Object(attrs[attr]);
466
cfg = _wlmix({}, attrs[attr], cfgPropsHash);
469
clone = cfg.cloneDefaultValue;
472
if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
473
cfg.value = Y.clone(val);
474
} else if (clone === SHALLOW) {
475
cfg.value = Y.merge(val);
477
// else if (clone === false), don't clone the static default value.
478
// It's intended to be used by reference.
482
if (attr.indexOf(DOT) !== -1) {
483
path = attr.split(DOT);
487
if (path && aggAttrs[attr] && aggAttrs[attr].value) {
488
O.setValue(aggAttrs[attr].value, path, val);
490
if (!aggAttrs[attr]) {
491
aggAttrs[attr] = cfg;
493
_wlmix(aggAttrs[attr], cfg, cfgPropsHash);
505
* Initializes the class hierarchy for the instance, which includes
506
* initializing attributes for each class defined in the class's
507
* static <a href="#property_BaseCore.ATTRS">ATTRS</a> property and
508
* invoking the initializer method on the prototype of each class in the hierarchy.
510
* @method _initHierarchy
511
* @param {Object} userVals Object with configuration property name/value pairs
514
_initHierarchy : function(userVals) {
515
var lazy = this._lazyAddAttrs,
523
classes = this._getClasses(),
524
attrCfgs = this._getAttrCfgs(),
525
cl = classes.length - 1;
527
for (ci = cl; ci >= 0; ci--) {
529
constr = classes[ci];
530
constrProto = constr.prototype;
531
exts = constr._yuibuild && constr._yuibuild.exts;
534
for (ei = 0, el = exts.length; ei < el; ei++) {
535
exts[ei].apply(this, arguments);
539
this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
541
if (this._allowAdHocAttrs && ci === cl) {
542
this.addAttrs(this._filterAdHocAttrs(attrCfgs, userVals), userVals, lazy);
545
// Using INITIALIZER in hasOwnProperty check, for performance reasons (helps IE6 avoid GC thresholds when
546
// referencing string literals). Not using it in apply, again, for performance "." is faster.
547
if (constrProto.hasOwnProperty(INITIALIZER)) {
548
constrProto.initializer.apply(this, arguments);
552
for (ei = 0; ei < el; ei++) {
553
extProto = exts[ei].prototype;
554
if (extProto.hasOwnProperty(INITIALIZER)) {
555
extProto.initializer.apply(this, arguments);
563
* Destroys the class hierarchy for this instance by invoking
564
* the destructor method on the prototype of each class in the hierarchy.
566
* @method _destroyHierarchy
569
_destroyHierarchy : function() {
572
ci, cl, ei, el, exts, extProto,
573
classes = this._getClasses();
575
for (ci = 0, cl = classes.length; ci < cl; ci++) {
576
constr = classes[ci];
577
constrProto = constr.prototype;
578
exts = constr._yuibuild && constr._yuibuild.exts;
581
for (ei = 0, el = exts.length; ei < el; ei++) {
582
extProto = exts[ei].prototype;
583
if (extProto.hasOwnProperty(DESTRUCTOR)) {
584
extProto.destructor.apply(this, arguments);
589
if (constrProto.hasOwnProperty(DESTRUCTOR)) {
590
constrProto.destructor.apply(this, arguments);
596
* Default toString implementation. Provides the constructor NAME
597
* and the instance guid, if set.
600
* @return {String} String representation for this object
602
toString: function() {
603
return this.name + "[" + Y.stamp(this, true) + "]";
607
// Straightup augment, no wrapper functions
608
Y.mix(BaseCore, AttributeCore, false, null, 1);
611
BaseCore.prototype.constructor = BaseCore;
613
Y.BaseCore = BaseCore;
616
}, '3.5.1' ,{requires:['attribute-core']});