Fix #6465 - drag drop for cards
[roojs1] / Roo / bootstrap / PhoneInput.js
index 49be863..fae2356 100644 (file)
@@ -9,25 +9,13 @@
 /**
  * @class Roo.bootstrap.PhoneInput
  * @extends Roo.bootstrap.TriggerField
- * An input combines with  dial-code selection
- * @cfg {Boolean} append (true|false) default false
- * @cfg {Boolean} autoFocus (true|false) auto focus the first item, default true
- * @cfg {Boolean} tickable ComboBox with tickable selections (true|false), default false
- * @cfg {Boolean} triggerList trigger show the list or not (true|false) default true
- * @cfg {Boolean} showToggleBtn show toggle button or not (true|false) default true
- * @cfg {String} btnPosition set the position of the trigger button (left | right) default right
- * @cfg {Boolean} animate default true
- * @cfg {Boolean} emptyResultText only for touch device
- * @cfg {String} triggerText multiple combobox trigger button text default 'Select'
- * @cfg {String} emptyTitle default ''
+ * An input with International dial-code selection
  
- * @cfg {String} emptyTitle default ''
- * @cfg {String} emptyTitle default ''
- * @cfg {String} emptyTitle default ''
- * @cfg {String} emptyTitle default ''
+ * @cfg {String} defaultDialCode default '+852'
+ * @cfg {Array} preferedCountries default []
   
  * @constructor
- * Create a new ComboBox.
+ * Create a new PhoneInput.
  * @param {Object} config Configuration options
  */
 
@@ -37,8 +25,6 @@ Roo.bootstrap.PhoneInput = function(config) {
 
 Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         
-        triggerList : true,
-        
         listWidth: undefined,
         
         selectedClass: 'active',
@@ -47,19 +33,19 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         
         validClass: 'has-success',
         
-        defaultDialCode: '+852',
-        
-        allCountries: [],
+        allowed: '0123456789',
         
-        dialCodeMapping: [],
+        max_length: 15,
         
-        preferedCountries: [
-            'hk',
-            'gb',
-            'us'
-        ],
+        /**
+         * @cfg {String} defaultDialCode The default dial code when initializing the input
+         */
+        defaultDialCode: '+852',
         
-        //white list / black list for countries?
+        /**
+         * @cfg {Array} preferedCountries A list of iso2 in array (e.g. ['hk','us']). Those related countries will show at the top of the input's choices
+         */
+        preferedCountries: false,
         
         getAutoCreate : function()
         {
@@ -67,6 +53,9 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
             var align = this.labelAlign || this.parentLabelAlign();
             var id = Roo.id();
             
+            this.allCountries = [];
+            this.dialCodeMapping = [];
+            
             for (var i = 0; i < data.length; i++) {
               var c = data[i];
               this.allCountries[i] = {
@@ -92,12 +81,20 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
             var input =  {
                 tag: 'input',
                 id : id,
+                // type: 'number', -- do not use number - we get the flaky up/down arrows.
+                maxlength: this.max_length,
                 cls : 'form-control tel-input',
                 autocomplete: 'new-password'
             };
             
+            var hiddenInput = {
+                tag: 'input',
+                type: 'hidden',
+                cls: 'hidden-tel-input'
+            };
+            
             if (this.name) {
-                input.name = this.name;
+                hiddenInput.name = this.name;
             }
             
             if (this.disabled) {
@@ -123,6 +120,7 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
                 tag: 'div',
                 cls: this.hasFeedback ? 'has-feedback' : '',
                 cn: [
+                    hiddenInput,
                     input,
                     {
                         tag: 'input',
@@ -246,6 +244,14 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
                 })
             });
             
+            if(!this.preferedCountries) {
+                this.preferedCountries = [
+                    'hk',
+                    'gb',
+                    'us'
+                ];
+            }
+            
             var p = this.preferedCountries.reverse();
             
             if(p) {
@@ -289,7 +295,8 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
             
             this.list.on('mouseover', this.onViewOver, this);
             this.list.on('mousemove', this.onViewMove, this);
-            //this.list.on('scroll', this.onViewScroll, this);
+            this.inputEl().on("keyup", this.onKeyUp, this);
+            this.inputEl().on("keypress", this.onKeyPress, this);
             
             this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
 
@@ -304,7 +311,7 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         onTriggerClick : function(e)
         {
             Roo.log('trigger click');
-            if(this.disabled || !this.triggerList){
+            if(this.disabled){
                 return;
             }
             
@@ -413,7 +420,8 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
                 cls: 'typeahead typeahead-long dropdown-menu tel-list',
                 style: 'display:none'
             });
-            this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';;
+            
+            this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
         },
         
         collapseIf : function(e)
@@ -480,28 +488,34 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         setValue : function(v)
         {
             var d = this.getDialCode(v);
-            this.value = v;
             
-            if(!d || d.length == 0) {
+            //invalid dial code
+            if(v.length == 0 || !d || d.length == 0) {
                 if(this.rendered){
                     this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
+                    this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
                 }
                 return;
             }
             
+            //valid dial code
             this.setFlagClass(this.dialCodeMapping[d].iso2);
             this.setDialCode(d);
             this.inputEl().dom.value = v.replace('+'+d,'');
+            this.hiddenEl().dom.value = this.getValue();
+            
+            this.validate();
         },
         
-        getDialCode : function(v = '')
+        getDialCode : function(v)
         {
+            v = v ||  '';
+            
             if (v.length == 0) {
                 return this.dialCodeHolder.dom.value;
             }
             
             var dialCode = "";
-            // only interested in international numbers (starting with a plus)
             if (v.charAt(0) != "+") {
                 return false;
             }
@@ -524,5 +538,23 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         reset : function()
         {
             this.setValue(this.defaultDialCode);
+            this.validate();
+        },
+        
+        hiddenEl : function()
+        {
+            return this.el.select('input.hidden-tel-input',true).first();
+        },
+        
+        // after setting val
+        onKeyUp : function(e){
+            this.setValue(this.getValue());
+        },
+        
+        onKeyPress : function(e){
+            if(this.allowed.indexOf(String.fromCharCode(e.getCharCode())) === -1){
+                e.stopEvent();
+            }
         }
+        
 });
\ No newline at end of file