Path: blob/master/web-gui/buildyourownbotnet/assets/js/icheck/icheck.js
1293 views
/*!1* iCheck v1.0.1, http://git.io/arlzeA2* ===================================3* Powerful jQuery and Zepto plugin for checkboxes and radio buttons customization4*5* (c) 2013 Damir Sultanov, http://fronteed.com6* MIT Licensed7*/89(function($) {1011// Cached vars12var _iCheck = 'iCheck',13_iCheckHelper = _iCheck + '-helper',14_checkbox = 'checkbox',15_radio = 'radio',16_checked = 'checked',17_unchecked = 'un' + _checked,18_disabled = 'disabled',a19_determinate = 'determinate',20_indeterminate = 'in' + _determinate,21_update = 'update',22_type = 'type',23_click = 'click',24_touch = 'touchbegin.i touchend.i',25_add = 'addClass',26_remove = 'removeClass',27_callback = 'trigger',28_label = 'label',29_cursor = 'cursor',30_mobile = /ipad|iphone|ipod|android|blackberry|windows phone|opera mini|silk/i.test(navigator.userAgent);3132// Plugin init33$.fn[_iCheck] = function(options, fire) {3435// Walker36var handle = 'input[type="' + _checkbox + '"], input[type="' + _radio + '"]',37stack = $(),38walker = function(object) {39object.each(function() {40var self = $(this);4142if (self.is(handle)) {43stack = stack.add(self);44} else {45stack = stack.add(self.find(handle));46}47});48};4950// Check if we should operate with some method51if (/^(check|uncheck|toggle|indeterminate|determinate|disable|enable|update|destroy)$/i.test(options)) {5253// Normalize method's name54options = options.toLowerCase();5556// Find checkboxes and radio buttons57walker(this);5859return stack.each(function() {60var self = $(this);6162if (options == 'destroy') {63tidy(self, 'ifDestroyed');64} else {65operate(self, true, options);66}6768// Fire method's callback69if ($.isFunction(fire)) {70fire();71}72});7374// Customization75} else if (typeof options == 'object' || !options) {7677// Check if any options were passed78var settings = $.extend({79checkedClass: _checked,80disabledClass: _disabled,81indeterminateClass: _indeterminate,82labelHover: true83}, options),8485selector = settings.handle,86hoverClass = settings.hoverClass || 'hover',87focusClass = settings.focusClass || 'focus',88activeClass = settings.activeClass || 'active',89labelHover = !!settings.labelHover,90labelHoverClass = settings.labelHoverClass || 'hover',9192// Setup clickable area93area = ('' + settings.increaseArea).replace('%', '') | 0;9495// Selector limit96if (selector == _checkbox || selector == _radio) {97handle = 'input[type="' + selector + '"]';98}99100// Clickable area limit101if (area < -50) {102area = -50;103}104105// Walk around the selector106walker(this);107108return stack.each(function() {109var self = $(this);110111// If already customized112tidy(self);113114var node = this,115id = node.id,116117// Layer styles118offset = -area + '%',119size = 100 + (area * 2) + '%',120layer = {121position: 'absolute',122top: offset,123left: offset,124display: 'block',125width: size,126height: size,127margin: 0,128padding: 0,129background: '#fff',130border: 0,131opacity: 0132},133134// Choose how to hide input135hide = _mobile ? {136position: 'absolute',137visibility: 'hidden'138} : area ? layer : {139position: 'absolute',140opacity: 0141},142143// Get proper class144className = node[_type] == _checkbox ? settings.checkboxClass || 'i' + _checkbox : settings.radioClass || 'i' + _radio,145146// Find assigned labels147label = $(_label + '[for="' + id + '"]').add(self.closest(_label)),148149// Check ARIA option150aria = !!settings.aria,151152// Set ARIA placeholder153ariaID = _iCheck + '-' + Math.random().toString(36).substr(2,6),154155// Parent & helper156parent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''),157helper;158159// Set ARIA "labelledby"160if (aria) {161label.each(function() {162parent += 'aria-labelledby="';163164if (this.id) {165parent += this.id;166} else {167this.id = ariaID;168parent += ariaID;169}170171parent += '"';172});173}174175// Wrap input176parent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert);177178// Layer addition179helper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent);180181// Finalize customization182self.data(_iCheck, {o: settings, s: self.attr('style')}).css(hide);183!!settings.inheritClass && parent[_add](node.className || '');184!!settings.inheritID && id && parent.attr('id', _iCheck + '-' + id);185parent.css('position') == 'static' && parent.css('position', 'relative');186operate(self, true, _update);187188// Label events189if (label.length) {190label.on(_click + '.i mouseover.i mouseout.i ' + _touch, function(event) {191var type = event[_type],192item = $(this);193194// Do nothing if input is disabled195if (!node[_disabled]) {196197// Click198if (type == _click) {199if ($(event.target).is('a')) {200return;201}202operate(self, false, true);203204// Hover state205} else if (labelHover) {206207// mouseout|touchend208if (/ut|nd/.test(type)) {209parent[_remove](hoverClass);210item[_remove](labelHoverClass);211} else {212parent[_add](hoverClass);213item[_add](labelHoverClass);214}215}216217if (_mobile) {218event.stopPropagation();219} else {220return false;221}222}223});224}225226// Input events227self.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i', function(event) {228var type = event[_type],229key = event.keyCode;230231// Click232if (type == _click) {233return false;234235// Keydown236} else if (type == 'keydown' && key == 32) {237if (!(node[_type] == _radio && node[_checked])) {238if (node[_checked]) {239off(self, _checked);240} else {241on(self, _checked);242}243}244245return false;246247// Keyup248} else if (type == 'keyup' && node[_type] == _radio) {249!node[_checked] && on(self, _checked);250251// Focus/blur252} else if (/us|ur/.test(type)) {253parent[type == 'blur' ? _remove : _add](focusClass);254}255});256257// Helper events258helper.on(_click + ' mousedown mouseup mouseover mouseout ' + _touch, function(event) {259var type = event[_type],260261// mousedown|mouseup262toggle = /wn|up/.test(type) ? activeClass : hoverClass;263264// Do nothing if input is disabled265if (!node[_disabled]) {266267// Click268if (type == _click) {269operate(self, false, true);270271// Active and hover states272} else {273274// State is on275if (/wn|er|in/.test(type)) {276277// mousedown|mouseover|touchbegin278parent[_add](toggle);279280// State is off281} else {282parent[_remove](toggle + ' ' + activeClass);283}284285// Label hover286if (label.length && labelHover && toggle == hoverClass) {287288// mouseout|touchend289label[/ut|nd/.test(type) ? _remove : _add](labelHoverClass);290}291}292293if (_mobile) {294event.stopPropagation();295} else {296return false;297}298}299});300});301} else {302return this;303}304};305306// Do something with inputs307function operate(input, direct, method) {308var node = input[0],309state = /er/.test(method) ? _indeterminate : /bl/.test(method) ? _disabled : _checked,310active = method == _update ? {311checked: node[_checked],312disabled: node[_disabled],313indeterminate: input.attr(_indeterminate) == 'true' || input.attr(_determinate) == 'false'314} : node[state];315316// Check, disable or indeterminate317if (/^(ch|di|in)/.test(method) && !active) {318on(input, state);319320// Uncheck, enable or determinate321} else if (/^(un|en|de)/.test(method) && active) {322off(input, state);323324// Update325} else if (method == _update) {326327// Handle states328for (var each in active) {329if (active[each]) {330on(input, each, true);331} else {332off(input, each, true);333}334}335336} else if (!direct || method == 'toggle') {337338// Helper or label was clicked339if (!direct) {340input[_callback]('ifClicked');341}342343// Toggle checked state344if (active) {345if (node[_type] !== _radio) {346off(input, state);347}348} else {349on(input, state);350}351}352}353354// Add checked, disabled or indeterminate state355function on(input, state, keep) {356var node = input[0],357parent = input.parent(),358checked = state == _checked,359indeterminate = state == _indeterminate,360disabled = state == _disabled,361callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',362regular = option(input, callback + capitalize(node[_type])),363specific = option(input, state + capitalize(node[_type]));364365// Prevent unnecessary actions366if (node[state] !== true) {367368// Toggle assigned radio buttons369if (!keep && state == _checked && node[_type] == _radio && node.name) {370var form = input.closest('form'),371inputs = 'input[name="' + node.name + '"]';372373inputs = form.length ? form.find(inputs) : $(inputs);374375inputs.each(function() {376if (this !== node && $(this).data(_iCheck)) {377off($(this), state);378}379});380}381382// Indeterminate state383if (indeterminate) {384385// Add indeterminate state386node[state] = true;387388// Remove checked state389if (node[_checked]) {390off(input, _checked, 'force');391}392393// Checked or disabled state394} else {395396// Add checked or disabled state397if (!keep) {398node[state] = true;399}400401// Remove indeterminate state402if (checked && node[_indeterminate]) {403off(input, _indeterminate, false);404}405}406407// Trigger callbacks408callbacks(input, checked, state, keep);409}410411// Add proper cursor412if (node[_disabled] && !!option(input, _cursor, true)) {413parent.find('.' + _iCheckHelper).css(_cursor, 'default');414}415416// Add state class417parent[_add](specific || option(input, state) || '');418419// Set ARIA attribute420if (!!parent.attr('role') && !indeterminate) {421parent.attr('aria-' + (disabled ? _disabled : _checked), 'true');422}423424// Remove regular state class425parent[_remove](regular || option(input, callback) || '');426}427428// Remove checked, disabled or indeterminate state429function off(input, state, keep) {430var node = input[0],431parent = input.parent(),432checked = state == _checked,433indeterminate = state == _indeterminate,434disabled = state == _disabled,435callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',436regular = option(input, callback + capitalize(node[_type])),437specific = option(input, state + capitalize(node[_type]));438439// Prevent unnecessary actions440if (node[state] !== false) {441442// Toggle state443if (indeterminate || !keep || keep == 'force') {444node[state] = false;445}446447// Trigger callbacks448callbacks(input, checked, callback, keep);449}450451// Add proper cursor452if (!node[_disabled] && !!option(input, _cursor, true)) {453parent.find('.' + _iCheckHelper).css(_cursor, 'pointer');454}455456// Remove state class457parent[_remove](specific || option(input, state) || '');458459// Set ARIA attribute460if (!!parent.attr('role') && !indeterminate) {461parent.attr('aria-' + (disabled ? _disabled : _checked), 'false');462}463464// Add regular state class465parent[_add](regular || option(input, callback) || '');466}467468// Remove all traces469function tidy(input, callback) {470if (input.data(_iCheck)) {471472// Remove everything except input473input.parent().html(input.attr('style', input.data(_iCheck).s || ''));474475// Callback476if (callback) {477input[_callback](callback);478}479480// Unbind events481input.off('.i').unwrap();482$(_label + '[for="' + input[0].id + '"]').add(input.closest(_label)).off('.i');483}484}485486// Get some option487function option(input, state, regular) {488if (input.data(_iCheck)) {489return input.data(_iCheck).o[state + (regular ? '' : 'Class')];490}491}492493// Capitalize some string494function capitalize(string) {495return string.charAt(0).toUpperCase() + string.slice(1);496}497498// Executable handlers499function callbacks(input, checked, callback, keep) {500if (!keep) {501if (checked) {502input[_callback]('ifToggled');503}504505input[_callback]('ifChanged')[_callback]('if' + capitalize(callback));506}507}508})(window.jQuery || window.Zepto);509510511