Blame view

app/bower_components/jquery/src/callbacks.js 5.37 KB
87c93a029   Dang YoungWorld   add modal
1
  define( [
f986e111b   TRUONG   add libs
2
  	"./core",
87c93a029   Dang YoungWorld   add modal
3
4
  	"./var/rnothtmlwhite"
  ], function( jQuery, rnothtmlwhite ) {
f986e111b   TRUONG   add libs
5

87c93a029   Dang YoungWorld   add modal
6
  "use strict";
f986e111b   TRUONG   add libs
7

87c93a029   Dang YoungWorld   add modal
8
  // Convert String-formatted options into Object-formatted ones
f986e111b   TRUONG   add libs
9
  function createOptions( options ) {
87c93a029   Dang YoungWorld   add modal
10
11
  	var object = {};
  	jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
f986e111b   TRUONG   add libs
12
  		object[ flag ] = true;
87c93a029   Dang YoungWorld   add modal
13
  	} );
f986e111b   TRUONG   add libs
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   Dang YoungWorld   add modal
44
  		createOptions( options ) :
f986e111b   TRUONG   add libs
45
46
47
48
  		jQuery.extend( {}, options );
  
  	var // Flag to know if list is currently firing
  		firing,
87c93a029   Dang YoungWorld   add modal
49
50
  
  		// Last fire value for non-forgettable lists
f986e111b   TRUONG   add libs
51
  		memory,
87c93a029   Dang YoungWorld   add modal
52

f986e111b   TRUONG   add libs
53
54
  		// Flag to know if list was already fired
  		fired,
87c93a029   Dang YoungWorld   add modal
55
56
57
  
  		// Flag to prevent firing
  		locked,
f986e111b   TRUONG   add libs
58
59
  		// Actual callback list
  		list = [],
87c93a029   Dang YoungWorld   add modal
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   TRUONG   add libs
66
  		// Fire callbacks
87c93a029   Dang YoungWorld   add modal
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   TRUONG   add libs
87
88
  				}
  			}
87c93a029   Dang YoungWorld   add modal
89
90
91
92
93
  
  			// Forget the data if we're done with it
  			if ( !options.memory ) {
  				memory = false;
  			}
f986e111b   TRUONG   add libs
94
  			firing = false;
87c93a029   Dang YoungWorld   add modal
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   TRUONG   add libs
101
  					list = [];
87c93a029   Dang YoungWorld   add modal
102
103
  
  				// Otherwise, this object is spent
f986e111b   TRUONG   add libs
104
  				} else {
87c93a029   Dang YoungWorld   add modal
105
  					list = "";
f986e111b   TRUONG   add libs
106
107
108
  				}
  			}
  		},
87c93a029   Dang YoungWorld   add modal
109

f986e111b   TRUONG   add libs
110
111
  		// Actual Callbacks object
  		self = {
87c93a029   Dang YoungWorld   add modal
112

f986e111b   TRUONG   add libs
113
114
115
  			// Add a callback or a collection of callbacks to the list
  			add: function() {
  				if ( list ) {
87c93a029   Dang YoungWorld   add modal
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   TRUONG   add libs
124
  						jQuery.each( args, function( _, arg ) {
87c93a029   Dang YoungWorld   add modal
125
  							if ( jQuery.isFunction( arg ) ) {
f986e111b   TRUONG   add libs
126
127
128
  								if ( !options.unique || !self.has( arg ) ) {
  									list.push( arg );
  								}
87c93a029   Dang YoungWorld   add modal
129
  							} else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
f986e111b   TRUONG   add libs
130
131
132
  								// Inspect recursively
  								add( arg );
  							}
87c93a029   Dang YoungWorld   add modal
133
134
135
136
137
  						} );
  					} )( arguments );
  
  					if ( memory && !firing ) {
  						fire();
f986e111b   TRUONG   add libs
138
139
140
141
  					}
  				}
  				return this;
  			},
87c93a029   Dang YoungWorld   add modal
142

f986e111b   TRUONG   add libs
143
144
  			// Remove a callback from the list
  			remove: function() {
87c93a029   Dang YoungWorld   add modal
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   TRUONG   add libs
153
  						}
87c93a029   Dang YoungWorld   add modal
154
155
  					}
  				} );
f986e111b   TRUONG   add libs
156
157
  				return this;
  			},
87c93a029   Dang YoungWorld   add modal
158

f986e111b   TRUONG   add libs
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   Dang YoungWorld   add modal
162
163
164
  				return fn ?
  					jQuery.inArray( fn, list ) > -1 :
  					list.length > 0;
f986e111b   TRUONG   add libs
165
  			},
87c93a029   Dang YoungWorld   add modal
166

f986e111b   TRUONG   add libs
167
168
  			// Remove all callbacks from the list
  			empty: function() {
87c93a029   Dang YoungWorld   add modal
169
170
171
  				if ( list ) {
  					list = [];
  				}
f986e111b   TRUONG   add libs
172
173
  				return this;
  			},
87c93a029   Dang YoungWorld   add modal
174
175
176
177
  
  			// Disable .fire and .add
  			// Abort any current/pending executions
  			// Clear all callbacks and values
f986e111b   TRUONG   add libs
178
  			disable: function() {
87c93a029   Dang YoungWorld   add modal
179
180
  				locked = queue = [];
  				list = memory = "";
f986e111b   TRUONG   add libs
181
182
  				return this;
  			},
f986e111b   TRUONG   add libs
183
184
185
  			disabled: function() {
  				return !list;
  			},
87c93a029   Dang YoungWorld   add modal
186
187
188
189
  
  			// Disable .fire
  			// Also disable .add unless we have memory (since it would have no effect)
  			// Abort any pending executions
f986e111b   TRUONG   add libs
190
  			lock: function() {
87c93a029   Dang YoungWorld   add modal
191
192
193
  				locked = queue = [];
  				if ( !memory && !firing ) {
  					list = memory = "";
f986e111b   TRUONG   add libs
194
195
196
  				}
  				return this;
  			},
f986e111b   TRUONG   add libs
197
  			locked: function() {
87c93a029   Dang YoungWorld   add modal
198
  				return !!locked;
f986e111b   TRUONG   add libs
199
  			},
87c93a029   Dang YoungWorld   add modal
200

f986e111b   TRUONG   add libs
201
202
  			// Call all callbacks with the given context and arguments
  			fireWith: function( context, args ) {
87c93a029   Dang YoungWorld   add modal
203
  				if ( !locked ) {
f986e111b   TRUONG   add libs
204
205
  					args = args || [];
  					args = [ context, args.slice ? args.slice() : args ];
87c93a029   Dang YoungWorld   add modal
206
207
208
  					queue.push( args );
  					if ( !firing ) {
  						fire();
f986e111b   TRUONG   add libs
209
210
211
212
  					}
  				}
  				return this;
  			},
87c93a029   Dang YoungWorld   add modal
213

f986e111b   TRUONG   add libs
214
215
216
217
218
  			// Call all the callbacks with the given arguments
  			fire: function() {
  				self.fireWith( this, arguments );
  				return this;
  			},
87c93a029   Dang YoungWorld   add modal
219

f986e111b   TRUONG   add libs
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   Dang YoungWorld   add modal
230
  } );