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