1 | /* file: ui.inputCheckbox.js - v1.0.0 | minified on 2011/01/27 with http://jscompress.com/ */ |
---|
2 | (function($) |
---|
3 | {var publicMethods={init:function(opt) |
---|
4 | {return this.each(function() |
---|
5 | {var $this=$(this),data=$this.data('options'),objects=$this.data('objects'),properties=$this.data('properties'),options={values:{forced:false,checked:'yes',unchecked:'no'},returnMode:'selected',change:null};$this.data('options',options).addClass('ui-inputCheckbox');if(!properties) |
---|
6 | {$this.data('properties',{initialized:false,isValid:true,checkboxList:null,isCB:true});properties=$this.data('properties');} |
---|
7 | if($this.get(0).tagName=='INPUT'&&$this.get(0).type=='checkbox') |
---|
8 | {properties.checkboxList=$this;} |
---|
9 | else |
---|
10 | {properties.checkboxList=$this.find('input[type=checkbox]');properties.isCB=false;} |
---|
11 | properties.checkboxList.bind('click.inputCheckbox',function(event) |
---|
12 | {privateMethods.setValue($this,$(this).attr('id'),$(this).attr('checked')?options.values.checked:options.values.unchecked,false);});privateMethods.setOptions($this,opt);});},destroy:function() |
---|
13 | {return this.each(function() |
---|
14 | {var properties=this.data('properties');properties.checkboxList.unbind('.inputCheckbox');this.removeClass('ui-inputCheckbox');});},options:function(value) |
---|
15 | {return(this.each(function() |
---|
16 | {privateMethods.setOptions($(this),value);}));},disabled:function(value) |
---|
17 | {if(value!=null) |
---|
18 | {return(this.each(function() |
---|
19 | {privateMethods.setDisabled($(this),value);}));} |
---|
20 | else |
---|
21 | {return(privateMethods.getDisabled($(this)));}},values:function(value) |
---|
22 | {if(value!=null) |
---|
23 | {return(this.each(function() |
---|
24 | {privateMethods.setValues($(this),value);}));} |
---|
25 | else |
---|
26 | {var options=this.data('options');return(options.values);}},value:function(id,value) |
---|
27 | {var properties=this.data('properties');if(properties.isCB) |
---|
28 | {value=id;id=$(this).get(0).id;} |
---|
29 | if(value!=null||id==':all'||id==':invert'||id==':none') |
---|
30 | {return(this.each(function() |
---|
31 | {privateMethods.setValue($(this),id,value,true);}));} |
---|
32 | else if(id!=null&&id!='') |
---|
33 | {var options=this.data('options');return($(this).find('#'+id).attr('checked')?options.values.checked:options.values.unchecked);} |
---|
34 | else |
---|
35 | {var options=this.data('options'),returned=[];if(options.values.forced) |
---|
36 | {$(this).find('input').each(function() |
---|
37 | {returned.push({id:$(this).attr('id'),value:$(this).attr('checked')?options.values.checked:options.values.unchecked});});} |
---|
38 | else |
---|
39 | {var filter=(options.returnMode=='selected'?':checked':':not(:checked)');$(this).find('input'+filter).each(function() |
---|
40 | {returned.push($(this).attr('value'));});} |
---|
41 | return(returned);}},returnMode:function(value) |
---|
42 | {if(value!=null) |
---|
43 | {return this.each(function() |
---|
44 | {privateMethods.setReturnMode($(this),value);});} |
---|
45 | else |
---|
46 | {var options=this.data('options');if(options) |
---|
47 | {return(options.returnMode);} |
---|
48 | else |
---|
49 | {return('selected');}}},isValid:function(value) |
---|
50 | {if(value!=null) |
---|
51 | {return(this.each(function() |
---|
52 | {privateMethods.setIsValid($(this),value);}));} |
---|
53 | else |
---|
54 | {var properties=this.data('properties');return(properties.isValid);}},change:function(value) |
---|
55 | {if(value!=null&&$.isFunction(value)) |
---|
56 | {return(this.each(function() |
---|
57 | {privateMethods.setEventChange($(this),value);}));} |
---|
58 | else |
---|
59 | {var options=this.data('options');if(options) |
---|
60 | {return(options.change);} |
---|
61 | else |
---|
62 | {return(null);}}}};var privateMethods={setOptions:function(object,value) |
---|
63 | {var properties=object.data('properties'),options=object.data('options');if(!$.isPlainObject(value))return(false);properties.initialized=false;privateMethods.setReturnMode(object,(value.returnMode!=null)?value.returnMode:options.returnMode);privateMethods.setValues(object,(value.values!=null)?value.values:options.values);privateMethods.setEventChange(object,(value.change!=null)?value.change:options.change);properties.initialized=true;},setIsValid:function(object,value) |
---|
64 | {var properties=object.data('properties');if(properties.isValid!=value&&properties.initialized) |
---|
65 | {properties.isValid=value;if(properties.isValid) |
---|
66 | {if(properties.isCB) |
---|
67 | {object.parent().removeClass('ui-inputCheckbox ui-error');} |
---|
68 | else |
---|
69 | {object.removeClass('ui-error');}} |
---|
70 | else |
---|
71 | {if(properties.isCB) |
---|
72 | {object.parent().addClass('ui-inputCheckbox ui-error');} |
---|
73 | else |
---|
74 | {object.addClass('ui-error');}}} |
---|
75 | return(properties.isValid);},setReturnMode:function(object,value) |
---|
76 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.returnMode!=value)&&(value=='selected'||value=='notSelected')) |
---|
77 | {options.returnMode=value;} |
---|
78 | return(options.returnMode);},setValues:function(object,value) |
---|
79 | {var options=object.data('options');if(value.forced!=null)options.values.forced=value.forced;if(value.checked!=null)options.values.checked=value.checked;if(value.unchecked!=null)options.values.unchecked=value.unchecked;return(options.values);},setValue:function(object,id,value,apply) |
---|
80 | {var options=object.data('options'),properties=object.data('properties');if(apply) |
---|
81 | {if(id==':all'||id==':none'||id==':invert') |
---|
82 | {switch(id) |
---|
83 | {case':all':properties.checkboxList.attr('checked',true);break;case':none':properties.checkboxList.attr('checked',false);break;case':invert':properties.checkboxList.each(function() |
---|
84 | {$(this).attr('checked',!$(this).attr('checked'));});break;}} |
---|
85 | else if($.isArray(value)&&!properties.isCB) |
---|
86 | {properties.checkboxList.attr('checked',false);for(var i=0;i<value.length;i++) |
---|
87 | {if(value[i].id!=null&&value[i].value!=null) |
---|
88 | {if($('#'+value[i].id).attr('value')==value[i].value)$('#'+value[i].id).attr('checked',true);} |
---|
89 | else |
---|
90 | {properties.checkboxList.filter('[value='+value[i]+']').attr('checked',true);}}} |
---|
91 | else |
---|
92 | {if(options.values.checked==value) |
---|
93 | {if(properties.isCB) |
---|
94 | {properties.checkboxList.attr('checked',true);} |
---|
95 | else |
---|
96 | {if(id=='') |
---|
97 | {properties.checkboxList.attr('checked',true);} |
---|
98 | else |
---|
99 | {properties.checkboxList.filter('#'+id).attr('checked',true);}}} |
---|
100 | else |
---|
101 | {if(properties.isCB) |
---|
102 | {properties.checkboxList.attr('checked',false);} |
---|
103 | else |
---|
104 | {if(id=='') |
---|
105 | {properties.checkboxList.attr('checked',false);} |
---|
106 | else |
---|
107 | {properties.checkboxList.filter('#'+id).attr('checked',false);}}}}} |
---|
108 | if(options.change)object.trigger('inputCheckboxChange',{id:id,state:value});return(true);},setEventChange:function(object,value) |
---|
109 | {var options=object.data('options');options.change=value;object.unbind('inputCheckboxChange');if(value)object.bind('inputCheckboxChange',options.change);return(options.change);}};$.fn.inputCheckbox=function(method) |
---|
110 | {if(publicMethods[method]) |
---|
111 | {return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1));} |
---|
112 | else if(typeof method==='object'||!method) |
---|
113 | {return publicMethods.init.apply(this,arguments);} |
---|
114 | else |
---|
115 | {$.error('Method '+method+' does not exist on jQuery.inputCheckbox');}}})(jQuery); |
---|