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