1 | /* file: ui.inputNum.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={numDec:0,minValue:'none',maxValue:'none',stepValue:1,showSlider:'no',disabled:false,textAlign:'right',btInc:'+',btDec:'-',unitValue:'',change:null};$this.data('options',options);if(!properties) |
---|
6 | {$this.data('properties',{initialized:false,re:/^\d+$/,value:0,factor:1,isSlider:false,isValid:true,mouseIsOver:false,isSliderCurrentlyVisible:false,inputMargins:0});properties=$this.data('properties');} |
---|
7 | if(!objects) |
---|
8 | {objects={container:$('<div/>',{'class':'ui-inputNum',css:{width:'100%'}}).bind('click.inputNum',function() |
---|
9 | {objects.input.focus();}).bind('mouseenter',function() |
---|
10 | {properties.mouseIsOver=true;}).bind('mouseleave',function() |
---|
11 | {properties.mouseIsOver=false;}),input:$('<input>',{type:"text",value:''}).bind('focusout.inputNum',function() |
---|
12 | {privateMethods.lostFocus($this);}).bind('focus.inputNum',function() |
---|
13 | {privateMethods.getFocus($this);}).bind('keydown.inputNum',function(event) |
---|
14 | {return(privateMethods.keyDown($this,event));}).bind('keyup.inputNum',function(event) |
---|
15 | {privateMethods.keyUp($this,event);}),slider:$('<div/>',{css:{display:'none'}}),extraContainer:$('<div/>',{'class':'ui-inputNum-extra'}),unit:$('<div/>',{html:"",'class':'ui-inputNum-unit',css:{display:'none'}}),btInc:$('<div/>',{'class':'ui-inputNum-btInc',tabindex:0}).bind('click',function(event) |
---|
16 | {privateMethods.incValue($this);}).bind('mousedown',function(){$(this).addClass('ui-inputNum-btInc-active');}).bind('mouseup',function(){$(this).removeClass('ui-inputNum-btInc-active');}),btDec:$('<div/>',{'class':'ui-inputNum-btDec',tabindex:0}).bind('click',function(event) |
---|
17 | {privateMethods.decValue($this);}).bind('mousedown',function(){$(this).addClass('ui-inputNum-btDec-active');}).bind('mouseup',function(){$(this).removeClass('ui-inputNum-btDec-active');})};$this.html('').append(objects.container.append(objects.input).append(objects.extraContainer.append(objects.unit).append(objects.btInc).append(objects.btDec)).append(objects.slider));properties.inputMargins=objects.input.outerWidth(true)-objects.input.width();$this.data('objects',objects);} |
---|
18 | privateMethods.setOptions($this,opt);});},destroy:function() |
---|
19 | {return this.each(function() |
---|
20 | {var $this=$(this),objects=$this.data('objects');objects.input.unbind().remove();objects.btInc.unbind().remove();objects.btDec.unbind().remove();objects.container.unbind().remove();$this.unbind('.inputNum').css({width:'',height:''});});},options:function(value) |
---|
21 | {return this.each(function() |
---|
22 | {privateMethods.setOptions($(this),value);});},showSlider:function(value) |
---|
23 | {if(value!=null) |
---|
24 | {this.each(function() |
---|
25 | {privateMethods.setShowSlider($(this),value);});} |
---|
26 | else |
---|
27 | {var options=this.data('options');if(options) |
---|
28 | {return(options.showSlider);} |
---|
29 | else |
---|
30 | {return('');}}},minValue:function(value) |
---|
31 | {if(value!=null) |
---|
32 | {return this.each(function() |
---|
33 | {privateMethods.setMinValue($(this),value);});} |
---|
34 | else |
---|
35 | {var options=this.data('options');if(options) |
---|
36 | {return(options.minValue);} |
---|
37 | else |
---|
38 | {return('');}}},maxValue:function(value) |
---|
39 | {if(value!=null) |
---|
40 | {return this.each(function() |
---|
41 | {privateMethods.setMaxValue($(this),value);});} |
---|
42 | else |
---|
43 | {var options=this.data('options');if(options) |
---|
44 | {return(options.maxValue);} |
---|
45 | else |
---|
46 | {return('');}}},stepValue:function(value) |
---|
47 | {if(value!=null) |
---|
48 | {return this.each(function() |
---|
49 | {privateMethods.setStepValue($(this),value);});} |
---|
50 | else |
---|
51 | {var options=this.data('options');if(options) |
---|
52 | {return(options.stepValue);} |
---|
53 | else |
---|
54 | {return('');}}},numDec:function(value) |
---|
55 | {if(value!=null) |
---|
56 | {return this.each(function() |
---|
57 | {privateMethods.setNumDec($(this),value);});} |
---|
58 | else |
---|
59 | {var options=this.data('options');if(options) |
---|
60 | {return(options.numDec);} |
---|
61 | else |
---|
62 | {return('');}}},unitValue:function(value) |
---|
63 | {if(value!=null) |
---|
64 | {return this.each(function() |
---|
65 | {privateMethods.setUnitValue($(this),value);});} |
---|
66 | else |
---|
67 | {var options=this.data('options');if(options) |
---|
68 | {return(options.unitValue);} |
---|
69 | else |
---|
70 | {return('');}}},disabled:function(value) |
---|
71 | {if(value!=null) |
---|
72 | {return this.each(function() |
---|
73 | {privateMethods.setDisabled($(this),value);});} |
---|
74 | else |
---|
75 | {var options=this.data('options');if(options) |
---|
76 | {return(options.disabled);} |
---|
77 | else |
---|
78 | {return('');}}},textAlign:function(value) |
---|
79 | {if(value!=null) |
---|
80 | {return this.each(function() |
---|
81 | {privateMethods.setTextAlign($(this),value);});} |
---|
82 | else |
---|
83 | {var options=this.data('options');if(options) |
---|
84 | {return(options.textAlign);} |
---|
85 | else |
---|
86 | {return('');}}},btInc:function(value) |
---|
87 | {if(value!=null) |
---|
88 | {return this.each(function() |
---|
89 | {privateMethods.setBtInc($(this),value);});} |
---|
90 | else |
---|
91 | {var options=this.data('options');if(options) |
---|
92 | {return(options.btInc);} |
---|
93 | else |
---|
94 | {return('');}}},btDec:function(value) |
---|
95 | {if(value!=null) |
---|
96 | {return this.each(function() |
---|
97 | {privateMethods.setBtDec($(this),value);});} |
---|
98 | else |
---|
99 | {var options=this.data('options');if(options) |
---|
100 | {return(options.btDec);} |
---|
101 | else |
---|
102 | {return('');}}},value:function(value) |
---|
103 | {if(value!=null) |
---|
104 | {return this.each(function() |
---|
105 | {privateMethods.setValue($(this),value,true);});} |
---|
106 | else |
---|
107 | {var properties=this.data('properties');return(properties.value);}},isValid:function(value) |
---|
108 | {if(value!=null) |
---|
109 | {return this.each(function() |
---|
110 | {privateMethods.setIsValid($(this),value);});} |
---|
111 | else |
---|
112 | {var properties=this.data('properties');return(properties.isValid);}},change:function(value) |
---|
113 | {if(value!=null&&$.isFunction(value)) |
---|
114 | {return this.each(function() |
---|
115 | {privateMethods.setEventChange($(this),value);});} |
---|
116 | else |
---|
117 | {var options=this.data('options');if(options) |
---|
118 | {return(options.change);} |
---|
119 | else |
---|
120 | {return(null);}}}};var privateMethods={isValid:function(object,value) |
---|
121 | {var properties=object.data('properties');return(properties.re.exec(value))},setRE:function(object) |
---|
122 | {var properties=object.data('properties'),options=object.data('options'),tmpRe="\\d+";if(options.numDec>0) |
---|
123 | {tmpRe+="(\\.\\d{0,"+options.numDec+"})?";} |
---|
124 | tmpRe+="$";if(options.minValue=='none'||options.minValue!='none'&&options.minValue<0) |
---|
125 | {if(options.maxValue!='none'&&options.maxValue<0) |
---|
126 | {tmpRe="\\-"+tmpRe;} |
---|
127 | else |
---|
128 | {tmpRe="(\\-)?"+tmpRe;}} |
---|
129 | tmpRe="^"+tmpRe;properties.re=new RegExp(tmpRe);},setOptions:function(object,value) |
---|
130 | {var properties=object.data('properties'),options=object.data('options');if(!$.isPlainObject(value))return(false);properties.initialized=false;privateMethods.setNumDec(object,(value.numDec!=null)?value.numDec:options.numDec);privateMethods.setStepValue(object,(value.stepValue!=null)?value.stepValue:options.stepValue);privateMethods.setMinValue(object,(value.minValue!=null)?value.minValue:options.minValue);privateMethods.setMaxValue(object,(value.maxValue!=null)?value.maxValue:options.maxValue);privateMethods.setValue(object,(value.value!=null)?value.value:options.value,true);privateMethods.setUnitValue(object,(value.unitValue!=null)?value.unitValue:options.unitValue);privateMethods.setShowSlider(object,(value.showSlider!=null)?value.showSlider:options.showSlider);privateMethods.setDisabled(object,(value.disabled!=null)?value.disabled:options.disabled);privateMethods.setBtInc(object,(value.btInc!=null)?value.btInc:options.btInc);privateMethods.setBtDec(object,(value.btDec!=null)?value.btDec:options.btDec);privateMethods.setTextAlign(object,(value.textAlign!=null)?value.textAlign:options.textAlign);privateMethods.setEventChange(object,(value.change!=null)?value.change:options.change);privateMethods.calculateInputWidth(object);properties.initialized=true;},setIsValid:function(object,value) |
---|
131 | {var objects=object.data('objects'),properties=object.data('properties');if(properties.isValid!=value) |
---|
132 | {properties.isValid=value;if(properties.isValid) |
---|
133 | {objects.container.removeClass('ui-error');objects.input.removeClass('ui-error');} |
---|
134 | else |
---|
135 | {objects.container.addClass('ui-error');objects.input.addClass('ui-error');}} |
---|
136 | return(properties.isValid);},setNumDec:function(object,value) |
---|
137 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.numDec!=value)&&value>=0&&value<=15) |
---|
138 | {options.numDec=value;privateMethods.setRE(object);properties.factor=Math.pow(10,options.numDec);} |
---|
139 | return(options.numDec);},setStepValue:function(object,value) |
---|
140 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.stepValue!=value)&&value>0&&privateMethods.isValid(object,value)) |
---|
141 | {options.stepValue=value;} |
---|
142 | return(options.stepValue);},setShowSlider:function(object,value) |
---|
143 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if((!properties.initialized||options.showSlider!=value)&&(value=='no'||value=='yes'||value=='auto')) |
---|
144 | {options.showSlider=value;privateMethods.manageSlider(object);} |
---|
145 | return(options.showSlider);},setMinValue:function(object,value) |
---|
146 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.minValue!=value)&&(value=='none'||privateMethods.isValid(object,value))) |
---|
147 | {options.minValue=value;if(options.minValue>options.maxValue)options.maxValue=options.minValue;privateMethods.setRE(object);privateMethods.manageSlider(object);} |
---|
148 | return(options.minValue);},setMaxValue:function(object,value) |
---|
149 | {var options=object.data('options'),properties=object.data('properties');if((!properties.initialized||options.maxValue!=value)&&(value=='none'||privateMethods.isValid(object,value)&&(options.minValue<=value&&options.minValue!='none'||options.minValue=='none'))) |
---|
150 | {options.maxValue=value;privateMethods.setRE(object);privateMethods.manageSlider(object);} |
---|
151 | return(options.maxValue);},setUnitValue:function(object,value) |
---|
152 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if(!properties.initialized||options.unitValue!=value) |
---|
153 | {options.unitValue=value;objects.unit.html(options.unitValue).css('display',($.trim(value)=='')?'none':'');privateMethods.calculateInputWidth(object);} |
---|
154 | return(options.unitValue);},setBtInc:function(object,value) |
---|
155 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if(!properties.initialized||options.btInc!=value) |
---|
156 | {options.btInc=value;objects.btInc.html(options.btInc);} |
---|
157 | return(options.btInc);},setBtDec:function(object,value) |
---|
158 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if(!properties.initialized||options.btDec!=value) |
---|
159 | {options.btDec=value;objects.btDec.html(options.btDec);} |
---|
160 | return(options.btDec);},setDisabled:function(object,value) |
---|
161 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if((!properties.initialized||options.disabled!=value)&&(value==true||value==false)) |
---|
162 | {options.disabled=value;if(options.disabled) |
---|
163 | {objects.btDec.attr('disabled',true);objects.btInc.attr('disabled',true);objects.input.attr('disabled',true);} |
---|
164 | else |
---|
165 | {objects.input.attr('disabled',false);privateMethods.setButtonsState(object);}} |
---|
166 | return(options.disabled);},setTextAlign:function(object,value) |
---|
167 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if((!properties.initialized||options.textAlign!=value)&&(value=='left'||value=='right')) |
---|
168 | {options.textAlign=value;objects.input.css('text-align',options.textAlign);} |
---|
169 | return(options.textAlign);},setButtonsState:function(object) |
---|
170 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if(options.minValue!='none'&&properties.value-options.stepValue<options.minValue) |
---|
171 | {objects.btDec.attr('disabled',true);} |
---|
172 | else |
---|
173 | {objects.btDec.attr('disabled',false);} |
---|
174 | if(options.maxValue!='none'&&properties.value+options.stepValue>options.maxValue) |
---|
175 | {objects.btInc.attr('disabled',true);} |
---|
176 | else |
---|
177 | {objects.btInc.attr('disabled',false);}},setValue:function(object,value,apply) |
---|
178 | {var options=object.data('options'),properties=object.data('properties'),objects=object.data('objects');if(!privateMethods.isValid(object,value)||(!apply&&(options.minValue!='none'&&value<options.minValue||options.maxValue!='none'&&value>options.maxValue))) |
---|
179 | {return(privateMethods.setIsValid(object,false));} |
---|
180 | else if(apply) |
---|
181 | {if(options.minValue!='none'&&value<options.minValue)value=options.minValue;if(options.maxValue!='none'&&value>options.maxValue)value=options.maxValue;} |
---|
182 | privateMethods.setIsValid(object,true);properties.value=value;privateMethods.setButtonsState(object);if(apply)objects.input.val(properties.value.toFixed(options.numDec));if(properties.isSlider&&properties.isSliderCurrentlyVisible)objects.slider.slider('value',properties.value);if(options.change)object.trigger('inputNumChange',properties.value);return(true);},getFocus:function(object) |
---|
183 | {var objects=object.data('objects'),options=object.data('options'),properties=object.data('properties');if(properties.isSlider&&options.showSlider=='auto') |
---|
184 | {objects.slider.css('display','block').css('width',(objects.container.width()-objects.slider.children('.ui-slider-handle').outerWidth(true))+'px').slider('value',properties.value);properties.isSliderCurrentlyVisible=true;}},lostFocus:function(object) |
---|
185 | {var objects=object.data('objects'),options=object.data('options'),properties=object.data('properties');if(properties.mouseIsOver) |
---|
186 | {objects.input.focus();} |
---|
187 | else if(properties.isSlider&&options.showSlider=='auto') |
---|
188 | {objects.slider.css('display','none');properties.isSliderCurrentlyVisible=false;}},setEventChange:function(object,value) |
---|
189 | {var options=object.data('options');options.change=value;object.unbind('inputNumChange');if(value)object.bind('inputNumChange',options.change);return(options.change);},keyUp:function(object,event) |
---|
190 | {var properties=object.data('properties'),objects=object.data('objects');if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==190||event.keyCode==109||event.keyCode==110||event.keyCode==8||event.keyCode==9||event.keyCode==12||event.keyCode==46))return(false);privateMethods.setValue(object,parseFloat(objects.input.val()),false);},keyDown:function(object,event) |
---|
191 | {var properties=object.data('properties'),objects=object.data('objects');if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==190||event.keyCode==109||event.keyCode==110||event.keyCode==8||event.keyCode==9||event.keyCode==12||event.keyCode==16||event.keyCode==17||event.keyCode==18||event.keyCode==33||event.keyCode==34||event.keyCode==35||event.keyCode==36||event.keyCode==37||event.keyCode==38||event.keyCode==39||event.keyCode==40||event.keyCode==45||event.keyCode==46||event.keyCode==93))return(false);return(true);},incValue:function(object) |
---|
192 | {var options=object.data('options'),properties=object.data('properties');nextValue=Math.round(properties.value*properties.factor+options.stepValue*properties.factor)/properties.factor;if(options.maxValue=='none'||nextValue<=options.maxValue) |
---|
193 | {privateMethods.setValue(object,nextValue,true);}},decValue:function(object) |
---|
194 | {var options=object.data('options'),properties=object.data('properties');nextValue=Math.round(properties.value*properties.factor-options.stepValue*properties.factor)/properties.factor;if(options.minValue=='none'||nextValue>=options.minValue) |
---|
195 | {privateMethods.setValue(object,nextValue,true);}},manageSlider:function(object) |
---|
196 | {var options=object.data('options'),objects=object.data('objects'),properties=object.data('properties');if(!properties.isSlider&&options.minValue!='none'&&options.maxValue!='none'&&(options.showSlider=='yes'||options.showSlider=='auto')) |
---|
197 | {properties.isSlider=true;objects.slider.slider({max:options.maxValue,min:options.minValue,step:options.stepValue,value:properties.value,slide:function(event,ui) |
---|
198 | {privateMethods.setValue(object,ui.value,true);}});objects.slider.children('.ui-slider-handle').bind('focusout',function(){privateMethods.lostFocus(object);});if(options.showSlider=='yes') |
---|
199 | {objects.slider.css('display','block').css('width',(objects.container.width()-objects.slider.children('.ui-slider-handle').outerWidth(true))+'px');properties.isSliderCurrentlyVisible=true;}} |
---|
200 | else if(properties.isSlider&&(options.minValue=='none'||options.maxValue=='none'||options.showSlider=='no')) |
---|
201 | {properties.isSlider=false;properties.isSliderCurrentlyVisible=false;objects.slider.slider('destroy');}},calculateInputWidth:function(object) |
---|
202 | {var objects=object.data('objects'),properties=object.data('properties');objects.input.css('width',(objects.container.width()-objects.extraContainer.outerWidth()-properties.inputMargins)+'px');}};$.fn.inputNum=function(method) |
---|
203 | {if(publicMethods[method]) |
---|
204 | {return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1));} |
---|
205 | else if(typeof method==='object'||!method) |
---|
206 | {return publicMethods.init.apply(this,arguments);} |
---|
207 | else |
---|
208 | {$.error('Method '+method+' does not exist on jQuery.inputNum');}}})(jQuery); |
---|