~ubuntu-branches/ubuntu/precise/maas/precise-updates

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/recordset-base/recordset-base-debug.js

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('recordset-base', function(Y) {
 
8
 
 
9
/**
 
10
 * Provides a wrapper around a standard javascript object. Can be inserted into a Recordset instance.
 
11
 *
 
12
 * @class Record
 
13
 */
 
14
var Record = Y.Base.create('record', Y.Base, [], {
 
15
    _setId: function() {
 
16
        return Y.guid();
 
17
    },
 
18
 
 
19
    initializer: function() {
 
20
    },
 
21
 
 
22
    destructor: function() {
 
23
    },
 
24
 
 
25
    /**
 
26
     * Retrieve a particular (or all) values from the object
 
27
     *
 
28
     * @param field {string} (optional) The key to retrieve the value from. If not supplied, the entire object is returned.
 
29
     * @method getValue
 
30
     * @public
 
31
     */
 
32
    getValue: function(field) {
 
33
        if (field === undefined) {
 
34
            return this.get("data");
 
35
        }
 
36
        else {
 
37
            return this.get("data")[field];
 
38
        }
 
39
        return null;
 
40
    }
 
41
},
 
42
{
 
43
    ATTRS: {
 
44
 
 
45
        /**
 
46
        * @description Unique ID of the record instance
 
47
        * @attribute id
 
48
        * @type string
 
49
        */
 
50
        id: {
 
51
            valueFn: "_setId"
 
52
        },
 
53
 
 
54
        /**
 
55
        * @description The object stored within the record instance
 
56
        * @attribute data
 
57
        * @type object
 
58
        */
 
59
        data: {
 
60
            value: null
 
61
        }
 
62
    }
 
63
});
 
64
 
 
65
Y.Record = Record;
 
66
/**
 
67
The Recordset utility provides a standard way for dealing with
 
68
a collection of similar objects.
 
69
@module recordset
 
70
@main recordset
 
71
@submodule recordset-base
 
72
**/
 
73
 
 
74
 
 
75
var ArrayList = Y.ArrayList,
 
76
Lang = Y.Lang,
 
77
 
 
78
/**
 
79
The Recordset utility provides a standard way for dealing with
 
80
a collection of similar objects.
 
81
 
 
82
Provides the base Recordset implementation, which can be extended to add
 
83
additional functionality, such as custom indexing. sorting, and filtering.
 
84
 
 
85
@class Recordset
 
86
@extends Base
 
87
@uses ArrayList
 
88
@param config {Object} Configuration object with initial attribute values
 
89
@constructor
 
90
**/
 
91
Recordset = Y.Base.create('recordset', Y.Base, [], {
 
92
 
 
93
 
 
94
    /**
 
95
     * Publish default functions for events. Create the initial hash table.
 
96
     *
 
97
     * @method initializer
 
98
     * @protected
 
99
     */
 
100
    initializer: function() {
 
101
        // The reason the conditional is needed is because of two scenarios:
 
102
        // 1. Instantiating new Y.Recordset() will not go into the setter of "records", and so it is necessary to create this._items in the initializer.
 
103
        // 2. Instantiating new Y.Recordset({records: [{...}]}) will call the setter of "records" and create this._items. In this case, we don't want that to be overwritten by [].
 
104
        if (!this._items) {
 
105
            this._items = [];
 
106
        }
 
107
 
 
108
        //set up event listener to fire events when recordset is modified in anyway
 
109
        this.publish({
 
110
            /**
 
111
             * <p>At least one record is being added. Additional properties of
 
112
             * the event are:</p>
 
113
             * <dl>
 
114
             *     <dt>added</dt>
 
115
             *         <dd>Array of new records to be added</dd>
 
116
             *     <dt>index</dt>
 
117
             *         <dd>The insertion index in the Recordset's internal
 
118
             *         array</dd>
 
119
             * </dl>
 
120
             *
 
121
             * <p>Preventing this event will cause the new records NOT to be
 
122
             * added to the Recordset's internal collection.</p>
 
123
             *
 
124
             * @event add
 
125
             * @preventable _defAddFn
 
126
             */
 
127
            add: { defaultFn: this._defAddFn },
 
128
 
 
129
            /**
 
130
             * <p>At least one record is being removed. Additional properties of
 
131
             * the event are:</p>
 
132
             * <dl>
 
133
             *     <dt>removed</dt>
 
134
             *         <dd>Array of records to be removed</dd>
 
135
             *     <dt>range</dt>
 
136
             *         <dd>Number of records to be removed</dd>
 
137
             *     <dt>index</dt>
 
138
             *         <dd>The starting index in the Recordset's internal
 
139
             *         array from which to remove records</dd>
 
140
             * </dl>
 
141
             *
 
142
             * <p>Preventing this event will cause the records NOT to be
 
143
             * removed from the Recordset's internal collection.</p>
 
144
             *
 
145
             * @event remove
 
146
             * @preventable _defRemoveFn
 
147
             */
 
148
            remove: { defaultFn: this._defRemoveFn },
 
149
 
 
150
            /**
 
151
             * The Recordset is being flushed of all records.
 
152
             *
 
153
             * @event empty
 
154
             * @preventable _defEmptyFn
 
155
             */
 
156
            empty: { defaultFn: this._defEmptyFn },
 
157
 
 
158
            /**
 
159
             * <p>At least one record is being updated. Additional properties of
 
160
             * the event are:</p>
 
161
             * <dl>
 
162
             *     <dt>updated</dt>
 
163
             *         <dd>Array of records with updated values</dd>
 
164
             *     <dt>overwritten</dt>
 
165
             *         <dd>Array of current records that will be replaced</dd>
 
166
             *     <dt>index</dt>
 
167
             *         <dd>The starting index in the Recordset's internal
 
168
             *         array from which to update will apply</dd>
 
169
             * </dl>
 
170
             *
 
171
             * <p>Preventing this event will cause the records NOT to be
 
172
             * updated in the Recordset's internal collection.</p>
 
173
             *
 
174
             * @event update
 
175
             * @preventable _defUpdateFn
 
176
             */
 
177
            update: { defaultFn: this._defUpdateFn }
 
178
        });
 
179
 
 
180
        this._buildHashTable(this.get('key'));
 
181
 
 
182
        this.after([
 
183
            'recordsChange',
 
184
            'add',
 
185
            'remove',
 
186
            'update',
 
187
            'empty'], this._updateHash);
 
188
    },
 
189
 
 
190
    /**
 
191
     * Returns the record with particular ID or index
 
192
     *
 
193
     * @method getRecord
 
194
     * @param i {String, Number} The ID of the record if a string, or the index if a number.
 
195
     * @return {Record} A Y.Record instance
 
196
     */
 
197
    getRecord: function(i) {
 
198
 
 
199
        if (Lang.isString(i)) {
 
200
            return this.get('table')[i];
 
201
        }
 
202
        else if (Lang.isNumber(i)) {
 
203
            return this._items[i];
 
204
        }
 
205
        return null;
 
206
    },
 
207
 
 
208
 
 
209
    /**
 
210
     * Returns the record at a particular index
 
211
     *
 
212
     * @method getRecordByIndex
 
213
     * @param i {Number} Index at which the required record resides
 
214
     * @return {Record} A Y.Record instance
 
215
     */
 
216
    getRecordByIndex: function(i) {
 
217
        return this._items[i];
 
218
    },
 
219
 
 
220
    /**
 
221
     * Returns a range of records beginning at particular index
 
222
     *
 
223
     * @method getRecordsByIndex
 
224
     * @param index {Number} Index at which the required record resides
 
225
     * @param range {Number} (Optional) Number of records to retrieve. The default is 1
 
226
     * @return {Array} An array of Y.Record instances
 
227
     */
 
228
    getRecordsByIndex: function(index, range) {
 
229
        var i = 0,
 
230
        returnedRecords = [];
 
231
        //Range cannot take on negative values
 
232
        range = (Lang.isNumber(range) && (range > 0)) ? range: 1;
 
233
 
 
234
        for (; i < range; i++) {
 
235
            returnedRecords.push(this._items[index + i]);
 
236
        }
 
237
        return returnedRecords;
 
238
    },
 
239
 
 
240
    /**
 
241
     * Returns the length of the recordset
 
242
     *
 
243
     * @method getLength
 
244
     * @return {Number} Number of records in the recordset
 
245
     */
 
246
    getLength: function() {
 
247
        return this.size();
 
248
    },
 
249
 
 
250
    /**
 
251
    Gets an array of values for a data _key_ in the set's records.  If no _key_
 
252
    is supplied, the returned array will contain the full data object for each
 
253
    record.
 
254
 
 
255
    @method getValuesByKey
 
256
    @param {String} [key] Data property to get from all records
 
257
    @return {Array} An array of values for the given _key_ if supplied.
 
258
        Otherwise, an array of each record's data hash.
 
259
    **/
 
260
    getValuesByKey: function(key) {
 
261
        var i = 0,
 
262
        len = this._items.length,
 
263
        retVals = [];
 
264
        for (; i < len; i++) {
 
265
            retVals.push(this._items[i].getValue(key));
 
266
        }
 
267
        return retVals;
 
268
    },
 
269
 
 
270
 
 
271
    /**
 
272
     * Adds one or more Records to the RecordSet at the given index. If index is null, then adds the Records to the end of the RecordSet.
 
273
     *
 
274
     * @method add
 
275
     * @param {Record|Object|Array} oData A Y.Record instance, An object literal of data or an array of object literals
 
276
     * @param [index] {Number} [index] Index at which to add the record(s)
 
277
     * @return {Recordset} The updated recordset instance
 
278
     */
 
279
    add: function(oData, index) {
 
280
 
 
281
        var newRecords = [],
 
282
        idx,
 
283
        i = 0;
 
284
 
 
285
        idx = (Lang.isNumber(index) && (index > -1)) ? index: this._items.length;
 
286
 
 
287
        //Passing in array of object literals for oData
 
288
        if (Lang.isArray(oData)) {
 
289
            for (; i < oData.length; i++) {
 
290
                newRecords[i] = this._changeToRecord(oData[i]);
 
291
            }
 
292
        } else if (Lang.isObject(oData)) {
 
293
            newRecords[0] = this._changeToRecord(oData);
 
294
        }
 
295
 
 
296
        this.fire('add', {
 
297
            added: newRecords,
 
298
            index: idx
 
299
        });
 
300
        return this;
 
301
    },
 
302
 
 
303
    /**
 
304
    Removes one or more Records to the RecordSet at the given index. If index
 
305
    is null, then removes a single Record from the end of the RecordSet.
 
306
    
 
307
    @method remove
 
308
    @param {Number} [index] Index at which to remove the record(s) from
 
309
    @param {Number} [range] Number of records to remove (including the one
 
310
        at the index)
 
311
    @return {Recordset} The updated recordset instance
 
312
    **/
 
313
    remove: function(index, range) {
 
314
        var remRecords = [];
 
315
 
 
316
        //Default is to only remove the last record - the length is always 1 greater than the last index
 
317
        index = (index > -1) ? index: (this._items.length - 1);
 
318
        range = (range > 0) ? range: 1;
 
319
 
 
320
        remRecords = this._items.slice(index, (index + range));
 
321
        this.fire('remove', {
 
322
            removed: remRecords,
 
323
            range: range,
 
324
            index: index
 
325
        });
 
326
        //this._recordRemoved(remRecords, index);
 
327
        //return ({data: remRecords, index:index});
 
328
        return this;
 
329
    },
 
330
 
 
331
    /**
 
332
     * Empties the recordset
 
333
     *
 
334
     * @method empty
 
335
     * @return {Recordset} The updated recordset instance
 
336
     */
 
337
    empty: function() {
 
338
        this.fire('empty', {});
 
339
        return this;
 
340
    },
 
341
 
 
342
    /**
 
343
    Updates the recordset with the new records passed in. Overwrites existing
 
344
    records when updating the index with the new records.
 
345
    
 
346
    @method update
 
347
    @param {Record|Object|Array} data A Y.Record instance, An object literal of
 
348
        data or an array of object literals
 
349
    @param {Number} [index] The index to start updating from. 
 
350
    @return {Recordset} The updated recordset instance
 
351
    **/
 
352
    update: function(data, index) {
 
353
        var rec,
 
354
            arr,
 
355
            i = 0;
 
356
 
 
357
        // Whatever is passed in, we are changing it to an array so that it can
 
358
        // be easily iterated in the _defUpdateFn method
 
359
        arr = (!(Lang.isArray(data))) ? [data] : data;
 
360
        rec = this._items.slice(index, index + arr.length);
 
361
 
 
362
        for (; i < arr.length; i++) {
 
363
            arr[i] = this._changeToRecord(arr[i]);
 
364
        }
 
365
 
 
366
        this.fire('update', {
 
367
            updated: arr,
 
368
            overwritten: rec,
 
369
            index: index
 
370
        });
 
371
 
 
372
        return this;
 
373
    },
 
374
 
 
375
    /**
 
376
     * Default behavior for the "add" event. Adds Record instances starting from
 
377
     * the index specified in `e.index`.
 
378
     *
 
379
     * @method _defAddFn
 
380
     * @param {EventFacade} e The add event
 
381
     * @private
 
382
     */
 
383
    _defAddFn: function(e) {
 
384
        this._items.splice.apply(this._items, [e.index, 0].concat(e.added));
 
385
    },
 
386
 
 
387
    /**
 
388
     * Default behavior for the "remove" event. Removes Records from the
 
389
     * internal array starting from `e.index`.  By default, it will remove one
 
390
     * Record. But if `e.range` is set, it will remove that many Records.
 
391
     *
 
392
     * @method _defRemoveFn
 
393
     * @param {EventFacade} e The remove event
 
394
     * @private
 
395
     */
 
396
    _defRemoveFn: function(e) {
 
397
        this._items.splice(e.index, e.range || 1);
 
398
    },
 
399
 
 
400
    /**
 
401
     * Default behavior for the "update" event. Sets Record instances for each
 
402
     * item in `e.updated` at indexes starting from `e.index`.
 
403
     *
 
404
     * @method _defUpdateFn
 
405
     * @param {EventFacade} e The update event
 
406
     * @private
 
407
     */
 
408
    _defUpdateFn: function(e) {
 
409
        for (var i = 0; i < e.updated.length; i++) {
 
410
            this._items[e.index + i] = this._changeToRecord(e.updated[i]);
 
411
        }
 
412
    },
 
413
 
 
414
    /**
 
415
     * Default behavior for the "empty" event. Clears the internal array of
 
416
     * Records.
 
417
     *
 
418
     * @method _defEmptyFn
 
419
     * @param {EventFacade} e The empty event
 
420
     * @private
 
421
     */
 
422
    _defEmptyFn: function(e) {
 
423
        this._items = [];
 
424
        Y.log('empty fired');
 
425
    },
 
426
 
 
427
    /**
 
428
    Updates the internal hash table.
 
429
 
 
430
    @method _defUpdateHash
 
431
    @param {EventFacade} e Event triggering the hash table update
 
432
    @private
 
433
    **/
 
434
    _updateHash: function (e) {
 
435
        var handler = "_hash",
 
436
            type = e.type.replace(/.*:/,''),
 
437
            newHash;
 
438
 
 
439
        // _hashAdd, _hashRemove, _hashEmpty, etc
 
440
        // Not a switch or else if setup to allow for external expansion.
 
441
        handler += type.charAt(0).toUpperCase() + type.slice(1);
 
442
 
 
443
        newHash = this[handler] &&
 
444
                    this[handler](this.get('table'), this.get('key'), e);
 
445
 
 
446
        if (newHash) {
 
447
            this.set('table', newHash);
 
448
        }
 
449
    },
 
450
 
 
451
    /**
 
452
    Regenerates the hash table from the current internal array of Records.
 
453
 
 
454
    @method _hashRecordsChange
 
455
    @param {Object} hash The hash map before replacement
 
456
    @param {String} key The key by which to add items to the hash
 
457
    @param {Object} e The event or object containing the items to be added.
 
458
                      Items are expected to be stored in an array assigned to
 
459
                      the `added` property.
 
460
    @return {Object} The updated hash map
 
461
    @private
 
462
    **/
 
463
    _hashRecordsChange: function (hash, key, e) {
 
464
        return this._buildHashTable(key);
 
465
    },
 
466
 
 
467
    /**
 
468
    Builds a hash table from the current internal array of Records.
 
469
 
 
470
    @method _buildHashTable
 
471
    @param {String} key The Record key to hash the items by
 
472
    @return {Object} A new hash map of Records keyed by each Records' key
 
473
    @private
 
474
    **/
 
475
    _buildHashTable: function (key) {
 
476
        return this._hashAdd({}, key, { added: this._items });
 
477
    },
 
478
 
 
479
    /**
 
480
    Adds items to the hash table.  Items are the values, and the keys are the
 
481
    values of the item's attribute named in the `key` parameter.
 
482
 
 
483
    @method _hashAdd
 
484
    @param {Object} hash The hash map before adding items
 
485
    @param {String} key The key by which to add the items to the hash
 
486
    @param {Object} e The event or object containing the items to be added.
 
487
                      Items are expected to be stored in an array assigned to
 
488
                      the `added` property.
 
489
    @return {Object} The updated hash map
 
490
    @private
 
491
    **/
 
492
    _hashAdd: function(hash, key, e) {
 
493
        var items = e.added,
 
494
            i, len;
 
495
 
 
496
        for (i = 0, len = e.added.length; i < len; ++i) {
 
497
            hash[items[i].get(key)] = items[i];
 
498
        }
 
499
 
 
500
        return hash;
 
501
    },
 
502
 
 
503
    /**
 
504
    Removes items from the hash table.
 
505
 
 
506
    @method _hashRemove
 
507
    @param {Object} hash The hash map before removing items
 
508
    @param {String} key The key by which to remove the items from the hash
 
509
    @param {Object} e The event or object containing the items to be removed.
 
510
                      Items are expected to be stored in an array assigned to
 
511
                      the `removed` property.
 
512
    @return {Object} The updated hash map
 
513
    @private
 
514
    **/
 
515
    _hashRemove: function(hash, key, e) {
 
516
        for (var i = e.removed.length - 1; i >= 0; --i) {
 
517
            delete hash[e.removed[i].get(key)];
 
518
        }
 
519
 
 
520
        return hash;
 
521
    },
 
522
 
 
523
    /**
 
524
    Updates items in the hash table.
 
525
 
 
526
    @method _hashUpdate
 
527
    @param {Object} hash The hash map before updating items
 
528
    @param {String} key The key by which to update the items to the hash
 
529
    @param {Object} e The event or object containing the items to be updated.
 
530
                      Items are expected to be stored in an array assigned to
 
531
                      the `updated` property. Optionally, items can be
 
532
                      identified for being overwritten by including them in an
 
533
                      array assigned to the `overwritten` property.
 
534
    @return {Object} The updated hash map
 
535
    @private
 
536
    **/
 
537
    _hashUpdate: function (hash, key, e) {
 
538
        if (e.overwritten && e.overwritten.length) {
 
539
            hash = this._hashRemove(hash, key, { removed: e.overwritten });
 
540
        }
 
541
 
 
542
        return this._hashAdd(hash, key, { added: e.updated });
 
543
    },
 
544
 
 
545
    /**
 
546
    Clears the hash table.
 
547
 
 
548
    @method _hashEmpty
 
549
    @param {Object} hash The hash map before adding items
 
550
    @param {String} key The key by which to remove the items from the hash
 
551
    @param {Object} e The event or object containing the items to be removed.
 
552
                      Items are expected to be stored in an array assigned to
 
553
                      the `removed` property.
 
554
    @return {Object} An empty hash
 
555
    @private
 
556
    **/
 
557
    _hashEmpty: function() {
 
558
        return {};
 
559
    },
 
560
 
 
561
    /**
 
562
     * Sets up the hashtable with all the records currently in the recordset
 
563
     *
 
564
     * @method _initHashTable
 
565
     * @private
 
566
     */
 
567
    _initHashTable: function() {
 
568
        return this._hashAdd({}, this.get('key'), { added: this._items || [] });
 
569
    },
 
570
 
 
571
    /**
 
572
     * Helper method - it takes an object bag and converts it to a Y.Record
 
573
     *
 
574
     * @method _changeToRecord
 
575
     * @param obj {Object|Record} Any objet literal or Y.Record instance
 
576
     * @return {Record} A Record instance.
 
577
     * @private
 
578
     */
 
579
    _changeToRecord: function(obj) {
 
580
        return (obj instanceof Y.Record) ? obj : new Y.Record({ data: obj });
 
581
    },
 
582
 
 
583
    /**
 
584
    Ensures the value being set is an array of Record instances. If array items
 
585
    are raw object data, they are turned into Records.
 
586
 
 
587
    @method _setRecords
 
588
    @param {Record[]|Object[]} items The Records or data Objects to store as
 
589
                                     Records.
 
590
    @return {Record[]}
 
591
    **/
 
592
    _setRecords: function (items) {
 
593
        if (!Y.Lang.isArray(items)) {
 
594
            return Y.Attribute.INVALID_VALUE;
 
595
        }
 
596
 
 
597
        var records = [],
 
598
            i, len;
 
599
 
 
600
        // FIXME: This should use the flyweight pattern if possible
 
601
        for (i = 0, len = items.length; i < len; ++i) {
 
602
            records[i] = this._changeToRecord(items[i]);
 
603
        }
 
604
 
 
605
        return (this._items = records);
 
606
    }
 
607
}, {
 
608
    ATTRS: {
 
609
 
 
610
        /**
 
611
        * An array of Records that the Recordset is storing.  Passing an array
 
612
        * of raw record data is also accepted.  The data for each item will be
 
613
        * wrapped in a Record instance.
 
614
        *
 
615
        * @attribute records
 
616
        * @type {Record[]}
 
617
        */
 
618
        records: {
 
619
            // TODO: necessary? valueFn?
 
620
            lazyAdd: false,
 
621
            getter: function() {
 
622
                // give them a copy, not the internal object
 
623
                return Y.Array(this._items);
 
624
            },
 
625
            setter: "_setRecords"
 
626
        },
 
627
 
 
628
        /**
 
629
        A hash table where the ID of the record is the key, and the record
 
630
        instance is the value.
 
631
        
 
632
        @attribute table
 
633
        @type object
 
634
        **/
 
635
        table: {
 
636
            valueFn: '_initHashTable'
 
637
        },
 
638
 
 
639
        /**
 
640
        The ID to use as the key in the hash table.
 
641
        
 
642
        @attribute key
 
643
        @type string
 
644
        **/
 
645
        key: {
 
646
            value: 'id',
 
647
            readOnly: true
 
648
        }
 
649
 
 
650
    }
 
651
});
 
652
Y.augment(Recordset, ArrayList);
 
653
Y.Recordset = Recordset;
 
654
 
 
655
 
 
656
 
 
657
}, '3.5.1' ,{requires:['base','arraylist']});