2012-04-25 00:16:37 +00:00
|
|
|
// String to Object options format cache
|
|
|
|
var optionsCache = {};
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-04-25 00:16:37 +00:00
|
|
|
// Convert String-formatted options into Object-formatted ones and store in cache
|
|
|
|
function createOptions( options ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
var object = optionsCache[ options ] = {};
|
2012-11-26 08:20:43 +00:00
|
|
|
jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
object[ flag ] = true;
|
|
|
|
});
|
2011-09-12 15:37:41 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a callback list using the following parameters:
|
|
|
|
*
|
2012-04-25 00:16:37 +00:00
|
|
|
* options: an optional list of space-separated options that will change how
|
|
|
|
* the callback list behaves or a more traditional option object
|
2011-09-12 15:37:41 +00:00
|
|
|
*
|
|
|
|
* By default a callback list will act like an event callback list and can be
|
|
|
|
* "fired" multiple times.
|
|
|
|
*
|
2012-04-25 00:16:37 +00:00
|
|
|
* Possible options:
|
2011-09-12 15:37:41 +00:00
|
|
|
*
|
|
|
|
* once: will ensure the callback list can only be fired once (like a Deferred)
|
|
|
|
*
|
|
|
|
* memory: will keep track of previous values and will call any callback added
|
|
|
|
* after the list has been fired right away with the latest "memorized"
|
|
|
|
* values (like a Deferred)
|
|
|
|
*
|
|
|
|
* unique: will ensure a callback can only be added once (no duplicate in the list)
|
|
|
|
*
|
|
|
|
* stopOnFalse: interrupt callings when a callback returns false
|
|
|
|
*
|
|
|
|
*/
|
2012-04-25 00:16:37 +00:00
|
|
|
jQuery.Callbacks = function( options ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-04-25 00:16:37 +00:00
|
|
|
// Convert options from String-formatted to Object-formatted if needed
|
2011-09-12 15:37:41 +00:00
|
|
|
// (we check in cache first)
|
2012-04-25 00:16:37 +00:00
|
|
|
options = typeof options === "string" ?
|
|
|
|
( optionsCache[ options ] || createOptions( options ) ) :
|
|
|
|
jQuery.extend( {}, options );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-07-10 01:38:11 +00:00
|
|
|
var // Last fire value (for non-forgettable lists)
|
2011-09-12 15:37:41 +00:00
|
|
|
memory,
|
2012-01-31 01:31:22 +00:00
|
|
|
// Flag to know if list was already fired
|
|
|
|
fired,
|
2011-09-12 15:37:41 +00:00
|
|
|
// Flag to know if list is currently firing
|
|
|
|
firing,
|
|
|
|
// First callback to fire (used internally by add and fireWith)
|
|
|
|
firingStart,
|
|
|
|
// End of the loop when firing
|
|
|
|
firingLength,
|
|
|
|
// Index of currently firing callback (modified by remove if needed)
|
|
|
|
firingIndex,
|
2012-07-10 01:38:11 +00:00
|
|
|
// Actual callback list
|
|
|
|
list = [],
|
|
|
|
// Stack of fire calls for repeatable lists
|
|
|
|
stack = !options.once && [],
|
2011-09-12 15:37:41 +00:00
|
|
|
// Fire callbacks
|
2012-04-25 13:50:26 +00:00
|
|
|
fire = function( data ) {
|
2012-04-28 20:54:13 +00:00
|
|
|
memory = options.memory && data;
|
2012-01-31 01:31:22 +00:00
|
|
|
fired = true;
|
2011-09-12 15:37:41 +00:00
|
|
|
firingIndex = firingStart || 0;
|
|
|
|
firingStart = 0;
|
|
|
|
firingLength = list.length;
|
2012-04-25 13:50:26 +00:00
|
|
|
firing = true;
|
2011-09-12 15:37:41 +00:00
|
|
|
for ( ; list && firingIndex < firingLength; firingIndex++ ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) {
|
2012-04-28 20:54:13 +00:00
|
|
|
memory = false; // To prevent further calls using add
|
2011-09-12 15:37:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firing = false;
|
|
|
|
if ( list ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
if ( stack ) {
|
|
|
|
if ( stack.length ) {
|
|
|
|
fire( stack.shift() );
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
2012-04-28 20:54:13 +00:00
|
|
|
} else if ( memory ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
list = [];
|
2012-04-28 20:54:13 +00:00
|
|
|
} else {
|
|
|
|
self.disable();
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Actual Callbacks object
|
|
|
|
self = {
|
|
|
|
// Add a callback or a collection of callbacks to the list
|
|
|
|
add: function() {
|
|
|
|
if ( list ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
// First, we save the current length
|
|
|
|
var start = list.length;
|
|
|
|
(function add( args ) {
|
2012-04-25 15:15:48 +00:00
|
|
|
jQuery.each( args, function( _, arg ) {
|
2012-08-16 17:12:59 +00:00
|
|
|
var type = jQuery.type( arg );
|
2012-10-06 20:00:43 +00:00
|
|
|
if ( type === "function" ) {
|
|
|
|
if ( !options.unique || !self.has( arg ) ) {
|
|
|
|
list.push( arg );
|
|
|
|
}
|
2012-08-16 17:12:59 +00:00
|
|
|
} else if ( arg && arg.length && type !== "string" ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
// Inspect recursively
|
|
|
|
add( arg );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})( arguments );
|
2011-09-12 15:37:41 +00:00
|
|
|
// Do we need to add the callbacks to the
|
|
|
|
// current firing batch?
|
|
|
|
if ( firing ) {
|
2011-09-21 15:00:55 +00:00
|
|
|
firingLength = list.length;
|
2011-09-12 15:37:41 +00:00
|
|
|
// With memory, if we're not firing then
|
2012-04-28 20:54:13 +00:00
|
|
|
// we should call right away
|
|
|
|
} else if ( memory ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
firingStart = start;
|
|
|
|
fire( memory );
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Remove a callback from the list
|
|
|
|
remove: function() {
|
|
|
|
if ( list ) {
|
2012-04-25 15:15:48 +00:00
|
|
|
jQuery.each( arguments, function( _, arg ) {
|
2012-04-25 16:18:44 +00:00
|
|
|
var index;
|
2012-04-25 15:15:48 +00:00
|
|
|
while( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
list.splice( index, 1 );
|
|
|
|
// Handle firing indexes
|
|
|
|
if ( firing ) {
|
|
|
|
if ( index <= firingLength ) {
|
|
|
|
firingLength--;
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
2012-04-25 13:50:26 +00:00
|
|
|
if ( index <= firingIndex ) {
|
|
|
|
firingIndex--;
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 13:50:26 +00:00
|
|
|
});
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2013-01-03 22:05:00 +00:00
|
|
|
// Check if a given callback is in the list.
|
|
|
|
// If no argument is given, return whether or not list has callbacks attached.
|
2011-09-12 15:37:41 +00:00
|
|
|
has: function( fn ) {
|
2013-01-03 22:05:00 +00:00
|
|
|
return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length );
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
|
|
|
// Remove all callbacks from the list
|
|
|
|
empty: function() {
|
|
|
|
list = [];
|
2013-02-27 15:12:16 +00:00
|
|
|
firingLength = 0;
|
2011-09-12 15:37:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Have the list do nothing anymore
|
|
|
|
disable: function() {
|
|
|
|
list = stack = memory = undefined;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Is it disabled?
|
|
|
|
disabled: function() {
|
|
|
|
return !list;
|
|
|
|
},
|
|
|
|
// Lock the list in its current state
|
|
|
|
lock: function() {
|
|
|
|
stack = undefined;
|
2012-04-28 20:54:13 +00:00
|
|
|
if ( !memory ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
self.disable();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Is it locked?
|
|
|
|
locked: function() {
|
|
|
|
return !stack;
|
|
|
|
},
|
|
|
|
// Call all callbacks with the given context and arguments
|
|
|
|
fireWith: function( context, args ) {
|
2012-04-25 16:18:44 +00:00
|
|
|
args = args || [];
|
|
|
|
args = [ context, args.slice ? args.slice() : args ];
|
2012-04-25 13:50:26 +00:00
|
|
|
if ( list && ( !fired || stack ) ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
if ( firing ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
stack.push( args );
|
|
|
|
} else {
|
|
|
|
fire( args );
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Call all the callbacks with the given arguments
|
|
|
|
fire: function() {
|
|
|
|
self.fireWith( this, arguments );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// To know if the callbacks have already been called at least once
|
|
|
|
fired: function() {
|
2012-01-31 01:31:22 +00:00
|
|
|
return !!fired;
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|