Starting to clean up the internal API for animations

This commit is contained in:
gnarf 2011-03-02 19:30:27 -06:00
parent 58b730f7c7
commit 2ad1cf3319
2 changed files with 41 additions and 43 deletions

View File

@ -17,17 +17,17 @@ $.effects.blind = function(o) {
return this.queue(function() { return this.queue(function() {
// Create element // Create element
var el = $(this), props = ['position','top','bottom','left','right']; var el = $(this),
props = ['position','top','bottom','left','right'],
// Set options mode = $.effects.setMode( el, o.mode || 'hide' ),
var mode = $.effects.setMode(el, o.options.mode || 'hide'); // Set Mode direction = o.direction || 'vertical',
var direction = o.options.direction || 'vertical'; // Default direction ref = (direction == 'vertical') ? 'height' : 'width',
wrapper, distance; // Default direction
// Adjust // Adjust
$.effects.save(el, props); el.show(); // Save & Show $.effects.save(el, props); el.show(); // Save & Show
var wrapper = $.effects.createWrapper(el).css({overflow:'hidden'}); // Create Wrapper wrapper = $.effects.createWrapper(el).css({overflow:'hidden'}); // Create Wrapper
var ref = (direction == 'vertical') ? 'height' : 'width'; distance = wrapper[ direction == 'vertical' ? "height" : "width" ]();
var distance = (direction == 'vertical') ? wrapper.height() : wrapper.width();
if(mode == 'show') wrapper.css(ref, 0); // Shift if(mode == 'show') wrapper.css(ref, 0); // Shift
// Animation // Animation
@ -35,10 +35,10 @@ $.effects.blind = function(o) {
animation[ref] = mode == 'show' ? distance : 0; animation[ref] = mode == 'show' ? distance : 0;
// Animate // Animate
wrapper.animate(animation, o.duration, o.options.easing, function() { wrapper.animate(animation, o.duration, o.easing, function() {
if(mode == 'hide') el.hide(); // Hide if(mode == 'hide') el.hide(); // Hide
$.effects.restore(el, props); $.effects.removeWrapper(el); // Restore $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
if(o.callback) o.callback.apply(el[0], arguments); // Callback if(o.complete) o.complete.apply(el[0], arguments); // Callback
el.dequeue(); el.dequeue();
}); });

View File

@ -410,15 +410,17 @@ $.extend($.effects, {
} }
}); });
// return an effect options object for the given parameters:
function _normalizeArguments( effect, options, speed, callback ) { function _normalizeArguments( effect, options, speed, callback ) {
// shift params for method overloading var effectObj = {
if (typeof effect == 'object') { effect: effect
callback = options; };
speed = null;
options = effect; // passed an effect options object:
effect = options.effect; if ( $.isPlainObject( effect ) ) {
return effect;
} }
if ( $.isFunction(options) ) { if ( $.isFunction(options) ) {
callback = options; callback = options;
speed = null; speed = null;
@ -434,15 +436,17 @@ function _normalizeArguments(effect, options, speed, callback) {
speed = null; speed = null;
} }
options = options || {}; if ( options ) {
$.extend( effectObj, options );
}
speed = speed || options.duration; speed = speed || options.duration;
speed = $.fx.off ? 0 : typeof speed == 'number' effectObj.duration = $.fx.off ? 0 : typeof speed == 'number'
? speed : speed in $.fx.speeds ? $.fx.speeds[speed] : $.fx.speeds._default; ? speed : speed in $.fx.speeds ? $.fx.speeds[speed] : $.fx.speeds._default;
callback = callback || options.complete; effectObj.complete = callback || options.complete;
return [effect, options, speed, callback]; return effectObj;
} }
function standardSpeed( speed ) { function standardSpeed( speed ) {
@ -462,29 +466,23 @@ function standardSpeed( speed ) {
$.fn.extend({ $.fn.extend({
effect: function(effect, options, speed, callback) { effect: function(effect, options, speed, callback) {
var args = _normalizeArguments.apply(this, arguments), var args = _normalizeArguments.apply(this, arguments),
// TODO: make effects take actual parameters instead of a hash mode = args.mode,
args2 = { effectMethod = $.effects[args.effect];
options: args[1],
duration: args[2],
callback: args[3]
},
mode = args2.options.mode,
effectMethod = $.effects[effect];
if ( $.fx.off || !effectMethod ) { if ( $.fx.off || !effectMethod ) {
// delegate to the original method (e.g., .show()) if possible // delegate to the original method (e.g., .show()) if possible
if ( mode ) { if ( mode ) {
return this[ mode ]( args2.duration, args2.callback ); return this[ mode ]( args.duration, args.callback );
} else { } else {
return this.each(function() { return this.each(function() {
if ( args2.callback ) { if ( args.callback ) {
args2.callback.call( this ); args.callback.call( this );
} }
}); });
} }
} }
return effectMethod.call(this, args2); return effectMethod.call(this, args);
}, },
_show: $.fn.show, _show: $.fn.show,
@ -493,8 +491,8 @@ $.fn.extend({
return this._show.apply(this, arguments); return this._show.apply(this, arguments);
} else { } else {
var args = _normalizeArguments.apply(this, arguments); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'show'; args.mode = 'show';
return this.effect.apply(this, args); return this.effect.call(this, args);
} }
}, },
@ -504,8 +502,8 @@ $.fn.extend({
return this._hide.apply(this, arguments); return this._hide.apply(this, arguments);
} else { } else {
var args = _normalizeArguments.apply(this, arguments); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'hide'; args.mode = 'hide';
return this.effect.apply(this, args); return this.effect.call(this, args);
} }
}, },
@ -516,8 +514,8 @@ $.fn.extend({
return this.__toggle.apply(this, arguments); return this.__toggle.apply(this, arguments);
} else { } else {
var args = _normalizeArguments.apply(this, arguments); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'toggle'; args.mode = 'toggle';
return this.effect.apply(this, args); return this.effect.call(this, args);
} }
}, },