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