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