/* ****************************** Semantic Module: Transition Author: Jack Lukic Notes: First Commit March 25, 2013 A module for controlling css animations ****************************** */ ;(function ( $, window, document, undefined ) { $.fn.transition = function() { var $allModules = $(this), moduleSelector = $allModules.selector || '', time = new Date().getTime(), performance = [], moduleArguments = arguments, query = moduleArguments[0], queryArguments = [].slice.call(arguments, 1), methodInvoked = (typeof query === 'string'), requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { setTimeout(callback, 0); }, invokedResponse ; $allModules .each(function() { var $module = $(this), element = this, // set at run time settings, instance, error, className, metadata, animationEnd, animationName, namespace, moduleNamespace, module ; module = { initialize: function() { // get settings settings = module.get.settings.apply(element, moduleArguments); module.verbose('Converted arguments into settings object', settings); // set shortcuts error = settings.error; className = settings.className; namespace = settings.namespace; metadata = settings.metadata; moduleNamespace = 'module-' + namespace; animationEnd = module.get.animationEvent(); animationName = module.get.animationName(); instance = $module.data(moduleNamespace); if(instance === undefined) { module.instantiate(); } if(methodInvoked) { methodInvoked = module.invoke(query); } // no internal method was found matching query or query not made if(methodInvoked === false) { module.animate(); } }, instantiate: function() { module.verbose('Storing instance of module', module); instance = module; $module .data(moduleNamespace, instance) ; }, destroy: function() { module.verbose('Destroying previous module for', element); $module .removeData(moduleNamespace) ; }, animate: function(overrideSettings) { settings = overrideSettings || settings; module.debug('Preparing animation', settings.animation); if(module.is.animating()) { module.queue(settings.animation); return false; } module.save.conditions(); module.set.duration(settings.duration); module.set.animating(); module.repaint(); $module .addClass(className.transition) .addClass(settings.animation) .one(animationEnd, module.complete) ; if(!module.has.direction() && module.can.transition()) { module.set.direction(); } if(!module.can.animate()) { module.restore.conditions(); module.error(error.noAnimation); return false; } module.show(); module.debug('Starting tween', settings.animation, $module.attr('class')); }, queue: function(animation) { module.debug('Queueing animation of', animation); instance.queuing = true; $module .one(animationEnd, function() { instance.queuing = false; module.animate.apply(this, settings); }) ; }, complete: function () { module.verbose('CSS animation complete', settings.animation); if(!module.is.looping()) { if($module.hasClass(className.outward)) { module.restore.conditions(); module.hide(); } else if($module.hasClass(className.inward)) { module.restore.conditions(); module.show(); } else { module.restore.conditions(); } module.remove.animating(); } $.proxy(settings.complete, this)(); }, repaint: function(fakeAssignment) { module.verbose('Forcing repaint event'); fakeAssignment = element.offsetWidth; }, has: { direction: function(animation) { animation = animation || settings.animation; if( $module.hasClass(className.inward) || $module.hasClass(className.outward) ) { return true; } } }, set: { animating: function() { $module.addClass(className.animating); }, direction: function() { if($module.is(':visible')) { module.debug('Automatically determining the direction of animation', 'Outward'); $module .addClass(className.outward) .removeClass(className.inward) ; } else { module.debug('Automatically determining the direction of animation', 'Inward'); $module .addClass(className.inward) .removeClass(className.outward) ; } }, looping: function() { module.debug('Transition set to loop'); $module .addClass(className.looping) ; }, duration: function(duration) { duration = duration || settings.duration; duration = (typeof duration == 'number') ? duration + 'ms' : duration ; module.verbose('Setting animation duration', duration); $module .css({ '-webkit-animation-duration': duration, '-moz-animation-duration': duration, '-ms-animation-duration': duration, '-o-animation-duration': duration, 'animation-duration': duration }) ; } }, save: { conditions: function() { module.cache = { className : $module.attr('class'), style : $module.attr('style') }; module.verbose('Saving original attributes', module.cache); } }, restore: { conditions: function() { if(typeof module.cache === undefined) { module.error(error.cache); return false; } if(module.cache.className) { $module.attr('class', module.cache.className); } else { $module.removeAttr('class'); } if(module.cache.style) { $module.attr('style', module.cache.style); } else { $module.removeAttr('style'); } if(module.is.looping()) { module.remove.looping(); } module.verbose('Restoring original attributes', module.cache); } }, remove: { animating: function() { $module.removeClass(className.animating); }, looping: function() { module.debug('Transitions are no longer looping'); $module .removeClass(className.looping) ; module.repaint(); } }, get: { settings: function(animation, duration, complete) { // single settings object if($.isPlainObject(animation)) { return $.extend(true, {}, $.fn.transition.settings, animation); } // all arguments provided else if(typeof complete == 'function') { return $.extend(true, {}, $.fn.transition.settings, { animation : animation, complete : complete, duration : duration }); } // only duration provided else if(typeof duration == 'string' || typeof duration == 'number') { return $.extend(true, {}, $.fn.transition.settings, { animation : animation, duration : duration }); } // duration is actually settings object else if(typeof duration == 'object') { return $.extend(true, {}, $.fn.transition.settings, duration, { animation : animation }); } // duration is actually callback else if(typeof duration == 'function') { return $.extend(true, {}, $.fn.transition.settings, { animation : animation, complete : duration }); } // only animation provided else { return $.extend(true, {}, $.fn.transition.settings, { animation : animation }); } return $.fn.transition.settings; }, animationName: function() { var element = document.createElement('div'), animations = { 'animation' :'animationName', 'OAnimation' :'oAnimationName', 'MozAnimation' :'mozAnimationName', 'WebkitAnimation' :'webkitAnimationName' }, animation ; for(animation in animations){ if( element.style[animation] !== undefined ){ module.verbose('Determining animation vendor name property', animations[animation]); return animations[animation]; } } return false; }, animationEvent: function() { var element = document.createElement('div'), animations = { 'animation' :'animationend', 'OAnimation' :'oAnimationEnd', 'MozAnimation' :'mozAnimationEnd', 'WebkitAnimation' :'webkitAnimationEnd' }, animation ; for(animation in animations){ if( element.style[animation] !== undefined ){ module.verbose('Determining animation vendor end event', animations[animation]); return animations[animation]; } } return false; } }, can: { animate: function() { if($module.css(animationName) !== 'none') { module.debug('CSS definition found'); return true; } else { module.debug('Unable to find css definition'); return false; } }, transition: function() { var $clone = $('