diff --git a/build/js/intlTelInput.js b/build/js/intlTelInput.js index f186ee552..aa5dd7396 100644 --- a/build/js/intlTelInput.js +++ b/build/js/intlTelInput.js @@ -350,10 +350,13 @@ https://github.com/Bluefieldscom/intl-tel-input.git var isCtrl = e.which == keys.CTRL || e.which == keys.CMD1 || e.which == keys.CMD2, input = that.telInput[0], // noSelection defaults to false for bad browsers, else would be reformatting on all ctrl keys e.g. select-all/copy noSelection = that.isGoodBrowser && input.selectionStart == input.selectionEnd, // cursorAtEnd defaults to false for bad browsers else they would never get a reformat on delete cursorAtEnd = that.isGoodBrowser && input.selectionStart == that.telInput.val().length; - // if delete in the middle: reformat with no suffix (no need to reformat if delete at end) - // if backspace: reformat with no suffix (need to reformat if at end to remove any lingering suffix - this is a feature) - // if ctrl and no selection (i.e. could have just been a paste): reformat (if cursorAtEnd: add suffix) - if (e.which == keys.DEL && !cursorAtEnd || e.which == keys.BSPACE || isCtrl && noSelection) { + if (!that.telInput.val()) { + // if they just cleared the input, update the flag to the default + that._updateFlagFromNumber(""); + } else if (e.which == keys.DEL && !cursorAtEnd || e.which == keys.BSPACE || isCtrl && noSelection) { + // if delete in the middle: reformat with no suffix (no need to reformat if delete at end) + // if backspace: reformat with no suffix (need to reformat if at end to remove any lingering suffix - this is a feature) + // if ctrl and no selection (i.e. could have just been a paste): reformat (if cursorAtEnd: add suffix) // important to remember never to add suffix on any delete key as can fuck up in ie8 so you can never delete a formatting char at the end // UPDATE: pass true for 3rd arg (isAllowedKey) if might have been a paste event - this is just passed through to intlTelInputUtils.formatNumber and used to check an extensions edge case that._handleInputKey(null, isCtrl && cursorAtEnd, isCtrl); @@ -665,7 +668,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git _updateFlagFromNumber: function(number) { // if we're in nationalMode and we're on US/Canada, make sure the number starts with a +1 so _getDialCode will be able to extract the area code // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag from the number), that means we're initialising the plugin with a number that already has a dial code, so fine to ignore this bit - if (this.options.nationalMode && this.selectedCountryData && this.selectedCountryData.dialCode == "1" && number.charAt(0) != "+") { + if (number && this.options.nationalMode && this.selectedCountryData && this.selectedCountryData.dialCode == "1" && number.charAt(0) != "+") { if (number.charAt(0) != "1") { number = "1" + number; } @@ -686,17 +689,12 @@ https://github.com/Bluefieldscom/intl-tel-input.git } } } - } else if (number.charAt(0) == "+") { - // no valid dial code, but only empty it if they've actually typed an invalid one, not just a plus + } else if (number.charAt(0) == "+" && this._getNumeric(number).length) { + // invalid dial code, so empty // Note: use getNumeric here because the number has not been formatted yet, so could contain bad shit - if (this._getNumeric(number).length) { - countryCode = ""; - } else if (!this.selectedCountryData.iso2) { - // if just a plus and there's no currently selected country, revert to default - countryCode = this.options.defaultCountry.iso2; - } - } else if (!this.selectedCountryData.iso2 && !number) { - // if no selected country and no number, revert to default + countryCode = ""; + } else if (!number || number == "+") { + // empty, or just a plus, so default countryCode = this.options.defaultCountry.iso2; } if (countryCode !== null) { @@ -728,9 +726,16 @@ https://github.com/Bluefieldscom/intl-tel-input.git } }, // select the given flag, update the placeholder and the active list item - _selectFlag: function(countryCode) { + _selectFlag: function(countryCode, updateDefault) { // do this first as it will throw an error and stop if countryCode is invalid this.selectedCountryData = countryCode ? this._getCountryData(countryCode, false, false) : {}; + // update the "defaultCountry" - we only need the iso2 from now on, so just store that + if (updateDefault && this.selectedCountryData.iso2) { + // can't just make this equal to selectedCountryData as would be a ref to that object + this.options.defaultCountry = { + iso2: this.selectedCountryData.iso2 + }; + } this.selectedFlagInner.attr("class", "iti-flag " + countryCode); // update the selected country's title attribute var title = countryCode ? this.selectedCountryData.name + ": +" + this.selectedCountryData.dialCode : "Unknown"; @@ -754,7 +759,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git _selectListItem: function(listItem) { // update selected flag and active list item var countryCode = listItem.attr("data-country-code"); - this._selectFlag(countryCode); + this._selectFlag(countryCode, true); this._closeDropdown(); this._updateDialCode(listItem.attr("data-dial-code"), true); // always fire the change event as even if nationalMode=true (and we haven't updated the input val), the system as a whole has still changed - see country-sync example. think of it as making a selection from a select element. @@ -922,7 +927,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git countryCode = countryCode.toLowerCase(); // check if already selected if (!this.selectedFlagInner.hasClass(countryCode)) { - this._selectFlag(countryCode); + this._selectFlag(countryCode, true); this._updateDialCode(this.selectedCountryData.dialCode, false); } }, diff --git a/build/js/intlTelInput.min.js b/build/js/intlTelInput.min.js index 992efe07e..57a8145c2 100644 --- a/build/js/intlTelInput.min.js +++ b/build/js/intlTelInput.min.js @@ -2,4 +2,4 @@ International Telephone Input v5.1.7 https://github.com/Bluefieldscom/intl-tel-input.git */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],function(b){a(b,window,document)}):a(jQuery,window,document)}(function(a,b,c,d){"use strict";function e(b,c){this.a=b,c&&($.extend(c, c, {a:c.autoFormat,h:c.autoHideDialCode,d:c.defaultCountry,i:c.ipinfoToken,n:c.nationalMode,t:c.numberType,o:c.onlyCountries,p:c.preferredCountries,v:c.preventInvalidNumbers,u:c.utilsScript})),this.b=a.extend({},h,c),this.c=h,this.ns="."+f+g++,this.d=Boolean(b.setSelectionRange),this.e=Boolean(a(b).attr("placeholder")),this.f=f,this.init()}var f="intlTelInput",g=1,h={allowExtensions:!1,a:!0,autoPlaceholder:!0,h:!0,d:"",i:"",n:!0,t:"MOBILE",o:[],p:["us","gb"],u:""},i={b:38,c:40,d:13,e:27,f:43,A:65,Z:90,g:48,h:57,i:32,Bi:8,k:46,l:17,m:91,n:224},j=!1;a(b).load(function(){j=!0}),e.prototype={init:function(){var b=this;if("auto"==this.b.d){this.b.d="";var c="//ipinfo.io";this.b.i&&(c+="?token="+this.b.i),a.get(c,function(a){a&&a.country&&(b.b.d=a.country.toLowerCase())},"jsonp").always(function(){b._a()})}else this._a()},_a:function(){this.b.n&&(this.b.h=!1),navigator.userAgent.match(/IEMobile/i)&&(this.b.a=!1),this._b(),this._f(),this._h(),this._i()},_b:function(){this._d(),this._e()},_c:function(a,b,c){b in this.m||(this.m[b]=[]);var d=c||0;this.m[b][d]=a},_d:function(){var b;if(this.b.o.length){for(b=0;b",{"class":"intl-tel-input"}));var c=a("
",{"class":"flag-dropdown"}).insertAfter(this.g),d=a("
",{"class":"selected-flag"}).appendTo(c);this.h=a("
",{"class":"iti-flag"}).appendTo(d),a("
",{"class":"arrow"}).appendTo(this.h),this.i=a("
    ",{"class":"country-list v-hide"}).appendTo(c),this.n.length&&(this._g(this.n,"preferred"),a("
  • ",{"class":"divider"}).appendTo(this.i)),this._g(this.l,""),this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),b.innerWidth<500&&this.i.outerWidth(this.g.outerWidth()),this.k=this.i.children(".country")},_g:function(a,b){for(var c="",d=0;d",c+="
    ",c+=""+e.name+"",c+="+"+e.dialCode+"",c+="
  • "}this.i.append(c)},_h:function(){var a=this.g.val();this._af(a)?this._v(a):(this.b.d=this.b.d?this._y(this.b.d.toLowerCase(),!1,!1):this.n.length?this.n[0]:this.l[0],this._z(this.b.d.iso2),a||this._ae(this.b.d.dialCode,!1)),a&&this._u(a,!1)},_i:function(){var c=this;this._j(),(this.b.h||this.b.a)&&this._l();var d=this.g.closest("label");d.length&&d.on("click"+this.ns,function(a){c.i.hasClass("hide")?c.g.focus():a.preventDefault()});var e=this.h.parent();e.on("click"+this.ns,function(){!c.i.hasClass("hide")||c.g.prop("disabled")||c.g.prop("readonly")||c._n()}),this.b.u&&(j?this.loadUtils():a(b).load(function(){c.loadUtils()}))},_j:function(){var a=this;this.b.a&&this.g.on("keypress"+this.ns,function(c){if(c.which>=i.i&&!c.ctrlKey&&!c.metaKey&&b.intlTelInputUtils&&!a.g.prop("readonly")){c.preventDefault();var d=c.which>=i.g&&c.which<=i.h||c.which==i.f,e=a.g[0],f=a.d&&e.selectionStart==e.selectionEnd,g=a.g.attr("maxlength"),h=a.g.val(),j=g?h.length0;e--){var f=b.charAt(e-1);if(a.isNumeric(f)||b.substr(e-2,2)==d)return e}return 0},_getCursorFromDigitsOnRight:function(b,c){for(var d=b.length-1;d>=0;d--)if(a.isNumeric(b.charAt(d))&&0===--c)return d;return 0},_getDigitsOnRight:function(b,c){for(var d=0,e=c;ed,g=!e&&f?"-"+(this.j-1)+"px":"";this.i.css("top",g)},_p:function(){var b=this;this.i.on("mouseover"+this.ns,".country",function(){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(){d||b._ac(),d=!1});var e="",f=null;a(c).on("keydown"+this.ns,function(a){a.preventDefault(),a.which==i.b||a.which==i.c?b._q(a.which):a.which==i.d?b._r():a.which==i.e?b._ac():(a.which>=i.A&&a.which<=i.Z||a.which==i.i)&&(f&&clearTimeout(f),e+=String.fromCharCode(a.which),b._s(e),f=setTimeout(function(){e=""},1e3))})},_q:function(a){var b=this.i.children(".highlight").first(),c=a==i.b?b.prev():b.next();c.length&&(c.hasClass("divider")&&(c=a==i.b?c.prev():c.next()),this._x(c),this._ad(c))},_r:function(){var a=this.i.children(".highlight").first();a.length&&this._ab(a)},_s:function(a){for(var b=0;bg&&(f=f.substr(0,g))}else f=a;this.g.val(f)},_v:function(b){this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var c=this._af(b),d=null;if(c){var e=this.m[this._m(c)],f=this.o&&-1!=a.inArray(this.o.iso2,e);if(!f||this._w(b,c))for(var g=0;g=4},_x:function(a){this.k.removeClass("highlight"),a.addClass("highlight")},_y:function(a,b,c){for(var d=b?k:this.l,e=0;eh)b&&(j-=k),c.scrollTop(j);else if(i>f){b&&(j+=k);var l=d-g;c.scrollTop(j-l)}},_ae:function(b,c){var d,e=this.g.val();if(b="+"+b,this.b.n&&"+"!=e.charAt(0))d=e;else if(e){var f=this._af(e);if(f.length>1)d=e.replace(f,b);else{var g="+"!=e.charAt(0)?a.trim(e):"";d=b+g}}else d=!this.b.h||c?b:"";this._u(d,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e",{"class":"intl-tel-input"}));var c=a("
    ",{"class":"flag-dropdown"}).insertAfter(this.g),d=a("
    ",{"class":"selected-flag"}).appendTo(c);this.h=a("
    ",{"class":"iti-flag"}).appendTo(d),a("
    ",{"class":"arrow"}).appendTo(this.h),this.i=a("
      ",{"class":"country-list v-hide"}).appendTo(c),this.n.length&&(this._g(this.n,"preferred"),a("
    • ",{"class":"divider"}).appendTo(this.i)),this._g(this.l,""),this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),b.innerWidth<500&&this.i.outerWidth(this.g.outerWidth()),this.k=this.i.children(".country")},_g:function(a,b){for(var c="",d=0;d",c+="
      ",c+=""+e.name+"",c+="+"+e.dialCode+"",c+="
    • "}this.i.append(c)},_h:function(){var a=this.g.val();this._af(a)?this._v(a):(this.b.d=this.b.d?this._y(this.b.d.toLowerCase(),!1,!1):this.n.length?this.n[0]:this.l[0],this._z(this.b.d.iso2),a||this._ae(this.b.d.dialCode,!1)),a&&this._u(a,!1)},_i:function(){var c=this;this._j(),(this.b.h||this.b.a)&&this._l();var d=this.g.closest("label");d.length&&d.on("click"+this.ns,function(a){c.i.hasClass("hide")?c.g.focus():a.preventDefault()});var e=this.h.parent();e.on("click"+this.ns,function(){!c.i.hasClass("hide")||c.g.prop("disabled")||c.g.prop("readonly")||c._n()}),this.b.u&&(j?this.loadUtils():a(b).load(function(){c.loadUtils()}))},_j:function(){var a=this;this.b.a&&this.g.on("keypress"+this.ns,function(c){if(c.which>=i.i&&!c.ctrlKey&&!c.metaKey&&b.intlTelInputUtils&&!a.g.prop("readonly")){c.preventDefault();var d=c.which>=i.g&&c.which<=i.h||c.which==i.f,e=a.g[0],f=a.d&&e.selectionStart==e.selectionEnd,g=a.g.attr("maxlength"),h=a.g.val(),j=g?h.length0;e--){var f=b.charAt(e-1);if(a.isNumeric(f)||b.substr(e-2,2)==d)return e}return 0},_getCursorFromDigitsOnRight:function(b,c){for(var d=b.length-1;d>=0;d--)if(a.isNumeric(b.charAt(d))&&0===--c)return d;return 0},_getDigitsOnRight:function(b,c){for(var d=0,e=c;ed,g=!e&&f?"-"+(this.j-1)+"px":"";this.i.css("top",g)},_p:function(){var b=this;this.i.on("mouseover"+this.ns,".country",function(){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(){d||b._ac(),d=!1});var e="",f=null;a(c).on("keydown"+this.ns,function(a){a.preventDefault(),a.which==i.b||a.which==i.c?b._q(a.which):a.which==i.d?b._r():a.which==i.e?b._ac():(a.which>=i.A&&a.which<=i.Z||a.which==i.i)&&(f&&clearTimeout(f),e+=String.fromCharCode(a.which),b._s(e),f=setTimeout(function(){e=""},1e3))})},_q:function(a){var b=this.i.children(".highlight").first(),c=a==i.b?b.prev():b.next();c.length&&(c.hasClass("divider")&&(c=a==i.b?c.prev():c.next()),this._x(c),this._ad(c))},_r:function(){var a=this.i.children(".highlight").first();a.length&&this._ab(a)},_s:function(a){for(var b=0;bg&&(f=f.substr(0,g))}else f=a;this.g.val(f)},_v:function(b){b&&this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var c=this._af(b),d=null;if(c){var e=this.m[this._m(c)],f=this.o&&-1!=a.inArray(this.o.iso2,e);if(!f||this._w(b,c))for(var g=0;g=4},_x:function(a){this.k.removeClass("highlight"),a.addClass("highlight")},_y:function(a,b,c){for(var d=b?k:this.l,e=0;eh)b&&(j-=k),c.scrollTop(j);else if(i>f){b&&(j+=k);var l=d-g;c.scrollTop(j-l)}},_ae:function(b,c){var d,e=this.g.val();if(b="+"+b,this.b.n&&"+"!=e.charAt(0))d=e;else if(e){var f=this._af(e);if(f.length>1)d=e.replace(f,b);else{var g="+"!=e.charAt(0)?a.trim(e):"";d=b+g}}else d=!this.b.h||c?b:"";this._u(d,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e