Blame view
app/bower_components/jquery/src/callbacks.js
5.37 KB
|
87c93a029
|
1 |
define( [ |
|
f986e111b
|
2 |
"./core", |
|
87c93a029
|
3 4 |
"./var/rnothtmlwhite"
], function( jQuery, rnothtmlwhite ) {
|
|
f986e111b
|
5 |
|
|
87c93a029
|
6 |
"use strict"; |
|
f986e111b
|
7 |
|
|
87c93a029
|
8 |
// Convert String-formatted options into Object-formatted ones |
|
f986e111b
|
9 |
function createOptions( options ) {
|
|
87c93a029
|
10 11 |
var object = {};
jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
|
|
f986e111b
|
12 |
object[ flag ] = true; |
|
87c93a029
|
13 |
} ); |
|
f986e111b
|
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
return object;
}
/*
* Create a callback list using the following parameters:
*
* options: an optional list of space-separated options that will change how
* the callback list behaves or a more traditional option object
*
* By default a callback list will act like an event callback list and can be
* "fired" multiple times.
*
* Possible options:
*
* 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
*
*/
jQuery.Callbacks = function( options ) {
// Convert options from String-formatted to Object-formatted if needed
// (we check in cache first)
options = typeof options === "string" ?
|
|
87c93a029
|
44 |
createOptions( options ) : |
|
f986e111b
|
45 46 47 48 |
jQuery.extend( {}, options );
var // Flag to know if list is currently firing
firing,
|
|
87c93a029
|
49 50 |
// Last fire value for non-forgettable lists |
|
f986e111b
|
51 |
memory, |
|
87c93a029
|
52 |
|
|
f986e111b
|
53 54 |
// Flag to know if list was already fired fired, |
|
87c93a029
|
55 56 57 |
// Flag to prevent firing locked, |
|
f986e111b
|
58 59 |
// Actual callback list list = [], |
|
87c93a029
|
60 61 62 63 64 65 |
// Queue of execution data for repeatable lists queue = [], // Index of currently firing callback (modified by add/remove as needed) firingIndex = -1, |
|
f986e111b
|
66 |
// Fire callbacks |
|
87c93a029
|
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
fire = function() {
// Enforce single-firing
locked = options.once;
// 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;
}
|
|
f986e111b
|
87 88 |
} } |
|
87c93a029
|
89 90 91 92 93 |
// Forget the data if we're done with it
if ( !options.memory ) {
memory = false;
}
|
|
f986e111b
|
94 |
firing = false; |
|
87c93a029
|
95 96 97 98 99 100 |
// Clean up if we're done firing for good
if ( locked ) {
// Keep an empty list if we have data for future add calls
if ( memory ) {
|
|
f986e111b
|
101 |
list = []; |
|
87c93a029
|
102 103 |
// Otherwise, this object is spent |
|
f986e111b
|
104 |
} else {
|
|
87c93a029
|
105 |
list = ""; |
|
f986e111b
|
106 107 108 |
} } }, |
|
87c93a029
|
109 |
|
|
f986e111b
|
110 111 |
// Actual Callbacks object
self = {
|
|
87c93a029
|
112 |
|
|
f986e111b
|
113 114 115 |
// Add a callback or a collection of callbacks to the list
add: function() {
if ( list ) {
|
|
87c93a029
|
116 117 118 119 120 121 122 123 |
// If we have memory from a past run, we should fire after adding
if ( memory && !firing ) {
firingIndex = list.length - 1;
queue.push( memory );
}
( function add( args ) {
|
|
f986e111b
|
124 |
jQuery.each( args, function( _, arg ) {
|
|
87c93a029
|
125 |
if ( jQuery.isFunction( arg ) ) {
|
|
f986e111b
|
126 127 128 |
if ( !options.unique || !self.has( arg ) ) {
list.push( arg );
}
|
|
87c93a029
|
129 |
} else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
|
|
f986e111b
|
130 131 132 |
// Inspect recursively add( arg ); } |
|
87c93a029
|
133 134 135 136 137 |
} );
} )( arguments );
if ( memory && !firing ) {
fire();
|
|
f986e111b
|
138 139 140 141 |
} } return this; }, |
|
87c93a029
|
142 |
|
|
f986e111b
|
143 144 |
// Remove a callback from the list
remove: function() {
|
|
87c93a029
|
145 146 147 148 149 150 151 152 |
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--;
|
|
f986e111b
|
153 |
} |
|
87c93a029
|
154 155 |
} } ); |
|
f986e111b
|
156 157 |
return this; }, |
|
87c93a029
|
158 |
|
|
f986e111b
|
159 160 161 |
// Check if a given callback is in the list.
// If no argument is given, return whether or not list has callbacks attached.
has: function( fn ) {
|
|
87c93a029
|
162 163 164 |
return fn ? jQuery.inArray( fn, list ) > -1 : list.length > 0; |
|
f986e111b
|
165 |
}, |
|
87c93a029
|
166 |
|
|
f986e111b
|
167 168 |
// Remove all callbacks from the list
empty: function() {
|
|
87c93a029
|
169 170 171 |
if ( list ) {
list = [];
}
|
|
f986e111b
|
172 173 |
return this; }, |
|
87c93a029
|
174 175 176 177 |
// Disable .fire and .add // Abort any current/pending executions // Clear all callbacks and values |
|
f986e111b
|
178 |
disable: function() {
|
|
87c93a029
|
179 180 |
locked = queue = []; list = memory = ""; |
|
f986e111b
|
181 182 |
return this; }, |
|
f986e111b
|
183 184 185 |
disabled: function() {
return !list;
},
|
|
87c93a029
|
186 187 188 189 |
// Disable .fire // Also disable .add unless we have memory (since it would have no effect) // Abort any pending executions |
|
f986e111b
|
190 |
lock: function() {
|
|
87c93a029
|
191 192 193 |
locked = queue = [];
if ( !memory && !firing ) {
list = memory = "";
|
|
f986e111b
|
194 195 196 |
} return this; }, |
|
f986e111b
|
197 |
locked: function() {
|
|
87c93a029
|
198 |
return !!locked; |
|
f986e111b
|
199 |
}, |
|
87c93a029
|
200 |
|
|
f986e111b
|
201 202 |
// Call all callbacks with the given context and arguments
fireWith: function( context, args ) {
|
|
87c93a029
|
203 |
if ( !locked ) {
|
|
f986e111b
|
204 205 |
args = args || []; args = [ context, args.slice ? args.slice() : args ]; |
|
87c93a029
|
206 207 208 |
queue.push( args );
if ( !firing ) {
fire();
|
|
f986e111b
|
209 210 211 212 |
} } return this; }, |
|
87c93a029
|
213 |
|
|
f986e111b
|
214 215 216 217 218 |
// Call all the callbacks with the given arguments
fire: function() {
self.fireWith( this, arguments );
return this;
},
|
|
87c93a029
|
219 |
|
|
f986e111b
|
220 221 222 223 224 225 226 227 228 229 |
// To know if the callbacks have already been called at least once
fired: function() {
return !!fired;
}
};
return self;
};
return jQuery;
|
|
87c93a029
|
230 |
} ); |