From a98d50f02299e00310c2a1c8e5607f01636c19de Mon Sep 17 00:00:00 2001 From: Timothy Ruhle Date: Mon, 4 May 2015 11:09:48 +1000 Subject: [PATCH 01/11] adds support to multiple geoip data providers --- README.md | 19 +++++++++-- build/js/intlTelInput.js | 57 ++++++++++++++++++++++---------- build/js/intlTelInput.min.js | 2 +- src/js/intlTelInput.js | 63 +++++++++++++++++++++++++----------- 4 files changed, 102 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 6ae57eae6..2cc078d4a 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,22 @@ Add or remove input placeholder with an example number for the selected country. Type: `String` Default: `""` Set the default country by it's country code. You can also set it to `"auto"`, which will lookup the user's country based on their IP address - [see example](http://jackocnr.com/lib/intl-tel-input/examples/gen/default-country-ip.html). Otherwise it will just be the first country in the list. _Note that if you choose to do the auto lookup, and you also happen to use the [jquery-cookie](https://github.com/carhartl/jquery-cookie) plugin, it will store the loaded country code in a cookie for future use._ -**ipinfoToken** -Type: `String` Default: `""` -When setting `defaultCountry` to `"auto"`, we use a service called [ipinfo](http://ipinfo.io) which requires a special token to be used over https, or if you make >1000 requests/day. Use this option to pass in that token. +**geoipProvider** +Type: `String` Default: `"freegeoip"` +When setting `defaultCountry` to `"auto"`, we use a special service to provide us the location data of the given user. Currently we support three different third party apis: "[freegeoip](https://freegeoip.net/)", "[telize](https://telize.com/)" and "[ipinfo](https://ipinfo.io/)". + +You can also write a custom method to get the country code. For example +```javascript +geoipProvider: function(callback) { + $.get('http://ipinfo.io/json?token={token}').always(function(resp) { + callback(resp && resp['country'] || ''); + }); +} +``` + +**geoipToken** +Type: `String` Default: `""` +When setting `defaultCountry` to `"auto"`, the geoIP data provider may require you an auth token to enable special features, like calls over https or extra limits on your requests quota. Use this option to pass in that token. **nationalMode** Type: `Boolean` Default: `true` diff --git a/build/js/intlTelInput.js b/build/js/intlTelInput.js index 92936b14b..87d55ac16 100644 --- a/build/js/intlTelInput.js +++ b/build/js/intlTelInput.js @@ -26,8 +26,10 @@ https://github.com/Bluefieldscom/intl-tel-input.git autoHideDialCode: true, // default country defaultCountry: "", - // token for ipinfo - required for https or over 1000 daily page views support - ipinfoToken: "", + // geoIP provider name + geoipProvider: "freegeoip", + // token for the geoIP provider + geoipToken: "", // don't insert international dial codes nationalMode: true, // number type to use for placeholders @@ -358,23 +360,44 @@ https://github.com/Bluefieldscom/intl-tel-input.git } else if (!$.fn[pluginName].startedLoadingAutoCountry) { // don't do this twice! $.fn[pluginName].startedLoadingAutoCountry = true; - var ipinfoURL = "//ipinfo.io"; - if (this.options.ipinfoToken) { - ipinfoURL += "?token=" + this.options.ipinfoToken; - } - // dont bother with the success function arg - instead use always() as should still set a defaultCountry even if the lookup fails - $.get(ipinfoURL, function() {}, "jsonp").always(function(resp) { - $.fn[pluginName].autoCountry = resp && resp.country ? resp.country.toLowerCase() : ""; - if ($.cookie) { - $.cookie("itiAutoCountry", $.fn[pluginName].autoCountry, { - path: "/" - }); + var providers = { + ipinfo: { + url: "//ipinfo.io/json?token={token}", + field: "country" + }, + telize: { + url: "//www.telize.com/geoip", + field: "country_code" + }, + freegeoip: { + url: "//freegeoip.net/json/", + field: "country_code" } - // tell all instances the auto country is ready - // TODO: this should just be the current instances - $(".intl-tel-input input").intlTelInput("autoCountryLoaded"); + }; + if (typeof this.options.geoipProvider === "function") { + this.options.geoipProvider(function(resp) { + this._autoCountryLoaded(resp); + }); + } else { + var provider = providers[this.options.geoipProvider]; + var url = provider.url.replace("{token}", this.options.geoipToken || ""); + // dont bother with the success function arg - instead use always() as should still set a defaultCountry even if the lookup fails + $.get(url).always(function(resp) { + this._autoCountryLoaded(resp && resp[provider.field] || ""); + }); + } + } + }, + _autoCountryLoaded: function(country_code) { + $.fn[pluginName].autoCountry = country_code.toLowerCase(); + if ($.cookie) { + $.cookie("itiAutoCountry", $.fn[pluginName].autoCountry, { + path: "/" }); } + // tell all instances the auto country is ready + // TODO: this should just be the current instances + $(".intl-tel-input input").intlTelInput("autoCountryLoaded"); }, _initKeyListeners: function() { var that = this; @@ -955,7 +978,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git /******************** * PUBLIC METHODS ********************/ - // this is called when the ipinfo call returns + // this is called when the geoip call returns autoCountryLoaded: function() { if (this.options.defaultCountry == "auto") { this.options.defaultCountry = $.fn[pluginName].autoCountry; diff --git a/build/js/intlTelInput.min.js b/build/js/intlTelInput.min.js index 1e3350e71..cbbe80440 100644 --- a/build/js/intlTelInput.min.js +++ b/build/js/intlTelInput.min.js @@ -2,4 +2,4 @@ International Telephone Input v5.8.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&&(a.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}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,TAB:9,k:46,l:17,m:91,n:224},j=!1;a(b).load(function(){j=!0}),e.prototype={_init:function(){return this.b.n&&(this.b.h=!1),navigator.userAgent.match(/IEMobile/i)&&(this.b.a=!1),this.isMobile=/Android.+Mobile|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent),this.autoCountryDeferred=new a.Deferred,this.utilsScriptDeferred=new a.Deferred,this._b(),this._f(),this._h(),this._i(),this._initRequests(),[this.autoCountryDeferred,this.utilsScriptDeferred]},_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"})),this.flagsContainer=a("
",{"class":"flag-dropdown"}).insertBefore(this.g);var b=a("
",{tabindex:"0","class":"selected-flag"}).appendTo(this.flagsContainer);this.h=a("
",{"class":"iti-flag"}).appendTo(b),a("
",{"class":"arrow"}).appendTo(b),this.isMobile?this.i=a("").appendTo(this.flagsContainer):(this.i=a("
    ",{"class":"country-list v-hide"}).appendTo(this.flagsContainer),this.n.length&&!this.isMobile&&(this._g(this.n,"preferred"),a("
  • ",{"class":"divider"}).appendTo(this.i))),this._g(this.l,""),this.isMobile||(this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),this.k=this.i.children(".country"))},_g:function(a,b){for(var c="",d=0;d",c+=e.name+" +"+e.dialCode,c+=""):(c+="
  • ",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,!0):"auto"!=this.b.d&&(this.b.d?this.b.d=this._y(this.b.d.toLowerCase(),!1,!1):this.b.d=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)},_i:function(){var b=this;if(this._j(),(this.b.h||this.b.a)&&this._l(),this.isMobile)this.i.on("change"+this.ns,function(c){b._ab(a(this).find("option:selected"))});else{var c=this.g.closest("label");c.length&&c.on("click"+this.ns,function(a){b.i.hasClass("hide")?b.g.focus():a.preventDefault()});var d=this.h.parent();d.on("click"+this.ns,function(a){!b.i.hasClass("hide")||b.g.prop("disabled")||b.g.prop("readonly")||b._n()})}this.flagsContainer.on("keydown"+b.ns,function(a){var c=b.i.hasClass("hide");!c||a.which!=i.b&&a.which!=i.c&&a.which!=i.i&&a.which!=i.d||(a.preventDefault(),a.stopPropagation(),b._n()),a.which==i.TAB&&b._ac()})},_initRequests:function(){var c=this;this.b.u?j?this.loadUtils():a(b).load(function(){c.loadUtils()}):this.utilsScriptDeferred.resolve(),"auto"==this.b.d?this._loadAutoCountry():this.autoCountryDeferred.resolve()},_loadAutoCountry:function(){var b=a.cookie?a.cookie("itiAutoCountry"):"";if(b&&(a.fn[f].autoCountry=b),a.fn[f].autoCountry)this.autoCountryLoaded();else if(!a.fn[f].startedLoadingAutoCountry){a.fn[f].startedLoadingAutoCountry=!0;var c={ipinfo:{url:"//ipinfo.io/json?token={token}",field:"country"},telize:{url:"//www.telize.com/geoip",field:"country_code"},freegeoip:{url:"//freegeoip.net/json/",field:"country_code"}};if("function"==typeof this.b.geoipProvider)this.b.geoipProvider(function(a){this._autoCountryLoaded(a)});else{var d=c[this.b.geoipProvider],e=d.url.replace("{token}",this.b.geoipToken||"");a.get(e).always(function(a){this._autoCountryLoaded(a&&a[d.field]||"")})}}},_autoCountryLoaded:function(b){a.fn[f].autoCountry=b.toLowerCase(),a.cookie&&a.cookie("itiAutoCountry",a.fn[f].autoCountry,{path:"/"}),a(".intl-tel-input input").intlTelInput("autoCountryLoaded")},_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(c){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(c){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(a){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;bh&&(g=g.substr(0,h))}else g=a;this.g.val(g)},_v:function(b,c){b&&this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var d=this._af(b),e=null;if(d){var f=this.m[this._m(d)],g=this.o&&-1!=a.inArray(this.o.iso2,f);if(!g||this._w(b,d))for(var h=0;h=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,null,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e Date: Mon, 4 May 2015 12:05:10 +1000 Subject: [PATCH 02/11] Fixing pointing to this inside a wrong scope --- build/js/intlTelInput.js | 10 ++++++---- build/js/intlTelInput.min.js | 2 +- src/js/intlTelInput.js | 11 +++++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/build/js/intlTelInput.js b/build/js/intlTelInput.js index 87d55ac16..2f03054eb 100644 --- a/build/js/intlTelInput.js +++ b/build/js/intlTelInput.js @@ -373,17 +373,17 @@ https://github.com/Bluefieldscom/intl-tel-input.git url: "//freegeoip.net/json/", field: "country_code" } - }; + }, self = this; if (typeof this.options.geoipProvider === "function") { this.options.geoipProvider(function(resp) { - this._autoCountryLoaded(resp); + self._autoCountryLoaded(resp); }); } else { var provider = providers[this.options.geoipProvider]; var url = provider.url.replace("{token}", this.options.geoipToken || ""); // dont bother with the success function arg - instead use always() as should still set a defaultCountry even if the lookup fails $.get(url).always(function(resp) { - this._autoCountryLoaded(resp && resp[provider.field] || ""); + self._autoCountryLoaded(resp && resp[provider.field] || ""); }); } } @@ -397,7 +397,9 @@ https://github.com/Bluefieldscom/intl-tel-input.git } // tell all instances the auto country is ready // TODO: this should just be the current instances - $(".intl-tel-input input").intlTelInput("autoCountryLoaded"); + // $(".intl-tel-input input").intlTelInput("autoCountryLoaded"); + // Why not just call it directly?? + this.autoCountryLoaded(); }, _initKeyListeners: function() { var that = this; diff --git a/build/js/intlTelInput.min.js b/build/js/intlTelInput.min.js index cbbe80440..10b73437d 100644 --- a/build/js/intlTelInput.min.js +++ b/build/js/intlTelInput.min.js @@ -2,4 +2,4 @@ International Telephone Input v5.8.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&&(a.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}var f="intlTelInput",g=1,h={allowExtensions:!1,a:!0,autoPlaceholder:!0,h:!0,d:"",geoipProvider:"freegeoip",geoipToken:"",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,TAB:9,k:46,l:17,m:91,n:224},j=!1;a(b).load(function(){j=!0}),e.prototype={_init:function(){return this.b.n&&(this.b.h=!1),navigator.userAgent.match(/IEMobile/i)&&(this.b.a=!1),this.isMobile=/Android.+Mobile|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent),this.autoCountryDeferred=new a.Deferred,this.utilsScriptDeferred=new a.Deferred,this._b(),this._f(),this._h(),this._i(),this._initRequests(),[this.autoCountryDeferred,this.utilsScriptDeferred]},_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"})),this.flagsContainer=a("
    ",{"class":"flag-dropdown"}).insertBefore(this.g);var b=a("
    ",{tabindex:"0","class":"selected-flag"}).appendTo(this.flagsContainer);this.h=a("
    ",{"class":"iti-flag"}).appendTo(b),a("
    ",{"class":"arrow"}).appendTo(b),this.isMobile?this.i=a("").appendTo(this.flagsContainer):(this.i=a("
      ",{"class":"country-list v-hide"}).appendTo(this.flagsContainer),this.n.length&&!this.isMobile&&(this._g(this.n,"preferred"),a("
    • ",{"class":"divider"}).appendTo(this.i))),this._g(this.l,""),this.isMobile||(this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),this.k=this.i.children(".country"))},_g:function(a,b){for(var c="",d=0;d",c+=e.name+" +"+e.dialCode,c+=""):(c+="
    • ",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,!0):"auto"!=this.b.d&&(this.b.d?this.b.d=this._y(this.b.d.toLowerCase(),!1,!1):this.b.d=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)},_i:function(){var b=this;if(this._j(),(this.b.h||this.b.a)&&this._l(),this.isMobile)this.i.on("change"+this.ns,function(c){b._ab(a(this).find("option:selected"))});else{var c=this.g.closest("label");c.length&&c.on("click"+this.ns,function(a){b.i.hasClass("hide")?b.g.focus():a.preventDefault()});var d=this.h.parent();d.on("click"+this.ns,function(a){!b.i.hasClass("hide")||b.g.prop("disabled")||b.g.prop("readonly")||b._n()})}this.flagsContainer.on("keydown"+b.ns,function(a){var c=b.i.hasClass("hide");!c||a.which!=i.b&&a.which!=i.c&&a.which!=i.i&&a.which!=i.d||(a.preventDefault(),a.stopPropagation(),b._n()),a.which==i.TAB&&b._ac()})},_initRequests:function(){var c=this;this.b.u?j?this.loadUtils():a(b).load(function(){c.loadUtils()}):this.utilsScriptDeferred.resolve(),"auto"==this.b.d?this._loadAutoCountry():this.autoCountryDeferred.resolve()},_loadAutoCountry:function(){var b=a.cookie?a.cookie("itiAutoCountry"):"";if(b&&(a.fn[f].autoCountry=b),a.fn[f].autoCountry)this.autoCountryLoaded();else if(!a.fn[f].startedLoadingAutoCountry){a.fn[f].startedLoadingAutoCountry=!0;var c={ipinfo:{url:"//ipinfo.io/json?token={token}",field:"country"},telize:{url:"//www.telize.com/geoip",field:"country_code"},freegeoip:{url:"//freegeoip.net/json/",field:"country_code"}},d=this;if("function"==typeof this.b.geoipProvider)this.b.geoipProvider(function(a){d._autoCountryLoaded(a)});else{var e=c[this.b.geoipProvider],g=e.url.replace("{token}",this.b.geoipToken||"");a.get(g).always(function(a){d._autoCountryLoaded(a&&a[e.field]||"")})}}},_autoCountryLoaded:function(b){a.fn[f].autoCountry=b.toLowerCase(),a.cookie&&a.cookie("itiAutoCountry",a.fn[f].autoCountry,{path:"/"}),this.autoCountryLoaded()},_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(c){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(c){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(a){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;bh&&(g=g.substr(0,h))}else g=a;this.g.val(g)},_v:function(b,c){b&&this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var d=this._af(b),e=null;if(d){var f=this.m[this._m(d)],g=this.o&&-1!=a.inArray(this.o.iso2,f);if(!g||this._w(b,d))for(var h=0;h=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,null,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e Date: Mon, 4 May 2015 15:03:36 -0700 Subject: [PATCH 03/11] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6ae57eae6..4290f1160 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ A jQuery plugin for entering and validating international telephone numbers. It +If you like it, please upvote on [Product Hunt](http://www.producthunt.com/posts/intl-tel-input)! + ## Table of Contents - [Demo and Examples](#demo-and-examples) From 2c3283a760f9297b2baf4b0b96fd78433b3f4e2a Mon Sep 17 00:00:00 2001 From: Myles McNamara Date: Wed, 6 May 2015 16:58:00 -0400 Subject: [PATCH 04/11] add intl-tel-mobile-select Add `intl-tel-mobile-select` class to select element for mobile devices --- src/js/intlTelInput.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/js/intlTelInput.js b/src/js/intlTelInput.js index 373d5d3dd..cb7be9ad7 100644 --- a/src/js/intlTelInput.js +++ b/src/js/intlTelInput.js @@ -222,7 +222,9 @@ Plugin.prototype = { // mobile is just a native select element // desktop is a proper list containing: preferred countries, then divider, then all countries if (this.isMobile) { - this.countryList = $("", { + "class": "intl-tel-mobile-select" + }).appendTo(this.flagsContainer); } else { this.countryList = $("
        ", { "class": "country-list v-hide" @@ -1363,4 +1365,4 @@ $.fn[pluginName] = function(options) { // get the country data object $.fn[pluginName].getCountryData = function() { return allCountries; -}; \ No newline at end of file +}; From fc181c191d3ee544820b960c4744c00fcf1efb87 Mon Sep 17 00:00:00 2001 From: Myles McNamara Date: Wed, 13 May 2015 15:19:08 -0400 Subject: [PATCH 05/11] update mobile select class name --- src/js/intlTelInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/intlTelInput.js b/src/js/intlTelInput.js index cb7be9ad7..0a22cf77b 100644 --- a/src/js/intlTelInput.js +++ b/src/js/intlTelInput.js @@ -223,7 +223,7 @@ Plugin.prototype = { // desktop is a proper list containing: preferred countries, then divider, then all countries if (this.isMobile) { this.countryList = $("").appendTo(this.flagsContainer):(this.i=a("
          ",{"class":"country-list v-hide"}).appendTo(this.flagsContainer),this.n.length&&!this.isMobile&&(this._g(this.n,"preferred"),a("
        • ",{"class":"divider"}).appendTo(this.i))),this._g(this.l,""),this.isMobile||(this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),this.k=this.i.children(".country"))},_g:function(a,b){for(var c="",d=0;d",c+=e.name+" +"+e.dialCode,c+=""):(c+="
        • ",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,!0):"auto"!=this.b.d&&(this.b.d?this.b.d=this._y(this.b.d.toLowerCase(),!1,!1):this.b.d=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)},_i:function(){var b=this;if(this._j(),(this.b.h||this.b.a)&&this._l(),this.isMobile)this.i.on("change"+this.ns,function(c){b._ab(a(this).find("option:selected"))});else{var c=this.g.closest("label");c.length&&c.on("click"+this.ns,function(a){b.i.hasClass("hide")?b.g.focus():a.preventDefault()});var d=this.h.parent();d.on("click"+this.ns,function(a){!b.i.hasClass("hide")||b.g.prop("disabled")||b.g.prop("readonly")||b._n()})}this.flagsContainer.on("keydown"+b.ns,function(a){var c=b.i.hasClass("hide");!c||a.which!=i.b&&a.which!=i.c&&a.which!=i.i&&a.which!=i.d||(a.preventDefault(),a.stopPropagation(),b._n()),a.which==i.TAB&&b._ac()})},_initRequests:function(){var c=this;this.b.u?j?this.loadUtils():a(b).load(function(){c.loadUtils()}):this.utilsScriptDeferred.resolve(),"auto"==this.b.d?this._loadAutoCountry():this.autoCountryDeferred.resolve()},_loadAutoCountry:function(){var b=a.cookie?a.cookie("itiAutoCountry"):"";if(b&&(a.fn[f].autoCountry=b),a.fn[f].autoCountry)this.autoCountryLoaded();else if(!a.fn[f].startedLoadingAutoCountry){a.fn[f].startedLoadingAutoCountry=!0;var c={ipinfo:{url:"//ipinfo.io/json?token={token}",field:"country"},telize:{url:"//www.telize.com/geoip",field:"country_code"},freegeoip:{url:"//freegeoip.net/json/",field:"country_code"}},d=this;if("function"==typeof this.b.geoipProvider)this.b.geoipProvider(function(a){d._autoCountryLoaded(a)});else{var e=c[this.b.geoipProvider],g=e.url.replace("{token}",this.b.geoipToken||"");a.get(g).always(function(a){d._autoCountryLoaded(a&&a[e.field]||"")})}}},_autoCountryLoaded:function(b){a.fn[f].autoCountry=b.toLowerCase(),a.cookie&&a.cookie("itiAutoCountry",a.fn[f].autoCountry,{path:"/"}),this.autoCountryLoaded()},_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(c){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(c){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(a){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;bh&&(g=g.substr(0,h))}else g=a;this.g.val(g)},_v:function(b,c){b&&this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var d=this._af(b),e=null;if(d){var f=this.m[this._m(d)],g=this.o&&-1!=a.inArray(this.o.iso2,f);if(!g||this._w(b,d))for(var h=0;h=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,null,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e",{"class":"intl-tel-input"})),this.flagsContainer=a("
          ",{"class":"flag-dropdown"}).insertBefore(this.g);var b=a("
          ",{tabindex:"0","class":"selected-flag"}).appendTo(this.flagsContainer);this.h=a("
          ",{"class":"iti-flag"}).appendTo(b),a("
          ",{"class":"arrow"}).appendTo(b),this.isMobile?this.i=a("").appendTo(this.flagsContainer):(this.i=a("
            ",{"class":"country-list v-hide"}).appendTo(this.flagsContainer),this.n.length&&!this.isMobile&&(this._g(this.n,"preferred"),a("
          • ",{"class":"divider"}).appendTo(this.i))),this._g(this.l,""),this.isMobile||(this.j=this.i.outerHeight(),this.i.removeClass("v-hide").addClass("hide"),this.k=this.i.children(".country"))},_g:function(a,b){for(var c="",d=0;d",c+=e.name+" +"+e.dialCode,c+=""):(c+="
          • ",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,!0):"auto"!=this.b.d&&(this.b.d?this.b.d=this._y(this.b.d.toLowerCase(),!1,!1):this.b.d=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)},_i:function(){var b=this;if(this._j(),(this.b.h||this.b.a)&&this._l(),this.isMobile)this.i.on("change"+this.ns,function(c){b._ab(a(this).find("option:selected"))});else{var c=this.g.closest("label");c.length&&c.on("click"+this.ns,function(a){b.i.hasClass("hide")?b.g.focus():a.preventDefault()});var d=this.h.parent();d.on("click"+this.ns,function(a){!b.i.hasClass("hide")||b.g.prop("disabled")||b.g.prop("readonly")||b._n()})}this.flagsContainer.on("keydown"+b.ns,function(a){var c=b.i.hasClass("hide");!c||a.which!=i.b&&a.which!=i.c&&a.which!=i.i&&a.which!=i.d||(a.preventDefault(),a.stopPropagation(),b._n()),a.which==i.TAB&&b._ac()})},_initRequests:function(){var c=this;this.b.u?j?this.loadUtils():a(b).load(function(){c.loadUtils()}):this.utilsScriptDeferred.resolve(),"auto"==this.b.d?this._loadAutoCountry():this.autoCountryDeferred.resolve()},_loadAutoCountry:function(){var b=a.cookie?a.cookie("itiAutoCountry"):"";if(b&&(a.fn[f].autoCountry=b),a.fn[f].autoCountry)this.autoCountryLoaded();else if(!a.fn[f].startedLoadingAutoCountry){a.fn[f].startedLoadingAutoCountry=!0;var c=this;"function"==typeof this.b.geoipProvider&&this.b.geoipProvider(function(a){c._autoCountryLoaded(a)})}},_autoCountryLoaded:function(b){a.fn[f].autoCountry=b.toLowerCase(),a.cookie&&a.cookie("itiAutoCountry",a.fn[f].autoCountry,{path:"/"}),this.autoCountryLoaded()},_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(c){b._x(a(this))}),this.i.on("click"+this.ns,".country",function(c){b._ab(a(this))});var d=!0;a("html").on("click"+this.ns,function(a){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;bh&&(g=g.substr(0,h))}else g=a;this.g.val(g)},_v:function(b,c){b&&this.b.n&&this.o&&"1"==this.o.dialCode&&"+"!=b.charAt(0)&&("1"!=b.charAt(0)&&(b="1"+b),b="+"+b);var d=this._af(b),e=null;if(d){var f=this.m[this._m(d)],g=this.o&&-1!=a.inArray(this.o.iso2,f);if(!g||this._w(b,d))for(var h=0;h=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,null,c)},_af:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e",{"class":"intl-tel-input"})),this.flagsContainer=a("
            ",{"class":"flag-dropdown"}).insertBefore(this.g);var b=a("
            ",{tabindex:"0","class":"selected-flag"}).appendTo(this.flagsContainer);this.h=a("
            ",{"class":"iti-flag"}).appendTo(b),a("
            ",{"class":"arrow"}).appendTo(b),this.isMobile?this.i=a("