~ubuntu-branches/ubuntu/precise/whoopsie-daisy/precise-updates

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/build/base-build/base-build.js

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.0 (build 5089)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('base-build', function(Y) {
 
8
 
 
9
    /**
 
10
     * The base-build submodule provides Base.build functionality, which
 
11
     * can be used to create custom classes, by aggregating extensions onto 
 
12
     * a main class.
 
13
     *
 
14
     * @module base
 
15
     * @submodule base-build
 
16
     * @for Base
 
17
     */
 
18
    var Base = Y.Base,
 
19
        L = Y.Lang,
 
20
        INITIALIZER = "initializer",
 
21
        DESTRUCTOR = "destructor",
 
22
        build,
 
23
        arrayAggregator = function (prop, r, s) {
 
24
            if (s[prop]) {
 
25
                r[prop] = (r[prop] || []).concat(s[prop]);
 
26
            }    
 
27
        };
 
28
 
 
29
    Base._build = function(name, main, extensions, px, sx, cfg) {
 
30
 
 
31
        var build = Base._build,
 
32
 
 
33
            builtClass = build._ctor(main, cfg),
 
34
            buildCfg = build._cfg(main, cfg, extensions),
 
35
 
 
36
            _mixCust = build._mixCust,
 
37
 
 
38
            dynamic = builtClass._yuibuild.dynamic,
 
39
 
 
40
            i, l, extClass, extProto,
 
41
            initializer,
 
42
            destructor;
 
43
 
 
44
        // Augment/Aggregate
 
45
        for (i = 0, l = extensions.length; i < l; i++) {
 
46
            extClass = extensions[i];
 
47
 
 
48
            extProto = extClass.prototype;
 
49
            
 
50
            initializer = extProto[INITIALIZER];
 
51
            destructor = extProto[DESTRUCTOR];
 
52
            delete extProto[INITIALIZER];
 
53
            delete extProto[DESTRUCTOR];
 
54
 
 
55
            // Prototype, old non-displacing augment
 
56
            Y.mix(builtClass, extClass, true, null, 1);
 
57
 
 
58
            // Custom Statics
 
59
            _mixCust(builtClass, extClass, buildCfg);
 
60
 
 
61
            if (initializer) { 
 
62
                extProto[INITIALIZER] = initializer;
 
63
            }
 
64
 
 
65
            if (destructor) {
 
66
                extProto[DESTRUCTOR] = destructor;
 
67
            }
 
68
 
 
69
            builtClass._yuibuild.exts.push(extClass);
 
70
        }
 
71
 
 
72
        if (px) {
 
73
            Y.mix(builtClass.prototype, px, true);
 
74
        }
 
75
 
 
76
        if (sx) {
 
77
            Y.mix(builtClass, build._clean(sx, buildCfg), true);
 
78
            _mixCust(builtClass, sx, buildCfg);
 
79
        }
 
80
 
 
81
        builtClass.prototype.hasImpl = build._impl;
 
82
 
 
83
        if (dynamic) {
 
84
            builtClass.NAME = name;
 
85
            builtClass.prototype.constructor = builtClass;
 
86
        }
 
87
 
 
88
        return builtClass;
 
89
    };
 
90
 
 
91
    build = Base._build;
 
92
 
 
93
    Y.mix(build, {
 
94
 
 
95
        _mixCust: function(r, s, cfg) {
 
96
            
 
97
            var aggregates, 
 
98
                custom, 
 
99
                statics,
 
100
                aggr,
 
101
                l,
 
102
                i;
 
103
                
 
104
            if (cfg) {
 
105
                aggregates = cfg.aggregates;
 
106
                custom = cfg.custom;
 
107
                statics = cfg.statics;
 
108
            }
 
109
 
 
110
            if (statics) {
 
111
                Y.mix(r, s, true, statics);
 
112
            }
 
113
 
 
114
            if (aggregates) {
 
115
                for (i = 0, l = aggregates.length; i < l; i++) {
 
116
                    aggr = aggregates[i];
 
117
                    if (!r.hasOwnProperty(aggr) && s.hasOwnProperty(aggr)) {
 
118
                        r[aggr] = L.isArray(s[aggr]) ? [] : {};
 
119
                    }
 
120
                    Y.aggregate(r, s, true, [aggr]);
 
121
                }
 
122
            }
 
123
 
 
124
            if (custom) {
 
125
                for (i in custom) {
 
126
                    if (custom.hasOwnProperty(i)) {
 
127
                        custom[i](i, r, s);
 
128
                    }
 
129
                }
 
130
            }
 
131
            
 
132
        },
 
133
 
 
134
        _tmpl: function(main) {
 
135
 
 
136
            function BuiltClass() {
 
137
                BuiltClass.superclass.constructor.apply(this, arguments);
 
138
            }
 
139
            Y.extend(BuiltClass, main);
 
140
 
 
141
            return BuiltClass;
 
142
        },
 
143
 
 
144
        _impl : function(extClass) {
 
145
            var classes = this._getClasses(), i, l, cls, exts, ll, j;
 
146
            for (i = 0, l = classes.length; i < l; i++) {
 
147
                cls = classes[i];
 
148
                if (cls._yuibuild) {
 
149
                    exts = cls._yuibuild.exts;
 
150
                    ll = exts.length;
 
151
    
 
152
                    for (j = 0; j < ll; j++) {
 
153
                        if (exts[j] === extClass) {
 
154
                            return true;
 
155
                        }
 
156
                    }
 
157
                }
 
158
            }
 
159
            return false;
 
160
        },
 
161
 
 
162
        _ctor : function(main, cfg) {
 
163
 
 
164
           var dynamic = (cfg && false === cfg.dynamic) ? false : true,
 
165
               builtClass = (dynamic) ? build._tmpl(main) : main,
 
166
               buildCfg = builtClass._yuibuild;
 
167
 
 
168
            if (!buildCfg) {
 
169
                buildCfg = builtClass._yuibuild = {};
 
170
            }
 
171
 
 
172
            buildCfg.id = buildCfg.id || null;
 
173
            buildCfg.exts = buildCfg.exts || [];
 
174
            buildCfg.dynamic = dynamic;
 
175
 
 
176
            return builtClass;
 
177
        },
 
178
 
 
179
        _cfg : function(main, cfg, exts) {
 
180
            var aggr = [], 
 
181
                cust = {},
 
182
                statics = [],
 
183
                buildCfg,
 
184
                cfgAggr = (cfg && cfg.aggregates),
 
185
                cfgCustBuild = (cfg && cfg.custom),
 
186
                cfgStatics = (cfg && cfg.statics),
 
187
                c = main,
 
188
                i, 
 
189
                l;
 
190
 
 
191
            // Prototype Chain
 
192
            while (c && c.prototype) {
 
193
                buildCfg = c._buildCfg;
 
194
                if (buildCfg) {
 
195
                    if (buildCfg.aggregates) {
 
196
                        aggr = aggr.concat(buildCfg.aggregates);
 
197
                    }
 
198
                    if (buildCfg.custom) {
 
199
                        Y.mix(cust, buildCfg.custom, true);
 
200
                    }
 
201
                    if (buildCfg.statics) {
 
202
                        statics = statics.concat(buildCfg.statics);
 
203
                    }
 
204
                }
 
205
                c = c.superclass ? c.superclass.constructor : null;
 
206
            }
 
207
 
 
208
            // Exts
 
209
            if (exts) {
 
210
                for (i = 0, l = exts.length; i < l; i++) {
 
211
                    c = exts[i];
 
212
                    buildCfg = c._buildCfg;
 
213
                    if (buildCfg) {
 
214
                        if (buildCfg.aggregates) {
 
215
                            aggr = aggr.concat(buildCfg.aggregates);
 
216
                        }
 
217
                        if (buildCfg.custom) {
 
218
                            Y.mix(cust, buildCfg.custom, true);
 
219
                        }
 
220
                        if (buildCfg.statics) {
 
221
                            statics = statics.concat(buildCfg.statics);
 
222
                        }
 
223
                    }                    
 
224
                }
 
225
            }
 
226
 
 
227
            if (cfgAggr) {
 
228
                aggr = aggr.concat(cfgAggr);
 
229
            }
 
230
 
 
231
            if (cfgCustBuild) {
 
232
                Y.mix(cust, cfg.cfgBuild, true);
 
233
            }
 
234
 
 
235
            if (cfgStatics) {
 
236
                statics = statics.concat(cfgStatics);
 
237
            }
 
238
 
 
239
            return {
 
240
                aggregates: aggr,
 
241
                custom: cust,
 
242
                statics: statics
 
243
            };
 
244
        },
 
245
 
 
246
        _clean : function(sx, cfg) {
 
247
            var prop, i, l, sxclone = Y.merge(sx),
 
248
                aggregates = cfg.aggregates,
 
249
                custom = cfg.custom;
 
250
 
 
251
            for (prop in custom) {
 
252
                if (sxclone.hasOwnProperty(prop)) {
 
253
                    delete sxclone[prop];
 
254
                }
 
255
            }
 
256
 
 
257
            for (i = 0, l = aggregates.length; i < l; i++) {
 
258
                prop = aggregates[i];
 
259
                if (sxclone.hasOwnProperty(prop)) {
 
260
                    delete sxclone[prop];
 
261
                }
 
262
            }
 
263
 
 
264
            return sxclone;
 
265
        }
 
266
    });
 
267
 
 
268
    /**
 
269
     * <p>
 
270
     * Builds a custom constructor function (class) from the
 
271
     * main function, and array of extension functions (classes)
 
272
     * provided. The NAME field for the constructor function is 
 
273
     * defined by the first argument passed in.
 
274
     * </p>
 
275
     * <p>
 
276
     * The cfg object supports the following properties
 
277
     * </p>
 
278
     * <dl>
 
279
     *    <dt>dynamic &#60;boolean&#62;</dt>
 
280
     *    <dd>
 
281
     *    <p>If true (default), a completely new class
 
282
     *    is created which extends the main class, and acts as the 
 
283
     *    host on which the extension classes are augmented.</p>
 
284
     *    <p>If false, the extensions classes are augmented directly to
 
285
     *    the main class, modifying the main class' prototype.</p>
 
286
     *    </dd>
 
287
     *    <dt>aggregates &#60;String[]&#62;</dt>
 
288
     *    <dd>An array of static property names, which will get aggregated
 
289
     *    on to the built class, in addition to the default properties build 
 
290
     *    will always aggregate as defined by the main class' static _buildCfg
 
291
     *    property.
 
292
     *    </dd>
 
293
     * </dl>
 
294
     *
 
295
     * @method build
 
296
     * @deprecated Use the more convenient Base.create and Base.mix methods instead
 
297
     * @static
 
298
     * @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
 
299
     * @param {Function} main The main class on which to base the built class
 
300
     * @param {Function[]} extensions The set of extension classes which will be
 
301
     * augmented/aggregated to the built class.
 
302
     * @param {Object} cfg Optional. Build configuration for the class (see description).
 
303
     * @return {Function} A custom class, created from the provided main and extension classes
 
304
     */
 
305
    Base.build = function(name, main, extensions, cfg) {
 
306
        return build(name, main, extensions, null, null, cfg);
 
307
    };
 
308
 
 
309
    /**
 
310
     * Creates a new class (constructor function) which extends the base class passed in as the second argument, 
 
311
     * and mixes in the array of extensions provided.
 
312
     * 
 
313
     * Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).
 
314
     * 
 
315
     * Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).
 
316
     * 
 
317
     * **NOTE FOR COMPONENT DEVELOPERS**: Both the `base` class, and `extensions` can define static a `_buildCfg` 
 
318
     * property, which acts as class creation meta-data, and drives how special static properties from the base 
 
319
     * class, or extensions should be copied, aggregated or (custom) mixed into the newly created class.
 
320
     * 
 
321
     * The `_buildCfg` property is a hash with 3 supported properties: `statics`, `aggregates` and `custom`, e.g:
 
322
     * 
 
323
     *     // If the Base/Main class is the thing introducing the property:
 
324
     * 
 
325
     *     MyBaseClass._buildCfg = {
 
326
     *     
 
327
     *        // Static properties/methods to copy (Alias) to the built class.
 
328
     *        statics: ["CopyThisMethod", "CopyThisProperty"],
 
329
     * 
 
330
     *        // Static props to aggregate onto the built class.
 
331
     *        aggregates: ["AggregateThisProperty"],
 
332
     * 
 
333
     *        // Static properties which need custom handling (e.g. deep merge etc.)
 
334
     *        custom: {
 
335
     *           "CustomProperty" : function(property, Receiver, Supplier) {
 
336
     *              ...
 
337
     *              var triggers = Receiver.CustomProperty.triggers; 
 
338
                    Receiver.CustomProperty.triggers = triggers.concat(Supplier.CustomProperty.triggers);
 
339
     *              ...
 
340
     *           }
 
341
     *        }
 
342
     *     };
 
343
     * 
 
344
     *     MyBaseClass.CopyThisMethod = function() {...}; 
 
345
     *     MyBaseClass.CopyThisProperty = "foo";
 
346
     *     MyBaseClass.AggregateThisProperty = {...};
 
347
     *     MyBaseClass.CustomProperty = {
 
348
     *        triggers: [...]
 
349
     *     }
 
350
     *
 
351
     *     // Or, if the Extension is the thing introducing the property:
 
352
     * 
 
353
     *     MyExtension._buildCfg = {
 
354
     *         statics : ...
 
355
     *         aggregates : ...
 
356
     *         custom : ...  
 
357
     *     }    
 
358
     * 
 
359
     * This way, when users pass your base or extension class to `Y.Base.create` or `Y.Base.mix`, they don't need to
 
360
     * know which properties need special handling. `Y.Base` has a buildCfg which defines `ATTRS` for custom mix handling
 
361
     * (to protect the static config objects), and `Y.Widget` has a buildCfg which specifies `HTML_PARSER` for 
 
362
     * straight up aggregation.
 
363
     *
 
364
     * @method create
 
365
     * @static
 
366
     * @param {Function} name The name of the newly created class. Used to defined the NAME property for the new class.
 
367
     * @param {Function} main The base class which the new class should extend. This class needs to be Base or a class derived from base (e.g. Widget).
 
368
     * @param {Function[]} extensions The list of extensions which will be mixed into the built class.
 
369
     * @param {Object} px The set of prototype properties/methods to add to the built class.
 
370
     * @param {Object} sx The set of static properties/methods to add to the built class.
 
371
     * @return {Function} The newly created class.
 
372
     */
 
373
    Base.create = function(name, base, extensions, px, sx) {
 
374
        return build(name, base, extensions, px, sx);
 
375
    };
 
376
 
 
377
    /**
 
378
     * <p>Mixes in a list of extensions to an existing class.</p>
 
379
     * @method mix
 
380
     * @static
 
381
     * @param {Function} main The existing class into which the extensions should be mixed.  The class needs to be Base or a class derived from Base (e.g. Widget)
 
382
     * @param {Function[]} extensions The set of extension classes which will mixed into the existing main class.
 
383
     * @return {Function} The modified main class, with extensions mixed in.
 
384
     */
 
385
    Base.mix = function(main, extensions) {
 
386
        return build(null, main, extensions, null, null, {dynamic:false});
 
387
    };
 
388
 
 
389
    /**
 
390
     * The build configuration for the Base class.
 
391
     *
 
392
     * Defines the static fields which need to be aggregated
 
393
     * when the Base class is used as the main class passed to
 
394
     * the <a href="#method_Base.build">Base.build</a> method.
 
395
     *
 
396
     * @property _buildCfg
 
397
     * @type Object
 
398
     * @static
 
399
     * @final
 
400
     * @private
 
401
     */
 
402
    Base._buildCfg = {
 
403
        custom : {
 
404
            ATTRS : function(prop, r, s) {
 
405
 
 
406
                r.ATTRS = r.ATTRS || {};
 
407
 
 
408
                if (s.ATTRS) {
 
409
 
 
410
                    var sAttrs = s.ATTRS,
 
411
                        rAttrs = r.ATTRS,
 
412
                        a;
 
413
 
 
414
                    for (a in sAttrs) {
 
415
                        if (sAttrs.hasOwnProperty(a)) {
 
416
                            rAttrs[a] = rAttrs[a] || {};
 
417
                            Y.mix(rAttrs[a], sAttrs[a], true);
 
418
                        }
 
419
                    }
 
420
                }
 
421
            },
 
422
            _NON_ATTRS_CFG : arrayAggregator
 
423
        },
 
424
        aggregates : ["_PLUG", "_UNPLUG"]
 
425
    };
 
426
 
 
427
 
 
428
}, '3.5.0' ,{requires:['base-base']});