source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/form/NumberField.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 5.2 KB
RevLine 
[619]1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.form.NumberField
11 * @extends Ext.form.TextField
12 * Numeric text field that provides automatic keystroke filtering and numeric validation.
13 * @constructor
14 * Creates a new NumberField
15 * @param {Object} config Configuration options
16 */
17Ext.form.NumberField = Ext.extend(Ext.form.TextField,  {
18    /**
19     * @cfg {RegExp} stripCharsRe @hide
20     */
21    /**
22     * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
23     */
24    fieldClass: "x-form-field x-form-num-field",
25    /**
26     * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
27     */
28    allowDecimals : true,
29    /**
30     * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
31     */
32    decimalSeparator : ".",
33    /**
34     * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
35     */
36    decimalPrecision : 2,
37    /**
38     * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
39     */
40    allowNegative : true,
41    /**
42     * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
43     */
44    minValue : Number.NEGATIVE_INFINITY,
45    /**
46     * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
47     */
48    maxValue : Number.MAX_VALUE,
49    /**
50     * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
51     */
52    minText : "The minimum value for this field is {0}",
53    /**
54     * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
55     */
56    maxText : "The maximum value for this field is {0}",
57    /**
58     * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
59     * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
60     */
61    nanText : "{0} is not a valid number",
62    /**
63     * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
64     */
65    baseChars : "0123456789",
66
67    // private
68    initEvents : function(){
69        Ext.form.NumberField.superclass.initEvents.call(this);
70        var allowed = this.baseChars+'';
71        if(this.allowDecimals){
72            allowed += this.decimalSeparator;
73        }
74        if(this.allowNegative){
75            allowed += "-";
76        }
77        this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
78        var keyPress = function(e){
79            var k = e.getKey();
80            if(!Ext.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
81                return;
82            }
83            var c = e.getCharCode();
84            if(allowed.indexOf(String.fromCharCode(c)) === -1){
85                e.stopEvent();
86            }
87        };
88        this.el.on("keypress", keyPress, this);
89    },
90
91    // private
92    validateValue : function(value){
93        if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
94            return false;
95        }
96        if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
97             return true;
98        }
99        value = String(value).replace(this.decimalSeparator, ".");
100        if(isNaN(value)){
101            this.markInvalid(String.format(this.nanText, value));
102            return false;
103        }
104        var num = this.parseValue(value);
105        if(num < this.minValue){
106            this.markInvalid(String.format(this.minText, this.minValue));
107            return false;
108        }
109        if(num > this.maxValue){
110            this.markInvalid(String.format(this.maxText, this.maxValue));
111            return false;
112        }
113        return true;
114    },
115
116    getValue : function(){
117        return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
118    },
119
120    setValue : function(v){
121        v = typeof v == 'number' ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
122        v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
123        Ext.form.NumberField.superclass.setValue.call(this, v);
124    },
125
126    // private
127    parseValue : function(value){
128        value = parseFloat(String(value).replace(this.decimalSeparator, "."));
129        return isNaN(value) ? '' : value;
130    },
131
132    // private
133    fixPrecision : function(value){
134        var nan = isNaN(value);
135        if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
136           return nan ? '' : value;
137        }
138        return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
139    },
140
141    beforeBlur : function(){
142        var v = this.parseValue(this.getRawValue());
143        if(v || v === 0){
144            this.setValue(this.fixPrecision(v));
145        }
146    }
147});
148Ext.reg('numberfield', Ext.form.NumberField);
Note: See TracBrowser for help on using the repository browser.