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