Added in support for injecting area elements into map elements. Fixes #4484.
[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 != null ) {
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 != null ) {
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 = speed && 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                                         var old = jQuery.data(this.elem, "olddisplay");
363                                         this.elem.style.display = old ? old : this.options.display;
364
365                                         if ( jQuery.css(this.elem, "display") == "none" ) {
366                                                 this.elem.style.display = "block";
367                                         }
368                                 }
369
370                                 // Hide the element if the "hide" operation was done
371                                 if ( this.options.hide ) {
372                                         jQuery(this.elem).hide();
373                                 }
374                                 // Reset the properties, if the item has been hidden or shown
375                                 if ( this.options.hide || this.options.show ){
376                                         for ( var p in this.options.curAnim ) {
377                                                 jQuery.style(this.elem, p, this.options.orig[p]);
378                                         }
379                                 }
380                                 // Execute the complete function
381                                 this.options.complete.call( this.elem );
382                         }
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.extend( jQuery.fx, {
403
404         tick:function(){
405                 var timers = jQuery.timers;
406
407                 for ( var i = 0; i < timers.length; i++ ) {
408                         if ( !timers[i]() ) {
409                                 timers.splice(i--, 1);
410                         }
411                 }
412                 if ( !timers.length ) {
413                         jQuery.fx.stop();
414                 }
415         },
416                 
417         stop:function(){
418                 clearInterval( timerId );
419                 timerId = null;
420         },
421         
422         speeds:{
423                 slow: 600,
424                 fast: 200,
425                 // Default speed
426                 _default: 400
427         },
428
429         step: {
430
431                 opacity: function(fx){
432                         jQuery.style(fx.elem, "opacity", fx.now);
433                 },
434
435                 _default: function(fx){
436                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
437                                 fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
438                         } else {
439                                 fx.elem[ fx.prop ] = fx.now;
440                         }
441                 }
442         }
443 });
444
445 if ( jQuery.expr && jQuery.expr.filters ) {
446         jQuery.expr.filters.animated = function(elem){
447                 return jQuery.grep(jQuery.timers, function(fn){
448                         return elem === fn.elem;
449                 }).length;
450         };
451 }