1 | /* file: ui.inputRadio.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={disabled:[],hidden:[],value:'',change:null};$this.data('options',options);if(!properties) |
---|
6 | {$this.data('properties',{initialized:false,value:'',isValid:true});properties=$this.data('properties');} |
---|
7 | if(!objects) |
---|
8 | {objects={radio:[]};$this.data('objects',objects);} |
---|
9 | $this.find('input[type=radio]').each(function(index,item) |
---|
10 | {objects.radio.push($(item));$(item).attr('name','ir_'+$this.get(0).id).bind('click',function(event) |
---|
11 | {privateMethods.setValue($this,$(this).attr('value'),false);});});privateMethods.setOptions($this,opt);});},destroy:function() |
---|
12 | {return this.each(function() |
---|
13 | {var $this=$(this),objects=$this.data('objects');for(var i=0;i<objects.radio.length;i++) |
---|
14 | {objects.radio.unbind();} |
---|
15 | objects.container.unbind().remove();$this.unbind('.inputRadio').css({width:'',height:''});});},options:function(value) |
---|
16 | {return(this.each(function() |
---|
17 | {privateMethods.setOptions($(this),value);}));},disabled:function(value) |
---|
18 | {if(value!=null) |
---|
19 | {return(this.each(function() |
---|
20 | {privateMethods.setDisabled($(this),value);}));} |
---|
21 | else |
---|
22 | {return(privateMethods.getDisabled($(this)));}},hidden:function(value) |
---|
23 | {if(value!=null) |
---|
24 | {return(this.each(function() |
---|
25 | {privateMethods.setHidden($(this),value);}));} |
---|
26 | else |
---|
27 | {return(privateMethods.getHidden($(this)));}},value:function(value) |
---|
28 | {if(value!=null) |
---|
29 | {return(this.each(function() |
---|
30 | {privateMethods.setValue($(this),value,true);}));} |
---|
31 | else |
---|
32 | {var properties=this.data('properties');return(properties.value);}},isValid:function(value) |
---|
33 | {if(value!=null) |
---|
34 | {return(this.each(function() |
---|
35 | {privateMethods.setIsValid($(this),value);}));} |
---|
36 | else |
---|
37 | {var properties=this.data('properties');return(properties.isValid);}},change:function(value) |
---|
38 | {if(value!=null&&$.isFunction(value)) |
---|
39 | {return(this.each(function() |
---|
40 | {privateMethods.setEventChange($(this),value);}));} |
---|
41 | else |
---|
42 | {var options=this.data('options');if(options) |
---|
43 | {return(options.change);} |
---|
44 | else |
---|
45 | {return(null);}}}};var privateMethods={setOptions:function(object,value) |
---|
46 | {var properties=object.data('properties'),options=object.data('options');if(!$.isPlainObject(value))return(false);properties.initialized=false;privateMethods.setDisabled(object,(value.disabled!=null)?value.disabled:options.disabled);privateMethods.setHidden(object,(value.hidden!=null)?value.hidden:options.hidden);privateMethods.setValue(object,(value.value!=null)?value.value:options.value,true);privateMethods.setEventChange(object,(value.change!=null)?value.change:options.change);properties.initialized=true;},setIsValid:function(object,value) |
---|
47 | {var objects=object.data('objects'),properties=object.data('properties');if(properties.isValid!=value&&properties.initialized) |
---|
48 | {properties.isValid=value;if(properties.isValid) |
---|
49 | {objects.container.removeClass('ui-error');objects.input.removeClass('ui-error');} |
---|
50 | else |
---|
51 | {objects.container.addClass('ui-error');objects.input.addClass('ui-error');}} |
---|
52 | return(properties.isValid);},setDisabled:function(object,value) |
---|
53 | {var objects=object.data('objects'),index=-1;if(!$.isArray(value))value=[value];for(var i=0;i<objects.radio.length;i++) |
---|
54 | {index=$.inArray(objects.radio[i].attr('value'),value);objects.radio[i].attr('disabled',(index>-1));} |
---|
55 | return(privateMethods.getDisabled(object));},getDisabled:function(object) |
---|
56 | {var objects=object.data('objects'),returned=[];for(var i=0;i<objects.radio.length;i++) |
---|
57 | {if(objects.radio[i].attr('disabled'))returned.push(objects.radio[i].attr('value'));} |
---|
58 | return(returned);},setHidden:function(object,value) |
---|
59 | {var objects=object.data('objects');if(!$.isArray(value))value=[value];for(var i=0;i<objects.radio.length;i++) |
---|
60 | {if($.inArray(objects.radio[i].attr('value'),value)>-1) |
---|
61 | {if(objects.radio[i].parent().attr('nodeName')=='LABEL') |
---|
62 | {objects.radio[i].parent().hide();} |
---|
63 | else |
---|
64 | {objects.radio[i].hide();}} |
---|
65 | else |
---|
66 | {if(objects.radio[i].parent().attr('nodeName')=='LABEL') |
---|
67 | {objects.radio[i].parent().show();} |
---|
68 | else |
---|
69 | {objects.radio[i].show();}}} |
---|
70 | return(privateMethods.getHidden(object));},getHidden:function(object) |
---|
71 | {var objects=object.data('objects'),returned=[];for(var i=0;i<objects.radio.length;i++) |
---|
72 | {if(objects.radio[i].css('display')=='none')returned.push(objects.radio[i].attr('value'));} |
---|
73 | return(returned);},setValue:function(object,value,apply) |
---|
74 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects'),index=privateMethods.findRadioByVal(object,value);if(index==-1)return(false);properties.value=value;if(apply) |
---|
75 | {objects.radio[index].attr('checked',true);} |
---|
76 | if(options.change)object.trigger('inputRadioChange',properties.value);return(true);},setEventChange:function(object,value) |
---|
77 | {var options=object.data('options');options.change=value;object.unbind('inputRadioChange');if(value)object.bind('inputRadioChange',options.change);return(options.change);},findRadioByVal:function(object,value) |
---|
78 | {var objects=object.data('objects');for(var i=0;i<objects.radio.length;i++) |
---|
79 | {if(objects.radio[i].attr('value')==value)return(i);} |
---|
80 | return(-1);}};$.fn.inputRadio=function(method) |
---|
81 | {if(publicMethods[method]) |
---|
82 | {return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1));} |
---|
83 | else if(typeof method==='object'||!method) |
---|
84 | {return publicMethods.init.apply(this,arguments);} |
---|
85 | else |
---|
86 | {$.error('Method '+method+' does not exist on jQuery.inputRadio');}}})(jQuery); |
---|