Blame view

bower_components/jquery/src/callbacks.js 5.33 KB
a1a3bc73   Luigi Serra   graphs updates
1
  define( [
74249687   Luigi Serra   Cross browser con...
2
3
4
5
  	"./core",
  	"./var/rnotwhite"
  ], function( jQuery, rnotwhite ) {
  
a1a3bc73   Luigi Serra   graphs updates
6
  // Convert String-formatted options into Object-formatted ones
74249687   Luigi Serra   Cross browser con...
7
  function createOptions( options ) {
a1a3bc73   Luigi Serra   graphs updates
8
  	var object = {};
74249687   Luigi Serra   Cross browser con...
9
10
  	jQuery.each( options.match( rnotwhite ) || [], function( _, flag ) {
  		object[ flag ] = true;
a1a3bc73   Luigi Serra   graphs updates
11
  	} );
74249687   Luigi Serra   Cross browser con...
12
13
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
  	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" ?
a1a3bc73   Luigi Serra   graphs updates
42
  		createOptions( options ) :
74249687   Luigi Serra   Cross browser con...
43
44
  		jQuery.extend( {}, options );
  
a1a3bc73   Luigi Serra   graphs updates
45
46
47
48
  	var // Flag to know if list is currently firing
  		firing,
  
  		// Last fire value for non-forgettable lists
74249687   Luigi Serra   Cross browser con...
49
  		memory,
a1a3bc73   Luigi Serra   graphs updates
50
  
74249687   Luigi Serra   Cross browser con...
51
52
  		// Flag to know if list was already fired
  		fired,
a1a3bc73   Luigi Serra   graphs updates
53
54
55
56
  
  		// Flag to prevent firing
  		locked,
  
74249687   Luigi Serra   Cross browser con...
57
58
  		// Actual callback list
  		list = [],
a1a3bc73   Luigi Serra   graphs updates
59
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,
  
74249687   Luigi Serra   Cross browser con...
66
  		// Fire callbacks
a1a3bc73   Luigi Serra   graphs updates
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;
  					}
74249687   Luigi Serra   Cross browser con...
87
88
  				}
  			}
a1a3bc73   Luigi Serra   graphs updates
89
90
91
92
93
94
  
  			// Forget the data if we're done with it
  			if ( !options.memory ) {
  				memory = false;
  			}
  
74249687   Luigi Serra   Cross browser con...
95
  			firing = false;
a1a3bc73   Luigi Serra   graphs updates
96
97
98
99
100
101
  
  			// 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 ) {
74249687   Luigi Serra   Cross browser con...
102
  					list = [];
a1a3bc73   Luigi Serra   graphs updates
103
104
  
  				// Otherwise, this object is spent
74249687   Luigi Serra   Cross browser con...
105
  				} else {
a1a3bc73   Luigi Serra   graphs updates
106
  					list = "";
74249687   Luigi Serra   Cross browser con...
107
108
109
  				}
  			}
  		},
a1a3bc73   Luigi Serra   graphs updates
110
  
74249687   Luigi Serra   Cross browser con...
111
112
  		// Actual Callbacks object
  		self = {
a1a3bc73   Luigi Serra   graphs updates
113
  
74249687   Luigi Serra   Cross browser con...
114
115
116
  			// Add a callback or a collection of callbacks to the list
  			add: function() {
  				if ( list ) {
a1a3bc73   Luigi Serra   graphs updates
117
118
119
120
121
122
123
124
  
  					// 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 ) {
74249687   Luigi Serra   Cross browser con...
125
  						jQuery.each( args, function( _, arg ) {
a1a3bc73   Luigi Serra   graphs updates
126
  							if ( jQuery.isFunction( arg ) ) {
74249687   Luigi Serra   Cross browser con...
127
128
129
  								if ( !options.unique || !self.has( arg ) ) {
  									list.push( arg );
  								}
a1a3bc73   Luigi Serra   graphs updates
130
131
  							} else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
  
74249687   Luigi Serra   Cross browser con...
132
133
134
  								// Inspect recursively
  								add( arg );
  							}
a1a3bc73   Luigi Serra   graphs updates
135
136
137
138
139
  						} );
  					} )( arguments );
  
  					if ( memory && !firing ) {
  						fire();
74249687   Luigi Serra   Cross browser con...
140
141
142
143
  					}
  				}
  				return this;
  			},
