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