-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraylist.js
214 lines (189 loc) · 6.82 KB
/
arraylist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
YUI.add('arraylist', function (Y, NAME) {
/**
* Collection utilities beyond what is provided in the YUI core
* @module collection
* @submodule arraylist
*/
var YArray = Y.Array,
YArray_each = YArray.each,
ArrayListProto;
/**
* Generic ArrayList class for managing lists of items and iterating operations
* over them. The targeted use for this class is for augmentation onto a
* class that is responsible for managing multiple instances of another class
* (e.g. NodeList for Nodes). The recommended use is to augment your class with
* ArrayList, then use ArrayList.addMethod to mirror the API of the constituent
* items on the list's API.
*
* The default implementation creates immutable lists, but mutability can be
* provided via the arraylist-add submodule or by implementing mutation methods
* directly on the augmented class's prototype.
*
* @class ArrayList
* @constructor
* @param items { Array } array of items this list will be responsible for
*/
function ArrayList( items ) {
if ( items !== undefined ) {
this._items = Y.Lang.isArray( items ) ? items : YArray( items );
} else {
// ||= to support lazy initialization from augment
this._items = this._items || [];
}
}
ArrayListProto = {
/**
* Get an item by index from the list. Override this method if managing a
* list of objects that have a different public representation (e.g. Node
* instances vs DOM nodes). The iteration methods that accept a user
* function will use this method for access list items for operation.
*
* @method item
* @param i { Integer } index to fetch
* @return { mixed } the item at the requested index
*/
item: function ( i ) {
return this._items[i];
},
/**
* <p>Execute a function on each item of the list, optionally providing a
* custom execution context. Default context is the item.</p>
*
* <p>The callback signature is <code>callback( item, index )</code>.</p>
*
* @method each
* @param fn { Function } the function to execute
* @param context { mixed } optional override 'this' in the function
* @return { ArrayList } this instance
* @chainable
*/
each: function ( fn, context ) {
YArray_each( this._items, function ( item, i ) {
item = this.item( i );
fn.call( context || item, item, i, this );
}, this);
return this;
},
/**
* <p>Execute a function on each item of the list, optionally providing a
* custom execution context. Default context is the item.</p>
*
* <p>The callback signature is <code>callback( item, index )</code>.</p>
*
* <p>Unlike <code>each</code>, if the callback returns true, the
* iteration will stop.</p>
*
* @method some
* @param fn { Function } the function to execute
* @param context { mixed } optional override 'this' in the function
* @return { Boolean } True if the function returned true on an item
*/
some: function ( fn, context ) {
return YArray.some( this._items, function ( item, i ) {
item = this.item( i );
return fn.call( context || item, item, i, this );
}, this);
},
/**
* Finds the first index of the needle in the managed array of items.
*
* @method indexOf
* @param needle { mixed } The item to search for
* @return { Integer } Array index if found. Otherwise -1
*/
indexOf: function ( needle ) {
return YArray.indexOf( this._items, needle );
},
/**
* How many items are in this list?
*
* @method size
* @return { Integer } Number of items in the list
*/
size: function () {
return this._items.length;
},
/**
* Is this instance managing any items?
*
* @method isEmpty
* @return { Boolean } true if 1 or more items are being managed
*/
isEmpty: function () {
return !this.size();
},
/**
* Provides an array-like representation for JSON.stringify.
*
* @method toJSON
* @return { Array } an array representation of the ArrayList
*/
toJSON: function () {
return this._items;
}
};
// Default implementation does not distinguish between public and private
// item getter
/**
* Protected method for optimizations that may be appropriate for API
* mirroring. Similar in functionality to <code>item</code>, but is used by
* methods added with <code>ArrayList.addMethod()</code>.
*
* @method _item
* @protected
* @param i { Integer } Index of item to fetch
* @return { mixed } The item appropriate for pass through API methods
*/
ArrayListProto._item = ArrayListProto.item;
// Mixed onto existing proto to preserve constructor NOT being an own property.
// This has bitten me when composing classes by enumerating, copying prototypes.
Y.mix(ArrayList.prototype, ArrayListProto);
Y.mix( ArrayList, {
/**
* <p>Adds a pass through method to dest (typically the prototype of a list
* class) that calls the named method on each item in the list with
* whatever parameters are passed in. Allows for API indirection via list
* instances.</p>
*
* <p>Accepts a single string name or an array of string names.</p>
*
* <pre><code>list.each( function ( item ) {
* item.methodName( 1, 2, 3 );
* } );
* // becomes
* list.methodName( 1, 2, 3 );</code></pre>
*
* <p>Additionally, the pass through methods use the item retrieved by the
* <code>_item</code> method in case there is any special behavior that is
* appropriate for API mirroring.</p>
*
* <p>If the iterated method returns a value, the return value from the
* added method will be an array of values with each value being at the
* corresponding index for that item. If the iterated method does not
* return a value, the added method will be chainable.
*
* @method addMethod
* @static
* @param dest {Object} Object or prototype to receive the iterator method
* @param name {String|String[]} Name of method of methods to create
*/
addMethod: function ( dest, names ) {
names = YArray( names );
YArray_each( names, function ( name ) {
dest[ name ] = function () {
var args = YArray( arguments, 0, true ),
ret = [];
YArray_each( this._items, function ( item, i ) {
item = this._item( i );
var result = item[ name ].apply( item, args );
if ( result !== undefined && result !== item ) {
ret[i] = result;
}
}, this);
return ret.length ? ret : this;
};
} );
}
} );
Y.ArrayList = ArrayList;
}, '@VERSION@', {"requires": ["yui-base"]});