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