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