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);
69 // Fire method's callback
70 if ($.isFunction(fire)) {
76 } else if (typeof options == 'object' || !options) {
78 // Check if any options were passed
79 var settings = $.extend({
80 checkedClass: _checked,
81 disabledClass: _disabled,
82 indeterminateClass: _indeterminate,
87 selector = settings.handle,
88 hoverClass = settings.hoverClass || 'hover',
89 focusClass = settings.focusClass || 'focus',
90 activeClass = settings.activeClass || 'active',
91 labelHover = !!settings.labelHover,
92 labelHoverClass = settings.labelHoverClass || 'hover',
94 // Setup clickable area
95 area = ('' + settings.increaseArea).replace('%', '') | 0;
98 if (selector == _checkbox || selector == _radio) {
99 handle = 'input[type="' + selector + '"]';
102 // Clickable area limit
107 // Walk around the selector
110 return stack.each(function() {
113 // If already customized
120 offset = -area + '%',
121 size = 100 + (area * 2) + '%',
123 position: 'absolute',
136 // Choose how to hide input
138 position: 'absolute',
141 position: 'absolute',
146 className = node[_type] == _checkbox ? settings.checkboxClass || 'i' + _checkbox : settings.radioClass || 'i' + _radio,
148 // Find assigned labels
149 label = $(_label + '[for="' + id + '"]').add(self.closest(_label)),
152 aria = !!settings.aria,
154 // Set ARIA placeholder
155 ariaID = _iCheck + '-' + Math.random().toString(36).replace('0.', ''),
158 parent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''),
161 // Set ARIA "labelledby"
162 if (label.length && aria) {
163 label.each(function() {
164 parent += 'aria-labelledby="';
178 parent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert);
181 helper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent);
183 // Finalize customization
184 self.data(_iCheck, {o: settings, s: self.attr('style')}).css(hide);
185 !!settings.inheritClass && parent[_add](node.className || '');
186 !!settings.inheritID && id && parent.attr('id', _iCheck + '-' + id);
187 parent.css('position') == 'static' && parent.css('position', 'relative');
188 operate(self, true, _update);
192 label.on(_click + '.i mouseover.i mouseout.i ' + _touch, function(event) {
193 var type = event[_type],
196 // Do nothing if input is disabled
197 if (!node[_disabled]) {
200 if (type == _click) {
201 if ($(event.target).is('a')) {
204 operate(self, false, true);
207 } else if (labelHover) {
210 if (/ut|nd/.test(type)) {
211 parent[_remove](hoverClass);
212 item[_remove](labelHoverClass);
214 parent[_add](hoverClass);
215 item[_add](labelHoverClass);
220 event.stopPropagation();
229 self.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i', function(event) {
230 var type = event[_type],
234 if (type == _click) {
238 } else if (type == 'keydown' && key == 32) {
239 if (!(node[_type] == _radio && node[_checked])) {
240 if (node[_checked]) {
250 } else if (type == 'keyup' && node[_type] == _radio) {
251 !node[_checked] && on(self, _checked);
254 } else if (/us|ur/.test(type)) {
255 parent[type == 'blur' ? _remove : _add](focusClass);
260 helper.on(_click + ' mousedown mouseup mouseover mouseout ' + _touch, function(event) {
261 var type = event[_type],
264 toggle = /wn|up/.test(type) ? activeClass : hoverClass;
266 // Do nothing if input is disabled
267 if (!node[_disabled]) {
270 if (type == _click) {
271 operate(self, false, true);
273 // Active and hover states
277 if (/wn|er|in/.test(type)) {
279 // mousedown|mouseover|touchbegin
280 parent[_add](toggle);
284 parent[_remove](toggle + ' ' + activeClass);
288 if (label.length && labelHover && toggle == hoverClass) {
291 label[/ut|nd/.test(type) ? _remove : _add](labelHoverClass);
296 event.stopPropagation();
308 // Do something with inputs
309 function operate(input, direct, method) {
311 state = /er/.test(method) ? _indeterminate : /bl/.test(method) ? _disabled : _checked,
312 active = method == _update ? {
313 checked: node[_checked],
314 disabled: node[_disabled],
315 indeterminate: input.attr(_indeterminate) == 'true' || input.attr(_determinate) == 'false'
318 // Check, disable or indeterminate
319 if (/^(ch|di|in)/.test(method) && !active) {
322 // Uncheck, enable or determinate
323 } else if (/^(un|en|de)/.test(method) && active) {
327 } else if (method == _update) {
330 for (var state in active) {
332 on(input, state, true);
334 off(input, state, true);
338 } else if (!direct || method == 'toggle') {
340 // Helper or label was clicked
342 input[_callback]('ifClicked');
345 // Toggle checked state
347 if (node[_type] !== _radio) {
356 // Add checked, disabled or indeterminate state
357 function on(input, state, keep) {
359 parent = input.parent(),
360 checked = state == _checked,
361 indeterminate = state == _indeterminate,
362 disabled = state == _disabled,
363 callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',
364 regular = option(input, callback + capitalize(node[_type])),
365 specific = option(input, state + capitalize(node[_type]));
367 // Prevent unnecessary actions
368 if (node[state] !== true) {
370 // Toggle assigned radio buttons
371 if (!keep && state == _checked && node[_type] == _radio && node.name) {
372 var form = input.closest('form'),
373 inputs = 'input[name="' + node.name + '"]';
375 inputs = form.length ? form.find(inputs) : $(inputs);
377 inputs.each(function() {
378 if (this !== node && $(this).data(_iCheck)) {
384 // Indeterminate state
387 // Add indeterminate state
390 // Remove checked state
391 if (node[_checked]) {
392 off(input, _checked, 'force');
395 // Checked or disabled state
398 // Add checked or disabled state
403 // Remove indeterminate state
404 if (checked && node[_indeterminate]) {
405 off(input, _indeterminate, false);
410 callbacks(input, checked, state, keep);
414 if (node[_disabled] && !!option(input, _cursor, true)) {
415 parent.find('.' + _iCheckHelper).css(_cursor, 'default');
419 parent[_add](specific || option(input, state) || '');
421 // Set ARIA attribute
422 disabled ? parent.attr('aria-disabled', 'true') : parent.attr('aria-checked', indeterminate ? 'mixed' : 'true');
424 // Remove regular state class
425 parent[_remove](regular || option(input, callback) || '');
428 // Remove checked, disabled or indeterminate state
429 function off(input, state, keep) {
431 parent = input.parent(),
432 checked = state == _checked,
433 indeterminate = state == _indeterminate,
434 disabled = state == _disabled,
435 callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',
436 regular = option(input, callback + capitalize(node[_type])),
437 specific = option(input, state + capitalize(node[_type]));
439 // Prevent unnecessary actions
440 if (node[state] !== false) {
443 if (indeterminate || !keep || keep == 'force') {
448 callbacks(input, checked, callback, keep);
452 if (!node[_disabled] && !!option(input, _cursor, true)) {
453 parent.find('.' + _iCheckHelper).css(_cursor, 'pointer');
456 // Remove state class
457 parent[_remove](specific || option(input, state) || '');
459 // Set ARIA attribute
460 disabled ? parent.attr('aria-disabled', 'false') : parent.attr('aria-checked', 'false');
462 // Add regular state class
463 parent[_add](regular || option(input, callback) || '');
467 function tidy(input, callback) {
468 if (input.data(_iCheck)) {
470 // Remove everything except input
471 input.parent().html(input.attr('style', input.data(_iCheck).s || ''));
475 input[_callback](callback);
479 input.off('.i').unwrap();
480 $(_label + '[for="' + input[0].id + '"]').add(input.closest(_label)).off('.i');
485 function option(input, state, regular) {
486 if (input.data(_iCheck)) {
487 return input.data(_iCheck).o[state + (regular ? '' : 'Class')];
491 // Capitalize some string
492 function capitalize(string) {
493 return string.charAt(0).toUpperCase() + string.slice(1);
496 // Executable handlers
497 function callbacks(input, checked, callback, keep) {
500 input[_callback]('ifToggled');
503 input[_callback]('ifChanged')[_callback]('if' + capitalize(callback));
506 })(window.jQuery || window.Zepto);