2011-05-06 18:35:08 +02:00
|
|
|
(function( jQuery ) {
|
|
|
|
|
|
|
|
// String to Object flags format cache
|
|
|
|
var flagsCache = {};
|
|
|
|
|
|
|
|
// Convert String-formatted flags into Object-formatted ones and store in cache
|
|
|
|
function createFlags( flags ) {
|
|
|
|
var object = flagsCache[ flags ] = {},
|
|
|
|
i, length;
|
|
|
|
flags = flags.split( /\s+/ );
|
|
|
|
for ( i = 0, length = flags.length; i < length; i++ ) {
|
|
|
|
object[ flags[i] ] = true;
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a callback list using the following parameters:
|
|
|
|
*
|
|
|
|
* flags: an optional list of space-separated flags that will change how
|
|
|
|
* the callback list behaves
|
|
|
|
*
|
|
|
|
* filter: an optional function that will be applied to each added callbacks,
|
|
|
|
* what filter returns will then be added provided it is not falsy.
|
|
|
|
*
|
|
|
|
* By default a callback list will act like an event callback list and can be
|
|
|
|
* "fired" multiple times.
|
|
|
|
*
|
|
|
|
* Possible flags:
|
|
|
|
*
|
2011-05-07 00:23:00 +02:00
|
|
|
* once: will ensure the callback list can only be fired once (like a Deferred)
|
2011-05-06 18:35:08 +02:00
|
|
|
*
|
2011-05-07 00:23:00 +02:00
|
|
|
* 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)
|
2011-05-06 18:35:08 +02:00
|
|
|
*
|
2011-05-07 00:23:00 +02:00
|
|
|
* unique: will ensure a callback can only be added once (no duplicate in the list)
|
2011-05-06 18:35:08 +02:00
|
|
|
*
|
2011-05-07 00:23:00 +02:00
|
|
|
* relocate: like "unique" but will relocate the callback at the end of the list
|
|
|
|
*
|
|
|
|
* stopOnFalse: interrupt callings when a callback returns false
|
2011-05-06 18:35:08 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
jQuery.Callbacks = function( flags, filter ) {
|
|
|
|
|
|
|
|
// flags are optional
|
|
|
|
if ( typeof flags !== "string" ) {
|
|
|
|
filter = flags;
|
|
|
|
flags = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert flags from String-formatted to Object-formatted
|
|
|
|
// (we check in cache first)
|
|
|
|
flags = flags ? ( flagsCache[ flags ] || createFlags( flags ) ) : {};
|
|
|
|
|
|
|
|
var // Actual callback list
|
|
|
|
list = [],
|
|
|
|
// Stack of fire calls for repeatable lists
|
2011-05-07 03:39:41 +02:00
|
|
|
stack = [],
|
2011-05-06 18:35:08 +02:00
|
|
|
// Last fire value (for non-forgettable lists)
|
|
|
|
memory,
|
|
|
|
// Flag to know if list is currently firing
|
|
|
|
firing,
|
2011-05-08 16:11:00 +02:00
|
|
|
// First callback to fire (used internally by add and fireWith)
|
|
|
|
firingStart,
|
|
|
|
// Index of currently firing callback (modified by remove if needed)
|
|
|
|
firingIndex,
|
2011-05-06 18:35:08 +02:00
|
|
|
// Add a list of callbacks to the list
|
|
|
|
add = function( args ) {
|
|
|
|
var i,
|
|
|
|
length,
|
|
|
|
elem,
|
|
|
|
type,
|
|
|
|
actual;
|
|
|
|
for ( i = 0, length = args.length; i < length; i++ ) {
|
|
|
|
elem = args[ i ];
|
|
|
|
type = jQuery.type( elem );
|
|
|
|
if ( type === "array" ) {
|
|
|
|
// Inspect recursively
|
|
|
|
add( elem );
|
|
|
|
} else if ( type === "function" ) {
|
|
|
|
// If we have to relocate, we remove the callback
|
|
|
|
// if it already exists
|
|
|
|
if ( flags.relocate ) {
|
|
|
|
object.remove( elem );
|
2011-05-08 18:38:00 +02:00
|
|
|
// Skip if we're in unique mode and callback is already in
|
2011-05-07 03:39:41 +02:00
|
|
|
} else if ( flags.unique && object.has( elem ) ) {
|
|
|
|
continue;
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
2011-05-07 03:39:41 +02:00
|
|
|
// Get the filtered function if needs be
|
|
|
|
actual = filter ? filter( elem ) : elem;
|
|
|
|
if ( actual ) {
|
|
|
|
list.push( [ elem, actual ] );
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
object = {
|
|
|
|
// Add a callback or a collection of callbacks to the list
|
|
|
|
add: function() {
|
|
|
|
if ( list ) {
|
2011-05-07 03:39:41 +02:00
|
|
|
var length = list.length;
|
2011-05-06 18:35:08 +02:00
|
|
|
add( arguments );
|
2011-05-07 03:39:41 +02:00
|
|
|
// With memory, if we're not firing then
|
|
|
|
// we should call right away
|
2011-05-06 18:35:08 +02:00
|
|
|
if ( !firing && flags.memory && memory ) {
|
|
|
|
var tmp = memory;
|
|
|
|
memory = undefined;
|
2011-05-08 16:11:00 +02:00
|
|
|
firingStart = length;
|
|
|
|
object.fireWith( tmp[ 0 ], tmp[ 1 ] );
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Remove a callback from the list
|
|
|
|
remove: function( fn ) {
|
|
|
|
if ( list ) {
|
2011-05-08 16:11:00 +02:00
|
|
|
for ( var i = 0; i < list.length; i++ ) {
|
|
|
|
if ( fn === list[ i ][ 0 ] ) {
|
|
|
|
// Handle firingIndex
|
|
|
|
if ( firing && i <= firingIndex ) {
|
|
|
|
firingIndex--;
|
2011-05-07 03:39:41 +02:00
|
|
|
}
|
2011-05-08 16:11:00 +02:00
|
|
|
// Remove the element
|
|
|
|
list.splice( i--, 1 );
|
2011-05-06 18:35:08 +02:00
|
|
|
// If we have some unicity property then
|
|
|
|
// we only need to do this once
|
|
|
|
if ( flags.unique || flags.relocate ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Control if a given callback is in the list
|
|
|
|
has: function( fn ) {
|
|
|
|
if ( list ) {
|
|
|
|
var i = 0,
|
|
|
|
length = list.length;
|
|
|
|
for ( ; i < length; i++ ) {
|
2011-05-08 16:11:00 +02:00
|
|
|
if ( fn === list[ i ][ 0 ] ) {
|
2011-05-06 18:35:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
// Remove all callbacks from the list
|
|
|
|
empty: function() {
|
|
|
|
list = [];
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Have the list do nothing anymore
|
|
|
|
disable: function() {
|
|
|
|
list = stack = memory = undefined;
|
|
|
|
return this;
|
|
|
|
},
|
2011-05-08 18:38:00 +02:00
|
|
|
// Is it disabled?
|
|
|
|
disabled: function() {
|
|
|
|
return !list;
|
|
|
|
},
|
2011-05-06 18:35:08 +02:00
|
|
|
// Lock the list in its current state
|
|
|
|
lock: function() {
|
|
|
|
stack = undefined;
|
|
|
|
if ( !memory ) {
|
|
|
|
object.disable();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2011-05-08 18:38:00 +02:00
|
|
|
// Is it locked?
|
|
|
|
locked: function() {
|
|
|
|
return !stack;
|
|
|
|
},
|
2011-05-06 18:35:08 +02:00
|
|
|
// Call all callbacks with the given context and arguments
|
2011-05-08 16:11:00 +02:00
|
|
|
fireWith: function( context, args ) {
|
2011-05-08 18:38:00 +02:00
|
|
|
if ( list ) {
|
2011-05-06 18:35:08 +02:00
|
|
|
if ( firing ) {
|
2011-05-08 18:38:00 +02:00
|
|
|
if ( !flags.once ) {
|
|
|
|
stack.push( [ context, args ] );
|
|
|
|
}
|
|
|
|
} else if ( !( flags.once && memory ) ) {
|
2011-05-06 18:35:08 +02:00
|
|
|
args = args || [];
|
2011-05-08 18:38:00 +02:00
|
|
|
memory = [ context, args ];
|
2011-05-06 18:35:08 +02:00
|
|
|
firing = true;
|
2011-05-08 18:38:00 +02:00
|
|
|
firingIndex = firingStart || 0;
|
|
|
|
firingStart = 0;
|
|
|
|
for ( ; list && firingIndex < list.length; firingIndex++ ) {
|
2011-05-08 16:11:00 +02:00
|
|
|
if ( list[ firingIndex ][ 1 ].apply( context, args ) === false && flags.stopOnFalse ) {
|
|
|
|
break;
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
2011-05-08 16:11:00 +02:00
|
|
|
}
|
|
|
|
firing = false;
|
|
|
|
if ( list ) {
|
|
|
|
if ( !flags.once ) {
|
|
|
|
if ( stack && stack.length ) {
|
2011-05-08 18:38:00 +02:00
|
|
|
memory = stack.shift();
|
|
|
|
object.fireWith( memory[ 0 ], memory[ 1 ] );
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
2011-05-08 16:11:00 +02:00
|
|
|
} else if ( !flags.memory ) {
|
|
|
|
object.disable();
|
|
|
|
} else {
|
|
|
|
list = [];
|
2011-05-06 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// Call all the callbacks with the given arguments
|
|
|
|
fire: function() {
|
|
|
|
object.fireWith( this, arguments );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
// To know if the callbacks have already been called at least once
|
|
|
|
fired: function() {
|
|
|
|
return !!memory;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
|
|
|
})( jQuery );
|