2013-08-15 18:15:49 +00:00
|
|
|
define([
|
|
|
|
"./core",
|
|
|
|
"./var/rnotwhite"
|
|
|
|
], function( jQuery, rnotwhite ) {
|
|
|
|
|
2015-01-04 01:48:54 +00:00
|
|
|
// Convert String-formatted options into Object-formatted ones
|
2012-04-25 00:16:37 +00:00
|
|
|
function createOptions( options ) {
|
2015-01-04 01:48:54 +00:00
|
|
|
var object = {};
|
2013-08-15 18:15:49 +00:00
|
|
|
jQuery.each( options.match( 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" ?
|
2015-01-04 01:48:54 +00:00
|
|
|
createOptions( options ) :
|
2012-04-25 00:16:37 +00:00
|
|
|
jQuery.extend( {}, options );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2013-01-25 02:26:16 +00:00
|
|
|
var // Flag to know if list is currently firing
|
|
|
|
firing,
|
2015-01-04 01:48:54 +00:00
|
|
|
// 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,
|
2015-01-04 01:48:54 +00:00
|
|
|
// Flag to prevent firing
|
2015-01-02 14:42:06 +00:00
|
|
|
locked,
|
2012-07-10 01:38:11 +00:00
|
|
|
// Actual callback list
|
|
|
|
list = [],
|
2015-01-04 01:48:54 +00:00
|
|
|
// Queue of execution data for repeatable lists
|
|
|
|
queue = [],
|
|
|
|
// Index of currently firing callback (modified by add/remove as needed)
|
|
|
|
firingIndex = -1,
|
2011-09-12 15:37:41 +00:00
|
|
|
// Fire callbacks
|
2015-01-04 01:48:54 +00:00
|
|
|
fire = function() {
|
|
|
|
|
|
|
|
// Enforce single-firing
|
2015-01-02 14:42:06 +00:00
|
|
|
locked = options.once;
|
2015-01-04 01:48:54 +00:00
|
|
|
|
|
|
|
// Execute callbacks for all pending executions,
|
|
|
|
// respecting firingIndex overrides and runtime changes
|
|
|
|
fired = firing = true;
|
|
|
|
for ( ; queue.length; firingIndex = -1 ) {
|
|
|
|
memory = queue.shift();
|
|
|
|
while ( ++firingIndex < list.length ) {
|
|
|
|
|
|
|
|
// Run callback and check for early termination
|
|
|
|
if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false &&
|
|
|
|
options.stopOnFalse ) {
|
|
|
|
|
|
|
|
// Jump to end and forget the data so .add doesn't re-fire
|
|
|
|
firingIndex = list.length;
|
|
|
|
memory = false;
|
|
|
|
}
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2015-01-04 01:48:54 +00:00
|
|
|
// Forget the data if we're done with it
|
|
|
|
if ( !options.memory ) {
|
|
|
|
memory = false;
|
|
|
|
}
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2015-01-04 01:48:54 +00:00
|
|
|
firing = false;
|
|
|
|
|
|
|
|
// Clean up if we're done firing for good
|
|
|
|
if ( locked ) {
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2015-01-04 01:48:54 +00:00
|
|
|
// Keep an empty list if we have data for future add calls
|
|
|
|
if ( memory ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
list = [];
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2015-01-04 01:48:54 +00:00
|
|
|
// Otherwise, this object is spent
|
2012-04-28 20:54:13 +00:00
|
|
|
} else {
|
2015-01-04 01:48:54 +00:00
|
|
|
list = "";
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-01-04 01:48:54 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Actual Callbacks object
|
|
|
|
self = {
|
2015-01-04 01:48:54 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Add a callback or a collection of callbacks to the list
|
|
|
|
add: function() {
|
|
|
|
if ( list ) {
|
2015-01-04 01:48:54 +00:00
|
|
|
|
|
|
|
// If we have memory from a past run, we should fire after adding
|
|
|
|
if ( memory && !firing ) {
|
|
|
|
firingIndex = list.length - 1;
|
|
|
|
queue.push( memory );
|
|
|
|
}
|
|
|
|
|
2012-04-25 13:50:26 +00:00
|
|
|
(function add( args ) {
|
2012-04-25 15:15:48 +00:00
|
|
|
jQuery.each( args, function( _, arg ) {
|
2015-01-04 01:48:54 +00:00
|
|
|
if ( jQuery.isFunction( arg ) ) {
|
2012-10-06 20:00:43 +00:00
|
|
|
if ( !options.unique || !self.has( arg ) ) {
|
|
|
|
list.push( arg );
|
|
|
|
}
|
2015-01-04 01:48:54 +00:00
|
|
|
} else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
// Inspect recursively
|
|
|
|
add( arg );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})( arguments );
|
2015-01-04 01:48:54 +00:00
|
|
|
|
|
|
|
if ( memory && !firing ) {
|
|
|
|
fire();
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Remove a callback from the list
|
|
|
|
remove: function() {
|
2015-01-04 01:48:54 +00:00
|
|
|
jQuery.each( arguments, function( _, arg ) {
|
|
|
|
var index;
|
|
|
|
while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
|
|
|
|
list.splice( index, 1 );
|
|
|
|
|
|
|
|
// Handle firing indexes
|
|
|
|
if ( index <= firingIndex ) {
|
|
|
|
firingIndex--;
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
2015-01-04 01:48:54 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-12 15:37:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
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 ) {
|
2015-01-04 01:48:54 +00:00
|
|
|
return fn ?
|
|
|
|
jQuery.inArray( fn, list ) > -1 :
|
|
|
|
list.length > 0;
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Remove all callbacks from the list
|
|
|
|
empty: function() {
|
2014-12-08 01:51:04 +00:00
|
|
|
if ( list ) {
|
|
|
|
list = [];
|
|
|
|
}
|
2011-09-12 15:37:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
|
|
|
// Disable .fire and .add
|
|
|
|
// Abort any current/pending executions
|
|
|
|
// Clear all callbacks and values
|
2011-09-12 15:37:41 +00:00
|
|
|
disable: function() {
|
2015-01-04 01:48:54 +00:00
|
|
|
locked = queue = [];
|
|
|
|
list = memory = "";
|
2011-09-12 15:37:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
disabled: function() {
|
|
|
|
return !list;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
|
|
|
// Disable .fire
|
|
|
|
// Also disable .add unless we have memory (since it would have no effect)
|
|
|
|
// Abort any pending executions
|
2011-09-12 15:37:41 +00:00
|
|
|
lock: function() {
|
2015-01-04 01:48:54 +00:00
|
|
|
locked = queue = [];
|
2015-01-04 00:55:31 +00:00
|
|
|
if ( !memory && !firing ) {
|
2015-01-04 01:48:54 +00:00
|
|
|
list = memory = "";
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
locked: function() {
|
2015-01-02 14:42:06 +00:00
|
|
|
return !!locked;
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Call all callbacks with the given context and arguments
|
|
|
|
fireWith: function( context, args ) {
|
2015-01-02 14:42:06 +00:00
|
|
|
if ( !locked ) {
|
2013-06-19 21:36:32 +00:00
|
|
|
args = args || [];
|
|
|
|
args = [ context, args.slice ? args.slice() : args ];
|
2015-01-04 01:48:54 +00:00
|
|
|
queue.push( args );
|
|
|
|
if ( !firing ) {
|
|
|
|
fire();
|
2011-09-12 15:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// Call all the callbacks with the given arguments
|
|
|
|
fire: function() {
|
|
|
|
self.fireWith( this, arguments );
|
|
|
|
return this;
|
|
|
|
},
|
2015-01-02 14:42:06 +00:00
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
// 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;
|
|
|
|
};
|
2013-08-15 18:15:49 +00:00
|
|
|
|
2013-09-09 01:25:27 +00:00
|
|
|
return jQuery;
|
2013-08-15 18:15:49 +00:00
|
|
|
});
|