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