sync
[roojs1] / Roo / bootstrap / PhoneInput.js
index 37cb1bd..46c1506 100644 (file)
@@ -25,31 +25,25 @@ Roo.bootstrap.PhoneInput = function(config) {
 
 Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         
-        triggerList : true,
-        
         listWidth: undefined,
         
         selectedClass: 'active',
         
         invalidClass : "has-warning",
         
-        allCountries: [],
-        
-        dialCodeMapping: [],
-        
-        keyUpDelay: 500,
-        
         validClass: 'has-success',
         
-        defaultDialCode: '+852',
+        allowed: '0123456789',
         
-        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()
         {
@@ -57,6 +51,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] = {
@@ -243,6 +240,14 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
                 })
             });
             
+            if(!this.preferedCountries) {
+                this.preferedCountries = [
+                    'hk',
+                    'gb',
+                    'us'
+                ];
+            }
+            
             var p = this.preferedCountries.reverse();
             
             if(p) {
@@ -286,7 +291,6 @@ 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.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
@@ -302,7 +306,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;
             }
             
@@ -411,7 +415,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)
@@ -479,7 +484,8 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
         {
             var d = this.getDialCode(v);
             
-            if(!d || d.length == 0 || v.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);
@@ -487,6 +493,7 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
                 return;
             }
             
+            //valid dial code
             this.setFlagClass(this.dialCodeMapping[d].iso2);
             this.setDialCode(d);
             this.inputEl().dom.value = v.replace('+'+d,'');
@@ -495,8 +502,10 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
             this.validate();
         },
         
-        getDialCode : function(v = '')
+        getDialCode : function(v)
         {
+            v = v ||  '';
+            
             if (v.length == 0) {
                 return this.dialCodeHolder.dom.value;
             }
@@ -532,8 +541,25 @@ Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
             return this.el.select('input.hidden-tel-input',true).first();
         },
         
-        onKeyUp : function(e)
-        {
+        onKeyUp : function(e){
+            
+            var k = e.getKey();
+            var c = e.getCharCode();
+            
+            if(
+                    (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
+                    this.allowed.indexOf(String.fromCharCode(c)) === -1
+            ){
+                e.stopEvent();
+            }
+            
+            // if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
+            //     return;
+            // }
+            if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
+                e.stopEvent();
+            }
+            
             this.setValue(this.getValue());
         }