partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

NumericField.js (4089B)


      1 /**
      2  * @author: Frédéric Thomas
      3  * Date: 22/03/12
      4  * Time: 16:37
      5  */
      6 Ext.define('Ext.ux.NumericField', {
      7     extend: 'Ext.form.field.Number',
      8     alias: ['widget.currencyField'],
      9     config: {
     10         thousandSeparator: ' ',
     11         currencyAtEnd: true,
     12         currencySign: '€'
     13     },
     14 
     15     listeners: {
     16         /**
     17          * When this component get the focus, change the Currency
     18          * representation to a Float one for edition.
     19          *
     20          * @param me
     21          * @param eOpts
     22          */
     23         focus: function (me, eOpts) {
     24             me.inputEl.dom.value = this.getValue();
     25         }
     26     },
     27 
     28     /**
     29      * Converts a Float value into a currency formated value ready to display .
     30      *
     31      * @param {Object} value
     32      * @return {Object} The converted value.
     33      */
     34     valueToCurrency: function (value) {
     35         var format = Ext.util.Format;
     36         format.currencyPrecision = this.decimalPrecision;
     37         format.thousandSeparator = this.thousandSeparator;
     38         format.currencySign = this.currencySign;
     39         format.currencyAtEnd = this.currencyAtEnd;
     40         return format.currency(value);
     41     },
     42     /**
     43      * Converts a mixed-type value to a raw representation suitable for displaying in the field. This allows controlling
     44      * how value objects passed to {@link #setValue} are shown to the user, including localization.
     45      *
     46      * See {@link #rawToValue} for the opposite conversion.
     47      *
     48      * This implementation converts the raw value to a value formated as currency.
     49      *
     50      * @param {Object} value The mixed-type value to convert to the raw representation.
     51      * @return {Object} The converted raw value.
     52      */
     53     valueToRaw: function (value) {
     54         return this.valueToCurrency(value);
     55     },
     56 
     57     /**
     58      * Performs any necessary manipulation of a raw String value to prepare it for conversion and/or
     59      * {@link #validate validation}. Overrided to apply the {@link #parseValue}
     60      * to the raw value.
     61      *
     62      * @param {String} value The unprocessed string value
     63      * @return {String} The processed string value
     64      */
     65     processRawValue: function (value) {
     66     	value = this.callParent(arguments);
     67     	
     68     	if (isNaN(value) || value === null || value === "") {
     69     		return value;
     70     	}
     71 
     72         return this.parseValue(value);
     73     },
     74     /**
     75      * Runs all of Number's validations and returns an array of any errors. Note that this first runs Text's
     76      * validations, so the returned array is an amalgamation of all field errors. The additional validations run test
     77      * that the value is a number, and that it is within the configured min and max values.
     78      * @param {Object} [value] The value to get errors for (defaults to the current field value)
     79      * @return {String[]} All validation errors for this field
     80      */
     81     getErrors: function(value) {
     82         var me = this,
     83             errors = [], // This is a hack because of the strange class layout...
     84             format = Ext.String.format,
     85             num;
     86 
     87         value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
     88 
     89         if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
     90              return errors;
     91         }
     92 
     93         value = this.parseValue(value);
     94 
     95         if(isNaN(value)){
     96             errors.push(format(me.nanText, value));
     97         }
     98 
     99         if (me.minValue === 0 && value < 0) {
    100             errors.push(this.negativeText);
    101         }
    102         else if (value < me.minValue) {
    103             errors.push(format(me.minText, me.minValue));
    104         }
    105 
    106         if (value > me.maxValue) {
    107             errors.push(format(me.maxText, me.maxValue));
    108         }
    109 
    110 
    111         return errors;
    112     },
    113     /**
    114      * Overrided to remove thousand separator.
    115      *
    116      * @param value
    117      */
    118     parseValue: function (value) {
    119         value = String(value).replace(this.thousandSeparator, "");
    120     	value = String(value).replace(this.currencySign, "");
    121         value = parseFloat(String(value).replace(this.decimalSeparator, '.'));
    122         return isNaN(value) ? null : value;
    123     }
    124 });