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