Fixes regression in .attr(), patch by David Flanagan. Fixes #4884.
[jquery.git] / src / fx.js
1 var elemdisplay = {},
2         timerId,
3         fxAttrs = [
4                 // height animations
5                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
6                 // width animations
7                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
8                 // opacity animations
9                 [ "opacity" ]
10         ];
11
12 function genFx( type, num ){
13         var obj = {};
14         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function(){
15                 obj[ this ] = type;
16         });
17         return obj;
18 }
19
20 jQuery.fn.extend({
21         show: function(speed,callback){
22                 if ( speed ) {
23                         return this.animate( genFx("show", 3), speed, callback);
24                 } else {
25                         for ( var i = 0, l = this.length; i < l; i++ ){
26                                 var old = jQuery.data(this[i], "olddisplay");
27
28                                 this[i].style.display = old || "";
29
30                                 if ( jQuery.css(this[i], "display") === "none" ) {
31                                         var tagName = this[i].tagName, display;
32
33                                         if ( elemdisplay[ tagName ] ) {
34                                                 display = elemdisplay[ tagName ];
35                                         } else {
36                                                 var elem = jQuery("<" + tagName + " />").appendTo("body");
37
38                                                 display = elem.css("display");
39                                                 if ( display === "none" )
40                                                         display = "block";
41
42                                                 elem.remove();
43
44                                                 elemdisplay[ tagName ] = 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 i = 0, l = this.length; i < l; i++ ){
54                                 this[i].style.display = jQuery.data(this[i], "olddisplay") || "";
55                         }
56
57                         return this;
58                 }
59         },
60
61         hide: function(speed,callback){
62                 if ( speed ) {
63                         return this.animate( genFx("hide", 3), speed, callback);
64                 } else {
65                         for ( var i = 0, l = this.length; i < l; i++ ){
66                                 var old = jQuery.data(this[i], "olddisplay");
67                                 if ( !old && old !== "none" )
68                                         jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display"));
69                         }
70
71                         // Set the display of the elements in a second loop
72                         // to avoid the constant reflow
73                         for ( var i = 0, l = this.length; i < l; 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 ){
85                 var bool = typeof fn === "boolean";
86
87                 return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
88                         this._toggle.apply( this, arguments ) :
89                         fn == null || bool ?
90                                 this.each(function(){
91                                         var state = bool ? fn : jQuery(this).is(":hidden");
92                                         jQuery(this)[ state ? "show" : "hide" ]();
93                                 }) :
94                                 this.animate(genFx("toggle", 3), fn, fn2);
95         },
96
97         fadeTo: function(speed,to,callback){
98                 return this.filter(":hidden").css('opacity', 0).show().end()
99                                         .animate({opacity: to}, speed, callback);
100         },
101
102         animate: function( prop, speed, easing, callback ) {
103                 var optall = jQuery.speed(speed, easing, callback);
104
105                 return this[ optall.queue === false ? "each" : "queue" ](function(){
106
107                         var opt = jQuery.extend({}, optall), p,
108                                 hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),
109                                 self = this;
110
111                         for ( p in prop ) {
112                                 if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
113                                         return opt.complete.call(this);
114
115                                 if ( ( p == "height" || p == "width" ) && this.style ) {
116                                         // Store display property
117                                         opt.display = jQuery.css(this, "display");
118
119                                         // Make sure that nothing sneaks out
120                                         opt.overflow = this.style.overflow;
121                                 }
122                         }
123
124                         if ( opt.overflow != null )
125                                 this.style.overflow = "hidden";
126
127                         opt.curAnim = jQuery.extend({}, prop);
128
129                         jQuery.each( prop, function(name, val){
130                                 var e = new jQuery.fx( self, opt, name );
131
132                                 if ( /toggle|show|hide/.test(val) )
133                                         e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
134                                 else {
135                                         var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
136                                                 start = e.cur(true) || 0;
137
138                                         if ( parts ) {
139                                                 var end = parseFloat(parts[2]),
140                                                         unit = parts[3] || "px";
141
142                                                 // We need to compute starting value
143                                                 if ( unit != "px" ) {
144                                                         self.style[ name ] = (end || 1) + unit;
145                                                         start = ((end || 1) / e.cur(true)) * start;
146                                                         self.style[ name ] = start + unit;
147                                                 }
148
149                                                 // If a +=/-= token was provided, we're doing a relative animation
150                                                 if ( parts[1] )
151                                                         end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
152
153                                                 e.custom( start, end, unit );
154                                         } else
155                                                 e.custom( start, val, "" );
156                                 }
157                         });
158
159                         // For JS strict compliance
160                         return true;
161                 });
162         },
163
164         stop: function(clearQueue, gotoEnd){
165                 var timers = jQuery.timers;
166
167                 if (clearQueue)
168                         this.queue([]);
169
170                 this.each(function(){
171                         // go in reverse order so anything added to the queue during the loop is ignored
172                         for ( var i = timers.length - 1; i >= 0; i-- )
173                                 if ( timers[i].elem == this ) {
174                                         if (gotoEnd)
175                                                 // force the next step to be the last
176                                                 timers[i](true);
177                                         timers.splice(i, 1);
178                                 }
179                 });
180
181                 // start the next in the queue if the last step wasn't forced
182                 if (!gotoEnd)
183                         this.dequeue();
184
185                 return this;
186         }
187
188 });
189
190 // Generate shortcuts for custom animations
191 jQuery.each({
192         slideDown: genFx("show", 1),
193         slideUp: genFx("hide", 1),
194         slideToggle: genFx("toggle", 1),
195         fadeIn: { opacity: "show" },
196         fadeOut: { opacity: "hide" }
197 }, function( name, props ){
198         jQuery.fn[ name ] = function( speed, callback ){
199                 return this.animate( props, speed, callback );
200         };
201 });
202
203 jQuery.extend({
204
205         speed: function(speed, easing, fn) {
206                 var opt = typeof speed === "object" ? speed : {
207                         complete: fn || !fn && easing ||
208                                 jQuery.isFunction( speed ) && speed,
209                         duration: speed,
210                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
211                 };
212
213                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
214                         jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
215
216                 // Queueing
217                 opt.old = opt.complete;
218                 opt.complete = function(){
219                         if ( opt.queue !== false )
220                                 jQuery(this).dequeue();
221                         if ( jQuery.isFunction( opt.old ) )
222                                 opt.old.call( this );
223                 };
224
225                 return opt;
226         },
227
228         easing: {
229                 linear: function( p, n, firstNum, diff ) {
230                         return firstNum + diff * p;
231                 },
232                 swing: function( p, n, firstNum, diff ) {
233                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
234                 }
235         },
236
237         timers: [],
238
239         fx: function( elem, options, prop ){
240                 this.options = options;
241                 this.elem = elem;
242                 this.prop = prop;
243
244                 if ( !options.orig )
245                         options.orig = {};
246         }
247
248 });
249
250 jQuery.fx.prototype = {
251
252         // Simple function for setting a style value
253         update: function(){
254                 if ( this.options.step )
255                         this.options.step.call( this.elem, this.now, this );
256
257                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
258
259                 // Set display property to block for height/width animations
260                 if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )
261                         this.elem.style.display = "block";
262         },
263
264         // Get the current size
265         cur: function(force){
266                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )
267                         return this.elem[ this.prop ];
268
269                 var r = parseFloat(jQuery.css(this.elem, this.prop, force));
270                 return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
271         },
272
273         // Start an animation from one number to another
274         custom: function(from, to, unit){
275                 this.startTime = now();
276                 this.start = from;
277                 this.end = to;
278                 this.unit = unit || this.unit || "px";
279                 this.now = this.start;
280                 this.pos = this.state = 0;
281
282                 var self = this;
283                 function t(gotoEnd){
284                         return self.step(gotoEnd);
285                 }
286
287                 t.elem = this.elem;
288
289                 if ( t() && jQuery.timers.push(t) && !timerId )
290                         timerId = setInterval(jQuery.fx.tick, 13);
291         },
292
293         // Simple 'show' function
294         show: function(){
295                 // Remember where we started, so that we can go back to it later
296                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
297                 this.options.show = true;
298
299                 // Begin the animation
300                 // Make sure that we start at a small width/height to avoid any
301                 // flash of content
302                 this.custom(this.prop == "width" || this.prop == "height" ? 1 : 0, this.cur());
303
304                 // Start by showing the element
305                 jQuery(this.elem).show();
306         },
307
308         // Simple 'hide' function
309         hide: function(){
310                 // Remember where we started, so that we can go back to it later
311                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
312                 this.options.hide = true;
313
314                 // Begin the animation
315                 this.custom(this.cur(), 0);
316         },
317
318         // Each step of an animation
319         step: function(gotoEnd){
320                 var t = now();
321
322                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
323                         this.now = this.end;
324                         this.pos = this.state = 1;
325                         this.update();
326
327                         this.options.curAnim[ this.prop ] = true;
328
329                         var done = true;
330                         for ( var i in this.options.curAnim )
331                                 if ( this.options.curAnim[i] !== true )
332                                         done = false;
333
334                         if ( done ) {
335                                 if ( this.options.display != null ) {
336                                         // Reset the overflow
337                                         this.elem.style.overflow = this.options.overflow;
338
339                                         // Reset the display
340                                         this.elem.style.display = this.options.display;
341                                         if ( jQuery.css(this.elem, "display") == "none" )
342                                                 this.elem.style.display = "block";
343                                 }
344
345                                 // Hide the element if the "hide" operation was done
346                                 if ( this.options.hide )
347                                         jQuery(this.elem).hide();
348
349                                 // Reset the properties, if the item has been hidden or shown
350                                 if ( this.options.hide || this.options.show )
351                                         for ( var p in this.options.curAnim )
352                                                 jQuery.style(this.elem, p, this.options.orig[p]);
353
354                                 // Execute the complete function
355                                 this.options.complete.call( this.elem );
356                         }
357
358                         return false;
359                 } else {
360                         var n = t - this.startTime;
361                         this.state = n / this.options.duration;
362
363                         // Perform the easing function, defaults to swing
364                         this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
365                         this.now = this.start + ((this.end - this.start) * this.pos);
366
367                         // Perform the next step of the animation
368                         this.update();
369                 }
370
371                 return true;
372         }
373
374 };
375
376 jQuery.extend( jQuery.fx, {
377
378         tick:function(){
379                 var timers = jQuery.timers;
380
381                 for ( var i = 0; i < timers.length; i++ )
382                         if ( !timers[i]() )
383                                 timers.splice(i--, 1);
384
385                 if ( !timers.length )
386                         jQuery.fx.stop();
387         },
388                 
389         stop:function(){
390                 clearInterval( timerId );
391                 timerId = null;
392         },
393         
394         speeds:{
395                 slow: 600,
396                 fast: 200,
397                 // Default speed
398                 _default: 400
399         },
400
401         step: {
402
403                 opacity: function(fx){
404                         jQuery.style(fx.elem, "opacity", fx.now);
405                 },
406
407                 _default: function(fx){
408                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null )
409                                 fx.elem.style[ fx.prop ] = fx.now + fx.unit;
410                         else
411                                 fx.elem[ fx.prop ] = fx.now;
412                 }
413         }
414 });