Temporarily backing out commit bd474799bf95f4ecdc47a7577750eaf69e2236d4.
[jquery.git] / src / effects.js
1 (function( jQuery ) {
2
3 var elemdisplay = {},
4         rfxtypes = /^(?:toggle|show|hide)$/,
5         rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/,
6         timerId,
7         fxAttrs = [
8                 // height animations
9                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
10                 // width animations
11                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
12                 // opacity animations
13                 [ "opacity" ]
14         ];
15
16 jQuery.fn.extend({
17         show: function( speed, easing, callback ) {
18                 if ( speed || speed === 0 ) {
19                         return this.animate( genFx("show", 3), speed, easing, callback);
20                 } else {
21                         for ( var i = 0, j = this.length; i < j; i++ ) {
22                                 // Reset the inline display of this element to learn if it is
23                                 // being hidden by cascaded rules or not
24                                 if ( !jQuery.data(this[i], "olddisplay") && this[i].style.display === "none" ) {
25                                         this[i].style.display = "";
26                                 }
27
28                                 // Set elements which have been overridden with display: none
29                                 // in a stylesheet to whatever the default browser style is
30                                 // for such an element
31                                 if ( this[i].style.display === "" && jQuery.css( this[i], "display" ) === "none" ) {
32                                         jQuery.data(this[i], "olddisplay", defaultDisplay(this[i].nodeName));
33                                 }
34                         }
35
36                         // Set the display of most of the elements in a second loop
37                         // to avoid the constant reflow
38                         for ( i = 0; i < j; i++ ) {
39                                 this[i].style.display = jQuery.data(this[i], "olddisplay") || "";
40                         }
41
42                         return this;
43                 }
44         },
45
46         hide: function( speed, easing, callback ) {
47                 if ( speed || speed === 0 ) {
48                         return this.animate( genFx("hide", 3), speed, easing, callback);
49
50                 } else {
51                         for ( var i = 0, j = this.length; i < j; i++ ) {
52                                 var display = jQuery.css( this[i], "display" );
53
54                                 if ( display !== "none" ) {
55                                         jQuery.data( this[i], "olddisplay", display );
56                                 }
57                         }
58
59                         // Set the display of the elements in a second loop
60                         // to avoid the constant reflow
61                         for ( i = 0; i < j; i++ ) {
62                                 this[i].style.display = "none";
63                         }
64
65                         return this;
66                 }
67         },
68
69         // Save the old toggle function
70         _toggle: jQuery.fn.toggle,
71
72         toggle: function( fn, fn2, callback ) {
73                 var bool = typeof fn === "boolean";
74
75                 if ( jQuery.isFunction(fn) && jQuery.isFunction(fn2) ) {
76                         this._toggle.apply( this, arguments );
77
78                 } else if ( fn == null || bool ) {
79                         this.each(function() {
80                                 var state = bool ? fn : jQuery(this).is(":hidden");
81                                 jQuery(this)[ state ? "show" : "hide" ]();
82                         });
83
84                 } else {
85                         this.animate(genFx("toggle", 3), fn, fn2, callback);
86                 }
87
88                 return this;
89         },
90
91         fadeTo: function( speed, to, easing, callback ) {
92                 return this.filter(":hidden").css("opacity", 0).show().end()
93                                         .animate({opacity: to}, speed, easing, callback);
94         },
95
96         animate: function( prop, speed, easing, callback ) {
97                 var optall = jQuery.speed(speed, easing, callback);
98
99                 if ( jQuery.isEmptyObject( prop ) ) {
100                         return this.each( optall.complete );
101                 }
102
103                 return this[ optall.queue === false ? "each" : "queue" ](function() {
104                         // XXX ‘this’ does not always have a nodeName when running the
105                         // test suite
106
107                         var opt = jQuery.extend({}, optall), p,
108                                 isElement = this.nodeType === 1,
109                                 hidden = isElement && jQuery(this).is(":hidden"),
110                                 self = this;
111
112                         for ( p in prop ) {
113                                 var name = jQuery.camelCase( p );
114
115                                 if ( p !== name ) {
116                                         prop[ name ] = prop[ p ];
117                                         delete prop[ p ];
118                                         p = name;
119                                 }
120
121                                 if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
122                                         return opt.complete.call(this);
123                                 }
124
125                                 if ( isElement && ( p === "height" || p === "width" ) ) {
126                                         // Make sure that nothing sneaks out
127                                         // Record all 3 overflow attributes because IE does not
128                                         // change the overflow attribute when overflowX and
129                                         // overflowY are set to the same value
130                                         opt.overflow = [ this.style.overflow, this.style.overflowX, this.style.overflowY ];
131
132                                         // Set display property to inline-block for height/width
133                                         // animations on inline elements that are having width/height
134                                         // animated
135                                         if ( jQuery.css( this, "display" ) === "inline" &&
136                                                         jQuery.css( this, "float" ) === "none" ) {
137                                                 if ( !jQuery.support.inlineBlockNeedsLayout ) {
138                                                         this.style.display = "inline-block";
139
140                                                 } else {
141                                                         var display = defaultDisplay(this.nodeName);
142
143                                                         // inline-level elements accept inline-block;
144                                                         // block-level elements need to be inline with layout
145                                                         if ( display === "inline" ) {
146                                                                 this.style.display = "inline-block";
147
148                                                         } else {
149                                                                 this.style.display = "inline";
150                                                                 this.style.zoom = 1;
151                                                         }
152                                                 }
153                                         }
154                                 }
155
156                                 if ( jQuery.isArray( prop[p] ) ) {
157                                         // Create (if needed) and add to specialEasing
158                                         (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
159                                         prop[p] = prop[p][0];
160                                 }
161                         }
162
163                         if ( opt.overflow != null ) {
164                                 this.style.overflow = "hidden";
165                         }
166
167                         opt.curAnim = jQuery.extend({}, prop);
168
169                         jQuery.each( prop, function( name, val ) {
170                                 var e = new jQuery.fx( self, opt, name );
171
172                                 if ( rfxtypes.test(val) ) {
173                                         e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
174
175                                 } else {
176                                         var parts = rfxnum.exec(val),
177                                                 start = e.cur() || 0;
178
179                                         if ( parts ) {
180                                                 var end = parseFloat( parts[2] ),
181                                                         unit = parts[3] || "px";
182
183                                                 // We need to compute starting value
184                                                 if ( unit !== "px" ) {
185                                                         jQuery.style( self, name, (end || 1) + unit);
186                                                         start = ((end || 1) / e.cur()) * start;
187                                                         jQuery.style( self, name, start + unit);
188                                                 }
189
190                                                 // If a +=/-= token was provided, we're doing a relative animation
191                                                 if ( parts[1] ) {
192                                                         end = ((parts[1] === "-=" ? -1 : 1) * end) + start;
193                                                 }
194
195                                                 e.custom( start, end, unit );
196
197                                         } else {
198                                                 e.custom( start, val, "" );
199                                         }
200                                 }
201                         });
202
203                         // For JS strict compliance
204                         return true;
205                 });
206         },
207
208         stop: function( clearQueue, gotoEnd ) {
209                 var timers = jQuery.timers;
210
211                 if ( clearQueue ) {
212                         this.queue([]);
213                 }
214
215                 this.each(function() {
216                         // go in reverse order so anything added to the queue during the loop is ignored
217                         for ( var i = timers.length - 1; i >= 0; i-- ) {
218                                 if ( timers[i].elem === this ) {
219                                         if (gotoEnd) {
220                                                 // force the next step to be the last
221                                                 timers[i](true);
222                                         }
223
224                                         timers.splice(i, 1);
225                                 }
226                         }
227                 });
228
229                 // start the next in the queue if the last step wasn't forced
230                 if ( !gotoEnd ) {
231                         this.dequeue();
232                 }
233
234                 return this;
235         }
236
237 });
238
239 function genFx( type, num ) {
240         var obj = {};
241
242         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
243                 obj[ this ] = type;
244         });
245
246         return obj;
247 }
248
249 // Generate shortcuts for custom animations
250 jQuery.each({
251         slideDown: genFx("show", 1),
252         slideUp: genFx("hide", 1),
253         slideToggle: genFx("toggle", 1),
254         fadeIn: { opacity: "show" },
255         fadeOut: { opacity: "hide" },
256         fadeToggle: { opacity: "toggle" }
257 }, function( name, props ) {
258         jQuery.fn[ name ] = function( speed, easing, callback ) {
259                 return this.animate( props, speed, easing, callback );
260         };
261 });
262
263 jQuery.extend({
264         speed: function( speed, easing, fn ) {
265                 var opt = speed && typeof speed === "object" ? jQuery.extend({}, speed) : {
266                         complete: fn || !fn && easing ||
267                                 jQuery.isFunction( speed ) && speed,
268                         duration: speed,
269                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
270                 };
271
272                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
273                         opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[opt.duration] : jQuery.fx.speeds._default;
274
275                 // Queueing
276                 opt.old = opt.complete;
277                 opt.complete = function() {
278                         if ( opt.queue !== false ) {
279                                 jQuery(this).dequeue();
280                         }
281                         if ( jQuery.isFunction( opt.old ) ) {
282                                 opt.old.call( this );
283                         }
284                 };
285
286                 return opt;
287         },
288
289         easing: {
290                 linear: function( p, n, firstNum, diff ) {
291                         return firstNum + diff * p;
292                 },
293                 swing: function( p, n, firstNum, diff ) {
294                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
295                 }
296         },
297
298         timers: [],
299
300         fx: function( elem, options, prop ) {
301                 this.options = options;
302                 this.elem = elem;
303                 this.prop = prop;
304
305                 if ( !options.orig ) {
306                         options.orig = {};
307                 }
308         }
309
310 });
311
312 jQuery.fx.prototype = {
313         // Simple function for setting a style value
314         update: function() {
315                 if ( this.options.step ) {
316                         this.options.step.call( this.elem, this.now, this );
317                 }
318
319                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
320         },
321
322         // Get the current size
323         cur: function() {
324                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
325                         return this.elem[ this.prop ];
326                 }
327
328                 var r = parseFloat( jQuery.css( this.elem, this.prop ) );
329                 return r && r > -10000 ? r : 0;
330         },
331
332         // Start an animation from one number to another
333         custom: function( from, to, unit ) {
334                 this.startTime = jQuery.now();
335                 this.start = from;
336                 this.end = to;
337                 this.unit = unit || this.unit || "px";
338                 this.now = this.start;
339                 this.pos = this.state = 0;
340
341                 var self = this, fx = jQuery.fx;
342                 function t( gotoEnd ) {
343                         return self.step(gotoEnd);
344                 }
345
346                 t.elem = this.elem;
347
348                 if ( t() && jQuery.timers.push(t) && !timerId ) {
349                         timerId = setInterval(fx.tick, fx.interval);
350                 }
351         },
352
353         // Simple 'show' function
354         show: function() {
355                 // Remember where we started, so that we can go back to it later
356                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
357                 this.options.show = true;
358
359                 // Begin the animation
360                 // Make sure that we start at a small width/height to avoid any
361                 // flash of content
362                 this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
363
364                 // Start by showing the element
365                 jQuery( this.elem ).show();
366         },
367
368         // Simple 'hide' function
369         hide: function() {
370                 // Remember where we started, so that we can go back to it later
371                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
372                 this.options.hide = true;
373
374                 // Begin the animation
375                 this.custom(this.cur(), 0);
376         },
377
378         // Each step of an animation
379         step: function( gotoEnd ) {
380                 var t = jQuery.now(), done = true;
381
382                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
383                         this.now = this.end;
384                         this.pos = this.state = 1;
385                         this.update();
386
387                         this.options.curAnim[ this.prop ] = true;
388
389                         for ( var i in this.options.curAnim ) {
390                                 if ( this.options.curAnim[i] !== true ) {
391                                         done = false;
392                                 }
393                         }
394
395                         if ( done ) {
396                                 // Reset the overflow
397                                 if ( this.options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
398                                         var elem = this.elem, options = this.options;
399                                         jQuery.each( [ "", "X", "Y" ], function (index, value) {
400                                                 elem.style[ "overflow" + value ] = options.overflow[index];
401                                         } );
402                                 }
403
404                                 // Hide the element if the "hide" operation was done
405                                 if ( this.options.hide ) {
406                                         jQuery(this.elem).hide();
407                                 }
408
409                                 // Reset the properties, if the item has been hidden or shown
410                                 if ( this.options.hide || this.options.show ) {
411                                         for ( var p in this.options.curAnim ) {
412                                                 jQuery.style( this.elem, p, this.options.orig[p] );
413                                         }
414                                 }
415
416                                 // Execute the complete function
417                                 this.options.complete.call( this.elem );
418                         }
419
420                         return false;
421
422                 } else {
423                         var n = t - this.startTime;
424                         this.state = n / this.options.duration;
425
426                         // Perform the easing function, defaults to swing
427                         var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
428                         var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
429                         this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
430                         this.now = this.start + ((this.end - this.start) * this.pos);
431
432                         // Perform the next step of the animation
433                         this.update();
434                 }
435
436                 return true;
437         }
438 };
439
440 jQuery.extend( jQuery.fx, {
441         tick: function() {
442                 var timers = jQuery.timers;
443
444                 for ( var i = 0; i < timers.length; i++ ) {
445                         if ( !timers[i]() ) {
446                                 timers.splice(i--, 1);
447                         }
448                 }
449
450                 if ( !timers.length ) {
451                         jQuery.fx.stop();
452                 }
453         },
454
455         interval: 13,
456
457         stop: function() {
458                 clearInterval( timerId );
459                 timerId = null;
460         },
461
462         speeds: {
463                 slow: 600,
464                 fast: 200,
465                 // Default speed
466                 _default: 400
467         },
468
469         step: {
470                 opacity: function( fx ) {
471                         jQuery.style( fx.elem, "opacity", fx.now );
472                 },
473
474                 _default: function( fx ) {
475                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
476                                 fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
477                         } else {
478                                 fx.elem[ fx.prop ] = fx.now;
479                         }
480                 }
481         }
482 });
483
484 if ( jQuery.expr && jQuery.expr.filters ) {
485         jQuery.expr.filters.animated = function( elem ) {
486                 return jQuery.grep(jQuery.timers, function( fn ) {
487                         return elem === fn.elem;
488                 }).length;
489         };
490 }
491
492 function defaultDisplay( nodeName ) {
493         if ( !elemdisplay[ nodeName ] ) {
494                 var elem = jQuery("<" + nodeName + ">").appendTo("body"),
495                         display = elem.css("display");
496
497                 elem.remove();
498
499                 if ( display === "none" || display === "" ) {
500                         display = "block";
501                 }
502
503                 elemdisplay[ nodeName ] = display;
504         }
505
506         return elemdisplay[ nodeName ];
507 }
508
509 })( jQuery );