scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | define( [ |
1 | define([ |
|
2 | "./core", |
2 | "./core", |
|
3 | "./var/rnothtmlwhite" |
3 | "./var/rnotwhite" |
|
4 | ], function( jQuery, rnothtmlwhite ) { |
4 | ], function( jQuery, rnotwhite ) { |
|
Line -... | Line 5... | |||
- | 5 | |
||
5 | |
6 | // String to Object options format cache |
|
Line 6... | Line 7... | |||
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 | }); |
|
Line 14... | Line 15... | |||
14 | return object; |
15 | return object; |
|
15 | } |
16 | } |
|
Line 39... | Line 40... | |||
39 | jQuery.Callbacks = function( options ) { |
40 | jQuery.Callbacks = function( options ) { |
|
Line 40... | Line 41... | |||
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 ) ) : |
|
Line 45... | Line -... | |||
45 | jQuery.extend( {}, options ); |
- | ||
46 | |
- | ||
47 | var // Flag to know if list is currently firing |
- | ||
48 | firing, |
46 | jQuery.extend( {}, options ); |
|
49 | |
47 | |
|
50 | // Last fire value for non-forgettable lists |
- | ||
51 | memory, |
48 | var // Last fire value (for non-forgettable lists) |
|
52 | |
49 | memory, |
|
- | 50 | // Flag to know if list was already fired |
||
53 | // Flag to know if list was already fired |
51 | fired, |
|
- | 52 | // Flag to know if list is currently firing |
||
- | 53 | firing, |
||
54 | fired, |
54 | // First callback to fire (used internally by add and fireWith) |
|
- | 55 | firingStart, |
||
- | 56 | // End of the loop when firing |
||
55 | |
57 | firingLength, |
|
56 | // Flag to prevent firing |
- | ||
57 | locked, |
58 | // Index of currently firing callback (modified by remove if needed) |
|
58 | |
59 | firingIndex, |
|
59 | // Actual callback list |
- | ||
60 | list = [], |
60 | // Actual callback list |
|
61 | |
- | ||
62 | // Queue of execution data for repeatable lists |
- | ||
63 | queue = [], |
- | ||
64 | |
61 | list = [], |
|
65 | // Index of currently firing callback (modified by add/remove as needed) |
- | ||
66 | firingIndex = -1, |
62 | // Stack of fire calls for repeatable lists |
|
67 | |
63 | stack = !options.once && [], |
|
68 | // Fire callbacks |
- | ||
69 | fire = function() { |
64 | // Fire callbacks |
|
70 | |
65 | fire = function( data ) { |
|
71 | // Enforce single-firing |
- | ||
72 | locked = options.once; |
- | ||
73 | |
66 | memory = options.memory && data; |
|
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(); |
- | ||
79 | while ( ++firingIndex < list.length ) { |
- | ||
80 | |
71 | firing = true; |
|
81 | // Run callback and check for early termination |
- | ||
82 | if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false && |
- | ||
83 | options.stopOnFalse ) { |
72 | for ( ; list && firingIndex < firingLength; firingIndex++ ) { |
|
84 | |
- | ||
85 | // Jump to end and forget the data so .add doesn't re-fire |
- | ||
86 | firingIndex = list.length; |
73 | if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) { |
|
87 | memory = false; |
74 | memory = false; // To prevent further calls using add |
|
88 | } |
75 | break; |
|
89 | } |
- | ||
90 | } |
- | ||
91 | |
- | ||
92 | // Forget the data if we're done with it |
- | ||
93 | if ( !options.memory ) { |
- | ||
94 | memory = false; |
- | ||
95 | } |
76 | } |
|
96 | |
- | ||
97 | firing = false; |
77 | } |
|
98 | |
78 | firing = false; |
|
99 | // Clean up if we're done firing for good |
- | ||
- | 79 | if ( list ) { |
||
100 | if ( locked ) { |
80 | if ( stack ) { |
|
- | 81 | if ( stack.length ) { |
||
101 | |
82 | fire( stack.shift() ); |
|
102 | // Keep an empty list if we have data for future add calls |
83 | } |
|
103 | if ( memory ) { |
- | ||
104 | list = []; |
- | ||
105 | |
84 | } else if ( memory ) { |
|
106 | // Otherwise, this object is spent |
85 | list = []; |
|
107 | } else { |
86 | } else { |
|
108 | list = ""; |
87 | self.disable(); |
|
109 | } |
88 | } |
|
110 | } |
- | ||
111 | }, |
89 | } |
|
112 | |
90 | }, |
|
113 | // Actual Callbacks object |
- | ||
114 | self = { |
91 | // Actual Callbacks object |
|
115 | |
92 | self = { |
|
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() { |
- | ||
118 | if ( list ) { |
- | ||
119 | |
94 | add: function() { |
|
120 | // If we have memory from a past run, we should fire after adding |
95 | if ( list ) { |
|
121 | if ( memory && !firing ) { |
- | ||
122 | firingIndex = list.length - 1; |
- | ||
123 | queue.push( memory ); |
- | ||
124 | } |
96 | // First, we save the current length |
|
125 | |
97 | var start = list.length; |
|
- | 98 | (function add( args ) { |
||
126 | ( function add( args ) { |
99 | jQuery.each( args, function( _, arg ) { |
|
127 | 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 | } |
- | ||
132 | } else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) { |
104 | } |
|
133 | |
105 | } else if ( arg && arg.length && type !== "string" ) { |
|
134 | // Inspect recursively |
106 | // Inspect recursively |
|
135 | add( arg ); |
107 | add( arg ); |
|
136 | } |
108 | } |
|
137 | } ); |
- | ||
- | 109 | }); |
||
- | 110 | })( arguments ); |
||
138 | } )( arguments ); |
111 | // Do we need to add the callbacks to the |
|
- | 112 | // current firing batch? |
||
- | 113 | if ( firing ) { |
||
- | 114 | firingLength = list.length; |
||
- | 115 | // With memory, if we're not firing then |
||
- | 116 | // we should call right away |
||
139 | |
117 | } else if ( memory ) { |
|
140 | if ( memory && !firing ) { |
118 | firingStart = start; |
|
141 | fire(); |
119 | fire( memory ); |
|
142 | } |
120 | } |
|
143 | } |
121 | } |
|
144 | return this; |
- | ||
145 | }, |
122 | return this; |
|
146 | |
123 | }, |
|
- | 124 | // Remove a callback from the list |
||
147 | // Remove a callback from the list |
125 | remove: function() { |
|
148 | 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 ) { |
- | ||
152 | list.splice( index, 1 ); |
129 | while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) { |
|
- | 130 | list.splice( index, 1 ); |
||
- | 131 | // Handle firing indexes |
||
- | 132 | if ( firing ) { |
||
- | 133 | if ( index <= firingLength ) { |
||
153 | |
134 | firingLength--; |
|
154 | // Handle firing indexes |
135 | } |
|
- | 136 | if ( index <= firingIndex ) { |
||
- | 137 | firingIndex--; |
||
155 | if ( index <= firingIndex ) { |
138 | } |
|
156 | firingIndex--; |
139 | } |
|
157 | } |
140 | } |
|
158 | } |
141 | }); |
|
159 | } ); |
142 | } |
|
160 | return this; |
- | ||
161 | }, |
143 | return this; |
|
162 | |
144 | }, |
|
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. |
- | ||
165 | has: function( fn ) { |
146 | // If no argument is given, return whether or not list has callbacks attached. |
|
166 | return fn ? |
- | ||
167 | jQuery.inArray( fn, list ) > -1 : |
147 | has: function( fn ) { |
|
168 | list.length > 0; |
- | ||
169 | }, |
148 | return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length ); |
|
170 | |
149 | }, |
|
171 | // Remove all callbacks from the list |
150 | // Remove all callbacks from the list |
|
172 | empty: function() { |
151 | empty: function() { |
|
173 | if ( list ) { |
- | ||
174 | list = []; |
152 | list = []; |
|
175 | } |
153 | firingLength = 0; |
|
176 | return this; |
- | ||
177 | }, |
- | ||
178 | |
154 | return this; |
|
179 | // Disable .fire and .add |
- | ||
180 | // Abort any current/pending executions |
155 | }, |
|
181 | // Clear all callbacks and values |
- | ||
182 | disable: function() { |
156 | // Have the list do nothing anymore |
|
183 | locked = queue = []; |
157 | disable: function() { |
|
184 | list = memory = ""; |
158 | list = stack = memory = undefined; |
|
- | 159 | return this; |
||
185 | return this; |
160 | }, |
|
186 | }, |
161 | // Is it disabled? |
|
187 | disabled: function() { |
162 | disabled: function() { |
|
188 | return !list; |
- | ||
189 | }, |
- | ||
190 | |
- | ||
191 | // Disable .fire |
163 | return !list; |
|
192 | // Also disable .add unless we have memory (since it would have no effect) |
164 | }, |
|
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 | } |
|
- | 171 | return this; |
||
199 | return this; |
172 | }, |
|
200 | }, |
173 | // Is it locked? |
|
201 | locked: function() { |
174 | locked: function() { |
|
202 | return !!locked; |
- | ||
203 | }, |
175 | return !stack; |
|
204 | |
176 | }, |
|
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 ) ) { |
|
- | 180 | args = args || []; |
||
208 | args = args || []; |
181 | args = [ context, args.slice ? args.slice() : args ]; |
|
209 | 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; |
- | ||
216 | }, |
188 | return this; |
|
217 | |
189 | }, |
|
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; |
- | ||
222 | }, |
193 | return this; |
|
223 | |
194 | }, |
|
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; |
|
Line 227... | Line 198... | |||
227 | } |
198 | } |
|
228 | }; |
199 | }; |
|
Line 229... | Line 200... | |||
229 | |
200 | |
|
230 | return self; |
201 | return self; |