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