/* * # Semantic - Transition * http://github.com/jlukic/semantic-ui/ * * * Copyright 2013 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT * */ ;(function ( $, window, document, undefined ) { module.exports = 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); }, returnedValue ; $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) || module; if(methodInvoked) { methodInvoked = module.invoke(query); } // no internal method was found matching query or query not made if(methodInvoked === false) { module.animate(); } module.instantiate(); }, instantiate: function() { module.verbose('Storing instance of module', module); $module .data(moduleNamespace, instance) ; }, destroy: function() { module.verbose('Destroying previous module for', element); $module .removeData(moduleNamespace) ; }, refresh: function() { module.verbose('Refreshing display type on next animation'); delete instance.displayType; }, forceRepaint: function() { module.verbose('Forcing element repaint'); var $parentElement = $module.parent(), $nextElement = $module.next() ; if($nextElement.size() === 0) { $module.detach().appendTo($parentElement); } else { $module.detach().insertBefore($nextElement); } }, repaint: function() { module.verbose('Repainting element'); var fakeAssignment = element.offsetWidth ; }, animate: function(overrideSettings) { settings = overrideSettings || settings; if(!module.is.supported()) { module.error(error.support); return false; } module.debug('Preparing animation', settings.animation); if(module.is.animating() && settings.queue) { if(!settings.allowRepeats && module.has.direction() && module.is.occuring() && instance.queuing !== true) { module.error(error.repeated); } else { module.queue(settings.animation); } return false; } if(module.can.animate) { module.set.animating(settings.animation); } else { module.error(error.noAnimation, settings.animation); } }, reset: function() { module.debug('Resetting animation to beginning conditions'); $module.off(animationEnd); module.restore.conditions(); module.hide(); module.remove.animating(); }, queue: function(animation) { module.debug('Queueing animation of', animation); instance.queuing = true; $module .one(animationEnd, function() { instance.queuing = false; module.repaint(); module.animate.apply(this, settings); }) ; }, complete: function () { module.verbose('CSS animation complete', settings.animation); if(!module.is.looping()) { if( module.is.outward() ) { module.verbose('Animation is outward, hiding element'); module.restore.conditions(); module.remove.display(); module.hide(); $.proxy(settings.onHide, this)(); } else if( module.is.inward() ) { module.verbose('Animation is outward, showing element'); module.restore.conditions(); module.show(); $.proxy(settings.onShow, this)(); } else { module.restore.conditions(); } module.remove.duration(); module.remove.animating(); } $.proxy(settings.complete, this)(); }, has: { direction: function(animation) { animation = animation || settings.animation; if( animation.search(className.inward) !== -1 || animation.search(className.outward) !== -1) { module.debug('Direction already set in animation'); return true; } return false; } }, set: { animating: function(animation) { animation = animation || settings.animation; module.save.conditions(); if(module.can.transition() && !module.has.direction()) { module.set.direction(); } module.remove.hidden(); module.set.display(); $module .addClass(className.animating) .addClass(className.transition) .addClass(animation) .one(animationEnd, module.complete) ; module.set.duration(settings.duration); module.debug('Starting tween', settings.animation, $module.attr('class')); }, display: function() { var displayType = module.get.displayType() ; if(displayType !== 'block') { module.verbose('Setting final visibility to', displayType); $module .css({ display: displayType }) ; } }, direction: function() { if($module.is(':visible')) { module.debug('Automatically determining the direction of animation', 'Outward'); $module .removeClass(className.inward) .addClass(className.outward) ; } else { module.debug('Automatically determining the direction of animation', 'Inward'); $module .removeClass(className.outward) .addClass(className.inward) ; } }, 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 }) ; }, hidden: function() { $module .addClass(className.transition) .addClass(className.hidden) ; }, visible: function() { $module .addClass(className.transition) .addClass(className.visible) ; } }, save: { displayType: function(displayType) { instance.displayType = displayType; }, transitionExists: function(animation, exists) { module.exports.exists[animation] = exists; module.verbose('Saving existence of transition', animation, exists); }, conditions: function() { instance.cache = { className : $module.attr('class'), style : $module.attr('style') }; module.verbose('Saving original attributes', instance.cache); } }, restore: { conditions: function() { if(instance.cache === undefined) { return false; } if(instance.cache.className) { $module.attr('class', instance.cache.className); } else { $module.removeAttr('class'); } if(instance.cache.style) { $module.attr('style', instance.cache.style); } else { if(module.get.displayType() === 'block') { $module.removeAttr('style'); } } if(module.is.looping()) { module.remove.looping(); } module.verbose('Restoring original attributes', instance.cache); } }, remove: { animating: function() { $module.removeClass(className.animating); }, display: function() { if(instance.displayType !== undefined) { $module.css('display', ''); } }, duration: function() { $module .css({ '-webkit-animation-duration' : '', '-moz-animation-duration' : '', '-ms-animation-duration' : '', '-o-animation-duration' : '', 'animation-duration' : '' }) ; }, hidden: function() { $module.removeClass(className.hidden); }, visible: function() { $module.removeClass(className.visible); }, looping: function() { module.debug('Transitions are no longer looping'); $module .removeClass(className.looping) ; module.forceRepaint(); } }, get: { settings: function(animation, duration, complete) { // single settings object if(typeof animation == 'object') { return $.extend(true, {}, module.exports.settings, animation); } // all arguments provided else if(typeof complete == 'function') { return $.extend({}, module.exports.settings, { animation : animation, complete : complete, duration : duration }); } // only duration provided else if(typeof duration == 'string' || typeof duration == 'number') { return $.extend({}, module.exports.settings, { animation : animation, duration : duration }); } // duration is actually settings object else if(typeof duration == 'object') { return $.extend({}, module.exports.settings, duration, { animation : animation }); } // duration is actually callback else if(typeof duration == 'function') { return $.extend({}, module.exports.settings, { animation : animation, complete : duration }); } // only animation provided else { return $.extend({}, module.exports.settings, { animation : animation }); } return module.exports.settings; }, displayType: function() { if(instance.displayType === undefined) { // create fake element to determine display state module.can.transition(); } return instance.displayType; }, transitionExists: function(animation) { return module.exports.exists[animation]; }, 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('Determined animation vendor name property', animations[animation]); return animations[animation]; } } return false; }, animationEvent: function() { var element = document.createElement('div'), animations = { 'animation' :'animationend', 'OAnimation' :'oAnimationEnd', 'MozAnimation' :'animationend', 'WebkitAnimation' :'webkitAnimationEnd' }, animation ; for(animation in animations){ if( element.style[animation] !== undefined ){ module.verbose('Determined animation vendor end event', animations[animation]); return animations[animation]; } } return false; } }, can: { animate: function() { if($module.css(settings.animation) !== 'none') { module.debug('CSS definition found', $module.css(settings.animation)); return true; } else { module.debug('Unable to find css definition', $module.attr('class')); return false; } }, transition: function() { var elementClass = $module.attr('class'), animation = settings.animation, transitionExists = module.get.transitionExists(settings.animation), $clone, currentAnimation, inAnimation, displayType ; if( transitionExists === undefined || instance.displayType === undefined) { module.verbose('Determining whether animation exists'); $clone = $('