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