/* YUI 3.17.2 (build 9c3c78e) Copyright 2014 Yahoo! Inc. All rights reserved. Licensed under the BSD License. http://yuilibrary.com/license/ */ 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]; }, /** *
Execute a function on each item of the list, optionally providing a * custom execution context. Default context is the item.
* *The callback signature is callback( item, index )
.
Execute a function on each item of the list, optionally providing a * custom execution context. Default context is the item.
* *The callback signature is callback( item, index )
.
Unlike each
, if the callback returns true, the
* iteration will stop.
item
, but is used by
* methods added with ArrayList.addMethod()
.
*
* @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, {
/**
* 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.
* *Accepts a single string name or an array of string names.
* *list.each( function ( item ) {
* item.methodName( 1, 2, 3 );
* } );
* // becomes
* list.methodName( 1, 2, 3 );
*
* Additionally, the pass through methods use the item retrieved by the
* _item
method in case there is any special behavior that is
* appropriate for API mirroring.
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; }, '3.17.2', {"requires": ["yui-base"]});