jquery fx: sync animations were being left on jQuery.timers (double callback)
[jquery.git] / src / fx.js
1 var elemdisplay = {},
2         timerId,
3         fxAttrs = [
4                 // height animations
5                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
6                 // width animations
7                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
8                 // opacity animations
9                 [ "opacity" ]
10         ];
11
12 function genFx( type, num ){
13         var obj = {};
14         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function(){
15                 obj[ this ] = type;
16         });
17         return obj;
18 }
19
20 jQuery.fn.extend({
21         show: function(speed,callback){
22                 if ( speed ) {
23                         return this.animate( genFx("show", 3), speed, callback);
24                 } else {
25                         for ( var i = 0, l = this.length; i < l; i++ ){
26                                 var old = jQuery.data(this[i], "olddisplay");
27                                 
28                                 this[i].style.display = old || "";
29                                 
30                                 if ( jQuery.css(this[i], "display") === "none" ) {
31                                         var tagName = this[i].tagName, display;
32                                         
33                                         if ( elemdisplay[ tagName ] ) {
34                                                 display = elemdisplay[ tagName ];
35                                         } else {
36                                                 var elem = jQuery("<" + tagName + " />").appendTo("body");
37                                                 
38                                                 display = elem.css("display");
39                                                 if ( display === "none" )
40                                                         display = "block";
41                                                 
42                                                 elem.remove();
43                                                 
44                                                 elemdisplay[ tagName ] = display;
45                                         }
46                                         
47                                         this[i].style.display = jQuery.data(this[i], "olddisplay", display);
48                                 }
49                         }
50                         
51                         return this;
52                 }
53         },
54
55         hide: function(speed,callback){
56                 if ( speed ) {
57                         return this.animate( genFx("hide", 3), speed, callback);
58                 } else {
59                         for ( var i = 0, l = this.length; i < l; i++ ){
60                                 var old = jQuery.data(this[i], "olddisplay");
61                                 if ( !old && old !== "none" )
62                                         jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display"));
63                                 this[i].style.display = "none";
64                         }
65                         return this;
66                 }
67         },
68
69         // Save the old toggle function
70         _toggle: jQuery.fn.toggle,
71
72         toggle: function( fn, fn2 ){
73                 var bool = typeof fn === "boolean";
74
75                 return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
76                         this._toggle.apply( this, arguments ) :
77                         fn == null || bool ?
78                                 this.each(function(){
79                                         var state = bool ? fn : jQuery(this).is(":hidden");
80                                         jQuery(this)[ state ? "show" : "hide" ]();
81                                 }) :
82                                 this.animate(genFx("toggle", 3), fn, fn2);
83         },
84
85         fadeTo: function(speed,to,callback){
86                 return this.animate({opacity: to}, speed, callback);
87         },
88
89         animate: function( prop, speed, easing, callback ) {
90                 var optall = jQuery.speed(speed, easing, callback);
91
92                 return this[ optall.queue === false ? "each" : "queue" ](function(){
93                 
94                         var opt = jQuery.extend({}, optall), p,
95                                 hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),
96                                 self = this;
97         
98                         for ( p in prop ) {
99                                 if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
100                                         return opt.complete.call(this);
101
102                                 if ( ( p == "height" || p == "width" ) && this.style ) {
103                                         // Store display property
104                                         opt.display = jQuery.css(this, "display");
105
106                                         // Make sure that nothing sneaks out
107                                         opt.overflow = this.style.overflow;
108                                 }
109                         }
110
111                         if ( opt.overflow != null )
112                                 this.style.overflow = "hidden";
113
114                         opt.curAnim = jQuery.extend({}, prop);
115
116                         jQuery.each( prop, function(name, val){
117                                 var e = new jQuery.fx( self, opt, name );
118
119                                 if ( /toggle|show|hide/.test(val) )
120                                         e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
121                                 else {
122                                         var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
123                                                 start = e.cur(true) || 0;
124
125                                         if ( parts ) {
126                                                 var end = parseFloat(parts[2]),
127                                                         unit = parts[3] || "px";
128
129                                                 // We need to compute starting value
130                                                 if ( unit != "px" ) {
131                                                         self.style[ name ] = (end || 1) + unit;
132                                                         start = ((end || 1) / e.cur(true)) * start;
133                                                         self.style[ name ] = start + unit;
134                                                 }
135
136                                                 // If a +=/-= token was provided, we're doing a relative animation
137                                                 if ( parts[1] )
138                                                         end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
139
140                                                 e.custom( start, end, unit );
141                                         } else
142                                                 e.custom( start, val, "" );
143                                 }
144                         });
145
146                         // For JS strict compliance
147                         return true;
148                 });
149         },
150
151         stop: function(clearQueue, gotoEnd){
152                 var timers = jQuery.timers;
153
154                 if (clearQueue)
155                         this.queue([]);
156
157                 this.each(function(){
158                         // go in reverse order so anything added to the queue during the loop is ignored
159                         for ( var i = timers.length - 1; i >= 0; i-- )
160                                 if ( timers[i].elem == this ) {
161                                         if (gotoEnd)
162                                                 // force the next step to be the last
163                                                 timers[i](true);
164                                         timers.splice(i, 1);
165                                 }
166                 });
167
168                 // start the next in the queue if the last step wasn't forced
169                 if (!gotoEnd)
170                         this.dequeue();
171
172                 return this;
173         }
174
175 });
176
177 // Generate shortcuts for custom animations
178 jQuery.each({
179         slideDown: genFx("show", 1),
180         slideUp: genFx("hide", 1),
181         slideToggle: genFx("toggle", 1),
182         fadeIn: { opacity: "show" },
183         fadeOut: { opacity: "hide" }
184 }, function( name, props ){
185         jQuery.fn[ name ] = function( speed, callback ){
186                 return this.animate( props, speed, callback );
187         };
188 });
189
190 jQuery.extend({
191
192         speed: function(speed, easing, fn) {
193                 var opt = typeof speed === "object" ? speed : {
194                         complete: fn || !fn && easing ||
195                                 jQuery.isFunction( speed ) && speed,
196                         duration: speed,
197                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
198                 };
199
200                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
201                         jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
202
203                 // Queueing
204                 opt.old = opt.complete;
205                 opt.complete = function(){
206                         if ( opt.queue !== false )
207                                 jQuery(this).dequeue();
208                         if ( jQuery.isFunction( opt.old ) )
209                                 opt.old.call( this );
210                 };
211
212                 return opt;
213         },
214
215         easing: {
216                 linear: function( p, n, firstNum, diff ) {
217                         return firstNum + diff * p;
218                 },
219                 swing: function( p, n, firstNum, diff ) {
220                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
221                 }
222         },
223
224         timers: [],
225
226         fx: function( elem, options, prop ){
227                 this.options = options;
228                 this.elem = elem;
229                 this.prop = prop;
230
231                 if ( !options.orig )
232                         options.orig = {};
233         }
234
235 });
236
237 jQuery.fx.prototype = {
238
239         // Simple function for setting a style value
240         update: function(){
241                 if ( this.options.step )
242                         this.options.step.call( this.elem, this.now, this );
243
244                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
245
246                 // Set display property to block for height/width animations
247                 if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )
248                         this.elem.style.display = "block";
249         },
250
251         // Get the current size
252         cur: function(force){
253                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )
254                         return this.elem[ this.prop ];
255
256                 var r = parseFloat(jQuery.css(this.elem, this.prop, force));
257                 return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
258         },
259
260         // Start an animation from one number to another
261         custom: function(from, to, unit){
262                 this.startTime = now();
263                 this.start = from;
264                 this.end = to;
265                 this.unit = unit || this.unit || "px";
266                 this.now = this.start;
267                 this.pos = this.state = 0;
268
269                 var self = this;
270                 function t(gotoEnd){
271                         return self.step(gotoEnd);
272                 }
273
274                 t.elem = this.elem;
275
276                 if ( t() && jQuery.timers.push(t) == 1 ) {
277                         timerId = setInterval(function(){
278                                 var timers = jQuery.timers;
279
280                                 for ( var i = 0; i < timers.length; i++ )
281                                         if ( !timers[i]() )
282                                                 timers.splice(i--, 1);
283
284                                 if ( !timers.length ) {
285                                         clearInterval( timerId );
286                                 }
287                         }, 13);
288                 }
289         },
290
291         // Simple 'show' function
292         show: function(){
293                 // Remember where we started, so that we can go back to it later
294                 this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
295                 this.options.show = true;
296
297                 // Begin the animation
298                 // Make sure that we start at a small width/height to avoid any
299                 // flash of content
300                 this.custom(this.prop == "width" || this.prop == "height" ? 1 : 0, this.cur());
301
302                 // Start by showing the element
303                 jQuery(this.elem).show();
304         },
305
306         // Simple 'hide' function
307         hide: function(){
308                 // Remember where we started, so that we can go back to it later
309                 this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
310                 this.options.hide = true;
311
312                 // Begin the animation
313                 this.custom(this.cur(), 0);
314         },
315
316         // Each step of an animation
317         step: function(gotoEnd){
318                 var t = now();
319
320                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
321                         this.now = this.end;
322                         this.pos = this.state = 1;
323                         this.update();
324
325                         this.options.curAnim[ this.prop ] = true;
326
327                         var done = true;
328                         for ( var i in this.options.curAnim )
329                                 if ( this.options.curAnim[i] !== true )
330                                         done = false;
331
332                         if ( done ) {
333                                 if ( this.options.display != null ) {
334                                         // Reset the overflow
335                                         this.elem.style.overflow = this.options.overflow;
336
337                                         // Reset the display
338                                         this.elem.style.display = this.options.display;
339                                         if ( jQuery.css(this.elem, "display") == "none" )
340                                                 this.elem.style.display = "block";
341                                 }
342
343                                 // Hide the element if the "hide" operation was done
344                                 if ( this.options.hide )
345                                         jQuery(this.elem).hide();
346
347                                 // Reset the properties, if the item has been hidden or shown
348                                 if ( this.options.hide || this.options.show )
349                                         for ( var p in this.options.curAnim )
350                                                 jQuery.attr(this.elem.style, p, this.options.orig[p]);
351                                         
352                                 // Execute the complete function
353                                 this.options.complete.call( this.elem );
354                         }
355
356                         return false;
357                 } else {
358                         var n = t - this.startTime;
359                         this.state = n / this.options.duration;
360
361                         // Perform the easing function, defaults to swing
362                         this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
363                         this.now = this.start + ((this.end - this.start) * this.pos);
364
365                         // Perform the next step of the animation
366                         this.update();
367                 }
368
369                 return true;
370         }
371
372 };
373
374 jQuery.extend( jQuery.fx, {
375         speeds:{
376                 slow: 600,
377                 fast: 200,
378                 // Default speed
379                 _default: 400
380         },
381         step: {
382
383                 opacity: function(fx){
384                         jQuery.attr(fx.elem.style, "opacity", fx.now);
385                 },
386
387                 _default: function(fx){
388                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null )
389                                 fx.elem.style[ fx.prop ] = fx.now + fx.unit;
390                         else
391                                 fx.elem[ fx.prop ] = fx.now;
392                 }
393         }
394 });