1;(function($) { 2 3 function animateAuto(element, options, duration, easing, callback) { 4 5 var $el = $(element), 6 settings = $.extend({}, $.fn.animateAuto.defaults, options), 7 dimension = settings.dimension, 8 oppositeDimension = (dimension === 'height') ? 'width' : 'height'; 9 10 // Determine which function to run based on the setting `action`. 11 switch (settings.action) { 12 case ('open'): 13 openEl($el); 14 break; 15 case ('close'): 16 closeEl($el); 17 break; 18 case ('toggle'): 19 toggleEl($el); 20 break; 21 default: 22 throw new Error('jquery.animateAuto only performs the actions "open", "close" and "toggle". You seem to have tried something else.'); 23 } 24 25 function getTargetDimension($el) { 26 // Create a hidden clone of $el, appended to 27 // $el's parent and with $el's `oppositeDimension`, 28 // to ensure it will have dimensions tailored to 29 // $el's context. 30 // Return the clone's relevant dimension. 31 var $clone = $el.clone() 32 .css({ 33 oppositeDimension: $el.css(oppositeDimension), 34 'visibility': 'hidden' 35 }) 36 .appendTo($el.parent()); 37 var cloneContentDimension = $clone 38 .css(dimension, 'auto') 39 .css(dimension); 40 $clone.remove(); 41 return cloneContentDimension; 42 } 43 44 function openEl($el) { 45 // Pass jQuery.animate() $el's target dimension 46 // and all the other parameters. 47 // As part of the callback, set $el's 48 // inline-style dimension to `auto`. 49 // And add the `openClass`. 50 if (!$el.hasClass(settings.openClass)) { 51 var animObj = {}; 52 animObj[dimension] = getTargetDimension($el); 53 $el.animate(animObj, duration, easing, function() { 54 $el.css(dimension, 'auto'); 55 callback(); 56 }) 57 .addClass(settings.openClass); 58 } 59 } 60 61 function closeEl($el) { 62 // Pass jQuery.animate() $el's `closed` 63 // and all the other parameters. 64 // And remove the `openClass`. 65 if ($el.css(dimension) !== settings.closed) { 66 var animObj = {}; 67 animObj[dimension] = settings.closed; 68 $el.animate(animObj, duration, easing, callback) 69 .removeClass(settings.openClass); 70 } 71 } 72 73 function toggleEl($el) { 74 if ($el.hasClass(settings.openClass)) { 75 closeEl($el); 76 } 77 else { 78 openEl($el); 79 } 80 } 81 } 82 83 function processArgs() { 84 // User can pass the 4 possible arguments in any order. 85 // `options` are plugins-specific settings. 86 // The options `dimensions` and `action` can also 87 // be passed as isolated strings. 88 // `duration`, `easing`, and `callback` corresponds to 89 // (and become) jQuery.animate() arguments. 90 var options = {}, 91 callback = function(){}, 92 duration, easing; 93 var l = arguments.length; 94 for (var i=0;i<l;i++) { 95 var arg = arguments[i], 96 argType = typeof arg; 97 if (!arg) { 98 continue; 99 } 100 // Check for pre-established string values. 101 switch (arg) { 102 // Check for `dimension` string. 103 case 'height': 104 case 'width': 105 $.extend(options, { dimension: arg }); 106 continue; 107 // Check for `action` string. 108 case 'open': 109 case 'close': 110 case 'toggle': 111 $.extend(options, { action: arg }); 112 continue; 113 // Check for `duration` string (in jQuery API). 114 case 'fast': 115 case 'slow': 116 duration = arg; 117 continue; 118 } 119 // For other arguments. 120 switch (argType) { 121 // Numbers will always be durations. 122 case 'number': 123 duration = arg; 124 continue; 125 // Strings, after above filtering, will 126 // always be easing. 127 case 'string': 128 easing = arg; 129 continue; 130 // Functions will always be callbacks. 131 case 'function': 132 callback = arg; 133 continue; 134 // Objects will always be arguments. 135 case 'object': 136 $.extend(options, arg); 137 continue; 138 } 139 } 140 return [options, duration, easing, callback]; 141 } 142 143 $.fn.animateAuto = function() { 144 var argsArray = processArgs.apply(this, arguments); 145 return this.each(function () { 146 animateAuto.apply(null, [this].concat(argsArray)); 147 }); 148 }; 149 150 $.fn.animateAuto.defaults = { 151 dimension: 'height', // or 'width' 152 action: 'toggle', // or 'open' or 'close' 153 closed: 0, 154 openClass: 'is-opened' 155 }; 156 157})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.