Effects core: Refactored show/hide/toggle methods.

This commit is contained in:
Scott González 2009-06-10 01:22:38 +00:00
parent e318d42f13
commit 347f86d99a

View File

@ -131,15 +131,28 @@ $.effects = {
}; };
function _normalizeArguments(a, m) { function _normalizeArguments(effect, options, speed, callback) {
// shift params for method overloading
if ($.isFunction(options)) {
callback = options;
speed = null;
options = {};
}
if (typeof options == 'number') {
callback = speed;
speed = options;
options = {};
}
var o = a[1] && a[1].constructor == Object ? a[1] : {}; if(m) o.mode = m; options = options || {};
var speed = a[1] && a[1].constructor != Object ? a[1] : (o.duration ? o.duration : a[2]); //either comes from options.duration or the secon/third argument
speed = $.fx.off ? 0 : typeof speed === "number" ? speed : $.fx.speeds[speed] || $.fx.speeds._default;
var callback = o.callback || ( $.isFunction(a[1]) && a[1] ) || ( $.isFunction(a[2]) && a[2] ) || ( $.isFunction(a[3]) && a[3] );
return [a[0], o, speed, callback]; speed = speed || options.duration;
speed = $.fx.off ? 0 : typeof speed == 'number'
? speed : $.fx.speeds[speed] || $.fx.speeds._default;
callback = callback || options.callback;
return [effect, options, speed, callback];
} }
//Extend the methods of jQuery //Extend the methods of jQuery
@ -158,29 +171,34 @@ $.fn.extend({
return $.effects[fx] ? $.effects[fx].call(this, {method: fx, options: options || {}, duration: speed, callback: callback }) : null; return $.effects[fx] ? $.effects[fx].call(this, {method: fx, options: options || {}, duration: speed, callback: callback }) : null;
}, },
show: function() { show: function(speed) {
if(!arguments[0] || (arguments[0].constructor == Number || (/(slow|normal|fast)/).test(arguments[0]))) if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._show.apply(this, arguments); return this._show.apply(this, arguments);
else { } else {
return this.effect.apply(this, _normalizeArguments(arguments, 'show')); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'show';
return this.effect.apply(this, args);
} }
}, },
hide: function() { hide: function(speed) {
if(!arguments[0] || (arguments[0].constructor == Number || (/(slow|normal|fast)/).test(arguments[0]))) if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._hide.apply(this, arguments); return this._hide.apply(this, arguments);
else { } else {
return this.effect.apply(this, _normalizeArguments(arguments, 'hide')); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'hide';
return this.effect.apply(this, args);
} }
}, },
toggle: function(){ toggle: function(speed) {
if(!arguments[0] || if (!speed || typeof speed == 'number' || $.fx.speeds[speed] ||
(arguments[0].constructor == Number || (/(slow|normal|fast)/).test(arguments[0])) || typeof speed == 'boolean' || $.isFunction(speed)) {
($.isFunction(arguments[0]) || typeof arguments[0] == 'boolean')) {
return this.__toggle.apply(this, arguments); return this.__toggle.apply(this, arguments);
} else { } else {
return this.effect.apply(this, _normalizeArguments(arguments, 'toggle')); var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'toggle';
return this.effect.apply(this, args);
} }
}, },