Skip to content

Instantly share code, notes, and snippets.

@jimmed
Forked from devotox/Radio2_Check2.js
Last active December 20, 2015 04:19
Show Gist options
  • Save jimmed/6069754 to your computer and use it in GitHub Desktop.
Save jimmed/6069754 to your computer and use it in GitHub Desktop.
(function ($, window, undefined) {
$.fn.extend({
check2: function(option) {
var check2Class = 'check2',
conf = {
check2Class: check2,
offscreenClass: 'offscreen',
fontSize: '1.2em',
hasCheck2Class: [
'has',
conf.check2Class.charAt(0).toUpperCase(),
conf.check2Class.slice(1)
].join('')
},
_fn = {
configure: function(checkInput, check2) {
var bindClass = _fn.bindClass(),
changeClass = _fn.bindClass('change');
$(checkInput)
.unbind(changeClass)
.bind(changeClass, function() {
var checked = $(this).is(':checked');
check2
.toggleClass('icon-check', checked)
.toggleClass('icon-check-empty', !checked);
});
$(check2)
.unbind(bindClass)
.bind(bindClass, function() {
$(this)
.toggleClass('icon-check-empty icon-check')
.prev()
.trigger('click');
});
},
bindClass: function(fn) {
if(!fn) {
fn = (AF && AF.plugins && AF.plugins.Device)
? AF.plugins.Device.click()
: 'click';
}
return [fn, conf.check2Class].join('.');
},
unbind: function(checkInput) {
var bindClass = _fn.bindClass(),
changeClass = _fn.bindClass('change');
$checkInput = $(checkInput);
$checkInput.next().unbind(bindClass);
$checkInput.unbind(changeClass);
}
},
fn = {
create: function(checkInput) {
var $checkInput = $(checkInput);
if($checkInput.next().hasClass(conf.check2Class)) {
return false;
}
var check2 = $('<i/>'),
input_classes = $checkInput.attr('class');
check2
.css('fontSize', conf.fontSize)
.addClass(conf.check2Class)
.addClass(input_classes);
if(checkInput.is(':checked')) {
check2.addClass('icon-check');
} else {
check2.addClass('icon-check-empty');
}
_fn.configure($checkInput, check2);
$checkInput
.addClass(conf.offscreenClass)
.addClass(conf.hasCheck2Class)
.after(check2);
return check2;
},
destroy: function(checkInput) {
var $checkInput = $(checkInput);
if(!$checkInput.next().hasClass(conf.check2Class)) {
return false;
}
$checkInput
.removeClass(conf.offscreenClass)
.removeClass(conf.hasCheck2Class);
_fn.unbind($checkInput);
$checkInput.next().remove();
},
disable: function(checkInput) {
var $checkInput = $(checkInput);
if(!$checkInput.next().hasClass(conf.check2Class)) {
return false;
}
_fn.unbind($checkInput);
},
enable: function(checkInput) {
var check2 = $(checkInput).next();
if(!check2.hasClass(conf.check2Class)) {
return fn.create(checkInput);
}
_fn.configure(checkInput, check2);
}
},
optionFn = fn[option || 'click'];
if(optionFn) {
$(this)
.filter('input:checkbox')
.each(function(i,v) { optionFn(v); });
}
return this;
},
radio2: function(option) {
option = option || 'create'
var conf = {
radio2Class: 'radio2',
offscreenClass: 'offscreen',
fontSize: '1.2em'
};
conf.hasRadio2Class = 'has'+conf.radio2Class.charAt(0).toUpperCase() + conf.radio2Class.substr(1);
var _fn = {
configure: function(radioInput, radio2){
radioInput = $(radioInput);
radio2 = $(radio2);
var $name = radioInput.attr('name');
var bindClass = _fn.bindClass();
var changeClass = _fn.bindClass('change');
radio2.unbind(bindClass);
radioInput.unbind(changeClass);
radio2.bind(bindClass, function(){
var $this = $(this);
var others = $(document).find("input:radio[name='"+$name+"']").not(radioInput);
others.each(function(i, v){
$(v).next().removeClass('icon-circle').addClass('icon-circle-blank');
});
$this.addClass('icon-circle').removeClass('icon-circle-blank');
$this.prev().trigger('click');
})
radioInput.bind(changeClass, function(){
var $this = $(this);
var others = $(document).find("input:radio[name='"+$name+"']").not(radioInput);
others.each(function(i, v){
$(v).next().removeClass('icon-circle').addClass('icon-circle-blank');
});
if($this.is(':checked') && radio2.hasClass('icon-circle-blank'))
radio2.removeClass('icon-circle-blank').addClass('icon-circle');
else if(!$this.is(':checked') && radio2.hasClass('icon-circle'))
radio2.addClass('icon-circle-blank').removeClass('icon-circle');
})
},
bindClass: function(fn){
if(!fn) fn = (AF && AF.plugins && AF.plugins.Device) ? AF.plugins.Device.click() : 'click';
return fn+'.'+conf.radio2Class;
},
unbind: function(radioInput){
radioInput = $(radioInput);
var bindClass = _fn.bindClass();
var changeClass = _fn.bindClass('change');
radioInput.next().unbind(bindClass);
radioInput.unbind(changeClass);
}
}
var fn = {
create: function(radioInput){
radioInput = $(radioInput);
if(radioInput.next().hasClass(conf.radio2Class)) return false;
var radio2 = $('<i/>');
var input_classes = $(radioInput).attr('class');
radio2.css('fontSize', conf.fontSize).addClass(conf.radio2Class).addClass(input_classes);
if(radioInput.is(':checked'))
radio2.addClass('icon-circle');
else radio2.addClass('icon-circle-blank');
_fn.configure(radioInput, radio2);
radioInput.addClass(conf.offscreenClass).addClass(conf.hasRadio2Class);
radioInput.after(radio2);
return radio2
},
destroy: function(radioInput){
radioInput = $(radioInput);
if(!radioInput.next().hasClass(conf.radio2Class)) return false;
radioInput.removeClass(conf.offscreenClass).removeClass(conf.hasRadio2Class);
_fn.unbind(radioInput);
radioInput.next().remove();
},
disable: function(radioInput){
radioInput = $(radioInput);
if(!radioInput.next().hasClass(conf.radio2Class)) return false;
_fn.unbind(radioInput);
},
enable: function(radioInput){
var radio2 = $(radioInput).next();
if(!radio2.hasClass(conf.radio2Class)) return fn.create(radioInput);
_fn.configure(radioInput, radio2);
}
};
if(fn[option]) {
$(this).filter('input:radio').each(function(i,v){
fn[option](v);
});
} else {
return this;
}
}
});
})(window.jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment