Skip to content

Commit

Permalink
Effects.*: DRY the complete callback execution into the 'done' callba…
Browse files Browse the repository at this point in the history
…ck passed into an effect
  • Loading branch information
gnarf committed Jun 21, 2011
1 parent 65a6c46 commit ab627e0
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 83 deletions.
7 changes: 2 additions & 5 deletions ui/jquery.effects.blind.js
Expand Up @@ -15,7 +15,7 @@
var rvertical = /up|down|vertical/, var rvertical = /up|down|vertical/,
rpositivemotion = /up|left|vertical|horizontal/; rpositivemotion = /up|left|vertical|horizontal/;


$.effects.effect.blind = function( o, next ) { $.effects.effect.blind = function( o, done ) {
// Create element // Create element
var el = $( this ), var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "height", "width" ], props = [ "position", "top", "bottom", "left", "right", "height", "width" ],
Expand Down Expand Up @@ -70,10 +70,7 @@ $.effects.effect.blind = function( o, next ) {
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( el[ 0 ], arguments );
}
next();
} }
}); });


Expand Down
11 changes: 4 additions & 7 deletions ui/jquery.effects.bounce.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.bounce = function( o, next ) { $.effects.effect.bounce = function( o, done ) {
var el = $( this ), var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "height", "width" ], props = [ "position", "top", "bottom", "left", "right", "height", "width" ],


Expand Down Expand Up @@ -91,24 +91,21 @@ $.effects.effect.bounce = function( o, next ) {
el.animate( upAnim, speed, easing ); el.animate( upAnim, speed, easing );
} }


el.queue( function( next ) { el.queue(function() {
if ( hide ) { if ( hide ) {
el.hide(); el.hide();
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( o.complete ) { done();
o.complete.apply( el[ 0 ] );
}
next();
}); });


// inject all the animations we just queued to be first in line (after "inprogress") // inject all the animations we just queued to be first in line (after "inprogress")
if ( queuelen > 1) { if ( queuelen > 1) {
queue.splice.apply( queue, queue.splice.apply( queue,
[ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) );
} }
next(); el.dequeue();


}; };


Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.clip.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.clip = function( o, next ) { $.effects.effect.clip = function( o, done ) {
// Create element // Create element
var el = $( this ), var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "height", "width" ], props = [ "position", "top", "bottom", "left", "right", "height", "width" ],
Expand Down Expand Up @@ -57,10 +57,7 @@ $.effects.effect.clip = function( o, next ) {
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( el[ 0 ], arguments );
}
el.dequeue();
} }
}); });


Expand Down
14 changes: 13 additions & 1 deletion ui/jquery.effects.core.js
Expand Up @@ -556,7 +556,19 @@ $.fn.extend({
} }


function run( next ) { function run( next ) {
effectMethod.call( this, args, $.isFunction( next ) ? next : $.noop ); var elem = this,
complete = args.complete;

function done() {
if ( $.isFunction( complete ) ) {
complete.call( elem );
}
if ( $.isFunction( next ) ) {
next();
}
}

effectMethod.call( elem, args, done );
} }


