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