/* * # Semantic - Accordion * http://github.com/jlukic/semantic-ui/ * * * Copyright 2014 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT * */ ;(function ($, window, document, undefined) { module.exports = function(parameters) { var $allModules = $(this), time = new Date().getTime(), performance = [], query = arguments[0], methodInvoked = (typeof query == 'string'), queryArguments = [].slice.call(arguments, 1), returnedValue ; $allModules .each(function() { var settings = ( $.isPlainObject(parameters) ) ? $.extend(true, {}, module.exports.settings, parameters) : $.extend({}, module.exports.settings), className = settings.className, namespace = settings.namespace, selector = settings.selector, error = settings.error, eventNamespace = '.' + namespace, moduleNamespace = 'module-' + namespace, moduleSelector = $allModules.selector || '', $module = $(this), $title = $module.find(selector.title), $content = $module.find(selector.content), element = this, instance = $module.data(moduleNamespace), module ; module = { initialize: function() { module.debug('Initializing accordion with bound events', $module); // initializing $title .on('click' + eventNamespace, module.event.click) ; module.instantiate(); }, instantiate: function() { instance = module; $module .data(moduleNamespace, module) ; }, destroy: function() { module.debug('Destroying previous accordion for', $module); $module .removeData(moduleNamespace) ; $title .off(eventNamespace) ; }, event: { click: function() { module.verbose('Title clicked', this); var $activeTitle = $(this), index = $title.index($activeTitle) ; module.toggle(index); }, resetDisplay: function() { $(this).css('display', ''); if( $(this).attr('style') == '') { $(this) .attr('style', '') .removeAttr('style') ; } }, resetOpacity: function() { $(this).css('opacity', ''); if( $(this).attr('style') == '') { $(this) .attr('style', '') .removeAttr('style') ; } } }, toggle: function(index) { module.debug('Toggling content content at index', index); var $activeTitle = $title.eq(index), $activeContent = $activeTitle.next($content), contentIsOpen = $activeContent.is(':visible') ; if(contentIsOpen) { if(settings.collapsible) { module.close(index); } else { module.debug('Cannot close accordion content collapsing is disabled'); } } else { module.open(index); } }, open: function(index) { var $activeTitle = $title.eq(index), $activeContent = $activeTitle.next($content), $otherSections = module.is.menu() ? $activeTitle.parent().siblings(selector.item).find(selector.title) : $activeTitle.siblings(selector.title), $previousTitle = $otherSections.filter('.' + className.active), $previousContent = $previousTitle.next($title), contentIsOpen = ($previousTitle.size() > 0) ; if( !$activeContent.is(':animated') ) { module.debug('Opening accordion content', $activeTitle); if(settings.exclusive && contentIsOpen) { $previousTitle .removeClass(className.active) ; $previousContent .stop() .children() .stop() .animate({ opacity: 0 }, settings.duration, module.event.resetOpacity) .end() .slideUp(settings.duration , settings.easing, function() { $previousContent .removeClass(className.active) .children() ; $.proxy(module.event.resetDisplay, this)(); }) ; } $activeTitle .addClass(className.active) ; $activeContent .stop() .children() .stop() .animate({ opacity: 1 }, settings.duration) .end() .slideDown(settings.duration, settings.easing, function() { $activeContent .addClass(className.active) ; $.proxy(module.event.resetDisplay, this)(); $.proxy(settings.onOpen, $activeContent)(); $.proxy(settings.onChange, $activeContent)(); }) ; } }, close: function(index) { var $activeTitle = $title.eq(index), $activeContent = $activeTitle.next($content) ; module.debug('Closing accordion content', $activeContent); $activeTitle .removeClass(className.active) ; $activeContent .removeClass(className.active) .show() .stop() .children() .stop() .animate({ opacity: 0 }, settings.duration, module.event.resetOpacity) .end() .slideUp(settings.duration, settings.easing, function(){ $.proxy(module.event.resetDisplay, this)(); $.proxy(settings.onClose, $activeContent)(); $.proxy(settings.onChange, $activeContent)(); }) ; }, is: { menu: function () { return $module.hasClass(className.menu); } }, setting: function(name, value) { if( $.isPlainObject(name) ) { $.extend(true, settings, name); } else if(value !== undefined) { settings[name] = value; } else { return settings[name]; } }, internal: function(name, value) { module.debug('Changing internal', 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( (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 object = instance, maxDepth, found, response ; passedArguments = passedArguments || queryArguments; context = element || context; if(typeof query == 'string' && object !== 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( object[camelCaseValue] ) && (depth != maxDepth) ) { object = object[camelCaseValue]; } else if( object[camelCaseValue] !== undefined ) { found = object[camelCaseValue]; return false; } else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) { object = object[value]; } else if( object[value] !== undefined ) { found = object[value]; return false; } else { return false; } }); } if ( $.isFunction( found ) ) { response = found.apply(context, passedArguments); } else if(found !== undefined) { response = found; } if($.isArray(returnedValue)) { returnedValue.push(response); } else if(returnedValue !== undefined) { returnedValue = [returnedValue, response]; } else if(response !== undefined) { returnedValue = response; } return found; } }; if(methodInvoked) { if(instance === undefined) { module.initialize(); } module.invoke(query); } else { if(instance !== undefined) { module.destroy(); } module.initialize(); } }) ; return (returnedValue !== undefined) ? returnedValue : this ; }; module.exports.settings = { name : 'Accordion', namespace : 'accordion', debug : false, verbose : true, performance : true, exclusive : true, collapsible : true, duration : 500, easing : 'easeInOutQuint', onOpen : function(){}, onClose : function(){}, onChange : function(){}, error: { method : 'The method you called is not defined' }, className : { active : 'active', menu : 'menu', }, selector : { title : '.title', content : '.content', menu : '.menu', item : '.item', } }; // Adds easing $.extend( $.easing, { easeInOutQuint: function (x, t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; return c/2*((t-=2)*t*t*t*t + 2) + b; } }); })( require("jquery"), window , document );