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