/* * # Semantic - Checkbox * http://github.com/semantic-org/semantic-ui/ * * * Copyright 2014 Contributor * Released under the MIT license * http://opensource.org/licenses/MIT * */ ;(function ( $, window, document, undefined ) { "use strict"; $.fn.checkbox = function(parameters) { var $allModules = $(this), moduleSelector = $allModules.selector || '', time = new Date().getTime(), performance = [], query = arguments[0], methodInvoked = (typeof query == 'string'), queryArguments = [].slice.call(arguments, 1), returnedValue ; $allModules .each(function() { var settings = $.extend(true, {}, $.fn.checkbox.settings, parameters), className = settings.className, namespace = settings.namespace, selector = settings.selector, error = settings.error, eventNamespace = '.' + namespace, moduleNamespace = 'module-' + namespace, $module = $(this), $label = $(this).find(selector.label).first(), $input = $(this).find(selector.input), instance = $module.data(moduleNamespace), observer, element = this, module ; module = { initialize: function() { module.verbose('Initializing checkbox', settings); module.create.label(); module.add.events(); if( module.is.checked() ) { module.set.checked(); if(settings.fireOnInit) { settings.onChecked.call($input.get()); } } else { module.remove.checked(); if(settings.fireOnInit) { settings.onUnchecked.call($input.get()); } } module.observeChanges(); module.instantiate(); }, instantiate: function() { module.verbose('Storing instance of module', module); instance = module; $module .data(moduleNamespace, module) ; }, destroy: function() { module.verbose('Destroying module'); module.remove.events(); $module .removeData(moduleNamespace) ; }, refresh: function() { $module = $(this); $label = $(this).find(selector.label).first(); $input = $(this).find(selector.input); }, observeChanges: function() { if('MutationObserver' in window) { observer = new MutationObserver(function(mutations) { module.debug('DOM tree modified, updating selector cache'); module.refresh(); }); observer.observe(element, { childList : true, subtree : true }); module.debug('Setting up mutation observer', observer); } }, attachEvents: function(selector, event) { var $element = $(selector) ; event = $.isFunction(module[event]) ? module[event] : module.toggle ; if($element.length > 0) { module.debug('Attaching checkbox events to element', selector, event); $element .on('click' + eventNamespace, event) ; } else { module.error(error.notFound); } }, event: { keydown: function(event) { var key = event.which, keyCode = { enter : 13, space : 32, escape : 27 } ; if( key == keyCode.escape) { module.verbose('Escape key pressed blurring field'); $module .blur() ; } if(!event.ctrlKey && (key == keyCode.enter || key == keyCode.space)) { module.verbose('Enter key pressed, toggling checkbox'); module.toggle.call(this); event.preventDefault(); } } }, is: { radio: function() { return $module.hasClass(className.radio); }, checked: function() { return $input.prop('checked') !== undefined && $input.prop('checked'); }, unchecked: function() { return !module.is.checked(); } }, can: { change: function() { return !( $module.hasClass(className.disabled) || $module.hasClass(className.readOnly) || $input.prop('disabled') ); }, uncheck: function() { return (typeof settings.uncheckable === 'boolean') ? settings.uncheckable : !module.is.radio() ; } }, set: { checked: function() { $module.addClass(className.checked); }, tab: function() { if( $input.attr('tabindex') === undefined) { $input .attr('tabindex', 0) ; } } }, create: { label: function() { if($input.prevAll(selector.label).length > 0) { $input.prev(selector.label).detach().insertAfter($input); module.debug('Moving existing label', $label); } else if( !module.has.label() ) { $label = $('