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     // private
70     initEvents : function()
71     {   
72         Roo.bootstrap.NumberField.superclass.initEvents.call(this);
73         
74         var allowed = "0123456789";
75         
76         if(this.allowDecimals){
77             allowed += this.decimalSeparator;
78         }
79         
80         if(this.allowNegative){
81             allowed += "-";
82         }
83         
84         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
85         
86         var keyPress = function(e){
87             
88             var k = e.getKey();
89             
90             var c = e.getCharCode();
91             
92             if(
93                     (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
94                     allowed.indexOf(String.fromCharCode(c)) === -1
95             ){
96                 e.stopEvent();
97                 return;
98             }
99             
100             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
101                 return;
102             }
103             
104             if(allowed.indexOf(String.fromCharCode(c)) === -1){
105                 e.stopEvent();
106             }
107         };
108         
109         this.el.on("keypress", keyPress, this);
110     },
111     
112     validateValue : function(value)
113     {
114         
115         if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
116             return false;
117         }
118         
119         var num = this.parseValue(value);
120         
121         if(isNaN(num)){
122             this.markInvalid(String.format(this.nanText, value));
123             return false;
124         }
125         
126         if(num < this.minValue){
127             this.markInvalid(String.format(this.minText, this.minValue));
128             return false;
129         }
130         
131         if(num > this.maxValue){
132             this.markInvalid(String.format(this.maxText, this.maxValue));
133             return false;
134         }
135         
136         return true;
137     },
138
139     getValue : function()
140     {
141         return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
142     },
143
144     parseValue : function(value)
145     {
146         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
147         return isNaN(value) ? '' : value;
148     },
149
150     fixPrecision : function(value)
151     {
152         var nan = isNaN(value);
153         
154         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
155             return nan ? '' : value;
156         }
157         return parseFloat(value).toFixed(this.decimalPrecision);
158     },
159
160     setValue : function(v)
161     {
162         v = this.fixPrecision(v);
163         Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
164     },
165
166     decimalPrecisionFcn : function(v)
167     {
168         return Math.floor(v);
169     },
170
171     beforeBlur : function()
172     {
173         if(!this.castInt){
174             return;
175         }
176         
177         var v = this.parseValue(this.getRawValue());
178         if(v){
179             this.setValue(v);
180         }
181     }
182     
183 });
184
185