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