Effects core: Reorganized code, no actual changes.

This commit is contained in:
Scott González 2009-11-10 15:55:41 +00:00
parent 56c6def926
commit 16c5c3a9cb

View File

@ -9,320 +9,13 @@
*/
;jQuery.effects || (function($) {
$.effects = {
version: "@VERSION",
$.effects = {};
// Saves a set of properties in a data storage
save: function(element, set) {
for(var i=0; i < set.length; i++) {
if(set[i] !== null) element.data("ec.storage."+set[i], element[0].style[set[i]]);
}
},
// Restores a set of previously saved properties from a data storage
restore: function(element, set) {
for(var i=0; i < set.length; i++) {
if(set[i] !== null) element.css(set[i], element.data("ec.storage."+set[i]));
}
},
setMode: function(el, mode) {
if (mode == 'toggle') mode = el.is(':hidden') ? 'show' : 'hide'; // Set for toggle
return mode;
},
getBaseline: function(origin, original) { // Translates a [top,left] array into a baseline value
// this should be a little more flexible in the future to handle a string & hash
var y, x;
switch (origin[0]) {
case 'top': y = 0; break;
case 'middle': y = 0.5; break;
case 'bottom': y = 1; break;
default: y = origin[0] / original.height;
};
switch (origin[1]) {
case 'left': x = 0; break;
case 'center': x = 0.5; break;
case 'right': x = 1; break;
default: x = origin[1] / original.width;
};
return {x: x, y: y};
},
// Wraps the element around a wrapper that copies position properties
createWrapper: function(element) {
//if the element is already wrapped, return it
if (element.parent().is('.ui-effects-wrapper'))
return element.parent();
//Cache width,height and float properties of the element, and create a wrapper around it
var props = { width: element.outerWidth(true), height: element.outerHeight(true), 'float': element.css('float') };
element.wrap('<div class="ui-effects-wrapper" style="font-size:100%;background:transparent;border:none;margin:0;padding:0"></div>');
var wrapper = element.parent();
//Transfer the positioning of the element to the wrapper
if (element.css('position') == 'static') {
wrapper.css({ position: 'relative' });
element.css({ position: 'relative'} );
} else {
var top = element.css('top'); if(isNaN(parseInt(top,10))) top = 'auto';
var left = element.css('left'); if(isNaN(parseInt(left,10))) left = 'auto';
wrapper.css({ position: element.css('position'), top: top, left: left, zIndex: element.css('z-index') }).show();
element.css({position: 'relative', top: 0, left: 0 });
}
wrapper.css(props);
return wrapper;
},
removeWrapper: function(element) {
if (element.parent().is('.ui-effects-wrapper'))
return element.parent().replaceWith(element);
return element;
},
setTransition: function(element, list, factor, value) {
value = value || {};
$.each(list, function(i, x){
unit = element.cssUnit(x);
if (unit[0] > 0) value[x] = unit[0] * factor + unit[1];
});
return value;
},
//Base function to animate from one class to another in a seamless transition
animateClass: function(value, duration, easing, callback) {
if ($.isFunction(easing)) {
callback = easing;
easing = null;
}
return this.each(function() {
var that = $(this),
originalStyleAttr = that.attr('style') || ' ',
originalStyle = filterStyles(getElementStyles.call(this)),
newStyle,
className = that.attr('className');
$.each(classAnimationActions, function(i, action) {
if (value[action]) {
that[action + 'Class'](value[action]);
}
});
newStyle = filterStyles(getElementStyles.call(this));
that.attr('className', className);
that.animate(styleDifference(originalStyle, newStyle), duration, easing, function() {
$.each(classAnimationActions, function(i, action) {
if (value[action]) { that[action + 'Class'](value[action]); }
});
// work around bug in IE by clearing the cssText before setting it
if (typeof that.attr('style') == 'object') {
that.attr('style').cssText = '';
that.attr('style').cssText = originalStyleAttr;
} else {
that.attr('style', originalStyleAttr);
}
if (callback) { callback.apply(this, arguments); }
});
});
}
};
// start class animations
var classAnimationActions = ['add', 'remove', 'toggle'];
function getElementStyles() {
var style = document.defaultView
? document.defaultView.getComputedStyle(this, null)
: this.currentStyle,
newStyle = {},
key,
camelCase;
// webkit enumerates style porperties
if (style && style.length && style[0] && style[style[0]]) {
var len = style.length;
while (len--) {
key = style[len];
if (typeof style[key] == 'string') {
camelCase = key.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
newStyle[camelCase] = style[key];
}
}
} else {
for (key in style) {
if (typeof style[key] === 'string') {
newStyle[key] = style[key];
}
}
}
return newStyle;
}
function filterStyles(styles) {
var name, value;
for (name in styles) {
value = styles[name];
if (
// ignore null and undefined values
value == null ||
// ignore functions (when does this occur?)
$.isFunction(value) ||
// ignore Mozilla specific styles (Moz and length)
// ignore scrollbars (break in IE)
(/(Moz)|(length)|(scrollbar)/).test(name) ||
// only colors or values that can be converted to numbers
(!(/color/i).test(name) && isNaN(parseFloat(value)))
) {
delete styles[name];
}
}
return styles;
}
function styleDifference(oldStyle, newStyle) {
var diff = { _: 0 }, // http://dev.jquery.com/ticket/5459
name;
for (name in newStyle) {
if (oldStyle[name] != newStyle[name]) {
diff[name] = newStyle[name];
}
}
return diff;
}
// end class animations
function _normalizeArguments(effect, options, speed, callback) {
// shift params for method overloading
if (typeof effect == 'object') {
callback = options;
speed = null;
options = effect;
effect = options.effect;
}
if ($.isFunction(options)) {
callback = options;
speed = null;
options = {};
}
if (typeof options == 'number') {
callback = speed;
speed = options;
options = {};
}
options = options || {};
speed = speed || options.duration;
speed = $.fx.off ? 0 : typeof speed == 'number'
? speed : $.fx.speeds[speed] || $.fx.speeds._default;
callback = callback || options.complete;
return [effect, options, speed, callback];
}
$.fn.extend({
effect: function(effect, options, speed, callback) {
var args = _normalizeArguments.apply(this, arguments),
// TODO: make effects takes actual parameters instead of a hash
args2 = {
options: args[1],
duration: args[2],
callback: args[3]
},
effectMethod = $.effects[effect];
return effectMethod && !$.fx.off ? effectMethod.call(this, args2) : this;
},
_show: $.fn.show,
show: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._show.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'show';
return this.effect.apply(this, args);
}
},
_hide: $.fn.hide,
hide: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._hide.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'hide';
return this.effect.apply(this, args);
}
},
// jQuery core overloads toggle and create _toggle
__toggle: $.fn.toggle,
toggle: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed] ||
typeof speed == 'boolean' || $.isFunction(speed)) {
return this.__toggle.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'toggle';
return this.effect.apply(this, args);
}
},
_addClass: $.fn.addClass,
addClass: function(classNames, speed, easing, callback) {
return speed ? $.effects.animateClass.apply(this, [{ add: classNames },speed,easing,callback]) : this._addClass(classNames);
},
_removeClass: $.fn.removeClass,
removeClass: function(classNames,speed,easing,callback) {
return speed ? $.effects.animateClass.apply(this, [{ remove: classNames },speed,easing,callback]) : this._removeClass(classNames);
},
_toggleClass: $.fn.toggleClass,
toggleClass: function(classNames, force, speed, easing, callback) {
if ( typeof force == "boolean" || force === undefined ) {
if ( !speed ) {
// without speed parameter;
return this._toggleClass(classNames, force);
} else {
return $.effects.animateClass.apply(this, [(force?{add:classNames}:{remove:classNames}),speed,easing,callback]);
}
} else {
// without switch parameter;
return $.effects.animateClass.apply(this, [{ toggle: classNames },force,speed,easing]);
}
},
switchClass: function(remove,add,speed,easing,callback) {
return $.effects.animateClass.apply(this, [{ add: add, remove: remove },speed,easing,callback]);
},
// helper functions
cssUnit: function(key) {
var style = this.css(key), val = [];
$.each( ['em','px','%','pt'], function(i, unit){
if(style.indexOf(unit) > 0)
val = [parseFloat(style), unit];
});
return val;
}
});
/******************************************************************************/
/****************************** COLOR ANIMATIONS ******************************/
/******************************************************************************/
// override the animation for color styles
$.each(['backgroundColor', 'borderColor', 'borderBottomColor', 'borderLeftColor',
@ -445,6 +138,336 @@ var colors = {
transparent: [255,255,255]
};
/******************************************************************************/
/****************************** CLASS ANIMATIONS ******************************/
/******************************************************************************/
var classAnimationActions = ['add', 'remove', 'toggle'];
function getElementStyles() {
var style = document.defaultView
? document.defaultView.getComputedStyle(this, null)
: this.currentStyle,
newStyle = {},
key,
camelCase;
// webkit enumerates style porperties
if (style && style.length && style[0] && style[style[0]]) {
var len = style.length;
while (len--) {
key = style[len];
if (typeof style[key] == 'string') {
camelCase = key.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
newStyle[camelCase] = style[key];
}
}
} else {
for (key in style) {
if (typeof style[key] === 'string') {
newStyle[key] = style[key];
}
}
}
return newStyle;
}
function filterStyles(styles) {
var name, value;
for (name in styles) {
value = styles[name];
if (
// ignore null and undefined values
value == null ||
// ignore functions (when does this occur?)
$.isFunction(value) ||
// ignore Mozilla specific styles (Moz and length)
// ignore scrollbars (break in IE)
(/(Moz)|(length)|(scrollbar)/).test(name) ||
// only colors or values that can be converted to numbers
(!(/color/i).test(name) && isNaN(parseFloat(value)))
) {
delete styles[name];
}
}
return styles;
}
function styleDifference(oldStyle, newStyle) {
var diff = { _: 0 }, // http://dev.jquery.com/ticket/5459
name;
for (name in newStyle) {
if (oldStyle[name] != newStyle[name]) {
diff[name] = newStyle[name];
}
}
return diff;
}
$.effects.animateClass = function(value, duration, easing, callback) {
if ($.isFunction(easing)) {
callback = easing;
easing = null;
}
return this.each(function() {
var that = $(this),
originalStyleAttr = that.attr('style') || ' ',
originalStyle = filterStyles(getElementStyles.call(this)),
newStyle,
className = that.attr('className');
$.each(classAnimationActions, function(i, action) {
if (value[action]) {
that[action + 'Class'](value[action]);
}
});
newStyle = filterStyles(getElementStyles.call(this));
that.attr('className', className);
that.animate(styleDifference(originalStyle, newStyle), duration, easing, function() {
$.each(classAnimationActions, function(i, action) {
if (value[action]) { that[action + 'Class'](value[action]); }
});
// work around bug in IE by clearing the cssText before setting it
if (typeof that.attr('style') == 'object') {
that.attr('style').cssText = '';
that.attr('style').cssText = originalStyleAttr;
} else {
that.attr('style', originalStyleAttr);
}
if (callback) { callback.apply(this, arguments); }
});
});
};
$.fn.extend({
_addClass: $.fn.addClass,
addClass: function(classNames, speed, easing, callback) {
return speed ? $.effects.animateClass.apply(this, [{ add: classNames },speed,easing,callback]) : this._addClass(classNames);
},
_removeClass: $.fn.removeClass,
removeClass: function(classNames,speed,easing,callback) {
return speed ? $.effects.animateClass.apply(this, [{ remove: classNames },speed,easing,callback]) : this._removeClass(classNames);
},
_toggleClass: $.fn.toggleClass,
toggleClass: function(classNames, force, speed, easing, callback) {
if ( typeof force == "boolean" || force === undefined ) {
if ( !speed ) {
// without speed parameter;
return this._toggleClass(classNames, force);
} else {
return $.effects.animateClass.apply(this, [(force?{add:classNames}:{remove:classNames}),speed,easing,callback]);
}
} else {
// without switch parameter;
return $.effects.animateClass.apply(this, [{ toggle: classNames },force,speed,easing]);
}
},
switchClass: function(remove,add,speed,easing,callback) {
return $.effects.animateClass.apply(this, [{ add: add, remove: remove },speed,easing,callback]);
},
});
/******************************************************************************/
/*********************************** EFFECTS **********************************/
/******************************************************************************/
$.extend($.effects, {
version: "@VERSION",
// Saves a set of properties in a data storage
save: function(element, set) {
for(var i=0; i < set.length; i++) {
if(set[i] !== null) element.data("ec.storage."+set[i], element[0].style[set[i]]);
}
},
// Restores a set of previously saved properties from a data storage
restore: function(element, set) {
for(var i=0; i < set.length; i++) {
if(set[i] !== null) element.css(set[i], element.data("ec.storage."+set[i]));
}
},
setMode: function(el, mode) {
if (mode == 'toggle') mode = el.is(':hidden') ? 'show' : 'hide'; // Set for toggle
return mode;
},
getBaseline: function(origin, original) { // Translates a [top,left] array into a baseline value
// this should be a little more flexible in the future to handle a string & hash
var y, x;
switch (origin[0]) {
case 'top': y = 0; break;
case 'middle': y = 0.5; break;
case 'bottom': y = 1; break;
default: y = origin[0] / original.height;
};
switch (origin[1]) {
case 'left': x = 0; break;
case 'center': x = 0.5; break;
case 'right': x = 1; break;
default: x = origin[1] / original.width;
};
return {x: x, y: y};
},
// Wraps the element around a wrapper that copies position properties
createWrapper: function(element) {
//if the element is already wrapped, return it
if (element.parent().is('.ui-effects-wrapper'))
return element.parent();
//Cache width,height and float properties of the element, and create a wrapper around it
var props = { width: element.outerWidth(true), height: element.outerHeight(true), 'float': element.css('float') };
element.wrap('<div class="ui-effects-wrapper" style="font-size:100%;background:transparent;border:none;margin:0;padding:0"></div>');
var wrapper = element.parent();
//Transfer the positioning of the element to the wrapper
if (element.css('position') == 'static') {
wrapper.css({ position: 'relative' });
element.css({ position: 'relative'} );
} else {
var top = element.css('top'); if(isNaN(parseInt(top,10))) top = 'auto';
var left = element.css('left'); if(isNaN(parseInt(left,10))) left = 'auto';
wrapper.css({ position: element.css('position'), top: top, left: left, zIndex: element.css('z-index') }).show();
element.css({position: 'relative', top: 0, left: 0 });
}
wrapper.css(props);
return wrapper;
},
removeWrapper: function(element) {
if (element.parent().is('.ui-effects-wrapper'))
return element.parent().replaceWith(element);
return element;
},
setTransition: function(element, list, factor, value) {
value = value || {};
$.each(list, function(i, x){
unit = element.cssUnit(x);
if (unit[0] > 0) value[x] = unit[0] * factor + unit[1];
});
return value;
}
});
function _normalizeArguments(effect, options, speed, callback) {
// shift params for method overloading
if (typeof effect == 'object') {
callback = options;
speed = null;
options = effect;
effect = options.effect;
}
if ($.isFunction(options)) {
callback = options;
speed = null;
options = {};
}
if (typeof options == 'number') {
callback = speed;
speed = options;
options = {};
}
options = options || {};
speed = speed || options.duration;
speed = $.fx.off ? 0 : typeof speed == 'number'
? speed : $.fx.speeds[speed] || $.fx.speeds._default;
callback = callback || options.complete;
return [effect, options, speed, callback];
}
$.fn.extend({
effect: function(effect, options, speed, callback) {
var args = _normalizeArguments.apply(this, arguments),
// TODO: make effects takes actual parameters instead of a hash
args2 = {
options: args[1],
duration: args[2],
callback: args[3]
},
effectMethod = $.effects[effect];
return effectMethod && !$.fx.off ? effectMethod.call(this, args2) : this;
},
_show: $.fn.show,
show: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._show.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'show';
return this.effect.apply(this, args);
}
},
_hide: $.fn.hide,
hide: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed]) {
return this._hide.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'hide';
return this.effect.apply(this, args);
}
},
// jQuery core overloads toggle and create _toggle
__toggle: $.fn.toggle,
toggle: function(speed) {
if (!speed || typeof speed == 'number' || $.fx.speeds[speed] ||
typeof speed == 'boolean' || $.isFunction(speed)) {
return this.__toggle.apply(this, arguments);
} else {
var args = _normalizeArguments.apply(this, arguments);
args[1].mode = 'toggle';
return this.effect.apply(this, args);
}
},
// helper functions
cssUnit: function(key) {
var style = this.css(key), val = [];
$.each( ['em','px','%','pt'], function(i, unit){
if(style.indexOf(unit) > 0)
val = [parseFloat(style), unit];
});
return val;
}
});
/******************************************************************************/
/*********************************** EASING ***********************************/
/******************************************************************************/
/*
* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
*