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