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