3
Copyright 2012 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 BaseCore.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 BaseCore.build.
22
* @submodule base-base
26
* The base module provides the Base class, which objects requiring attribute and custom event support can extend.
27
* The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides
28
* plugin support and also provides the Base.build method which provides a way to build custom classes using extensions.
34
* The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
35
* and without the extension support provided by Base.build.
38
* @submodule base-base
45
BUBBLETARGETS = "bubbleTargets",
46
_BUBBLETARGETS = "_bubbleTargets",
48
BaseCore = Y.BaseCore,
49
AttributeCore = Y.AttributeCore,
50
Attribute = Y.Attribute;
54
* A base class which objects requiring attributes and custom event support can
55
* extend. Base also handles the chaining of initializer and destructor methods across
56
* the hierarchy as part of object construction and destruction. Additionally, attributes configured
57
* through the static <a href="#property_Base.ATTRS">ATTRS</a> property for each class
58
* in the hierarchy will be initialized by Base.
62
* The static <a href="#property_Base.NAME">NAME</a> property of each class extending
63
* from Base will be used as the identifier for the class, and is used by Base to prefix
64
* all events fired by instances of that class.
72
* @uses AttributeEvents
73
* @uses AttributeExtras
76
* @param {Object} config Object with configuration property name/value pairs. The object can be
77
* used to provide default values for the objects published attributes.
80
* The config object can also contain the following non-attribute properties, providing a convenient
81
* way to configure events listeners and plugins for the instance, as part of the constructor call:
86
* <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>
88
* <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>
89
* <dt>bubbleTargets</dt>
90
* <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>
92
* <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>
96
BaseCore.apply(this, arguments);
100
* The list of properties which can be configured for
101
* each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
103
* @property _ATTR_CFG
108
Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
109
Base._ATTR_CFG_HASH = Y.Array.hash(Base._ATTR_CFG);
112
* The array of non-attribute configuration properties supported by this class.
114
* `Base` supports "on", "after", "plugins" and "bubbleTargets" properties,
115
* which are not set up as attributes.
117
* This property is primarily required so that when
118
* <a href="#property__allowAdHocAttrs">`_allowAdHocAttrs`</a> is enabled by
119
* a class, non-attribute configurations don't get added as ad-hoc attributes.
121
* @property _NON_ATTRS_CFG
126
Base._NON_ATTRS_CFG = BaseCore._NON_ATTRS_CFG.concat(["on", "after", "bubbleTargets"]);
130
* The string to be used to identify instances of
131
* this class, for example in prefixing events.
134
* Classes extending Base, should define their own
135
* static NAME property, which should be camelCase by
136
* convention (e.g. MyClass.NAME = "myClass";).
145
* The default set of attributes which will be available for instances of this class, and
146
* their configuration. In addition to the configuration properties listed by
147
* Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
148
* can also be configured with a "cloneDefaultValue" property, which defines how the statically
149
* defined value field should be protected ("shallow", "deep" and false are supported values).
151
* By default if the value is an object literal or an array it will be "shallow" cloned, to
152
* protect the default value.
158
Base.ATTRS = AttributeCore.prototype._protectAttrs(BaseCore.ATTRS);
163
* Internal construction logic for Base.
166
* @param {Object} config The constructor configuration object
169
_initBase: function(cfg) {
171
this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
173
Y.BaseCore.prototype._initBase.call(this, cfg);
177
* Initializes Attribute
179
* @method _initAttribute
182
_initAttribute: function(cfg) {
183
Attribute.call(this);
184
this._yuievt.config.prefix = this._eventPrefix;
188
* Utility method to define the attribute hash used to filter/whitelist property mixes for
191
* @method _attrCfgHash
194
_attrCfgHash: function() {
195
return Base._ATTR_CFG_HASH;
199
* Init lifecycle method, invoked during construction.
200
* Fires the init event prior to setting up attributes and
201
* invoking initializers for the class hierarchy.
205
* @param {Object} config Object with configuration property name/value pairs
206
* @return {Base} A reference to this object
208
init: function(config) {
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() {
293
* Lifecycle event for the destroy phase,
294
* fired prior to destruction. Invoking the preventDefault
295
* method on the event object provided to subscribers will
296
* prevent destruction from proceeding.
299
* Subscribers to the "after" moment of this event, will be notified
300
* after destruction is complete (and as a result cannot prevent
304
* @preventable _defDestroyFn
305
* @param {EventFacade} e Event object
307
this.publish(DESTROY, {
310
defaultTargetOnly:true,
311
defaultFn: this._defDestroyFn
320
* Default init event handler
323
* @param {EventFacade} e Event object, with a cfg property which
324
* refers to the configuration object passed to the constructor.
327
_defInitFn : function(e) {
328
this._baseInit(e.cfg);
332
* Default destroy event handler
334
* @method _defDestroyFn
335
* @param {EventFacade} e Event object
338
_defDestroyFn : function(e) {
339
this._baseDestroy(e.cfg);
343
Y.mix(Base, Attribute, false, null, 1);
344
Y.mix(Base, BaseCore, false, null, 1);
347
Base.prototype.constructor = Base;
352
}, '3.5.0' ,{requires:['base-core', 'attribute-base']});