3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('arraylist', function(Y) {
10
* Collection utilities beyond what is provided in the YUI core
12
* @submodule arraylist
16
YArray_each = YArray.each,
20
* Generic ArrayList class for managing lists of items and iterating operations
21
* over them. The targeted use for this class is for augmentation onto a
22
* class that is responsible for managing multiple instances of another class
23
* (e.g. NodeList for Nodes). The recommended use is to augment your class with
24
* ArrayList, then use ArrayList.addMethod to mirror the API of the constituent
25
* items on the list's API.
27
* The default implementation creates immutable lists, but mutability can be
28
* provided via the arraylist-add submodule or by implementing mutation methods
29
* directly on the augmented class's prototype.
33
* @param items { Array } array of items this list will be responsible for
35
function ArrayList( items ) {
36
if ( items !== undefined ) {
37
this._items = Y.Lang.isArray( items ) ? items : YArray( items );
39
// ||= to support lazy initialization from augment
40
this._items = this._items || [];
46
* Get an item by index from the list. Override this method if managing a
47
* list of objects that have a different public representation (e.g. Node
48
* instances vs DOM nodes). The iteration methods that accept a user
49
* function will use this method for access list items for operation.
52
* @param i { Integer } index to fetch
53
* @return { mixed } the item at the requested index
55
item: function ( i ) {
56
return this._items[i];
60
* <p>Execute a function on each item of the list, optionally providing a
61
* custom execution context. Default context is the item.</p>
63
* <p>The callback signature is <code>callback( item, index )</code>.</p>
66
* @param fn { Function } the function to execute
67
* @param context { mixed } optional override 'this' in the function
68
* @return { ArrayList } this instance
71
each: function ( fn, context ) {
72
YArray_each( this._items, function ( item, i ) {
73
item = this.item( i );
75
fn.call( context || item, item, i, this );
82
* <p>Execute a function on each item of the list, optionally providing a
83
* custom execution context. Default context is the item.</p>
85
* <p>The callback signature is <code>callback( item, index )</code>.</p>
87
* <p>Unlike <code>each</code>, if the callback returns true, the
88
* iteratation will stop.</p>
91
* @param fn { Function } the function to execute
92
* @param context { mixed } optional override 'this' in the function
93
* @return { Boolean } True if the function returned true on an item
95
some: function ( fn, context ) {
96
return YArray.some( this._items, function ( item, i ) {
97
item = this.item( i );
99
return fn.call( context || item, item, i, this );
104
* Finds the first index of the needle in the managed array of items.
107
* @param needle { mixed } The item to search for
108
* @return { Integer } Array index if found. Otherwise -1
110
indexOf: function ( needle ) {
111
return YArray.indexOf( this._items, needle );
115
* How many items are in this list?
118
* @return { Integer } Number of items in the list
121
return this._items.length;
125
* Is this instance managing any items?
128
* @return { Boolean } true if 1 or more items are being managed
130
isEmpty: function () {
135
* Provides an array-like representation for JSON.stringify.
138
* @return { Array } an array representation of the ArrayList
140
toJSON: function () {
144
// Default implementation does not distinguish between public and private
147
* Protected method for optimizations that may be appropriate for API
148
* mirroring. Similar in functionality to <code>item</code>, but is used by
149
* methods added with <code>ArrayList.addMethod()</code>.
153
* @param i { Integer } Index of item to fetch
154
* @return { mixed } The item appropriate for pass through API methods
156
ArrayListProto._item = ArrayListProto.item;
158
ArrayList.prototype = ArrayListProto;
163
* <p>Adds a pass through method to dest (typically the prototype of a list
164
* class) that calls the named method on each item in the list with
165
* whatever parameters are passed in. Allows for API indirection via list
168
* <p>Accepts a single string name or an array of string names.</p>
170
* <pre><code>list.each( function ( item ) {
171
* item.methodName( 1, 2, 3 );
174
* list.methodName( 1, 2, 3 );</code></pre>
176
* <p>Additionally, the pass through methods use the item retrieved by the
177
* <code>_item</code> method in case there is any special behavior that is
178
* appropriate for API mirroring.</p>
180
* <p>If the iterated method returns a value, the return value from the
181
* added method will be an array of values with each value being at the
182
* corresponding index for that item. If the iterated method does not
183
* return a value, the added method will be chainable.
187
* @param dest {Object} Object or prototype to receive the iterator method
188
* @param name {String|String[]} Name of method of methods to create
190
addMethod: function ( dest, names ) {
192
names = YArray( names );
194
YArray_each( names, function ( name ) {
195
dest[ name ] = function () {
196
var args = YArray( arguments, 0, true ),
199
YArray_each( this._items, function ( item, i ) {
200
item = this._item( i );
202
var result = item[ name ].apply( item, args );
204
if ( result !== undefined && result !== item ) {
209
return ret.length ? ret : this;
215
Y.ArrayList = ArrayList;
218
}, '3.4.1' ,{requires:['yui-base']});