scratch – Diff between revs 58 and 125

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