Roo/bootstrap/NumberField.js
[roojs1] / Roo / bootstrap / NumberField.js
1 /*
2  * - LGPL
3  *
4  * Input
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.NumberField
10  * @extends Roo.bootstrap.Input
11  * Bootstrap NumberField class
12  * 
13  * 
14  * 
15  * 
16  * @constructor
17  * Create a new NumberField
18  * @param {Object} config The config object
19  */
20
21 Roo.bootstrap.NumberField = function(config){
22     Roo.bootstrap.NumberField.superclass.constructor.call(this, config);
23 };
24
25 Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
26     
27     /**
28      * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
29      */
30     allowDecimals : true,
31     /**
32      * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
33      */
34     decimalSeparator : ".",
35     /**
36      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
37      */
38     decimalPrecision : 2,
39     /**
40      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
41      */
42     allowNegative : true,
43     /**
44      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
45      */
46     minValue : Number.NEGATIVE_INFINITY,
47     /**
48      * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
49      */
50     maxValue : Number.MAX_VALUE,
51     /**
52      * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
53      */
54     minText : "The minimum value for this field is {0}",
55     /**
56      * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
57      */
58     maxText : "The maximum value for this field is {0}",
59     /**
60      * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
61      * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
62      */
63     nanText : "{0} is not a valid number",
64     /**
65      * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
66      */
67     castInt : true,
68     /**
69      * @cfg {Boolean} allowThousandsDelimiter (true|false) display thousands delimiter if true (e.g. "100,000") (defalut false)
70      */
71     allowThousandsDelimiter : false,
72     /**
73      * @cfg {String} symbol of thousandsDelimiter
74      */
75     thousandsDelimiter : ",",
76
77     getAutoCreate : function()
78     {
79         var hidden_input_name = this.name;
80         
81         Roo.log(this.name);
82         
83         this.name = '';
84         
85         var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
86         
87         if(cfg.cn.length > 0) {
88             cfg.cn.push({
89                 tag : 'input',
90                 name : hidden_input_name,
91                 value : this.value,
92                 type : 'hidden',
93                 cls: 'form-hidden-field'
94             })
95         }
96         
97         Roo.log(cfg);
98         
99         return cfg;
100     },
101
102     // private
103     initEvents : function()
104     {   
105         Roo.bootstrap.NumberField.superclass.initEvents.call(this);
106         
107         var allowed = "0123456789";
108         
109         if(this.allowDecimals){
110             allowed += this.decimalSeparator;
111         }
112         
113         if(this.allowNegative){
114             allowed += "-";
115         }
116         
117         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
118         
119         var keyPress = function(e){
120             
121             var k = e.getKey();
122             
123             var c = e.getCharCode();
124             
125             if(
126                     (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
127                     allowed.indexOf(String.fromCharCode(c)) === -1
128             ){
129                 e.stopEvent();
130                 return;
131             }
132             
133             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
134                 return;
135             }
136             
137             if(allowed.indexOf(String.fromCharCode(c)) === -1){
138                 e.stopEvent();
139             }
140         };
141         
142         this.el.on("keypress", keyPress, this);
143     },
144     
145     validateValue : function(value)
146     {
147         
148         if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
149             return false;
150         }
151         
152         var num = this.parseValue(value);
153         
154         if(isNaN(num)){
155             this.markInvalid(String.format(this.nanText, value));
156             return false;
157         }
158         
159         if(num < this.minValue){
160             this.markInvalid(String.format(this.minText, this.minValue));
161             return false;
162         }
163         
164         if(num > this.maxValue){
165             this.markInvalid(String.format(this.maxText, this.maxValue));
166             return false;
167         }
168         
169         return true;
170     },
171
172     getValue : function()
173     {
174         return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
175     },
176
177     parseValue : function(value)
178     {
179         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
180         return isNaN(value) ? '' : value;
181     },
182
183     fixPrecision : function(value)
184     {
185         var nan = isNaN(value);
186         
187         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
188             return nan ? '' : value;
189         }
190         return parseFloat(value).toFixed(this.decimalPrecision);
191     },
192
193     setValue : function(v)
194     {
195         v = this.fixPrecision(v);
196         Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
197     },
198
199     decimalPrecisionFcn : function(v)
200     {
201         return Math.floor(v);
202     },
203
204     beforeBlur : function()
205     {
206         if(!this.castInt){
207             return;
208         }
209         
210         var v = this.parseValue(this.getRawValue());
211         if(v){
212             this.setValue(v);
213         }
214     },
215     
216     addThousandsDelimiter : function(v)
217     {
218         if(!this.allowThousandsDelimiter) {
219             return v;
220         }
221         
222         v += "";
223         
224         var x = v.split(".");
225         
226         var x1 = x[0];
227         
228         var x2 = x.length > 1 ? "." + x[1] : "";
229         
230         var rgx = /(\d+)(\d{3})/;
231         
232         while (rgx.test(x1)) {
233             x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
234         }
235         
236         return x1 + x2;
237     },
238     
239     inputEl: function ()
240     {
241         return this.el.select('input.form-hidden-field',true).first();
242     }
243     
244 });
245
246