a1a3bc73   Luigi Serra   graphs updates
144
  
74249687   Luigi Serra   Cross browser con...
145
146
  			// Remove a callback from the list
  			remove: function() {
a1a3bc73   Luigi Serra   graphs updates
147
148
149
150
151
152
153
154
  				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--;
74249687   Luigi Serra   Cross browser con...
155
  						}
a1a3bc73   Luigi Serra   graphs updates
156
157
  					}
  				} );
74249687   Luigi Serra   Cross browser con...
158
159
  				return this;
  			},
a1a3bc73   Luigi Serra   graphs updates
160
  
74249687   Luigi Serra   Cross browser con...
161
162
163
  			// 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 ) {
a1a3bc73   Luigi Serra   graphs updates
164
165
166
  				return fn ?
  					jQuery.inArray( fn, list ) > -1 :
  					list.length > 0;
74249687   Luigi Serra   Cross browser con...
167
  			},
a1a3bc73   Luigi Serra   graphs updates
168
  
74249687   Luigi Serra   Cross browser con...
169
170
  			// Remove all callbacks from the list
  			empty: function() {
a1a3bc73   Luigi Serra   graphs updates
171
172
173
  				if ( list ) {
  					list = [];
  				}
74249687   Luigi Serra   Cross browser con...
174
175
  				return this;
  			},
a1a3bc73   Luigi Serra   graphs updates
176
177
178
179
  
  			// Disable .fire and .add
  			// Abort any current/pending executions
  			// Clear all callbacks and values
74249687   Luigi Serra   Cross browser con...
180
  			disable: function() {
a1a3bc73   Luigi Serra   graphs updates
181
182
  				locked = queue = [];
  				list = memory = "";
74249687   Luigi Serra   Cross browser con...
183
184
  				return this;
  			},
74249687   Luigi Serra   Cross browser con...
185
186
187
  			disabled: function() {
  				return !list;
  			},
a1a3bc73   Luigi Serra   graphs updates
188
189
190
191
  
  			// Disable .fire
  			// Also disable .add unless we have memory (since it would have no effect)
  			// Abort any pending executions
74249687   Luigi Serra   Cross browser con...
192
  			lock: function() {
a1a3bc73   Luigi Serra   graphs updates
193
  				locked = queue = [];
74249687   Luigi Serra   Cross browser con...
194
  				if ( !memory ) {
a1a3bc73   Luigi Serra   graphs updates
195
  					list = memory = "";
74249687   Luigi Serra   Cross browser con...
196
197
198
  				}
  				return this;
  			},
74249687   Luigi Serra   Cross browser con...
199
  			locked: function() {
a1a3bc73   Luigi Serra   graphs updates
200
  				return !!locked;
74249687   Luigi Serra   Cross browser con...
201
  			},
a1a3bc73   Luigi Serra   graphs updates
202
  
74249687   Luigi Serra   Cross browser con...
203
204
  			// Call all callbacks with the given context and arguments
  			fireWith: function( context, args ) {
a1a3bc73   Luigi Serra   graphs updates
205
  				if ( !locked ) {
74249687   Luigi Serra   Cross browser con...
206
207
  					args = args || [];
  					args = [ context, args.slice ? args.slice() : args ];
a1a3bc73   Luigi Serra   graphs updates
208
209
210
  					queue.push( args );
  					if ( !firing ) {
  						fire();
74249687   Luigi Serra   Cross browser con...
211
212
213
214
  					}
  				}
  				return this;
  			},
a1a3bc73   Luigi Serra   graphs updates
215
  
74249687   Luigi Serra   Cross browser con...
216
217
218
219
220
  			// Call all the callbacks with the given arguments
  			fire: function() {
  				self.fireWith( this, arguments );
  				return this;
  			},
a1a3bc73   Luigi Serra   graphs updates
221
  
74249687   Luigi Serra   Cross browser con...
222
223
224
225
226
227
228
229
230
231
  			// To know if the callbacks have already been called at least once
  			fired: function() {
  				return !!fired;
  			}
  		};
  
  	return self;
  };
  
  return jQuery;
a1a3bc73   Luigi Serra   graphs updates
232
  } );