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