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