From 780b7983d0a2fbde5cedcdb431a1f2436b256d40 Mon Sep 17 00:00:00 2001 From: louisremi Date: Mon, 11 Apr 2011 19:01:04 +0200 Subject: [PATCH 1/5] Group all local functions at the top of the file and add comments --- src/effects.js | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/effects.js b/src/effects.js index f334ac95..80c033ab 100644 --- a/src/effects.js +++ b/src/effects.js @@ -17,13 +17,44 @@ var elemdisplay = {}, window.mozRequestAnimationFrame || window.oRequestAnimationFrame; +// Animations created synchronously will run synchronously +function createFxNow() { + setTimeout( clearFxNow, 0 ); + return ( fxNow = jQuery.now() ); +} + function clearFxNow() { fxNow = undefined; } -function createFxNow() { - setTimeout( clearFxNow, 0 ); - return ( fxNow = jQuery.now() ); +// Try to restore the default display value of an element +// fails if a display rule has been set for this element, e.g. div { display: inline; } +function defaultDisplay( nodeName ) { + if ( !elemdisplay[ nodeName ] ) { + var elem = jQuery("<" + nodeName + ">").appendTo("body"), + display = elem.css("display"); + + elem.remove(); + + if ( display === "none" || display === "" ) { + display = "block"; + } + + elemdisplay[ nodeName ] = display; + } + + return elemdisplay[ nodeName ]; +} + +// Generate parameters to create a standard animation +function genFx( type, num ) { + var obj = {}; + + jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() { + obj[ this ] = type; + }); + + return obj; } jQuery.fn.extend({ @@ -260,16 +291,6 @@ jQuery.fn.extend({ }); -function genFx( type, num ) { - var obj = {}; - - jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() { - obj[ this ] = type; - }); - - return obj; -} - // Generate shortcuts for custom animations jQuery.each({ slideDown: genFx("show", 1), @@ -536,21 +557,4 @@ if ( jQuery.expr && jQuery.expr.filters ) { }; } -function defaultDisplay( nodeName ) { - if ( !elemdisplay[ nodeName ] ) { - var elem = jQuery("<" + nodeName + ">").appendTo("body"), - display = elem.css("display"); - - elem.remove(); - - if ( display === "none" || display === "" ) { - display = "block"; - } - - elemdisplay[ nodeName ] = display; - } - - return elemdisplay[ nodeName ]; -} - })( jQuery ); From 7dc707042692398392bd910b01f3a6aab81a90a0 Mon Sep 17 00:00:00 2001 From: louisremi Date: Thu, 14 Apr 2011 15:21:08 +0200 Subject: [PATCH 2/5] reduce function calls simplify easing resolution code + some code cleanup --- src/effects.js | 108 ++++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/src/effects.js b/src/effects.js index e4f37e0c..d19f8f61 100644 --- a/src/effects.js +++ b/src/effects.js @@ -163,20 +163,33 @@ jQuery.fn.extend({ jQuery._mark( this ); } - var opt = jQuery.extend({}, optall), p, + var opt = jQuery.extend({}, optall), isElement = this.nodeType === 1, hidden = isElement && jQuery(this).is(":hidden"), - self = this; + self = this, + name, val, p, + easing, display, e, + parts, start, end, unit; + + // will store per property easing and be used to determine when an animation is complete + opt.animatedProperties = {}; for ( p in prop ) { - var name = jQuery.camelCase( p ); + // property name normalization + name = jQuery.camelCase( p ); if ( p !== name ) { prop[ name ] = prop[ p ]; delete prop[ p ]; p = name; } + val = prop[p]; + + if ( val === "hide" && hidden || val === "show" && !hidden ) { + return opt.complete.call(self); + } + if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) { return opt.complete.call(this); } @@ -212,38 +225,41 @@ jQuery.fn.extend({ } } - if ( jQuery.isArray( prop[p] ) ) { - // Create (if needed) and add to specialEasing - (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1]; - prop[p] = prop[p][0]; + // easing resolution: per property > opt.specialEasing > opt.easing > 'swing' (default) + if ( jQuery.isArray( val ) ) { + easing = val[1]; + val = val[0]; + } else { + easing = opt.specialEasing && opt.specialEasing[p] || opt.easing || 'swing'; } + opt.animatedProperties[p] = easing; } if ( opt.overflow != null ) { this.style.overflow = "hidden"; } - opt.curAnim = jQuery.extend({}, prop); + for ( p in prop ) { + e = new jQuery.fx( self, opt, p ); - jQuery.each( prop, function( name, val ) { - var e = new jQuery.fx( self, opt, name ); + val = prop[p]; if ( rfxtypes.test(val) ) { - e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop ); + e[ val === "toggle" ? hidden ? "show" : "hide" : val ](); } else { - var parts = rfxnum.exec(val), - start = e.cur(); + parts = rfxnum.exec(val); + start = e.cur(); if ( parts ) { - var end = parseFloat( parts[2] ), - unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" ); + end = parseFloat( parts[2] ); + unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" ); // We need to compute starting value if ( unit !== "px" ) { - jQuery.style( self, name, (end || 1) + unit); + jQuery.style( self, p, (end || 1) + unit); start = ((end || 1) / e.cur()) * start; - jQuery.style( self, name, start + unit); + jQuery.style( self, p, start + unit); } // If a +=/-= token was provided, we're doing a relative animation @@ -257,7 +273,7 @@ jQuery.fn.extend({ e.custom( start, val, "" ); } } - }); + } // For JS strict compliance return true; @@ -265,19 +281,19 @@ jQuery.fn.extend({ }, stop: function( clearQueue, gotoEnd ) { - var timers = jQuery.timers; - if ( clearQueue ) { this.queue([]); } this.each(function() { + var timers = jQuery.timers, + i = timers.length; // clear marker counters if we know they won't be if ( !gotoEnd ) { jQuery._unmark( true, this ); } // go in reverse order so anything added to the queue during the loop is ignored - for ( var i = timers.length - 1; i >= 0; i-- ) { + while ( i-- ) { if ( timers[i].elem === this ) { if (gotoEnd) { // force the next step to be the last @@ -358,9 +374,7 @@ jQuery.extend({ this.elem = elem; this.prop = prop; - if ( !options.orig ) { - options.orig = {}; - } + options.orig = options.orig || {}; } }); @@ -454,26 +468,26 @@ jQuery.fx.prototype = { // Each step of an animation step: function( gotoEnd ) { var t = fxNow || createFxNow(), - done = true; + done = true, + elem = this.elem, + options = this.options; - if ( gotoEnd || t >= this.options.duration + this.startTime ) { + if ( gotoEnd || t >= options.duration + this.startTime ) { this.now = this.end; this.pos = this.state = 1; this.update(); - this.options.curAnim[ this.prop ] = true; + options.animatedProperties[ this.prop ] = true; - for ( var i in this.options.curAnim ) { - if ( this.options.curAnim[i] !== true ) { + for ( var i in options.animatedProperties ) { + if ( options.animatedProperties[i] !== true ) { done = false; } } if ( done ) { // Reset the overflow - if ( this.options.overflow != null && !jQuery.support.shrinkWrapBlocks ) { - var elem = this.elem, - options = this.options; + if ( options.overflow != null && !jQuery.support.shrinkWrapBlocks ) { jQuery.each( [ "", "X", "Y" ], function (index, value) { elem.style[ "overflow" + value ] = options.overflow[index]; @@ -481,33 +495,35 @@ jQuery.fx.prototype = { } // Hide the element if the "hide" operation was done - if ( this.options.hide ) { - jQuery(this.elem).hide(); + if ( options.hide ) { + jQuery(elem).hide(); } // Reset the properties, if the item has been hidden or shown - if ( this.options.hide || this.options.show ) { - for ( var p in this.options.curAnim ) { - jQuery.style( this.elem, p, this.options.orig[p] ); + if ( options.hide || options.show ) { + for ( var p in options.animatedProperties ) { + jQuery.style( elem, p, options.orig[p] ); } } // Execute the complete function - this.options.complete.call( this.elem ); + options.complete.call( elem ); } return false; } else { - var n = t - this.startTime; - this.state = n / this.options.duration; - - // Perform the easing function, defaults to swing - var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop]; - var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear"); - this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration); - this.now = this.start + ((this.end - this.start) * this.pos); + // classical easing cannot be used with an Infinity duration + if ( options.duration == Infinity ) { + this.now = t; + } else { + var n = t - this.startTime; + this.state = n / options.duration; + // Perform the easing function, defaults to swing + this.pos = jQuery.easing[options.animatedProperties[this.prop]](this.state, n, 0, 1, options.duration); + this.now = this.start + ((this.end - this.start) * this.pos); + } // Perform the next step of the animation this.update(); } From 7666c3ef9a72fb77eaa018b692fd905be5559564 Mon Sep 17 00:00:00 2001 From: louisremi Date: Fri, 15 Apr 2011 15:18:21 +0200 Subject: [PATCH 3/5] remove more useless code. feels good. --- src/effects.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/effects.js b/src/effects.js index 80391d89..c30acb82 100644 --- a/src/effects.js +++ b/src/effects.js @@ -129,7 +129,7 @@ jQuery.fn.extend({ isElement = this.nodeType === 1, hidden = isElement && jQuery(this).is(":hidden"), name, val, p, - easing, display, e, + display, e, parts, start, end, unit; // will store per property easing and be used to determine when an animation is complete @@ -142,16 +142,15 @@ jQuery.fn.extend({ if ( p !== name ) { prop[ name ] = prop[ p ]; delete prop[ p ]; - p = name; } - val = prop[p]; + val = prop[name]; if ( val === "hide" && hidden || val === "show" && !hidden ) { return opt.complete.call(this); } - if ( isElement && ( p === "height" || p === "width" ) ) { + if ( isElement && ( name === "height" || name === "width" ) ) { // Make sure that nothing sneaks out // Record all 3 overflow attributes because IE does not // change the overflow attribute when overflowX and @@ -183,13 +182,9 @@ jQuery.fn.extend({ } // easing resolution: per property > opt.specialEasing > opt.easing > 'swing' (default) - if ( jQuery.isArray( val ) ) { - easing = val[1]; - val = val[0]; - } else { - easing = opt.specialEasing && opt.specialEasing[p] || opt.easing || 'swing'; - } - opt.animatedProperties[p] = easing; + opt.animatedProperties[name] = jQuery.isArray( val ) ? + val[1]: + opt.specialEasing && opt.specialEasing[p] || opt.easing || 'swing'; } if ( opt.overflow != null ) { From 7bc8227d2966d0e91eb1725261fb71157bcbe854 Mon Sep 17 00:00:00 2001 From: louisremi Date: Fri, 15 Apr 2011 15:30:19 +0200 Subject: [PATCH 4/5] typo --- src/effects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/effects.js b/src/effects.js index c30acb82..ba33d803 100644 --- a/src/effects.js +++ b/src/effects.js @@ -184,7 +184,7 @@ jQuery.fn.extend({ // easing resolution: per property > opt.specialEasing > opt.easing > 'swing' (default) opt.animatedProperties[name] = jQuery.isArray( val ) ? val[1]: - opt.specialEasing && opt.specialEasing[p] || opt.easing || 'swing'; + opt.specialEasing && opt.specialEasing[name] || opt.easing || 'swing'; } if ( opt.overflow != null ) { From bcc8187b059792d9f9c0510e23c08ae037916c7e Mon Sep 17 00:00:00 2001 From: louisremi Date: Fri, 15 Apr 2011 16:33:21 +0200 Subject: [PATCH 5/5] undefined should be as good as null here --- src/effects.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/effects.js b/src/effects.js index ba33d803..2c7b44cb 100644 --- a/src/effects.js +++ b/src/effects.js @@ -1,8 +1,7 @@ (function( jQuery ) { var elemdisplay = {}, - iframe = null, - iframeDoc = null, + iframe, iframeDoc, rfxtypes = /^(?:toggle|show|hide)$/, rfxnum = /^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i, timerId, @@ -547,12 +546,9 @@ function defaultDisplay( nodeName ) { elem.remove(); + // If the simple way fails, + // get element's real default display by attaching it to a temp iframe if ( display === "none" || display === "" ) { - - // Get element's real default display by attaching it to a temp iframe - // Conritbutions from Louis Remi and Julian Aurbourg - // based on recommendation by Louis Remi - // No iframe to use yet, so create it if ( !iframe ) { iframe = document.createElement( "iframe" );