/* * # Semantic - Shape * http://github.com/jlukic/semantic-ui/ * * * Copyright 2013 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT * */ ;(function ( $, window, document, undefined ) { $.fn.shape = function(parameters) { var $allModules = $(this), time = new Date().getTime(), performance = [], query = arguments[0], methodInvoked = (typeof query == 'string'), queryArguments = [].slice.call(arguments, 1), invokedResponse ; $allModules .each(function() { var moduleSelector = $allModules.selector || '', settings = $.extend(true, {}, $.fn.shape.settings, parameters), // internal aliases namespace = settings.namespace, selector = settings.selector, error = settings.error, className = settings.className, // define namespaces for modules eventNamespace = '.' + namespace, moduleNamespace = 'module-' + namespace, // selector cache $module = $(this), $sides = $module.find(selector.sides), $side = $module.find(selector.side), // private variables $activeSide, $nextSide, // standard module element = this, instance = $module.data(moduleNamespace), module ; module = { initialize: function() { module.verbose('Initializing module for', element); module.set.defaultSide(); module.instantiate(); }, 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) .off(eventNamespace) ; }, refresh: function() { module.verbose('Refreshing selector cache for', element); $module = $(element); $sides = $(this).find(selector.shape); $side = $(this).find(selector.side); }, repaint: function() { module.verbose('Forcing repaint event'); var shape = $sides.get(0) || document.createElement('div'), fakeAssignment = shape.offsetWidth ; }, animate: function(propertyObject, callback) { module.verbose('Animating box with properties', propertyObject); callback = callback || function(event) { module.verbose('Executing animation callback'); if(event !== undefined) { event.stopPropagation(); } module.reset(); module.set.active(); }; if(settings.useCSS) { if(module.get.transitionEvent()) { module.verbose('Starting CSS animation'); $module .addClass(className.animating) ; module.set.stageSize(); module.repaint(); $module .addClass(className.css) ; $activeSide .addClass(className.hidden) ; $sides .css(propertyObject) .one(module.get.transitionEvent(), callback) ; } else { callback(); } } else { // not yet supported until .animate() is extended to allow RotateX/Y module.verbose('Starting javascript animation'); $module .addClass(className.animating) .removeClass(className.css) ; module.set.stageSize(); module.repaint(); $activeSide .animate({ opacity: 0 }, settings.duration, settings.easing) ; $sides .animate(propertyObject, settings.duration, settings.easing, callback) ; } }, queue: function(method) { module.debug('Queueing animation of', method); $sides .one(module.get.transitionEvent(), function() { module.debug('Executing queued animation'); setTimeout(function(){ $module.shape(method); }, 0); }) ; }, reset: function() { module.verbose('Animating states reset'); $module .removeClass(className.css) .removeClass(className.animating) .attr('style', '') .removeAttr('style') ; // removeAttr style does not consistently work in safari $sides .attr('style', '') .removeAttr('style') ; $side .attr('style', '') .removeAttr('style') .removeClass(className.hidden) ; $nextSide .removeClass(className.animating) .attr('style', '') .removeAttr('style') ; }, is: { animating: function() { return $module.hasClass(className.animating); } }, get: { transform: { up: function() { var translate = { y: -(($activeSide.outerHeight() - $nextSide.outerHeight()) / 2), z: -($activeSide.outerHeight() / 2) } ; return { transform: 'translateY(' + translate.y + 'px) translateZ('+ translate.z + 'px) rotateX(-90deg)' }; }, down: function() { var translate = { y: -(($activeSide.outerHeight() - $nextSide.outerHeight()) / 2), z: -($activeSide.outerHeight() / 2) } ; return { transform: 'translateY(' + translate.y + 'px) translateZ('+ translate.z + 'px) rotateX(90deg)' }; }, left: function() { var translate = { x : -(($activeSide.outerWidth() - $nextSide.outerWidth()) / 2), z : -($activeSide.outerWidth() / 2) } ; return { transform: 'translateX(' + translate.x + 'px) translateZ(' + translate.z + 'px) rotateY(90deg)' }; }, right: function() { var translate = { x : -(($activeSide.outerWidth() - $nextSide.outerWidth()) / 2), z : -($activeSide.outerWidth() / 2) } ; return { transform: 'translateX(' + translate.x + 'px) translateZ(' + translate.z + 'px) rotateY(-90deg)' }; }, over: function() { var translate = { x : -(($activeSide.outerWidth() - $nextSide.outerWidth()) / 2) } ; return { transform: 'translateX(' + translate.x + 'px) rotateY(180deg)' }; }, back: function() { var translate = { x : -(($activeSide.outerWidth() - $nextSide.outerWidth()) / 2) } ; return { transform: 'translateX(' + translate.x + 'px) rotateY(-180deg)' }; } }, transitionEvent: function() { var element = document.createElement('element'), transitions = { 'transition' :'transitionend', 'OTransition' :'oTransitionEnd', 'MozTransition' :'transitionend', 'WebkitTransition' :'webkitTransitionEnd' }, transition ; for(transition in transitions){ if( element.style[transition] !== undefined ){ return transitions[transition]; } } }, nextSide: function() { return ( $activeSide.next(selector.side).size() > 0 ) ? $activeSide.next(selector.side) : $module.find(selector.side).first() ; } }, set: { defaultSide: function() { $activeSide = $module.find('.' + settings.className.active); $nextSide = ( $activeSide.next(selector.side).size() > 0 ) ? $activeSide.next(selector.side) : $module.find(selector.side).first() ; module.verbose('Active side set to', $activeSide); module.verbose('Next side set to', $nextSide); }, stageSize: function() { var stage = { width : $nextSide.outerWidth(), height : $nextSide.outerHeight() } ; module.verbose('Resizing stage to fit new content', stage); $module .css({ width : stage.width, height : stage.height }) ; }, nextSide: function(selector) { $nextSide = $module.find(selector); if($nextSide.size() === 0) { module.error(error.side); } module.verbose('Next side manually set to', $nextSide); }, active: function() { module.verbose('Setting new side to active', $nextSide); $side .removeClass(className.active) ; $nextSide .addClass(className.active) ; $.proxy(settings.onChange, $nextSide)(); module.set.defaultSide(); } }, flip: { up: function() { module.debug('Flipping up', $nextSide); if( !module.is.animating() ) { module.stage.above(); module.animate( module.get.transform.up() ); } else { module.queue('flip up'); } }, down: function() { module.debug('Flipping down', $nextSide); if( !module.is.animating() ) { module.stage.below(); module.animate( module.get.transform.down() ); } else { module.queue('flip down'); } }, left: function() { module.debug('Flipping left', $nextSide); if( !module.is.animating() ) { module.stage.left(); module.animate(module.get.transform.left() ); } else { module.queue('flip left'); } }, right: function() { module.debug('Flipping right', $nextSide); if( !module.is.animating() ) { module.stage.right(); module.animate(module.get.transform.right() ); } else { module.queue('flip right'); } }, over: function() { module.debug('Flipping over', $nextSide); if( !module.is.animating() ) { module.stage.behind(); module.animate(module.get.transform.over() ); } else { module.queue('flip over'); } }, back: function() { module.debug('Flipping back', $nextSide); if( !module.is.animating() ) { module.stage.behind(); module.animate(module.get.transform.back() ); } else { module.queue('flip back'); } } }, stage: { above: function() { var box = { origin : (($activeSide.outerHeight() - $nextSide.outerHeight()) / 2), depth : { active : ($nextSide.outerHeight() / 2), next : ($activeSide.outerHeight() / 2) } } ; module.verbose('Setting the initial animation position as above', $nextSide, box); $activeSide .css({ 'transform' : 'rotateY(0deg) translateZ(' + box.depth.active + 'px)' }) ; $nextSide .addClass(className.animating) .css({ 'display' : 'block', 'top' : box.origin + 'px', 'transform' : 'rotateX(90deg) translateZ(' + box.depth.next + 'px)' }) ; }, below: function() { var box = { origin : (($activeSide.outerHeight() - $nextSide.outerHeight()) / 2), depth : { active : ($nextSide.outerHeight() / 2), next : ($activeSide.outerHeight() / 2) } } ; module.verbose('Setting the initial animation position as below', $nextSide, box); $activeSide .css({ 'transform' : 'rotateY(0deg) translateZ(' + box.depth.active + 'px)' }) ; $nextSide .addClass(className.animating) .css({ 'display' : 'block', 'top' : box.origin + 'px', 'transform' : 'rotateX(-90deg) translateZ(' + box.depth.next + 'px)' }) ; }, left: function() { var box = { origin : ( ( $activeSide.outerWidth() - $nextSide.outerWidth() ) / 2), depth : { active : ($nextSide.outerWidth() / 2), next : ($activeSide.outerWidth() / 2) } } ; module.verbose('Setting the initial animation position as left', $nextSide, box); $activeSide .css({ 'transform' : 'rotateY(0deg) translateZ(' + box.depth.active + 'px)' }) ; $nextSide .addClass(className.animating) .css({ 'display' : 'block', 'left' : box.origin + 'px', 'transform' : 'rotateY(-90deg) translateZ(' + box.depth.next + 'px)' }) ; }, right: function() { var box = { origin : ( ( $activeSide.outerWidth() - $nextSide.outerWidth() ) / 2), depth : { active : ($nextSide.outerWidth() / 2), next : ($activeSide.outerWidth() / 2) } } ; module.verbose('Setting the initial animation position as left', $nextSide, box); $activeSide .css({ 'transform' : 'rotateY(0deg) translateZ(' + box.depth.active + 'px)' }) ; $nextSide .addClass(className.animating) .css({ 'display' : 'block', 'left' : box.origin + 'px', 'transform' : 'rotateY(90deg) translateZ(' + box.depth.next + 'px)' }) ; }, behind: function() { var box = { origin : ( ( $activeSide.outerWidth() - $nextSide.outerWidth() ) / 2), depth : { active : ($nextSide.outerWidth() / 2), next : ($activeSide.outerWidth() / 2) } } ; module.verbose('Setting the initial animation position as behind', $nextSide, box); $activeSide .css({ 'transform' : 'rotateY(0deg)' }) ; $nextSide .addClass(className.animating) .css({ 'display' : 'block', 'left' : box.origin + 'px', 'transform' : 'rotateY(-180deg)' }) ; } }, setting: function(name, value) { if(value !== undefined) { if( $.isPlainObject(name) ) { $.extend(true, settings, name); } else { settings[name] = value; } } else { return settings[name]; } }, internal: function(name, value) { if(value !== undefined) { if( $.isPlainObject(name) ) { $.extend(true, module, name); } else { module[name] = value; } } else { return module[name]; } }, debug: function() { if(settings.debug) { if(settings.performance) { module.performance.log(arguments); } else { module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':'); module.debug.apply(console, arguments); } } }, verbose: function() { if(settings.verbose && settings.debug) { if(settings.performance) { module.performance.log(arguments); } else { module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':'); module.verbose.apply(console, arguments); } } }, error: function() { module.error = Function.prototype.bind.call(console.error, console, settings.name + ':'); module.error.apply(console, arguments); }, performance: { log: function(message) { var currentTime, executionTime, previousTime ; if(settings.performance) { currentTime = new Date().getTime(); previousTime = time || currentTime; executionTime = currentTime - previousTime; time = currentTime; performance.push({ 'Element' : element, 'Name' : message[0], 'Arguments' : [].slice.call(message, 1) || '', 'Execution Time' : executionTime }); } clearTimeout(module.performance.timer); module.performance.timer = setTimeout(module.performance.display, 100); }, display: function() { var title = settings.name + ':', totalTime = 0 ; time = false; clearTimeout(module.performance.timer); $.each(performance, function(index, data) { totalTime += data['Execution Time']; }); title += ' ' + totalTime + 'ms'; if(moduleSelector) { title += ' \'' + moduleSelector + '\''; } if($allModules.size() > 1) { title += ' ' + '(' + $allModules.size() + ')'; } if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) { console.groupCollapsed(title); if(console.table) { console.table(performance); } else { $.each(performance, function(index, data) { console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); }); } console.groupEnd(); } performance = []; } }, invoke: function(query, passedArguments, context) { var maxDepth, found, response ; passedArguments = passedArguments || queryArguments; context = element || context; if(typeof query == 'string' && instance !== undefined) { query = query.split(/[\. ]/); maxDepth = query.length - 1; $.each(query, function(depth, value) { var camelCaseValue = (depth != maxDepth) ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1) : query ; if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) { instance = instance[value]; } else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) { instance = instance[camelCaseValue]; } else if( instance[value] !== undefined ) { found = instance[value]; return false; } else if( instance[camelCaseValue] !== undefined ) { found = instance[camelCaseValue]; return false; } else { module.error(error.method); return false; } }); } if ( $.isFunction( found ) ) { response = found.apply(context, passedArguments); } else if(found !== undefined) { response = found; } if($.isArray(invokedResponse)) { invokedResponse.push(response); } else if(typeof invokedResponse == 'string') { invokedResponse = [invokedResponse, response]; } else if(response !== undefined) { invokedResponse = response; } return found; } }; if(methodInvoked) { if(instance === undefined) { module.initialize(); } module.invoke(query); } else { if(instance !== undefined) { module.destroy(); } module.initialize(); } }) ; return (invokedResponse !== undefined) ? invokedResponse : this ; }; $.fn.shape.settings = { // module info name : 'Shape', // debug content outputted to console debug : true, // verbose debug output verbose : true, // performance data output performance: true, // event namespace namespace : 'shape', // callback occurs on side change beforeChange : function() {}, onChange : function() {}, // use css animation (currently only true is supported) useCSS : true, // animation duration (useful only with future js animations) duration : 1000, easing : 'easeInOutQuad', // possible errors error: { side : 'You tried to switch to a side that does not exist.', method : 'The method you called is not defined' }, // classnames used className : { css : 'css', animating : 'animating', hidden : 'hidden', active : 'active' }, // selectors used selector : { sides : '.sides', side : '.side' } }; })( jQuery, window , document );