Blame | Letzte Änderung | Log anzeigen | RSS feed
/*!* iCheck v1.0.2, http://git.io/arlzeA* ===================================* Powerful jQuery and Zepto plugin for checkboxes and radio buttons customization** (c) 2013 Damir Sultanov, http://fronteed.com* MIT Licensed*/(function($) {// Cached varsvar _iCheck = 'iCheck',_iCheckHelper = _iCheck + '-helper',_checkbox = 'checkbox',_radio = 'radio',_checked = 'checked',_unchecked = 'un' + _checked,_disabled = 'disabled',_determinate = 'determinate',_indeterminate = 'in' + _determinate,_update = 'update',_type = 'type',_click = 'click',_touch = 'touchbegin.i touchend.i',_add = 'addClass',_remove = 'removeClass',_callback = 'trigger',_label = 'label',_cursor = 'cursor',_mobile = /ipad|iphone|ipod|android|blackberry|windows phone|opera mini|silk/i.test(navigator.userAgent);// Plugin init$.fn[_iCheck] = function(options, fire) {// Walkervar handle = 'input[type="' + _checkbox + '"], input[type="' + _radio + '"]',stack = $(),walker = function(object) {object.each(function() {var self = $(this);if (self.is(handle)) {stack = stack.add(self);} else {stack = stack.add(self.find(handle));}});};// Check if we should operate with some methodif (/^(check|uncheck|toggle|indeterminate|determinate|disable|enable|update|destroy)$/i.test(options)) {// Normalize method's nameoptions = options.toLowerCase();// Find checkboxes and radio buttonswalker(this);return stack.each(function() {var self = $(this);if (options == 'destroy') {tidy(self, 'ifDestroyed');} else {operate(self, true, options);}// Fire method's callbackif ($.isFunction(fire)) {fire();}});// Customization} else if (typeof options == 'object' || !options) {// Check if any options were passedvar settings = $.extend({checkedClass: _checked,disabledClass: _disabled,indeterminateClass: _indeterminate,labelHover: true}, options),selector = settings.handle,hoverClass = settings.hoverClass || 'hover',focusClass = settings.focusClass || 'focus',activeClass = settings.activeClass || 'active',labelHover = !!settings.labelHover,labelHoverClass = settings.labelHoverClass || 'hover',// Setup clickable areaarea = ('' + settings.increaseArea).replace('%', '') | 0;// Selector limitif (selector == _checkbox || selector == _radio) {handle = 'input[type="' + selector + '"]';}// Clickable area limitif (area < -50) {area = -50;}// Walk around the selectorwalker(this);return stack.each(function() {var self = $(this);// If already customizedtidy(self);var node = this,id = node.id,// Layer stylesoffset = -area + '%',size = 100 + (area * 2) + '%',layer = {position: 'absolute',top: offset,left: offset,display: 'block',width: size,height: size,margin: 0,padding: 0,background: '#fff',border: 0,opacity: 0},// Choose how to hide inputhide = _mobile ? {position: 'absolute',visibility: 'hidden'} : area ? layer : {position: 'absolute',opacity: 0},// Get proper classclassName = node[_type] == _checkbox ? settings.checkboxClass || 'i' + _checkbox : settings.radioClass || 'i' + _radio,// Find assigned labelslabel = $(_label + '[for="' + id + '"]').add(self.closest(_label)),// Check ARIA optionaria = !!settings.aria,// Set ARIA placeholderariaID = _iCheck + '-' + Math.random().toString(36).substr(2,6),// Parent & helperparent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''),helper;// Set ARIA "labelledby"if (aria) {label.each(function() {parent += 'aria-labelledby="';if (this.id) {parent += this.id;} else {this.id = ariaID;parent += ariaID;}parent += '"';});}// Wrap inputparent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert);// Layer additionhelper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent);// Finalize customizationself.data(_iCheck, {o: settings, s: self.attr('style')}).css(hide);!!settings.inheritClass && parent[_add](node.className || '');!!settings.inheritID && id && parent.attr('id', _iCheck + '-' + id);parent.css('position') == 'static' && parent.css('position', 'relative');operate(self, true, _update);// Label eventsif (label.length) {label.on(_click + '.i mouseover.i mouseout.i ' + _touch, function(event) {var type = event[_type],item = $(this);// Do nothing if input is disabledif (!node[_disabled]) {// Clickif (type == _click) {if ($(event.target).is('a')) {return;}operate(self, false, true);// Hover state} else if (labelHover) {// mouseout|touchendif (/ut|nd/.test(type)) {parent[_remove](hoverClass);item[_remove](labelHoverClass);} else {parent[_add](hoverClass);item[_add](labelHoverClass);}}if (_mobile) {event.stopPropagation();} else {return false;}}});}// Input eventsself.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i', function(event) {var type = event[_type],key = event.keyCode;// Clickif (type == _click) {return false;// Keydown} else if (type == 'keydown' && key == 32) {if (!(node[_type] == _radio && node[_checked])) {if (node[_checked]) {off(self, _checked);} else {on(self, _checked);}}return false;// Keyup} else if (type == 'keyup' && node[_type] == _radio) {!node[_checked] && on(self, _checked);// Focus/blur} else if (/us|ur/.test(type)) {parent[type == 'blur' ? _remove : _add](focusClass);}});// Helper eventshelper.on(_click + ' mousedown mouseup mouseover mouseout ' + _touch, function(event) {var type = event[_type],// mousedown|mouseuptoggle = /wn|up/.test(type) ? activeClass : hoverClass;// Do nothing if input is disabledif (!node[_disabled]) {// Clickif (type == _click) {operate(self, false, true);// Active and hover states} else {// State is onif (/wn|er|in/.test(type)) {// mousedown|mouseover|touchbeginparent[_add](toggle);// State is off} else {parent[_remove](toggle + ' ' + activeClass);}// Label hoverif (label.length && labelHover && toggle == hoverClass) {// mouseout|touchendlabel[/ut|nd/.test(type) ? _remove : _add](labelHoverClass);}}if (_mobile) {event.stopPropagation();} else {return false;}}});});} else {return this;}};// Do something with inputsfunction operate(input, direct, method) {var node = input[0],state = /er/.test(method) ? _indeterminate : /bl/.test(method) ? _disabled : _checked,active = method == _update ? {checked: node[_checked],disabled: node[_disabled],indeterminate: input.attr(_indeterminate) == 'true' || input.attr(_determinate) == 'false'} : node[state];// Check, disable or indeterminateif (/^(ch|di|in)/.test(method) && !active) {on(input, state);// Uncheck, enable or determinate} else if (/^(un|en|de)/.test(method) && active) {off(input, state);// Update} else if (method == _update) {// Handle statesfor (var each in active) {if (active[each]) {on(input, each, true);} else {off(input, each, true);}}} else if (!direct || method == 'toggle') {// Helper or label was clickedif (!direct) {input[_callback]('ifClicked');}// Toggle checked stateif (active) {if (node[_type] !== _radio) {off(input, state);}} else {on(input, state);}}}// Add checked, disabled or indeterminate statefunction on(input, state, keep) {var node = input[0],parent = input.parent(),checked = state == _checked,indeterminate = state == _indeterminate,disabled = state == _disabled,callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',regular = option(input, callback + capitalize(node[_type])),specific = option(input, state + capitalize(node[_type]));// Prevent unnecessary actionsif (node[state] !== true) {// Toggle assigned radio buttonsif (!keep && state == _checked && node[_type] == _radio && node.name) {var form = input.closest('form'),inputs = 'input[name="' + node.name + '"]';inputs = form.length ? form.find(inputs) : $(inputs);inputs.each(function() {if (this !== node && $(this).data(_iCheck)) {off($(this), state);}});}// Indeterminate stateif (indeterminate) {// Add indeterminate statenode[state] = true;// Remove checked stateif (node[_checked]) {off(input, _checked, 'force');}// Checked or disabled state} else {// Add checked or disabled stateif (!keep) {node[state] = true;}// Remove indeterminate stateif (checked && node[_indeterminate]) {off(input, _indeterminate, false);}}// Trigger callbackscallbacks(input, checked, state, keep);}// Add proper cursorif (node[_disabled] && !!option(input, _cursor, true)) {parent.find('.' + _iCheckHelper).css(_cursor, 'default');}// Add state classparent[_add](specific || option(input, state) || '');// Set ARIA attributeif (!!parent.attr('role') && !indeterminate) {parent.attr('aria-' + (disabled ? _disabled : _checked), 'true');}// Remove regular state classparent[_remove](regular || option(input, callback) || '');}// Remove checked, disabled or indeterminate statefunction off(input, state, keep) {var node = input[0],parent = input.parent(),checked = state == _checked,indeterminate = state == _indeterminate,disabled = state == _disabled,callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',regular = option(input, callback + capitalize(node[_type])),specific = option(input, state + capitalize(node[_type]));// Prevent unnecessary actionsif (node[state] !== false) {// Toggle stateif (indeterminate || !keep || keep == 'force') {node[state] = false;}// Trigger callbackscallbacks(input, checked, callback, keep);}// Add proper cursorif (!node[_disabled] && !!option(input, _cursor, true)) {parent.find('.' + _iCheckHelper).css(_cursor, 'pointer');}// Remove state classparent[_remove](specific || option(input, state) || '');// Set ARIA attributeif (!!parent.attr('role') && !indeterminate) {parent.attr('aria-' + (disabled ? _disabled : _checked), 'false');}// Add regular state classparent[_add](regular || option(input, callback) || '');}// Remove all tracesfunction tidy(input, callback) {if (input.data(_iCheck)) {// Remove everything except inputinput.parent().html(input.attr('style', input.data(_iCheck).s || ''));// Callbackif (callback) {input[_callback](callback);}// Unbind eventsinput.off('.i').unwrap();$(_label + '[for="' + input[0].id + '"]').add(input.closest(_label)).off('.i');}}// Get some optionfunction option(input, state, regular) {if (input.data(_iCheck)) {return input.data(_iCheck).o[state + (regular ? '' : 'Class')];}}// Capitalize some stringfunction capitalize(string) {return string.charAt(0).toUpperCase() + string.slice(1);}// Executable handlersfunction callbacks(input, checked, callback, keep) {if (!keep) {if (checked) {input[_callback]('ifToggled');}input[_callback]('ifChanged')[_callback]('if' + capitalize(callback));}}})(window.jQuery || window.Zepto);