// TODO: remove this check in 2.0, effectMethod will always be true // TODO: remove this check in 2.0, effectMethod will always be true
Expand Down
5 changes: 2 additions & 3 deletions ui/jquery.effects.drop.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.drop = function( o, next ) { $.effects.effect.drop = function( o, done ) {


var el = $( this ), var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "opacity", "height", "width" ], props = [ "position", "top", "bottom", "left", "right", "opacity", "height", "width" ],
Expand Down Expand Up @@ -54,8 +54,7 @@ $.effects.effect.drop = function( o, next ) {
mode == "hide" && el.hide(); mode == "hide" && el.hide();
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
$.isFunction( o.complete ) && o.complete.apply( this, arguments ); done();
next();
} }
}); });


Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.explode.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.explode = function( o, next ) { $.effects.effect.explode = function( o, done ) {


var rows = o.pieces ? Math.round( Math.sqrt( o.pieces ) ) : 3, var rows = o.pieces ? Math.round( Math.sqrt( o.pieces ) ) : 3,
cells = rows, cells = rows,
Expand Down Expand Up @@ -89,10 +89,7 @@ $.effects.effect.explode = function( o, next ) {
if ( !show ) { if ( !show ) {
el.hide(); el.hide();
} }
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( el[ 0 ] );
}
next();
} }
}; };


Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.fade.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.fade = function( o, next ) { $.effects.effect.fade = function( o, done ) {
var el = $( this ), var el = $( this ),
mode = $.effects.setMode( el, o.mode || "toggle" ), mode = $.effects.setMode( el, o.mode || "toggle" ),
hide = mode === "hide"; hide = mode === "hide";
Expand All @@ -28,10 +28,7 @@ $.effects.effect.fade = function( o, next ) {
if ( hide ) { if ( hide ) {
el.hide(); el.hide();
} }
if ( o.complete ) { done();
o.complete.call( this );
}
next();
} }
}); });
}; };
Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.fold.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.fold = function( o, next ) { $.effects.effect.fold = function( o, done ) {


// Create element // Create element
var el = $( this ), var el = $( this ),
Expand Down Expand Up @@ -66,10 +66,7 @@ $.effects.effect.fold = function( o, next ) {
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( el[ 0 ], arguments );
}
next();
}); });


}; };
Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.highlight.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.highlight = function( o, next ) { $.effects.effect.highlight = function( o, done ) {
var elem = $( this ), var elem = $( this ),
props = [ "backgroundImage", "backgroundColor", "opacity" ], props = [ "backgroundImage", "backgroundColor", "opacity" ],
mode = $.effects.setMode( elem, o.mode || "show" ), mode = $.effects.setMode( elem, o.mode || "show" ),
Expand Down Expand Up @@ -41,10 +41,7 @@ $.effects.effect.highlight = function( o, next ) {
elem.hide(); elem.hide();
} }
$.effects.restore( elem, props ); $.effects.restore( elem, props );
if ( $.isFunction( o.complete) ) { done();
o.complete.apply( this, arguments );
}
next();
} }
}); });
}; };
Expand Down
11 changes: 4 additions & 7 deletions ui/jquery.effects.pulsate.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.pulsate = function( o, next ) { $.effects.effect.pulsate = function( o, done ) {
var elem = $( this ), var elem = $( this ),
mode = $.effects.setMode( elem, o.mode || "show" ), mode = $.effects.setMode( elem, o.mode || "show" ),
show = mode === "show", show = mode === "show",
Expand Down Expand Up @@ -44,22 +44,19 @@ $.effects.effect.pulsate = function( o, next ) {
opacity: animateTo opacity: animateTo
}, duration, o.easing); }, duration, o.easing);


elem.queue( function( next ) { elem.queue(function() {
if ( hide ) { if ( hide ) {
elem.hide(); elem.hide();
} }
if ( o.complete ) { done();
o.complete.apply( this );
}
next();
}); });


// We just queued up "anims" animations, we need to put them next in the queue // We just queued up "anims" animations, we need to put them next in the queue
if ( queuelen > 1 ) { if ( queuelen > 1 ) {
queue.splice.apply( queue, queue.splice.apply( queue,
[ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) );
} }
next(); elem.dequeue();
}; };


})(jQuery); })(jQuery);
24 changes: 6 additions & 18 deletions ui/jquery.effects.scale.js
Expand Up @@ -12,16 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


