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