2 * iCheck v1.0.1, http://git.io/arlzeA
3 * =================================
4 * Powerful jQuery and Zepto plugin for checkboxes and radio buttons customization
6 * (c) 2013 Damir Sultanov, http://fronteed.com
13 var _iCheck = 'iCheck',
14 _iCheckHelper = _iCheck + '-helper',
15 _checkbox = 'checkbox',
18 _unchecked = 'un' + _checked,
19 _disabled = 'disabled',
20 _determinate = 'determinate',
21 _indeterminate = 'in' + _determinate,
25 _touch = 'touchbegin.i touchend.i',
27 _remove = 'removeClass',
28 _callback = 'trigger',
31 _mobile = /ipad|iphone|ipod|android|blackberry|windows phone|opera mini|silk/i.test(navigator.userAgent);
34 $.fn[_iCheck] = function(options, fire) {
37 var handle = 'input[type="' + _checkbox + '"], input[type="' + _radio + '"]',
39 walker = function(object) {
40 object.each(function() {
43 if (self.is(handle)) {
44 stack = stack.add(self);
46 stack = stack.add(self.find(handle));
51 // Check if we should operate with some method
52 if (/^(check|uncheck|toggle|indeterminate|determinate|disable|enable|update|destroy)$/i.test(options)) {
54 // Normalize method's name
55 options = options.toLowerCase();
57 // Find checkboxes and radio buttons
60 return stack.each(function() {
63 if (options == 'destroy') {
64 tidy(self, 'ifDestroyed');
66 operate(self, true, options);
68 // Fire method's callback
69 if ($.isFunction(fire)) {
75 } else if (typeof options == 'object' || !options) {
77 // Check if any options were passed
78 var settings = $.extend({
79 checkedClass: _checked,
80 disabledClass: _disabled,
81 indeterminateClass: _indeterminate,
86 selector = settings.handle,
87 hoverClass = settings.hoverClass || 'hover',
88 focusClass = settings.focusClass || 'focus',
89 activeClass = settings.activeClass || 'active',
90 labelHover = !!settings.labelHover,
91 labelHoverClass = settings.labelHoverClass || 'hover',
93 // Setup clickable area
94 area = ('' + settings.increaseArea).replace('%', '') | 0;
97 if (selector == _checkbox || selector == _radio) {
98 handle = 'input[type="' + selector + '"]';
100 // Clickable area limit
104 // Walk around the selector
107 return stack.each(function() {
110 // If already customized
117 offset = -area + '%',
118 size = 100 + (area * 2) + '%',
120 position: 'absolute',
133 // Choose how to hide input
135 position: 'absolute',
138 position: 'absolute',
143 className = node[_type] == _checkbox ? settings.checkboxClass || 'i' + _checkbox : settings.radioClass || 'i' + _radio,
145 // Find assigned labels
146 label = $(_label + '[for="' + id + '"]').add(self.closest(_label)),
149 aria = !!settings.aria,
151 // Set ARIA placeholder
152 ariaID = _iCheck + '-' + Math.random().toString(36).replace('0.', ''),
155 parent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''),
158 // Set ARIA "labelledby"
159 if (label.length && aria) {
160 label.each(function() {
161 parent += 'aria-labelledby="';
174 parent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert);
177 helper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent);
179 // Finalize customization
180 self.data(_iCheck, {o: settings, s: self.attr('style')}).css(hide);
181 !!settings.inheritClass && parent[_add](node.className || '');
182 !!settings.inheritID && id && parent.attr('id', _iCheck + '-' + id);
183 parent.css('position') == 'static' && parent.css('position', 'relative');
184 operate(self, true, _update);
188 label.on(_click + '.i mouseover.i mouseout.i ' + _touch, function(event) {
189 var type = event[_type],
192 // Do nothing if input is disabled
193 if (!node[_disabled]) {
196 if (type == _click) {
197 if ($(event.target).is('a')) {
200 operate(self, false, true);
203 } else if (labelHover) {
206 if (/ut|nd/.test(type)) {
207 parent[_remove](hoverClass);
208 item[_remove](labelHoverClass);
210 parent[_add](hoverClass);
211 item[_add](labelHoverClass);
215 event.stopPropagation();
223 self.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i', function(event) {
224 var type = event[_type],
228 if (type == _click) {
232 } else if (type == 'keydown' && key == 32) {
233 if (!(node[_type] == _radio && node[_checked])) {
234 if (node[_checked]) {
243 } else if (type == 'keyup' && node[_type] == _radio) {
244 !node[_checked] && on(self, _checked);
247 } else if (/us|ur/.test(type)) {
248 parent[type == 'blur' ? _remove : _add](focusClass);
253 helper.on(_click + ' mousedown mouseup mouseover mouseout ' + _touch, function(event) {
254 var type = event[_type],
257 toggle = /wn|up/.test(type) ? activeClass : hoverClass;
259 // Do nothing if input is disabled
260 if (!node[_disabled]) {
263 if (type == _click) {
264 operate(self, false, true);
266 // Active and hover states
270 if (/wn|er|in/.test(type)) {
272 // mousedown|mouseover|touchbegin
273 parent[_add](toggle);
277 parent[_remove](toggle + ' ' + activeClass);
280 if (label.length && labelHover && toggle == hoverClass) {
283 label[/ut|nd/.test(type) ? _remove : _add](labelHoverClass);
287 event.stopPropagation();
299 // Do something with inputs
300 function operate(input, direct, method) {
302 state = /er/.test(method) ? _indeterminate : /bl/.test(method) ? _disabled : _checked,
303 active = method == _update ? {
304 checked: node[_checked],
305 disabled: node[_disabled],
306 indeterminate: input.attr(_indeterminate) == 'true' || input.attr(_determinate) == 'false'
309 // Check, disable or indeterminate
310 if (/^(ch|di|in)/.test(method) && !active) {
313 // Uncheck, enable or determinate
314 } else if (/^(un|en|de)/.test(method) && active) {
318 } else if (method == _update) {
321 for (var state in active) {
323 on(input, state, true);
325 off(input, state, true);
328 } else if (!direct || method == 'toggle') {
330 // Helper or label was clicked
332 input[_callback]('ifClicked');
334 // Toggle checked state
336 if (node[_type] !== _radio) {
344 // Add checked, disabled or indeterminate state
345 function on(input, state, keep) {
347 parent = input.parent(),
348 checked = state == _checked,
349 indeterminate = state == _indeterminate,
350 disabled = state == _disabled,
351 callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',
352 regular = option(input, callback + capitalize(node[_type])),
353 specific = option(input, state + capitalize(node[_type]));
355 // Prevent unnecessary actions
356 if (node[state] !== true) {
358 // Toggle assigned radio buttons
359 if (!keep && state == _checked && node[_type] == _radio && node.name) {
360 var form = input.closest('form'),
361 inputs = 'input[name="' + node.name + '"]';
363 inputs = form.length ? form.find(inputs) : $(inputs);
365 inputs.each(function() {
366 if (this !== node && $(this).data(_iCheck)) {
371 // Indeterminate state
374 // Add indeterminate state
377 // Remove checked state
378 if (node[_checked]) {
379 off(input, _checked, 'force');
381 // Checked or disabled state
384 // Add checked or disabled state
388 // Remove indeterminate state
389 if (checked && node[_indeterminate]) {
390 off(input, _indeterminate, false);
394 callbacks(input, checked, state, keep);
397 if (node[_disabled] && !!option(input, _cursor, true)) {
398 parent.find('.' + _iCheckHelper).css(_cursor, 'default');
401 parent[_add](specific || option(input, state) || '');
403 // Set ARIA attribute
404 disabled ? parent.attr('aria-disabled', 'true') : parent.attr('aria-checked', indeterminate ? 'mixed' : 'true');
406 // Remove regular state class
407 parent[_remove](regular || option(input, callback) || '');
409 // Remove checked, disabled or indeterminate state
410 function off(input, state, keep) {
412 parent = input.parent(),
413 checked = state == _checked,
414 indeterminate = state == _indeterminate,
415 disabled = state == _disabled,
416 callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',
417 regular = option(input, callback + capitalize(node[_type])),
418 specific = option(input, state + capitalize(node[_type]));
420 // Prevent unnecessary actions
421 if (node[state] !== false) {
424 if (indeterminate || !keep || keep == 'force') {
428 callbacks(input, checked, callback, keep);
431 if (!node[_disabled] && !!option(input, _cursor, true)) {
432 parent.find('.' + _iCheckHelper).css(_cursor, 'pointer');
434 // Remove state class
435 parent[_remove](specific || option(input, state) || '');
437 // Set ARIA attribute
438 disabled ? parent.attr('aria-disabled', 'false') : parent.attr('aria-checked', 'false');
440 // Add regular state class
441 parent[_add](regular || option(input, callback) || '');
444 function tidy(input, callback) {
445 if (input.data(_iCheck)) {
447 // Remove everything except input
448 input.parent().html(input.attr('style', input.data(_iCheck).s || ''));
452 input[_callback](callback);
455 input.off('.i').unwrap();
456 $(_label + '[for="' + input[0].id + '"]').add(input.closest(_label)).off('.i');
460 function option(input, state, regular) {
461 if (input.data(_iCheck)) {
462 return input.data(_iCheck).o[state + (regular ? '' : 'Class')];
465 // Capitalize some string
466 function capitalize(string) {
467 return string.charAt(0).toUpperCase() + string.slice(1);
469 // Executable handlers
470 function callbacks(input, checked, callback, keep) {
473 input[_callback]('ifToggled');
475 input[_callback]('ifChanged')[_callback]('if' + capitalize(callback));
478 })(window.jQuery || window.Zepto);