function compFunction( el, complete, next ) { $.effects.effect.puff = function( o, done ) {
return function() {
if ( $.isFunction( complete ) ) {
complete.apply( el );
}
next();
};
};

$.effects.effect.puff = function( o, next ) {
var elem = $( this ), var elem = $( this ),
mode = $.effects.setMode( elem, o.mode || "hide" ), mode = $.effects.setMode( elem, o.mode || "hide" ),
hide = mode === "hide", hide = mode === "hide",
Expand All @@ -37,7 +28,7 @@ $.effects.effect.puff = function( o, next ) {
queue: false, queue: false,
fade: true, fade: true,
mode: mode, mode: mode,
complete: compFunction( this, o.complete, next ), complete: done,
percent: hide ? percent : 100, percent: hide ? percent : 100,
from: hide from: hide
? original ? original
Expand All @@ -50,7 +41,7 @@ $.effects.effect.puff = function( o, next ) {
elem.effect( o ); elem.effect( o );
}; };


$.effects.effect.scale = function( o, next ) { $.effects.effect.scale = function( o, done ) {


// Create element // Create element
var el = $( this ), var el = $( this ),
Expand All @@ -73,7 +64,7 @@ $.effects.effect.scale = function( o, next ) {
// We are going to pass this effect to the size effect: // We are going to pass this effect to the size effect:
options.effect = "size"; options.effect = "size";
options.queue = false; options.queue = false;
options.complete = compFunction( this, options.complete, next ); options.complete = done;


// Set default origin and restore for show/hide // Set default origin and restore for show/hide
if ( mode != "effect" ) { if ( mode != "effect" ) {
Expand Down Expand Up @@ -105,7 +96,7 @@ $.effects.effect.scale = function( o, next ) {


}; };


$.effects.effect.size = function( o, next ) { $.effects.effect.size = function( o, done ) {


// Create element // Create element
var el = $( this ), var el = $( this ),
Expand Down Expand Up @@ -302,10 +293,7 @@ $.effects.effect.size = function( o, next ) {
} }


$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( this, arguments );
}
next();
} }
}); });


Expand Down
11 changes: 4 additions & 7 deletions ui/jquery.effects.shake.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.shake = function( o, next ) { $.effects.effect.shake = function( o, done ) {


var el = $( this ), var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "height", "width" ], props = [ "position", "top", "bottom", "left", "right", "height", "width" ],
Expand Down Expand Up @@ -53,24 +53,21 @@ $.effects.effect.shake = function( o, next ) {
el el
.animate( animation1, speed, o.easing ) .animate( animation1, speed, o.easing )
.animate( animation, speed / 2, o.easing ) .animate( animation, speed / 2, o.easing )
.queue( function( next ) { .queue(function() {
if ( mode === "hide" ) { if ( mode === "hide" ) {
el.hide(); el.hide();
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( this, arguments );
}
next();
}); });


// inject all the animations we just queued to be first in line (after "inprogress") // inject all the animations we just queued to be first in line (after "inprogress")
if ( queuelen > 1) { if ( queuelen > 1) {
queue.splice.apply( queue, queue.splice.apply( queue,
[ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) );
} }
next(); el.dequeue();


}; };


Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.slide.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.slide = function( o, next ) { $.effects.effect.slide = function( o, done ) {


// Create element // Create element
var el = $( this ), var el = $( this ),
Expand Down Expand Up @@ -58,10 +58,7 @@ $.effects.effect.slide = function( o, next ) {
} }
$.effects.restore( el, props ); $.effects.restore( el, props );
$.effects.removeWrapper( el ); $.effects.removeWrapper( el );
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( this, arguments );
}
next();
} }
}); });


Expand Down
7 changes: 2 additions & 5 deletions ui/jquery.effects.transfer.js
Expand Up @@ -12,7 +12,7 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {


$.effects.effect.transfer = function( o, next ) { $.effects.effect.transfer = function( o, done ) {
var elem = $( this ), var elem = $( this ),
target = $( o.to ), target = $( o.to ),
targetFixed = target.css( "position" ) === "fixed", targetFixed = target.css( "position" ) === "fixed",
Expand All @@ -39,10 +39,7 @@ $.effects.effect.transfer = function( o, next ) {
}) })
.animate( animation, o.duration, o.easing, function() { .animate( animation, o.duration, o.easing, function() {
transfer.remove(); transfer.remove();
if ( $.isFunction( o.complete ) ) { done();
o.complete.apply( elem[0], arguments );
}
next();
}); });
}; };


Expand Down

0 comments on commit ab627e0

Please sign in to comment.