better support for mailchimp emails
[roojs1] / Roo / bootstrap / form / NumberField.js
1 /*
2  * - LGPL
3  *
4  * Number field 
5  */
6
7 /**
8  * @class Roo.bootstrap.form.NumberField
9  * @extends Roo.bootstrap.form.Input
10  * Bootstrap NumberField class
11  * 
12  * 
13  * 
14  * 
15  * @constructor
16  * Create a new NumberField
17  * @param {Object} config The config object
18  */
19
20 Roo.bootstrap.form.NumberField = function(config){
21     Roo.bootstrap.form.NumberField.superclass.constructor.call(this, config);
22 };
23
24 Roo.extend(Roo.bootstrap.form.NumberField, Roo.bootstrap.form.Input, {
25     
26     /**
27      * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
28      */
29     allowDecimals : true,
30     /**
31      * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
32      */
33     decimalSeparator : ".",
34     /**
35      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
36      */
37     decimalPrecision : 2,
38     /**
39      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
40      */
41     allowNegative : true,
42     
43     /**
44      * @cfg {Boolean} allowZero False to blank out if the user enters '0' (defaults to true)
45      */
46     allowZero: true,
47     /**
48      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
49      */
50     minValue : Number.NEGATIVE_INFINITY,
51     /**
52      * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
53      */
54     maxValue : Number.MAX_VALUE,
55     /**
56      * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
57      */
58     minText : "The minimum value for this field is {0}",
59     /**
60      * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
61      */
62     maxText : "The maximum value for this field is {0}",
63     /**
64      * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
65      * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
66      */
67     nanText : "{0} is not a valid number",
68     /**
69      * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
70      */
71     thousandsDelimiter : false,
72     /**
73      * @cfg {String} valueAlign alignment of value
74      */
75     valueAlign : "left",
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.form.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         return cfg;
101     },
102
103     // private
104     initEvents : function()
105     {   
106         Roo.bootstrap.form.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         if(this.thousandsDelimiter) {
119             allowed += ",";
120         }
121         
122         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
123         
124         var keyPress = function(e){
125             
126             var k = e.getKey();
127             
128             var c = e.getCharCode();
129             
130             if(
131                     (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
132                     allowed.indexOf(String.fromCharCode(c)) === -1
133             ){
134                 e.stopEvent();
135                 return;
136             }
137             
138             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
139                 return;
140             }
141             
142             if(allowed.indexOf(String.fromCharCode(c)) === -1){
143                 e.stopEvent();
144             }
145         };
146         
147         this.el.on("keypress", keyPress, this);
148     },
149     
150     validateValue : function(value)
151     {
152         
153         if(!Roo.bootstrap.form.NumberField.superclass.validateValue.call(this, value)){
154             return false;
155         }
156         
157         var num = this.parseValue(value);
158         
159         if(isNaN(num)){
160             this.markInvalid(String.format(this.nanText, value));
161             return false;
162         }
163         
164         if(num < this.minValue){
165             this.markInvalid(String.format(this.minText, this.minValue));
166             return false;
167         }
168         
169         if(num > this.maxValue){
170             this.markInvalid(String.format(this.maxText, this.maxValue));
171             return false;
172         }
173         
174         return true;
175     },
176
177     getValue : function()
178     {
179         var v = this.hiddenEl().getValue();
180         
181         return this.fixPrecision(this.parseValue(v));
182     },
183
184     parseValue : function(value)
185     {
186         if(this.thousandsDelimiter) {
187             value += "";
188             r = new RegExp(",", "g");
189             value = value.replace(r, "");
190         }
191         
192         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
193         return isNaN(value) ? '' : value;
194     },
195
196     fixPrecision : function(value)
197     {
198         if(this.thousandsDelimiter) {
199             value += "";
200             r = new RegExp(",", "g");
201             value = value.replace(r, "");
202         }
203         
204         var nan = isNaN(value);
205         
206         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
207             return nan ? '' : value;
208         }
209         return parseFloat(value).toFixed(this.decimalPrecision);
210     },
211
212     setValue : function(v)
213     {
214         v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
215         
216         this.value = v;
217         
218         if(this.rendered){
219             
220             this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
221             
222             this.inputEl().dom.value = (v == '') ? '' :
223                 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
224             
225             if(!this.allowZero && v === '0') {
226                 this.hiddenEl().dom.value = '';
227                 this.inputEl().dom.value = '';
228             }
229             
230             this.validate();
231         }
232     },
233
234     decimalPrecisionFcn : function(v)
235     {
236         return Math.floor(v);
237     },
238
239     beforeBlur : function()
240     {
241         var v = this.parseValue(this.getRawValue());
242         
243         if(v || v === 0 || v === ''){
244             this.setValue(v);
245         }
246     },
247     
248     hiddenEl : function()
249     {
250         return this.el.select('input.hidden-number-input',true).first();
251     }
252     
253 });
254
255