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