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) {
170 Y.log('init called', 'life', 'base');
172 this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
174 Y.BaseCore.prototype._initBase.call(this, cfg);
178 * Initializes Attribute
180 * @method _initAttribute
183 _initAttribute: function(cfg) {
184 Attribute.call(this);
185 this._yuievt.config.prefix = this._eventPrefix;
189 * Utility method to define the attribute hash used to filter/whitelist property mixes for
192 * @method _attrCfgHash
195 _attrCfgHash: function() {
196 return Base._ATTR_CFG_HASH;
200 * Init lifecycle method, invoked during construction.
201 * Fires the init event prior to setting up attributes and
202 * invoking initializers for the class hierarchy.
206 * @param {Object} config Object with configuration property name/value pairs
207 * @return {Base} A reference to this object
209 init: function(config) {
212 * Lifecycle event for the init phase, fired prior to initialization.
213 * Invoking the preventDefault() method on the event object provided
214 * to subscribers will prevent initialization from occuring.
217 * Subscribers to the "after" momemt of this event, will be notified
218 * after initialization of the object is complete (and therefore
219 * cannot prevent initialization).
223 * @preventable _defInitFn
224 * @param {EventFacade} e Event object, with a cfg property which
225 * refers to the configuration object passed to the constructor.
230 defaultTargetOnly:true,
231 defaultFn:this._defInitFn
234 this._preInitEventCfg(config);
236 this.fire(INIT, {cfg: config});
242 * Handles the special on, after and target properties which allow the user to
243 * easily configure on and after listeners as well as bubble targets during
244 * construction, prior to init.
247 * @method _preInitEventCfg
248 * @param {Object} config The user configuration object
250 _preInitEventCfg : function(config) {
256 this.after(config.after);
261 userTargets = (config && BUBBLETARGETS in config);
263 if (userTargets || _BUBBLETARGETS in this) {
264 target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
265 if (L.isArray(target)) {
266 for (i = 0, l = target.length; i < l; i++) {
267 this.addTarget(target[i]);
270 this.addTarget(target);
277 * Destroy lifecycle method. Fires the destroy
278 * event, prior to invoking destructors for the
282 * Subscribers to the destroy
283 * event can invoke preventDefault on the event object, to prevent destruction
287 * @return {Base} A reference to this object
290 destroy: function() {
291 Y.log('destroy called', 'life', 'base');
295 * Lifecycle event for the destroy phase,
296 * fired prior to destruction. Invoking the preventDefault
297 * method on the event object provided to subscribers will
298 * prevent destruction from proceeding.
301 * Subscribers to the "after" moment of this event, will be notified
302 * after destruction is complete (and as a result cannot prevent
306 * @preventable _defDestroyFn
307 * @param {EventFacade} e Event object
309 this.publish(DESTROY, {
312 defaultTargetOnly:true,
313 defaultFn: this._defDestroyFn
322 * Default init event handler
325 * @param {EventFacade} e Event object, with a cfg property which
326 * refers to the configuration object passed to the constructor.
329 _defInitFn : function(e) {
330 this._baseInit(e.cfg);
334 * Default destroy event handler
336 * @method _defDestroyFn
337 * @param {EventFacade} e Event object
340 _defDestroyFn : function(e) {
341 this._baseDestroy(e.cfg);
345 Y.mix(Base, Attribute, false, null, 1);
346 Y.mix(Base, BaseCore, false, null, 1);
349 Base.prototype.constructor = Base;
354 }, '3.5.1' ,{requires:['base-core', 'attribute-base']});