access.js (1314B)
1 define( [ 2 "../core", 3 "../core/toType", 4 "../var/isFunction" 5 ], function( jQuery, toType, isFunction ) { 6 7 "use strict"; 8 9 // Multifunctional method to get and set values of a collection 10 // The value/s can optionally be executed if it's a function 11 var access = function( elems, fn, key, value, chainable, emptyGet, raw ) { 12 var i = 0, 13 len = elems.length, 14 bulk = key == null; 15 16 // Sets many values 17 if ( toType( key ) === "object" ) { 18 chainable = true; 19 for ( i in key ) { 20 access( elems, fn, i, key[ i ], true, emptyGet, raw ); 21 } 22 23 // Sets one value 24 } else if ( value !== undefined ) { 25 chainable = true; 26 27 if ( !isFunction( value ) ) { 28 raw = true; 29 } 30 31 if ( bulk ) { 32 33 // Bulk operations run against the entire set 34 if ( raw ) { 35 fn.call( elems, value ); 36 fn = null; 37 38 // ...except when executing function values 39 } else { 40 bulk = fn; 41 fn = function( elem, _key, value ) { 42 return bulk.call( jQuery( elem ), value ); 43 }; 44 } 45 } 46 47 if ( fn ) { 48 for ( ; i < len; i++ ) { 49 fn( 50 elems[ i ], key, raw ? 51 value : 52 value.call( elems[ i ], i, fn( elems[ i ], key ) ) 53 ); 54 } 55 } 56 } 57 58 if ( chainable ) { 59 return elems; 60 } 61 62 // Gets 63 if ( bulk ) { 64 return fn.call( elems ); 65 } 66 67 return len ? fn( elems[ 0 ], key ) : emptyGet; 68 }; 69 70 return access; 71 72 } );