Fixed some bugs relating to the setter arg change in val and html. Also optimized...
[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 != null ) {
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 != null ) {
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 // Generate shortcuts for custom animations
225 jQuery.each({
226         slideDown: genFx("show", 1),
227         slideUp: genFx("hide", 1),
228         slideToggle: genFx("toggle", 1),
229         fadeIn: { opacity: "show" },
230         fadeOut: { opacity: "hide" }
231 }, function( name, props ) {
232         jQuery.fn[ name ] = function( speed, callback ) {
233                 return this.animate( props, speed, callback );
234         };
235 });
236
237 jQuery.extend({
238         speed: function( speed, easing, fn ) {
239                 var opt = speed && typeof speed === "object" ? speed : {
240                         complete: fn || !fn && easing ||
241                                 jQuery.isFunction( speed ) && speed,
242                         duration: speed,
243                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
244                 };
245
246                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
247                         jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
248
249                 // Queueing
250                 opt.old = opt.complete;
251                 opt.complete = function() {
252                         if ( opt.queue !== false ) {
253                                 jQuery(this).dequeue();
254                         }
255                         if ( jQuery.isFunction( opt.old ) ) {
256                                 opt.old.call( this );
257                         }
258                 };
259
260                 return opt;
261         },
262
263         easing: {
264                 linear: function( p, n, firstNum, diff ) {
265                         return firstNum + diff * p;
266                 },
267                 swing: function( p, n, firstNum, diff ) {
268                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
269                 }
270         },
271
272         timers: [],
273
274         fx: function( elem, options, prop ) {
275                 this.options = options;
276                 this.elem = elem;
277                 this.prop = prop;
278
279                 if ( !options.orig ) {
280                         options.orig = {};
281                 }
282         }
283
284 });
285
286 jQuery.fx.prototype = {
287         // Simple function for setting a style value
288         update: function() {
289                 if ( this.options.step ) {
290                         this.options.step.call( this.elem, this.now, this );
291                 }
292
293                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
294
295                 // Set display property to block for height/width animations
296                 if ( ( this.prop === "height" || this.prop === "width" ) && this.elem.style ) {
297                         this.elem.style.display = "block";
298                 }
299         },
300
301         // Get the current size
302         cur: function( force ) {
303                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
304                         return this.elem[ this.prop ];
305                 }
306
307                 var r = parseFloat(jQuery.css(this.elem, this.prop, force));
308                 return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
309         },
310
311         // Start an animation from one number to another
312         custom: function( from, to, unit ) {
313                 this.startTime = now();
314                 this.start = from;
315                 this.end = to;
316                 this.unit = unit || this.unit || "px";
317                 this.now = this.start;
318                 this.pos = this.state = 0;
319
320                 var self = this;
321                 function t( gotoEnd ) {
322                         return self.step(gotoEnd);
323                 }
324
325                 t.elem = this.elem;
326
327                 if ( t() && jQuery.timers.push(t) && !timerId ) {
328                         timerId = setInterval(jQuery.fx.tick, 13);
329                 }
330         },
331
332         // Simple 'show' function
333         show: function() {
334                 // Remember where we started, so that we can go back to it later
335                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
336                 this.options.show = true;
337
338                 // Begin the animation
339                 // Make sure that we start at a small width/height to avoid any
340                 // flash of content
341                 this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
342
343                 // Start by showing the element
344                 jQuery( this.elem ).show();
345         },
346
347         // Simple 'hide' function
348         hide: function() {
349                 // Remember where we started, so that we can go back to it later
350                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
351                 this.options.hide = true;
352
353                 // Begin the animation
354                 this.custom(this.cur(), 0);
355         },
356
357         // Each step of an animation
358         step: function( gotoEnd ) {
359                 var t = now(), done = true;
360
361                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
362                         this.now = this.end;
363                         this.pos = this.state = 1;
364                         this.update();
365
366                         this.options.curAnim[ this.prop ] = true;
367
368                         for ( var i in this.options.curAnim ) {
369                                 if ( this.options.curAnim[i] !== true ) {
370                                         done = false;
371                                 }
372                         }
373
374                         if ( done ) {
375                                 if ( this.options.display != null ) {
376                                         // Reset the overflow
377                                         this.elem.style.overflow = this.options.overflow;
378
379                                         // Reset the display
380                                         var old = jQuery.data(this.elem, "olddisplay");
381                                         this.elem.style.display = old ? old : this.options.display;
382
383                                         if ( jQuery.css(this.elem, "display") === "none" ) {
384                                                 this.elem.style.display = "block";
385                                         }
386                                 }
387
388                                 // Hide the element if the "hide" operation was done
389                                 if ( this.options.hide ) {
390                                         jQuery(this.elem).hide();
391                                 }
392
393                                 // Reset the properties, if the item has been hidden or shown
394                                 if ( this.options.hide || this.options.show ) {
395                                         for ( var p in this.options.curAnim ) {
396                                                 jQuery.style(this.elem, p, this.options.orig[p]);
397                                         }
398                                 }
399
400                                 // Execute the complete function
401                                 this.options.complete.call( this.elem );
402                         }
403
404                         return false;
405
406                 } else {
407                         var n = t - this.startTime;
408                         this.state = n / this.options.duration;
409
410                         // Perform the easing function, defaults to swing
411                         var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
412                         var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
413                         this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
414                         this.now = this.start + ((this.end - this.start) * this.pos);
415
416                         // Perform the next step of the animation
417                         this.update();
418                 }
419
420                 return true;
421         }
422 };
423
424 jQuery.extend( jQuery.fx, {
425         tick: function() {
426                 var timers = jQuery.timers;
427
428                 for ( var i = 0; i < timers.length; i++ ) {
429                         if ( !timers[i]() ) {
430                                 timers.splice(i--, 1);
431                         }
432                 }
433
434                 if ( !timers.length ) {
435                         jQuery.fx.stop();
436                 }
437         },
438                 
439         stop: function() {
440                 clearInterval( timerId );
441                 timerId = null;
442         },
443         
444         speeds: {
445                 slow: 600,
446                 fast: 200,
447                 // Default speed
448                 _default: 400
449         },
450
451         step: {
452                 opacity: function( fx ) {
453                         jQuery.style(fx.elem, "opacity", fx.now);
454                 },
455
456                 _default: function( fx ) {
457                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
458                                 fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
459                         } else {
460                                 fx.elem[ fx.prop ] = fx.now;
461                         }
462                 }
463         }
464 });
465
466 if ( jQuery.expr && jQuery.expr.filters ) {
467         jQuery.expr.filters.animated = function( elem ) {
468                 return jQuery.grep(jQuery.timers, function( fn ) {
469                         return elem === fn.elem;
470                 }).length;
471         };
472 }
473
474 function genFx( type, num ) {
475         var obj = {};
476
477         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
478                 obj[ this ] = type;
479         });
480
481         return obj;
482 }