]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/collection/arraylist-add.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / collection / arraylist-add.js
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.3.0
6 build: 3167
7 */
8 YUI.add('arraylist-add', function(Y) {
9
10 /**
11  * Collection utilities beyond what is provided in the YUI core
12  * @module collection
13  * @submodule arraylist-add
14  */
15
16 /**
17  * Adds methods add and remove to Y.ArrayList
18  * @class ArrayList~add
19  */
20 Y.mix(Y.ArrayList.prototype, {
21
22     /**
23      * Add a single item to the ArrayList.  Does not prevent duplicates.
24      *
25      * @method add
26      * @param { mixed } item Item presumably of the same type as others in the
27      *                       ArrayList.
28      * @param {Number} index (Optional.)  Number representing the position at
29      * which the item should be inserted.
30      * @return {ArrayList} the instance.
31      * @chainable
32      */
33     add: function(item, index) {
34         var items = this._items;
35
36         if (Y.Lang.isNumber(index)) {
37             items.splice(index, 0, item);
38         }
39         else {
40             items.push(item);
41         }
42
43         return this;
44     },
45
46     /**
47      * Removes first or all occurrences of an item to the ArrayList.  If a
48      * comparator is not provided, uses itemsAreEqual method to determine
49      * matches.
50      *
51      * @method remove
52      * @param { mixed } needle Item to find and remove from the list.
53      * @param { Boolean } all If true, remove all occurrences.
54      * @param { Function } comparator optional a/b function to test equivalence.
55      * @return {ArrayList} the instance.
56      * @chainable
57      */
58     remove: function(needle, all, comparator) {
59         comparator = comparator || this.itemsAreEqual;
60
61         for (var i = this._items.length - 1; i >= 0; --i) {
62             if (comparator.call(this, needle, this.item(i))) {
63                 this._items.splice(i, 1);
64                 if (!all) {
65                     break;
66                 }
67             }
68         }
69
70         return this;
71     },
72
73     /**
74      * Default comparator for items stored in this list.  Used by remove().
75      *
76      * @method itemsAreEqual
77      * @param { mixed } a item to test equivalence with.
78      * @param { mixed } b other item to test equivalance.
79      * @return { Boolean } true if items are deemed equivalent.
80      */
81     itemsAreEqual: function(a, b) {
82         return a === b;
83     }
84
85 });
86
87
88 }, '3.3.0' ,{requires:['arraylist']});