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