~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/base/base-base-debug.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
7
 
*/
8
 
YUI.add('base-base', function(Y) {
9
 
 
10
 
    /**
11
 
     * The base module provides the Base class, which objects requiring attribute and custom event support can extend. 
12
 
     * The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides 
13
 
     * plugin support and also provides the Base.build method which provides a way to build custom classes using extensions.
14
 
     *
15
 
     * @module base
16
 
     */
17
 
 
18
 
    /**
19
 
     * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host, 
20
 
     * and without the extension support provided by Base.build.
21
 
     *
22
 
     * @module base
23
 
     * @submodule base-base
24
 
     */
25
 
    var O = Y.Object,
26
 
        L = Y.Lang,
27
 
        DOT = ".",
28
 
        DESTROY = "destroy",
29
 
        INIT = "init",
30
 
        INITIALIZED = "initialized",
31
 
        DESTROYED = "destroyed",
32
 
        INITIALIZER = "initializer",
33
 
        BUBBLETARGETS = "bubbleTargets",
34
 
        _BUBBLETARGETS = "_bubbleTargets",
35
 
        OBJECT_CONSTRUCTOR = Object.prototype.constructor,
36
 
        DEEP = "deep",
37
 
        SHALLOW = "shallow",
38
 
        DESTRUCTOR = "destructor",
39
 
 
40
 
        Attribute = Y.Attribute;
41
 
 
42
 
    /**
43
 
     * <p>
44
 
     * A base class which objects requiring attributes and custom event support can 
45
 
     * extend. Base also handles the chaining of initializer and destructor methods across 
46
 
     * the hierarchy as part of object construction and destruction. Additionally, attributes configured 
47
 
     * through the static <a href="#property_Base.ATTRS">ATTRS</a> property for each class 
48
 
     * in the hierarchy will be initialized by Base.
49
 
     * </p>
50
 
     *
51
 
     * <p>
52
 
     * The static <a href="#property_Base.NAME">NAME</a> property of each class extending 
53
 
     * from Base will be used as the identifier for the class, and is used by Base to prefix 
54
 
     * all events fired by instances of that class.
55
 
     * </p>
56
 
     *
57
 
     * @class Base
58
 
     * @constructor
59
 
     * @uses Attribute
60
 
     * @uses Plugin.Host
61
 
     *
62
 
     * @param {Object} config Object with configuration property name/value pairs. The object can be 
63
 
     * used to provide default values for the objects published attributes.
64
 
     *
65
 
     * <p>
66
 
     * The config object can also contain the following non-attribute properties, providing a convenient 
67
 
     * way to configure events listeners and plugins for the instance, as part of the constructor call:
68
 
     * </p>
69
 
     *
70
 
     * <dl>
71
 
     *     <dt>on</dt>
72
 
     *     <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>
73
 
     *     <dt>after</dt>
74
 
     *     <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>
75
 
     *     <dt>bubbleTargets</dt>
76
 
     *     <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>
77
 
     *     <dt>plugins</dt>
78
 
     *     <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>
79
 
     * </dl>
80
 
     */
81
 
    function Base() {
82
 
        Y.log('constructor called', 'life', 'base');
83
 
 
84
 
        Attribute.call(this);
85
 
 
86
 
        // If Plugin.Host has been augmented [ through base-pluginhost ], setup it's
87
 
        // initial state, but don't initialize Plugins yet. That's done after initialization.
88
 
        var PluginHost = Y.Plugin && Y.Plugin.Host;  
89
 
        if (this._initPlugins && PluginHost) {
90
 
            PluginHost.call(this);
91
 
        }
92
 
 
93
 
        if (this._lazyAddAttrs !== false) { this._lazyAddAttrs = true; }
94
 
 
95
 
        /**
96
 
         * The string used to identify the class of this object.
97
 
         *
98
 
         * @deprecated Use this.constructor.NAME
99
 
         * @property name
100
 
         * @type String
101
 
         */
102
 
        this.name = this.constructor.NAME;
103
 
        this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
104
 
 
105
 
        this.init.apply(this, arguments);
106
 
    }
107
 
 
108
 
    /**
109
 
     * The list of properties which can be configured for 
110
 
     * each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
111
 
     *
112
 
     * @property Base._ATTR_CFG
113
 
     * @type Array
114
 
     * @static
115
 
     * @private
116
 
     */
117
 
    Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
118
 
 
119
 
    /**
120
 
     * <p>
121
 
     * The string to be used to identify instances of 
122
 
     * this class, for example in prefixing events.
123
 
     * </p>
124
 
     * <p>
125
 
     * Classes extending Base, should define their own
126
 
     * static NAME property, which should be camelCase by
127
 
     * convention (e.g. MyClass.NAME = "myClass";).
128
 
     * </p>
129
 
     * @property Base.NAME
130
 
     * @type String
131
 
     * @static
132
 
     */
133
 
    Base.NAME = "base";
134
 
 
135
 
    /**
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
 
     * Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute 
139
 
     * can also be configured with a "cloneDefaultValue" property, which defines how the statically
140
 
     * defined value field should be protected ("shallow", "deep" and false are supported values). 
141
 
     *
142
 
     * By default if the value is an object literal or an array it will be "shallow" cloned, to 
143
 
     * protect the default value.
144
 
     *
145
 
     * @property Base.ATTRS
146
 
     * @type Object
147
 
     * @static
148
 
     */
149
 
    Base.ATTRS = {
150
 
        /**
151
 
         * Flag indicating whether or not this object
152
 
         * has been through the init lifecycle phase.
153
 
         *
154
 
         * @attribute initialized
155
 
         * @readonly
156
 
         * @default false
157
 
         * @type boolean
158
 
         */
159
 
        initialized: {
160
 
            readOnly:true,
161
 
            value:false
162
 
        },
163
 
 
164
 
        /**
165
 
         * Flag indicating whether or not this object
166
 
         * has been through the destroy lifecycle phase.
167
 
         *
168
 
         * @attribute destroyed
169
 
         * @readonly
170
 
         * @default false
171
 
         * @type boolean
172
 
         */
173
 
        destroyed: {
174
 
            readOnly:true,
175
 
            value:false
176
 
        }
177
 
    };
178
 
 
179
 
    Base.prototype = {
180
 
 
181
 
        /**
182
 
         * Init lifecycle method, invoked during construction.
183
 
         * Fires the init event prior to setting up attributes and 
184
 
         * invoking initializers for the class hierarchy.
185
 
         *
186
 
         * @method init
187
 
         * @final
188
 
         * @chainable
189
 
         * @param {Object} config Object with configuration property name/value pairs
190
 
         * @return {Base} A reference to this object
191
 
         */
192
 
        init: function(config) {
193
 
            Y.log('init called', 'life', 'base');
194
 
 
195
 
            this._yuievt.config.prefix = this._eventPrefix;
196
 
 
197
 
            /**
198
 
             * <p>
199
 
             * Lifecycle event for the init phase, fired prior to initialization. 
200
 
             * Invoking the preventDefault() method on the event object provided 
201
 
             * to subscribers will prevent initialization from occuring.
202
 
             * </p>
203
 
             * <p>
204
 
             * Subscribers to the "after" momemt of this event, will be notified
205
 
             * after initialization of the object is complete (and therefore
206
 
             * cannot prevent initialization).
207
 
             * </p>
208
 
             *
209
 
             * @event init
210
 
             * @preventable _defInitFn
211
 
             * @param {EventFacade} e Event object, with a cfg property which 
212
 
             * refers to the configuration object passed to the constructor.
213
 
             */
214
 
            this.publish(INIT, {
215
 
                queuable:false,
216
 
                fireOnce:true,
217
 
                defaultTargetOnly:true,
218
 
                defaultFn:this._defInitFn
219
 
            });
220
 
 
221
 
            this._preInitEventCfg(config);
222
 
 
223
 
            this.fire(INIT, {cfg: config});
224
 
 
225
 
            return this;
226
 
        },
227
 
 
228
 
        /**
229
 
         * Handles the special on, after and target properties which allow the user to
230
 
         * easily configure on and after listeners as well as bubble targets during 
231
 
         * construction, prior to init.
232
 
         *
233
 
         * @private
234
 
         * @method _preInitEventCfg
235
 
         * @param {Object} config The user configuration object
236
 
         */
237
 
        _preInitEventCfg : function(config) {
238
 
            if (config) {
239
 
                if (config.on) {
240
 
                    this.on(config.on);
241
 
                }
242
 
                if (config.after) {
243
 
                    this.after(config.after);
244
 
                }
245
 
            }
246
 
 
247
 
            var i, l, target,
248
 
                userTargets = (config && BUBBLETARGETS in config);
249
 
 
250
 
            if (userTargets || _BUBBLETARGETS in this) {
251
 
                target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
252
 
                if (L.isArray(target)) {
253
 
                    for (i = 0, l = target.length; i < l; i++) { 
254
 
                        this.addTarget(target[i]);
255
 
                    }
256
 
                } else if (target) {
257
 
                    this.addTarget(target);
258
 
                }
259
 
            }
260
 
        },
261
 
 
262
 
        /**
263
 
         * <p>
264
 
         * Destroy lifecycle method. Fires the destroy
265
 
         * event, prior to invoking destructors for the
266
 
         * class hierarchy.
267
 
         * </p>
268
 
         * <p>
269
 
         * Subscribers to the destroy
270
 
         * event can invoke preventDefault on the event object, to prevent destruction
271
 
         * from proceeding.
272
 
         * </p>
273
 
         * @method destroy
274
 
         * @return {Base} A reference to this object
275
 
         * @final
276
 
         * @chainable
277
 
         */
278
 
        destroy: function() {
279
 
            Y.log('destroy called', 'life', 'base');
280
 
 
281
 
            /**
282
 
             * <p>
283
 
             * Lifecycle event for the destroy phase, 
284
 
             * fired prior to destruction. Invoking the preventDefault 
285
 
             * method on the event object provided to subscribers will 
286
 
             * prevent destruction from proceeding.
287
 
             * </p>
288
 
             * <p>
289
 
             * Subscribers to the "after" moment of this event, will be notified
290
 
             * after destruction is complete (and as a result cannot prevent
291
 
             * destruction).
292
 
             * </p>
293
 
             * @event destroy
294
 
             * @preventable _defDestroyFn
295
 
             * @param {EventFacade} e Event object
296
 
             */
297
 
            this.publish(DESTROY, {
298
 
                queuable:false,
299
 
                fireOnce:true,
300
 
                defaultTargetOnly:true,
301
 
                defaultFn: this._defDestroyFn
302
 
            });
303
 
            this.fire(DESTROY);
304
 
 
305
 
            this.detachAll();
306
 
            return this;
307
 
        },
308
 
 
309
 
        /**
310
 
         * Default init event handler
311
 
         *
312
 
         * @method _defInitFn
313
 
         * @param {EventFacade} e Event object, with a cfg property which 
314
 
         * refers to the configuration object passed to the constructor.
315
 
         * @protected
316
 
         */
317
 
        _defInitFn : function(e) {
318
 
            this._initHierarchy(e.cfg);
319
 
            if (this._initPlugins) {
320
 
                // Need to initPlugins manually, to handle constructor parsing, static Plug parsing
321
 
                this._initPlugins(e.cfg);
322
 
            }
323
 
            this._set(INITIALIZED, true);
324
 
        },
325
 
 
326
 
        /**
327
 
         * Default destroy event handler
328
 
         *
329
 
         * @method _defDestroyFn
330
 
         * @param {EventFacade} e Event object
331
 
         * @protected
332
 
         */
333
 
        _defDestroyFn : function(e) {
334
 
            this._destroyHierarchy();
335
 
            if (this._destroyPlugins) {
336
 
                this._destroyPlugins();
337
 
            }
338
 
            this._set(DESTROYED, true);
339
 
        },
340
 
 
341
 
        /**
342
 
         * Returns the class hierarchy for this object, with Base being the last class in the array.
343
 
         *
344
 
         * @method _getClasses
345
 
         * @protected
346
 
         * @return {Function[]} An array of classes (constructor functions), making up the class hierarchy for this object.
347
 
         * This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the 
348
 
         * cached value.
349
 
         */
350
 
        _getClasses : function() {
351
 
            if (!this._classes) {
352
 
                this._initHierarchyData();
353
 
            }
354
 
            return this._classes;
355
 
        },
356
 
 
357
 
        /**
358
 
         * Returns an aggregated set of attribute configurations, by traversing the class hierarchy.
359
 
         *
360
 
         * @method _getAttrCfgs
361
 
         * @protected
362
 
         * @return {Object} The hash of attribute configurations, aggregated across classes in the hierarchy
363
 
         * This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return
364
 
         * the cached value.
365
 
         */
366
 
        _getAttrCfgs : function() {
367
 
            if (!this._attrs) {
368
 
                this._initHierarchyData();
369
 
            }
370
 
            return this._attrs;
371
 
        },
372
 
 
373
 
        /**
374
 
         * A helper method used when processing ATTRS across the class hierarchy during 
375
 
         * initialization. Returns a disposable object with the attributes defined for 
376
 
         * the provided class, extracted from the set of all attributes passed in .
377
 
         *
378
 
         * @method _filterAttrCfs
379
 
         * @private
380
 
         *
381
 
         * @param {Function} clazz The class for which the desired attributes are required.
382
 
         * @param {Object} allCfgs The set of all attribute configurations for this instance. 
383
 
         * Attributes will be removed from this set, if they belong to the filtered class, so
384
 
         * that by the time all classes are processed, allCfgs will be empty.
385
 
         * 
386
 
         * @return {Object} The set of attributes belonging to the class passed in, in the form
387
 
         * of an object with attribute name/configuration pairs.
388
 
         */
389
 
        _filterAttrCfgs : function(clazz, allCfgs) {
390
 
            var cfgs = null, attr, attrs = clazz.ATTRS;
391
 
 
392
 
            if (attrs) {
393
 
                for (attr in attrs) {
394
 
                    if (attrs.hasOwnProperty(attr) && allCfgs[attr]) {
395
 
                        cfgs = cfgs || {};
396
 
                        cfgs[attr] = allCfgs[attr];
397
 
                        delete allCfgs[attr];
398
 
                    }
399
 
                }
400
 
            }
401
 
 
402
 
            return cfgs;
403
 
        },
404
 
 
405
 
        /**
406
 
         * A helper method used by _getClasses and _getAttrCfgs, which determines both
407
 
         * the array of classes and aggregate set of attribute configurations
408
 
         * across the class hierarchy for the instance.
409
 
         * 
410
 
         * @method _initHierarchyData
411
 
         * @private
412
 
         */
413
 
        _initHierarchyData : function() {
414
 
            var c = this.constructor, 
415
 
                classes = [],
416
 
                attrs = [];
417
 
 
418
 
            while (c) {
419
 
                // Add to classes
420
 
                classes[classes.length] = c;
421
 
 
422
 
                // Add to attributes
423
 
                if (c.ATTRS) {
424
 
                    attrs[attrs.length] = c.ATTRS;
425
 
                }
426
 
                c = c.superclass ? c.superclass.constructor : null;
427
 
            }
428
 
 
429
 
            this._classes = classes;
430
 
            this._attrs = this._aggregateAttrs(attrs);
431
 
        },
432
 
 
433
 
        /**
434
 
         * A helper method, used by _initHierarchyData to aggregate 
435
 
         * attribute configuration across the instances class hierarchy.
436
 
         *
437
 
         * The method will potect the attribute configuration value to protect the statically defined 
438
 
         * default value in ATTRS if required (if the value is an object literal, array or the 
439
 
         * attribute configuration has cloneDefaultValue set to shallow or deep).
440
 
         *
441
 
         * @method _aggregateAttrs
442
 
         * @private
443
 
         * @param {Array} allAttrs An array of ATTRS definitions across classes in the hierarchy 
444
 
         * (subclass first, Base last)
445
 
         * @return {Object} The aggregate set of ATTRS definitions for the instance
446
 
         */
447
 
        _aggregateAttrs : function(allAttrs) {
448
 
            var attr,
449
 
                attrs,
450
 
                cfg,
451
 
                val,
452
 
                path,
453
 
                i, 
454
 
                clone, 
455
 
                cfgProps = Base._ATTR_CFG,
456
 
                aggAttrs = {};
457
 
 
458
 
            if (allAttrs) {
459
 
                for (i = allAttrs.length-1; i >= 0; --i) {
460
 
                    attrs = allAttrs[i];
461
 
 
462
 
                    for (attr in attrs) {
463
 
                        if (attrs.hasOwnProperty(attr)) {
464
 
 
465
 
                            // Protect config passed in
466
 
                            cfg = Y.mix({}, attrs[attr], true, cfgProps);
467
 
 
468
 
                            val = cfg.value;
469
 
                            clone = cfg.cloneDefaultValue;
470
 
 
471
 
                            if (val) {
472
 
                                if ( (clone === undefined && (OBJECT_CONSTRUCTOR === val.constructor || L.isArray(val))) || clone === DEEP || clone === true) {
473
 
                                    Y.log('Cloning default value for attribute:' + attr, 'info', 'base');
474
 
                                    cfg.value = Y.clone(val);
475
 
                                } else if (clone === SHALLOW) {
476
 
                                    Y.log('Merging default value for attribute:' + attr, 'info', 'base');
477
 
                                    cfg.value = Y.merge(val);
478
 
                                }
479
 
                                // else if (clone === false), don't clone the static default value. 
480
 
                                // It's intended to be used by reference.
481
 
                            }
482
 
 
483
 
                            path = null;
484
 
                            if (attr.indexOf(DOT) !== -1) {
485
 
                                path = attr.split(DOT);
486
 
                                attr = path.shift();
487
 
                            }
488
 
 
489
 
                            if (path && aggAttrs[attr] && aggAttrs[attr].value) {
490
 
                                O.setValue(aggAttrs[attr].value, path, val);
491
 
                            } else if (!path){
492
 
                                if (!aggAttrs[attr]) {
493
 
                                    aggAttrs[attr] = cfg;
494
 
                                } else {
495
 
                                    Y.mix(aggAttrs[attr], cfg, true, cfgProps);
496
 
                                }
497
 
                            }
498
 
                        }
499
 
                    }
500
 
                }
501
 
            }
502
 
 
503
 
            return aggAttrs;
504
 
        },
505
 
 
506
 
        /**
507
 
         * Initializes the class hierarchy for the instance, which includes 
508
 
         * initializing attributes for each class defined in the class's 
509
 
         * static <a href="#property_Base.ATTRS">ATTRS</a> property and 
510
 
         * invoking the initializer method on the prototype of each class in the hierarchy.
511
 
         *
512
 
         * @method _initHierarchy
513
 
         * @param {Object} userVals Object with configuration property name/value pairs
514
 
         * @private
515
 
         */
516
 
        _initHierarchy : function(userVals) {
517
 
            var lazy = this._lazyAddAttrs,
518
 
                constr,
519
 
                constrProto,
520
 
                ci,
521
 
                ei,
522
 
                el,
523
 
                classes = this._getClasses(),
524
 
                attrCfgs = this._getAttrCfgs();
525
 
 
526
 
            for (ci = classes.length-1; ci >= 0; ci--) {
527
 
 
528
 
                constr = classes[ci];
529
 
                constrProto = constr.prototype;
530
 
 
531
 
                if (constr._yuibuild && constr._yuibuild.exts) {
532
 
                    for (ei = 0, el = constr._yuibuild.exts.length; ei < el; ei++) {
533
 
                        constr._yuibuild.exts[ei].apply(this, arguments);
534
 
                    }
535
 
                }
536
 
 
537
 
                this.addAttrs(this._filterAttrCfgs(constr, attrCfgs), userVals, lazy);
538
 
 
539
 
                // Using INITIALIZER in hasOwnProperty check, for performance reasons (helps IE6 avoid GC thresholds when
540
 
                // referencing string literals). Not using it in apply, again, for performance "." is faster. 
541
 
                if (constrProto.hasOwnProperty(INITIALIZER)) {
542
 
                    constrProto.initializer.apply(this, arguments);
543
 
                }
544
 
            }
545
 
        },
546
 
 
547
 
        /**
548
 
         * Destroys the class hierarchy for this instance by invoking
549
 
         * the descructor method on the prototype of each class in the hierarchy.
550
 
         *
551
 
         * @method _destroyHierarchy
552
 
         * @private
553
 
         */
554
 
        _destroyHierarchy : function() {
555
 
            var constr,
556
 
                constrProto,
557
 
                ci, cl,
558
 
                classes = this._getClasses();
559
 
 
560
 
            for (ci = 0, cl = classes.length; ci < cl; ci++) {
561
 
                constr = classes[ci];
562
 
                constrProto = constr.prototype;
563
 
                if (constrProto.hasOwnProperty(DESTRUCTOR)) {
564
 
                    constrProto.destructor.apply(this, arguments);
565
 
                }
566
 
            }
567
 
        },
568
 
 
569
 
        /**
570
 
         * Default toString implementation. Provides the constructor NAME
571
 
         * and the instance ID.
572
 
         *
573
 
         * @method toString
574
 
         * @return {String} String representation for this object
575
 
         */
576
 
        toString: function() {
577
 
            return this.constructor.NAME + "[" + Y.stamp(this) + "]";
578
 
        }
579
 
 
580
 
    };
581
 
 
582
 
    // Straightup augment, no wrapper functions
583
 
    Y.mix(Base, Attribute, false, null, 1);
584
 
 
585
 
    // Fix constructor
586
 
    Base.prototype.constructor = Base;
587
 
 
588
 
    Y.Base = Base;
589
 
 
590
 
 
591
 
}, '3.2.0' ,{requires:['attribute-base']});