r2479 - improved "option-management" added cache-option for better performance

r2479 - improved "option-management" added cache-option for better performance


Author: a.farkas.pm
Date: Sun Apr 19 14:11:34 2009
New Revision: 2479
Modified:
branches/dev/effects/ui/effects.core.js
Log:
improved "option-management" added cache-option for better performance
Modified: branches/dev/effects/ui/effects.core.js
==============================================================================
--- branches/dev/effects/ui/effects.core.js    (original)
+++ branches/dev/effects/ui/effects.core.js    Sun Apr 19 14:11:34 2009
@@ -111,37 +111,30 @@
});
$.effects.animateClass = function(value, duration, easing, callback){
- var clearInlineStyles, animateChilds, filterCallback, queue, cb, ea;
-    
-    if (typeof duration == 'object') {
- easing = duration.easing;
- callback = duration.complete;
- //new features
- clearInlineStyles = duration.clearInlineStyles;
- animateChilds = duration.animateChilds;
-        filterCallback = duration.filterCallback;
-        queue = duration.queue;
- //last
- duration = duration.duration;
- }
+    if (typeof duration != 'object') {
+        var oldDuration = duration;
+        duration = {
+            duration: duration,
+            complete: (typeof easing == 'function' ? easing : (callback ?
callback : null)),
+             easing: (typeof easing == 'string' ? easing : null)
+        };
+    }
    
-    filterCallback = filterCallback ||
-        function(){};
+    var opt = $.extend({}, $.effects.animateClass.defaults, duration),
+        cb = opt.complete;
+    delete opt.complete;
    
- cb = (typeof easing == 'function' ? easing : (callback ? callback :
null));
- ea = (typeof easing == 'string' ? easing : null);
-
function getElementOptions(){
        var elem = $(this),
            newStyle,
            oldStyleAttr = elem.attr('style') || ' ';
        
-        (clearInlineStyles &&
+        (opt.clearInlineStyles &&
            elem.attr('style', ' '));
            
        newStyle = getElementStyles.call(this);
        
- if(clearInlineStyles){
+ if(opt.clearInlineStyles){
            elem.attr('style', oldStyleAttr);
            oldStyleAttr = ' ';
        }
@@ -238,15 +231,12 @@
        return opts;
    }
    
- function createOptions(that, oldStyleAttr, childs){
+ function createOptions(that, oldStyleAttr, elements){
        var options,
            className = that[0].className,
-            elements = (childs[0]) ?
-                $($.merge(that.get(), childs.get())) :
-                that,
            oldStyles = elements.map(getElementStyles);
-
+
if (value.add)
that.addClass(value.add);
if (value.remove)
@@ -260,21 +250,20 @@
}
    
return this.each(function(){
-        
        var that = $(this),
-            childs = (animateChilds) ?
-                $(animateChilds, this) :
-                [],
+            elements = (opt.animateChilds) ?
+                $(opt.animateChilds, this).add(this) :
+                that,
                oldStyleAttr = that.attr('style') || ' ';
        if (value.toggle) {
that.hasClass(value.toggle) ? value.remove = value.toggle :
value.add = value.toggle;
}
        
- //Let's get a style offset
- var options = createOptions(that, oldStyleAttr, childs);
-        
-        var len = options.length,
+        var cacheID = 'aninmateClass-add'+ value.add + '-remove'+ value.remove,
+            cachedStyles = (opt.cacheStyles) ? that.data(cacheID) : false,
+            options = cachedStyles || createOptions(that, oldStyleAttr, elements),
+            len = options.length,
            i = len,
            complete = function(){
                (value.add &&
@@ -290,14 +279,33 @@
                }
            };
        
+        if(opt.cacheStyles && !cachedStyles){
+            that.data(cacheID, options);
+        }
+        
        //go reverse order main fx.Element is always last
while(i--){
            var option = options[i];
            //last chance to manipulate the animateStyles-object, set starting
and/or reset-styles
-            filterCallback.call(option.element[0], option);
-            option.element.animate(option.animateStyles, duration, ea, (!i) ?
complete : false);
+            opt.filterCallback.call(option.element[0], option);
+            
+            opt = $.extend({}, opt, (!i) ? {complete: complete} : null);
+            option.element.animate(option.animateStyles, opt);
        }
});
+};
+
+$.effects.animateClass.defaults = {
+    /*
+     * easing: undefined,
+     * duration: undefined,
+     * callback: undefined,
+     * queue: undefined    
+     */
+    animateChilds: false,
+    clearInlineStyles: true,
+    filterCallback: function(){},
+    cacheStyles: true
};
function _normalizeArguments(a, m) {