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 hiddenInput = {
80             tag: 'input',
81             type: 'hidden',
82             cls: 'hidden-tel-input'
83         };
84         
85         if (this.name) {
86             hiddenInput.name = this.name;
87         }
88         
89         this.name = '';
90         
91         var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
92         
93         if(cfg.cn.length > 0) {
94             cfg.cn.push(hiddenInput)
95         }
96         
97         return cfg;
98     },
99
100     // private
101     initEvents : function()
102     {   
103         Roo.bootstrap.NumberField.superclass.initEvents.call(this);
104         
105         var allowed = "0123456789";
106         
107         if(this.allowDecimals){
108             allowed += this.decimalSeparator;
109         }
110         
111         if(this.allowNegative){
112             allowed += "-";
113         }
114         
115         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
116         
117         var keyPress = function(e){
118             
119             var k = e.getKey();
120             
121             var c = e.getCharCode();
122             
123             if(
124                     (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
125                     allowed.indexOf(String.fromCharCode(c)) === -1
126             ){
127                 e.stopEvent();
128                 return;
129             }
130             
131             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
132                 return;
133             }
134             
135             if(allowed.indexOf(String.fromCharCode(c)) === -1){
136                 e.stopEvent();
137             }
138         };
139         
140         this.el.on("keypress", keyPress, this);
141     },
142     
143     validateValue : function(value)
144     {
145         
146         if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
147             return false;
148         }
149         
150         var num = this.parseValue(value);
151         
152         if(isNaN(num)){
153             this.markInvalid(String.format(this.nanText, value));
154             return false;
155         }
156         
157         if(num < this.minValue){
158             this.markInvalid(String.format(this.minText, this.minValue));
159             return false;
160         }
161         
162         if(num > this.maxValue){
163             this.markInvalid(String.format(this.maxText, this.maxValue));
164             return false;
165         }
166         
167         return true;
168     },
169
170     getValue : function()
171     {
172         return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
173     },
174
175     parseValue : function(value)
176     {
177         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
178         return isNaN(value) ? '' : value;
179     },
180
181     fixPrecision : function(value)
182     {
183         var nan = isNaN(value);
184         
185         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
186             return nan ? '' : value;
187         }
188         return parseFloat(value).toFixed(this.decimalPrecision);
189     },
190
191     setValue : function(v)
192     {
193         v = this.fixPrecision(v);
194         Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
195     },
196
197     decimalPrecisionFcn : function(v)
198     {
199         return Math.floor(v);
200     },
201
202     beforeBlur : function()
203     {
204         if(!this.castInt){
205             return;
206         }
207         
208         var v = this.parseValue(this.getRawValue());
209         if(v){
210             this.setValue(v);
211         }
212     },
213     
214     addThousandsDelimiter : function(v)
215     {
216         if(!this.allowThousandsDelimiter) {
217             return v;
218         }
219         
220         v += "";
221         
222         var x = v.split(".");
223         
224         var x1 = x[0];
225         
226         var x2 = x.length > 1 ? "." + x[1] : "";
227         
228         var rgx = /(\d+)(\d{3})/;
229         
230         while (rgx.test(x1)) {
231             x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
232         }
233         
234         return x1 + x2;
235     },
236     
237     inputEl: function ()
238     {
239         return this.el.select('input.form-hidden-field',true).first();
240     }
241     
242 });
243
244