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