Roo/bootstrap/MoneyField.js
[roojs1] / Roo / bootstrap / MoneyField.js
index 779c524..f681c79 100644 (file)
@@ -28,11 +28,15 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
     /**
      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
      */
-    decimalPrecision : 2,
+    decimalPrecision : 0,
     /**
      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
      */
     allowNegative : true,
+    /**
+     * @cfg {Boolean} allowZero False to blank out if the user enters '0' (defaults to true)
+     */
+    allowZero: true,
     /**
      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
      */
@@ -58,6 +62,19 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
      * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
      */
     castInt : true,
+    /**
+     * @cfg {String} defaults currency of the MoneyField
+     * value should be in lkey
+     */
+    defaultCurrency : false,
+    /**
+     * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
+     */
+    thousandsDelimiter : false,
+    /**
+     * @cfg {Number} max_length Maximum input field length allowed (defaults to Number.MAX_VALUE)
+     */
+    max_length: false,
     
     inputlg : 9,
     inputmd : 9,
@@ -84,8 +101,19 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
             autocomplete: 'new-password'
         };
         
+        var hiddenInput = {
+            tag: 'input',
+            type: 'hidden',
+            id: Roo.id(),
+            cls: 'hidden-number-input'
+        };
+        
+        if(this.max_length) {
+            input.maxlength = this.max_length; 
+        }
+        
         if (this.name) {
-            input.name = this.name;
+            hiddenInput.name = this.name;
         }
 
         if (this.disabled) {
@@ -112,7 +140,9 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
                                 {
                                     tag : 'input',
                                     cls : 'form-control roo-money-currency-input',
-                                    autocomplete: 'new-password'
+                                    autocomplete: 'new-password',
+                                    readOnly : 1,
+                                    name : this.currencyName
                                 },
                                 {
                                     tag :'span',
@@ -212,9 +242,10 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
 
         cfg.cn = [
             label,
-            container
+            container,
+            hiddenInput
         ];
-
+        
         var settings = this;
 
         ['xs','sm','md','lg'].map(function(size){
@@ -224,7 +255,6 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         });
         
         return cfg;
-        
     },
     
     initEvents : function()
@@ -234,7 +264,6 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         this.initCurrencyEvent();
         
         this.initNumberEvent();
-        
     },
     
     initCurrencyEvent : function()
@@ -328,17 +357,7 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
             forceKeyDown: true
         });
         
-        this.queryDelay = Math.max(this.queryDelay || 10,
-                this.mode == 'local' ? 10 : 250);
-        
-        
-        this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
-        
-        if(this.typeAhead){
-            this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
-        }
-        
-        this.currencyEl().on("keyup", this.onCurrencyKeyUp, this);
+        this.currencyEl().on("click", this.onTriggerClick, this, { preventDefault : true });
         
     },
     
@@ -384,6 +403,10 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
             allowed += "-";
         }
         
+        if(this.thousandsDelimiter) {
+            allowed += ",";
+        }
+        
         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
         
         var keyPress = function(e){
@@ -487,6 +510,12 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         
         this.lastSelectionText = currency;
         
+        //setting default currency
+        if(o[this.currencyField] * 1 == 0 && this.defaultCurrency) {
+            this.setCurrency(this.defaultCurrency);
+            return;
+        }
+        
         this.setCurrency(currency);
     },
     
@@ -522,14 +551,22 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
     
     setValue : function(v)
     {
-        v = this.fixPrecision(v);
-        
-        v = String(v).replace(".", this.decimalSeparator);
+        v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
         
         this.value = v;
         
         if(this.rendered){
-            this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
+            
+            this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
+            
+            this.inputEl().dom.value = (v == '') ? '' :
+                Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
+            
+            if(!this.allowZero && v === '0') {
+                this.hiddenEl().dom.value = '';
+                this.inputEl().dom.value = '';
+            }
+            
             this.validate();
         }
     },
@@ -548,18 +585,30 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
     
     parseValue : function(value)
     {
+        if(this.thousandsDelimiter) {
+            value += "";
+            r = new RegExp(",", "g");
+            value = value.replace(r, "");
+        }
+        
         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
         return isNaN(value) ? '' : value;
+        
     },
     
     fixPrecision : function(value)
     {
+        if(this.thousandsDelimiter) {
+            value += "";
+            r = new RegExp(",", "g");
+            value = value.replace(r, "");
+        }
+        
         var nan = isNaN(value);
         
         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
             return nan ? '' : value;
         }
-        
         return parseFloat(value).toFixed(this.decimalPrecision);
     },
     
@@ -596,7 +645,7 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
     
     validate : function()
     {
-        if(this.disabled){
+        if(this.disabled || this.allowBlank){
             this.markValid();
             return true;
         }
@@ -625,7 +674,7 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         
         var v = this.parseValue(this.getRawValue());
         
-        if(v){
+        if(v || v == 0){
             this.setValue(v);
         }
     },
@@ -653,15 +702,6 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         this.fireEvent("blur", this);
     },
     
-    onCurrencyKeyUp : function(e)
-    {
-        if(!e.isSpecialKey()){
-            this.hasFocus = true;
-            this.lastKey = e.getKey();
-            this.dqTask.delay(this.queryDelay);
-        }
-    },
-    
     inputEl : function()
     {
         return this.el.select('.roo-money-amount-input', true).first();
@@ -672,45 +712,9 @@ Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
         return this.el.select('.roo-money-currency-input', true).first();
     },
     
-    initQuery : function()
-    {
-        var v = this.getCurrency();
-        
-        this.doQuery(v);
-    },
-    
-    onTypeAhead : function()
-    {
-        if(this.store.getCount() > 0){
-            var r = this.store.getAt(0);
-            var newValue = r.data[this.currencyField];
-            var len = newValue.length;
-            var selStart = this.getCurrency().length;
-            
-            if(selStart != len){
-                this.setCurrency(newValue);
-                this.selectText(selStart, newValue.length);
-            }
-        }
-    },
-    
-    selectText : function(start, end)
+    hiddenEl : function()
     {
-        var v = this.getCurrency();
-        
-        if(v.length > 0){
-            start = start === undefined ? 0 : start;
-            end = end === undefined ? v.length : end;
-            var d = this.el.dom;
-            if(d.setSelectionRange){
-                d.setSelectionRange(start, end);
-            }else if(d.createTextRange){
-                var range = d.createTextRange();
-                range.moveStart("character", start);
-                range.moveEnd("character", v.length-end);
-                range.select();
-            }
-        }
+        return this.el.select('input.hidden-number-input',true).first();
     }
     
 });
\ No newline at end of file