1/* 2 * International Telephone Input v13.0.4 3 * https://github.com/jackocnr/intl-tel-input.git 4 * Licensed under the MIT license 5 */ 6 7// wrap in UMD - see https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js 8(function(factory) { 9 if (typeof define === "function" && define.amd) { 10 define([ "jquery" ], function($) { 11 factory($, window, document); 12 }); 13 } else if (typeof module === "object" && module.exports) { 14 module.exports = factory(require("jquery"), window, document); 15 } else { 16 factory(jQuery, window, document); 17 } 18})(function($, window, document, undefined) { 19 "use strict"; 20 // these vars persist through all instances of the plugin 21 var pluginName = "intlTelInput", id = 1, // give each instance it's own id for namespaced event handling 22 defaults = { 23 // whether or not to allow the dropdown 24 allowDropdown: true, 25 // if there is just a dial code in the input: remove it on blur, and re-add it on focus 26 autoHideDialCode: true, 27 // add a placeholder in the input with an example number for the selected country 28 autoPlaceholder: "polite", 29 // modify the auto placeholder 30 customPlaceholder: null, 31 // append menu to a specific element 32 dropdownContainer: "", 33 // don't display these countries 34 excludeCountries: [], 35 // format the input value during initialisation and on setNumber 36 formatOnDisplay: true, 37 // geoIp lookup function 38 geoIpLookup: null, 39 // inject a hidden input with this name, and on submit, populate it with the result of getNumber 40 hiddenInput: "", 41 // initial country 42 initialCountry: "", 43 // localized country names e.g. { 'de': 'Deutschland' } 44 localizedCountries: null, 45 // don't insert international dial codes 46 nationalMode: true, 47 // display only these countries 48 onlyCountries: [], 49 // number type to use for placeholders 50 placeholderNumberType: "MOBILE", 51 // the countries at the top of the list. defaults to united states and united kingdom 52 preferredCountries: [ "us", "gb" ], 53 // display the country dial code next to the selected flag so it's not part of the typed number 54 separateDialCode: false, 55 // specify the path to the libphonenumber script to enable validation/formatting 56 utilsScript: "" 57 }, keys = { 58 UP: 38, 59 DOWN: 40, 60 ENTER: 13, 61 ESC: 27, 62 PLUS: 43, 63 A: 65, 64 Z: 90, 65 SPACE: 32, 66 TAB: 9 67 }, // https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes 68 regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ]; 69 // keep track of if the window.load event has fired as impossible to check after the fact 70 $(window).on("load", function() { 71 // UPDATE: use a public static field so we can fudge it in the tests 72 $.fn[pluginName].windowLoaded = true; 73 }); 74 function Plugin(element, options) { 75 this.telInput = $(element); 76 this.options = $.extend({}, defaults, options); 77 // event namespace 78 this.ns = "." + pluginName + id++; 79 // Chrome, FF, Safari, IE9+ 80 this.isGoodBrowser = Boolean(element.setSelectionRange); 81 this.hadInitialPlaceholder = Boolean($(element).attr("placeholder")); 82 } 83 Plugin.prototype = { 84 _init: function() { 85 // if in nationalMode, disable options relating to dial codes 86 if (this.options.nationalMode) { 87 this.options.autoHideDialCode = false; 88 } 89 // if separateDialCode then doesn't make sense to A) insert dial code into input (autoHideDialCode), and B) display national numbers (because we're displaying the country dial code next to them) 90 if (this.options.separateDialCode) { 91 this.options.autoHideDialCode = this.options.nationalMode = false;
92 } 93 // we cannot just test screen size as some smartphones/website meta tags will report desktop resolutions 94 // Note: for some reason jasmine breaks if you put this in the main Plugin function with the rest of these declarations 95 // Note: to target Android Mobiles (and not Tablets), we must find "Android" and "Mobile" 96 this.isMobile = /Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); 97 if (this.isMobile) { 98 // trigger the mobile dropdown css 99 $("body").addClass("iti-mobile"); 100 // on mobile, we want a full screen dropdown, so we must append it to the body 101 if (!this.options.dropdownContainer) { 102 this.options.dropdownContainer = "body"; 103 } 104 } 105 // we return these deferred objects from the _init() call so they can be watched, and then we resolve them when each specific request returns 106 // Note: again, jasmine breaks when I put these in the Plugin function 107 this.autoCountryDeferred = new $.Deferred(); 108 this.utilsScriptDeferred = new $.Deferred(); 109 // in various situations there could be no country selected initially, but we need to be able to assume this variable exists 110 this.selectedCountryData = {}; 111 // process all the data: onlyCountries, excludeCountries, preferredCountries etc 112 this._processCountryData(); 113 // generate the markup 114 this._generateMarkup(); 115 // set the initial state of the input value and the selected flag 116 this._setInitialState(); 117 // start all of the event listeners: autoHideDialCode, input keydown, selectedFlag click 118 this._initListeners(); 119 // utils script, and auto country 120 this._initRequests(); 121 // return the deferreds 122 return [ this.autoCountryDeferred, this.utilsScriptDeferred ]; 123 }, 124 /******************** 125 * PRIVATE METHODS 126 ********************/ 127 // prepare all of the country data, including onlyCountries, excludeCountries and preferredCountries options 128 _processCountryData: function() { 129 // process onlyCountries or excludeCountries array if present 130 this._processAllCountries(); 131 // process the countryCodes map 132 this._processCountryCodes(); 133 // process the preferredCountries 134 this._processPreferredCountries(); 135 // translate countries according to localizedCountries option 136 if (this.options.localizedCountries) { 137 this._translateCountriesByLocale(); 138 } 139 // sort countries by name 140 if (this.options.onlyCountries.length || this.options.localizedCountries) { 141 this.countries.sort(this._countryNameSort); 142 } 143 }, 144 // add a country code to this.countryCodes 145 _addCountryCode: function(iso2, dialCode, priority) { 146 if (!(dialCode in this.countryCodes)) { 147 this.countryCodes[dialCode] = []; 148 } 149 var index = priority || 0; 150 this.countryCodes[dialCode][index] = iso2; 151 }, 152 // process onlyCountries or excludeCountries array if present 153 _processAllCountries: function() { 154 if (this.options.onlyCountries.length) { 155 var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) { 156 return country.toLowerCase(); 157 }); 158 this.countries = allCountries.filter(function(country) { 159 return lowerCaseOnlyCountries.indexOf(country.iso2) > -1; 160 }); 161 } else if (this.options.excludeCountries.length) { 162 var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) { 163 return country.toLowerCase(); 164 }); 165 this.countries = allCountries.filter(function(country) { 166 return lowerCaseExcludeCountries.indexOf(country.iso2) === -1; 167 }); 168 } else { 169 this.countries = allCountries; 170 } 171 }, 172 // Translate Countries by object literal provided on config 173 _translateCountriesByLocale: function() { 174 for (var i = 0; i < this.countries.length; i++) { 175 var iso = this.countries[i].iso2.toLowerCase(); 176 if (iso in this.options.localizedCountries) { 177 this.countries[i].name = this.options.localizedCountries[iso]; 178 } 179 } 180 }, 181 // sort by country name 182 _countryNameSort: function(a, b) { 183 return a.name.localeCompare(b.name); 184 }, 185 // process the countryCodes map 186 _processCountryCodes: function() { 187 this.countryCodes = {}; 188 for (var i = 0; i < this.countries.length; i++) { 189 var c = this.countries[i]; 190 this._addCountryCode(c.iso2, c.dialCode, c.priority); 191 // area codes 192 if (c.areaCodes) { 193 for (var j = 0; j < c.areaCodes.length; j++) { 194 // full dial code is country code + dial code 195 this._addCountryCode(c.iso2, c.dialCode + c.areaCodes[j]); 196 } 197 } 198 } 199 }, 200 // process preferred countries - iterate through the preferences, fetching the country data for each one 201 _processPreferredCountries: function() { 202 this.preferredCountries = []; 203 for (var i = 0; i < this.options.preferredCountries.length; i++) { 204 var countryCode = this.options.preferredCountries[i].toLowerCase(), countryData = this._getCountryData(countryCode, false, true); 205 if (countryData) { 206 this.preferredCountries.push(countryData); 207 } 208 } 209 }, 210 // generate all of the markup for the plugin: the selected flag overlay, and the dropdown 211 _generateMarkup: function() { 212 // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can easily put the plugin in an inconsistent state e.g. the wrong flag selected for the autocompleted number, which on submit c
212ould mean the wrong number is saved (esp in nationalMode) 213 this.telInput.attr("autocomplete", "off"); 214 // containers (mostly for positioning) 215 var parentClass = "intl-tel-input"; 216 if (this.options.allowDropdown) { 217 parentClass += " allow-dropdown"; 218 } 219 if (this.options.separateDialCode) { 220 parentClass += " separate-dial-code"; 221 } 222 this.telInput.wrap($("<div>", { 223 "class": parentClass 224 })); 225 this.flagsContainer = $("<div>", { 226 "class": "flag-container" 227 }).insertBefore(this.telInput); 228 // currently selected flag (displayed to left of input) 229 var selectedFlag = $("<div>", { 230 "class": "selected-flag", 231 role: "combobox", 232 "aria-owns": "country-listbox" 233 }); 234 selectedFlag.appendTo(this.flagsContainer); 235 this.selectedFlagInner = $("<div>", { 236 "class": "iti-flag" 237 }).appendTo(selectedFlag); 238 if (this.options.separateDialCode) { 239 this.selectedDialCode = $("<div>", { 240 "class": "selected-dial-code" 241 }).appendTo(selectedFlag); 242 } 243 if (this.options.allowDropdown) { 244 // make element focusable and tab naviagable 245 selectedFlag.attr("tabindex", "0"); 246 // CSS triangle 247 $("<div>", { 248 "class": "iti-arrow" 249 }).appendTo(selectedFlag); 250 // country dropdown: preferred countries, then divider, then all countries 251 this.countryList = $("<ul>", { 252 "class": "country-list hide", 253 id: "country-listbox", 254 "aria-expanded": "false", 255 role: "listbox" 256 }); 257 if (this.preferredCountries.length) { 258 this._appendListItems(this.preferredCountries, "preferred"); 259 $("<li>", { 260 "class": "divider", 261 role: "separator", 262 "aria-disabled": "true" 263 }).appendTo(this.countryList); 264 } 265 this._appendListItems(this.countries, ""); 266 // this is useful in lots of places 267 this.countryListItems = this.countryList.children(".country"); 268 // create dropdownContainer markup 269 if (this.options.dropdownContainer) { 270 this.dropdown = $("<div>", { 271 "class": "intl-tel-input iti-container" 272 }).append(this.countryList); 273 } else { 274 this.countryList.appendTo(this.flagsContainer); 275 } 276 } else { 277 // a little hack so we don't break anything 278 this.countryListItems = $(); 279 } 280 if (this.options.hiddenInput) { 281 var hiddenInputName = this.options.hiddenInput; 282 var name = this.telInput.attr("name"); 283 if (name) { 284 var i = name.lastIndexOf("["); 285 // if input name contains square brackets, then give the hidden input the same name, 286 // replacing the contents of the last set of brackets with the given hiddenInput name 287 if (i !== -1) hiddenInputName = name.substr(0, i) + "[" + hiddenInputName + "]"; 288 } 289 this.hiddenInput = $("<input>", { 290 type: "hidden", 291 name: hiddenInputName 292 }).insertAfter(this.telInput); 293 } 294 }, 295 // add a country <li> to the countryList <ul> container 296 _appendListItems: function(countries, className) { 297 // we create so many DOM elements, it is faster to build a temp string 298 // and then add everything to the DOM in one go at the end 299 var tmp = ""; 300 // for each country 301 for (var i = 0; i < countries.length; i++) { 302 var c = countries[i]; 303 // open the list item 304 tmp += "<li class='country " + className + "' id='iti-item-" + c.iso2 + "' role='option' data-dial-code='" + c.dialCode + "' data-country-code='" + c.iso2 + "'>"; 305 // add the flag 306 tmp += "<div class='flag-box'><div class='iti-flag " + c.iso2 + "'></div></div>"; 307 // and the country name and dial code 308 tmp += "<span class='country-name'>" + c.name + "</span>"; 309 tmp += "<span class='dial-code'>+" + c.dialCode + "</span>"; 310 // close the list item 311 tmp += "</li>"; 312 } 313 this.countryList.append(tmp); 314 }, 315 // set the initial state of the input value and the selected flag by: 316 // 1. extracting a dial code from the given number 317 // 2. using explicit initialCountry 318 // 3. picking the first preferred country 319 // 4. picking the first country 320 _setInitialState: function() { 321 var val = this.telInput.val(); 322 var dialCode = this._getDialCode(val); 323 var isRegionlessNanp = this._isRegionlessNanp(val); 324 // if we already have a dial code, and it's not a regi
324onlessNanp, we can go ahead and set the flag, else fall back to the default country 325 if (dialCode && !isRegionlessNanp) { 326 this._updateFlagFromNumber(val); 327 } else if (this.options.initialCountry !== "auto") { 328 // see if we should select a flag 329 if (this.options.initialCountry) { 330 this._setFlag(this.options.initialCountry.toLowerCase()); 331 } else { 332 if (dialCode && isRegionlessNanp) { 333 // has intl dial code, is regionless nanp, and no initialCountry, so default to US 334 this._setFlag("us"); 335 } else { 336 // no dial code and no initialCountry, so default to first in list 337 this.defaultCountry = this.preferredCountries.length ? this.preferredCountries[0].iso2 : this.countries[0].iso2; 338 if (!val) { 339 this._setFlag(this.defaultCountry); 340 } 341 } 342 } 343 // if empty and no nationalMode and no autoHideDialCode then insert the default dial code 344 if (!val && !this.options.nationalMode && !this.options.autoHideDialCode && !this.options.separateDialCode) { 345 this.telInput.val("+" + this.selectedCountryData.dialCode); 346 } 347 } 348 // NOTE: if initialCountry is set to auto, that will be handled separately 349 // format 350 if (val) { 351 // this wont be run after _updateDialCode as that's only called
351if no val 352 this._updateValFromNumber(val); 353 } 354 }, 355 // initialise the main event listeners: input keyup, and click selected flag 356 _initListeners: function() { 357 this._initKeyListeners(); 358 if (this.options.autoHideDialCode) { 359 this._initFocusListeners(); 360 } 361 if (this.options.allowDropdown) { 362 this._initDropdownListeners(); 363 } 364 if (this.hiddenInput) { 365 this._initHiddenInputListener(); 366 } 367 }, 368 // update hidden input on form submit 369 _initHiddenInputListener: function() { 370 var that = this; 371 var form = this.telInput.closest("form"); 372 if (form.length) { 373 form.submit(function() { 374 that.hiddenInput.val(that.getNumber()); 375 }); 376 } 377 }, 378 // initialise the dropdown listeners 379 _initDropdownListeners: function() { 380 var that = this; 381 // hack for input nested inside label: clicking the selected-flag to open the dropdown would then automatically trigger a 2nd click on the input which would close it again 382 var label = this.telInput.closest("label"); 383 if (label.length) { 384 label.on("click" + this.ns, function(e) { 385 // if the dropdown is closed, then focus the input, else ignore the click 386 if (that.countryList.hasClass("hide")) { 387 that.telInput.focus(); 388 } else { 389 e.preventDefault(); 390 } 391 }); 392 } 393 // toggle country dropdown on click 394 var selectedFlag = this.selectedFlagInner.parent(); 395 selectedFlag.on("click" + this.ns, function(e) { 396 // only intercept this event if we're opening the dropdown 397 // else let it bubble up to the top ("click-off-to-close" listener) 398 // we cannot just stopPropagation as it may be needed to close another instance 399 if (that.countryList.hasClass("hide") && !that.telInput.prop("disabled") && !that.telInput.prop("readonly")) { 400 that._showDropdown(); 401 } 402 }); 403 // open dropdown list if currently focused 404 this.flagsContainer.on("keydown" + that.ns, function(e) { 405 var isDropdownHidden = that.countryList.hasClass("hide"); 406 if (isDropdownHidden && (e.which == keys.UP || e.which == keys.DOWN || e.which == keys.SPACE || e.which == keys.ENTER)) { 407 // prevent form from being submitted if "ENTER" was pressed 408 e.preventDefault(); 409 // prevent event from being handled again by document 410 e.stopPropagation(); 411 that._showDropdown(); 412 } 413 // allow navigation from dropdown to input on TAB 414 if (e.which == keys.TAB) { 415 that._closeDropdown(); 416 } 417 }); 418 }, 419 // init many requests: utils script / geo ip lookup 420 _initRequests: function() { 421 var that = this; 422 // if the user has specified the path to the utils script, fetch it on window.load, else resolve 423 if (this.options.utilsScript && !window.intlTelInputUtils) { 424 // if the plugin is being initialised after the window.load event has already been fired 425 if ($.fn[pluginName].windowLoaded) { 426 $.fn[pluginName].loadUtils(this.options.utilsScript); 427 } else { 428 // wait until the load event so we don't block any other requests e.g. the flags image 429 $(window).on("load", function() { 430 $.fn[pluginName].loadUtils(that.options.utilsScript); 431 }); 432 } 433 } else { 434 this.utilsScriptDeferred.resolve(); 435 } 436 if (this.options.initialCountry === "auto") { 437 this._loadAutoCountry(); 438 } else { 439 this.autoCountryDeferred.resolve(); 440 } 441 }, 442 // perform the geo ip lookup 443 _loadAutoCountry: function() { 444 var that = this; 445 // 3 options: 446 // 1) already loaded (we're done) 447 // 2) not already started loading (start) 448 // 3) already started loading (do nothing - just wait for loading callback to fire) 449 if ($.fn[pluginName].autoCountry) { 450 this.handleAutoCountry(); 451 } else if (!$.fn[pluginName].startedLoadingAutoCountry) { 452 // don't do this twice! 453 $.fn[pluginName].startedLoadingAutoCountry = true; 454 if (typeof this.options.geoIpLookup === "function") { 455 this.options.geoIpLookup(function(countryCode) { 456 $.fn[pluginName].autoCountry = countryCode.toLowerCase(); 457 // tell all instances the auto country is ready 458 // TODO: this should just be the current instances 459 // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight away (e.g. if they have already done the geo ip lookup somewhere else). Using setTimeout means that the current thread of execution will finish before executing this, which allows the plugin to finish initialising. 460 setTimeout(function() { 461 $(".intl-tel-input input").intlTelInput("handleAutoCountry"); 462 }); 463 }); 464 } 465 } 466 }, 467 // initialize any key listeners 468 _initKeyListeners: function() { 469 var that = this; 470 // update flag on keyup 471 // (keep this listener separate otherwise the setTimeout breaks all the tests) 472 this.telInput.on("keyup" + this.ns, function() { 473 if (that._updateFlagFromNumber(that.telInput.val())) { 474 that._triggerCountryChange(); 475 } 476 }); 477 // update flag on cut/paste events (now supported in all major browsers) 478 this.telInput.on("cut" + this.ns + " paste" + this.ns, function() { 479 // hack because "paste" event is fired before input is updated 480 setTimeout(function() { 481 if (that._updateFlagFromNumber(that.telInput.val())) { 482 that._triggerCountryChange(); 483 } 484 }); 485 }); 486 }, 487 // adhere to the input's maxlength attr 488 _cap: function(number) { 489 var max = this.telInput.attr("maxlength"); 490 return max && number.length > max ? number.substr(0, max) : number; 491 }, 492 // listen for mousedown, focus and blur 493 _initFocusListeners: function() { 494 var that = this; 495 // mousedown decides where the cursor goes, so if we're focusing we must preventDefault as we'll be inserting the dial code, and we want the cursor to be at the end no matter where they click 496 this.telInput.on("mousedown" + this.ns, function(e) { 497 if (!that.telInput.is(":focus") && !that.telInput.val()) { 498 e.preventDefault(); 499 // but this also cancels the focus, so we must trigger that manually 500 that.telInput.focus(); 501 } 502 }); 503 // on focus: if empty, insert the dial code for the currently selected flag 504 this.telInput.on("focus" + this.ns, function(e) { 505 if (!that.telInput.val() && !that.telInput.prop("readonly") && that.selectedCountryData.dialCode) { 506 // insert the dial code 507 that.telInput.val("+" + that.selectedCountryData.dialCode); 508 // after auto-inserting a dial code, if the first key they hit is '+' then assume they are entering a new number, so remove the dial code. use keypress instead of keydown because keydown gets triggered for the shift key (required to hit the + key), and instead of keyup because that shows the new '+' before removing the old one 509 that.telInput.one("keypress.plus" + that.ns, function(e) { 510 if (e.which == keys.PLUS) { 511 that.telInput.val(""); 512 } 513 }); 514 // after tabbing in, make sure the cursor is at the end we must use setTimeout to get outside of the focus handler as it seems the selection happens after that 515 setTimeout(function() { 516 var input = that.telInput[0]; 517 if (that.isGoodBrowser) { 518 var len = that.telInput.val().length; 519 input.setSelectionRange(len, len); 520 } 521 }); 522 } 523 }); 524 // on blur or form submit: if just a dial code then remove it 525 var form = this.telInput.prop("form"); 526 if (form) { 527 $(form).on("submit" + this.ns, function() { 528 that._removeEmptyDialCode(); 529 }); 530 } 531 this.telInput.on("blur" + this.ns, function() { 532 that._removeEmptyDialCode(); 533 }); 534 }, 535 _removeEmptyDialCode: function() { 536 var value = this.telInput.val(), startsPlus = value.charAt(0) == "+"; 537 if (startsPlus) { 538 var numeric = this._getNumeric(value); 539 // if just a plus, or if just a dial code 540 if (!numeric || this.selectedCountryData.dialCode == numeric) { 541 this.telInput.val(""); 542 } 543 } 544 // remove the keypress listener we added on focus 545 this.telInput.off("keypress.plus" + this.ns); 546 }, 547 // extract the numeric digits from the given string 548 _getNumeric: function(s) { 549 return s.replace(/\D/g, ""); 550 }, 551 // show the dropdown 552 _showDropdown: function() { 553 this._setDropdownPosition(); 554 // update highlighting and scroll to active list item 555 var activeListItem = this.countryList.children(".active"); 556 if (activeListItem.length) {
557 this._highlightListItem(activeListItem); 558 this._scrollTo(activeListItem); 559 } 560 // bind all the dropdown-related listeners: mouseover, click, click-off, keydown 561 this._bindDropdownListeners(); 562 // update the arrow 563 this.selectedFlagInner.children(".iti-arrow").addClass("up"); 564 this.telInput.trigger("open:countrydropdown"); 565 }, 566 // decide where to position dropdown (depends on position within viewport, and scroll) 567 _setDropdownPosition: function() { 568 var that = this; 569 if (this.options.dropdownContainer) { 570 this.dropdown.appendTo(this.options.dropdownContainer); 571 } 572 // show the menu and grab the dropdown height 573 this.dropdownHeight = this.countryList.removeClass("hide").attr("aria-expanded", "true").outerHeight(); 574 if (!this.isMobile) { 575 var pos = this.telInput.offset(), inputTop = pos.top, windowTop = $(window).scrollTop(), // dropdownFitsBelow = (dropdownBottom < windowBottom) 576 dropdownFitsBelow = inputTop + this.telInput.outerHeight() + this.dropdownHeight < windowTop + $(window).height(), dropdownFitsAbove = inputTop - this.dropdownHeight > windowTop; 577 // by default, the dropdown will be below the input. If we want to position it above the input, we add the dropup class. 578 this.countryList.toggleClass("dropup", !dropdownFitsBelow && dropdownFitsAbove); 579 // if dropdownContainer is enabled, calculate postion 580 if (this.options.dropdownContainer) { 581 // by default the dropdown will be directly over the input because it's not in the flow. If we want to position it below, we need to add some extra top value. 582 var extraTop = !dropdownFitsBelow && dropdownFitsAbove ? 0 : this.telInput.innerHeight(); 583 // calculate placement 584 this.dropdown.css({ 585 top: inputTop + extraTop, 586 left: pos.left 587 }); 588 // close menu on window scroll 589 $(window).on("scroll" + this.ns, function() { 590 that._closeDropdown(); 591 }); 592 } 593 } 594 }, 595 // we only bind dropdown listeners when the dropdown is open 596 _bindDropdownListeners: function() { 597 var that = this; 598 // when mouse over a list item, just highlight that one 599 // we add the class "highlight", so if they hit "enter" we know which one to select 600 this.countryList.on("mouseover" + this.ns, ".country", function(e) { 601 that._highlightListItem($(this)); 602 }); 603 // listen for country selection 604 this.countryList.on("click" + this.ns, ".country", function(e) { 605 that._selectListItem($(this)); 606 }); 607 // click off to close 608 // (except when this initial opening click is bubbling up) 609 // we cannot just stopPropagation as it may be needed to close another instance 610 var isOpening = true; 611 $("html").on("click" + this.ns, function(e) { 612 if (!isOpening) { 613 that._closeDropdown(); 614 } 615 isOpening = false; 616 }); 617 // listen for up/down scrolling, enter to select, or letters to jump to country name. 618 // use keydown as keypress doesn't fire for non-char keys and we want to catch if they 619 // just hit down and hold it to scroll down (no keyup event). 620 // listen on the document because that's where key events are triggered if no input has focus 621 var query = "", queryTimer = null; 622 $(document).on("keydown" + this.ns, function(e) { 623 // prevent down key from scrolling the whole page, 624 // and enter key from submitting a form etc 625 e.preventDefault(); 626 if (e.which == keys.UP || e.which == keys.DOWN) { 627 // up and down to navigate 628 that._handleUpDownKey(e.which); 629 } else if (e.which == keys.ENTER) { 630 // enter to select 631 that._handleEnterKey(); 632 } else if (e.which == keys.ESC) { 633 // esc to close 634 that._closeDropdown(); 635 } else if (e.which >
635= keys.A && e.which <= keys.Z || e.which == keys.SPACE) { 636 // upper case letters (note: keyup/keydown only return upper case letters) 637 // jump to countries that start with the query string 638 if (queryTimer) { 639 clearTimeout(queryTimer); 640 } 641 query += String.fromCharCode(e.which); 642 that._searchForCountry(query); 643 // if the timer hits 1 second, reset the query 644 queryTimer = setTimeout(function() { 645 query = ""; 646 }, 1e3); 647 } 648 }); 649 }, 650 // highlight the next/prev item in the list (and ensure it is visible) 651 _handleUpDownKey: function(key) { 652 var current = this.countryList.children(".highlight").first(); 653 var next = key == keys.UP ? current.prev() : current.next(); 654 if (next.length) { 655 // skip the divider 656 if (next.hasClass("divider")) { 657 next = key == keys.UP ? next.prev() : next.next(); 658 } 659 this._highlightListItem(next); 660 this._scrollTo(next); 661 } 662 }, 663 // select the currently highlighted item 664 _handleEnterKey: function() { 665 var currentCountry = this.countryList.children(".highlight").first(); 666 if (currentCountry.length) { 667 this._selectListItem(currentCountry); 668 } 669 }, 670 // find the first list item whose name starts with the query string 671 _searchForCountry: function(query) { 672 for (var i = 0; i < this.countries.length; i++) { 673 if (this._startsWith(this.countries[i].name, query)) { 674 var listItem = this.countryList.children("[data-country-code=" + this.countries[i].iso2 + "]").not(".preferred"); 675 // update highlighting and scroll 676 this._highlightListItem(listItem); 677 this._scrollTo(listItem, true); 678 break; 679 } 680 } 681 }, 682 // check if (uppercase) string a starts with string b 683 _startsWith: function(a, b) { 684 return a.substr(0, b.length).toUpperCase() == b; 685 }, 686 // update the input's value to the given val (format first if possible) 687 // NOTE: this is called from _setInitialState, handleUtils and setNumber
688 _updateValFromNumber: function(number) { 689 if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) { 690 var format = !this.options.separateDialCode && (this.options.nationalMode || number.charAt(0) != "+") ? intlTelInputUtils.numberFormat.NATIONAL : intlTelInputUtils.numberFormat.INTERNATIONAL; 691 number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format); 692 } 693 number = this._beforeSetNumber(number); 694 this.telInput.val(number); 695 }, 696 // check if need to select a new flag based on the given number 697 // Note: called from _setInitialState, keyup handler, setNumber 698 _updateFlagFromNumber: function(number) { 699 // if we're in nationalMode and we already have US/Canada selected, make sure the number starts with a +1 so _getDialCode will be able to extract the area code 700 // 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 701 if (number && this.options.nationalMode && this.selectedCountryData.dialCode == "1" && number.charAt(0) != "+") { 702 if (number.charAt(0) != "1") { 703 number = "1" + number; 704 } 705 number = "+" + number; 706 } 707 // try and extract valid dial code from input 708 var dialCode = this._getDialCode(number), countryCode = null, numeric = this._getNumeric(number); 709 if (dialCode) { 710 // check if one of the matching countries is already selected 711 var countryCodes = this.countryCodes[this._getNumeric(dialCode)], alreadySelected = $.inArray(this.selectedCountryData.iso2, countryCodes) > -1, // check if the given number contains a NANP area code i.e. the only dialCode that could be extracted was +1 (instead of say +1204) and the actual number's length is >=4 712 isNanpAreaCode = dialCode == "+1" && numeric.length >= 4, nanpSelected = this.selectedCountryData.dialCode == "1"; 713 // only update the flag if: 714 // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp) 715 // AND 716 // B) either a matching country is not already selected OR the number contains a NANP area code (ensure the flag is set to the first matching country) 717 if (!(nanpSelected && this._isRegionlessNanp(numeric)) && (!alreadySelected || isNanpAreaCode)) { 718 // if using onlyCountries option, countryCodes[0] may be empty, so we must find the first non-empty index 719 for (var j = 0; j < countryCodes.length; j++) { 720 if (countryCodes[j]) { 721 countryCode = countryCodes[j]; 722 break; 723 } 724 } 725 } 726 } else if (number.charAt(0) == "+" && numeric.length) { 727 // invalid dial code, so empty 728 // Note: use getNumeric here because the number has not been formatted yet, so could contain bad chars 729 countryCode = ""; 730 } else if (!number || number == "+") { 731 // empty, or just a plus, so default 732 countryCode = this.defaultCountry; 733 } 734 if (countryCode !== null) { 735 return this._setFlag(countryCode); 736 } 737 return false; 738 }, 739 // check if the given number is a regionless NANP number (expects the number to contain an international dial code) 740 _isRegionlessNanp: function(number) { 741 var numeric = this._getNumeric(number); 742 if (numeric.charAt(0) == "1") { 743 var areaCode = numeric.substr(1, 3); 744 return $.inArray(areaCode, regionlessNanpNumbers) > -1; 745 } 746 return false; 747 }, 748 // remove highlighting from other list items and highlight the given item 749 _highlightListItem: function(listItem) { 750 this.countryListItems.removeClass("highlight"); 751 listItem.addClass("highlight"); 752 }, 753 // find the country data for the given country code 754 // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array 755 _getCountryData: function(countryCode, ignoreOnlyCountriesOption, allowFail) { 756 var countryList = ignoreOnlyCountriesOption ? allCountries : this.countries; 757 for (var i = 0; i < countryList.length; i++) { 758 if (countryList[i].iso2 == countryCode) { 759 return countryList[i]; 760 } 761 } 762 if (allowFail) { 763 return null; 764 } else { 765 throw new Error("No country data for '" + countryCode + "'"); 766 } 767 }, 768 // select the given flag, update the placeholder and the active list item 769 // Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry 770 _setFlag: function(countryCode) { 771 var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {}; 772 // do this first as it will throw an error and stop if c
772ountryCode is invalid 773 this.selectedCountryData = countryCode ? this._getCountryData(countryCode, false, false) : {}; 774 // update the defaultCountry - we only need the iso2 from now on, so just store that 775 if (this.selectedCountryData.iso2) { 776 this.defaultCountry = this.selectedCountryData.iso2; 777 } 778 this.selectedFlagInner.attr("class", "iti-flag " + countryCode); 779 // update the selected country's title attribute 780 var title = countryCode ? this.selectedCountryData.name + ": +" + this.selectedCountryData.dialCode : "Unknown"; 781 this.selectedFlagInner.parent().attr("title", title); 782 if (this.options.separateDialCode) { 783 var dialCode = this.selectedCountryData.dialCode ? "+" + this.selectedCountryData.dialCode : "", parent = this.telInput.parent(); 784 if (prevCountry.dialCode) { 785 parent.removeClass("iti-sdc-" + (prevCountry.dialCode.length + 1)); 786 } 787 if (dialCode) { 788 parent.addClass("iti-sdc-" + dialCode.length); 789 } 790 this.selectedDialCode.text(dialCode); 791 } 792 // and the input's placeholder 793 this._updatePlaceholder(); 794 // update the active list item 795 if (this.options.allowDropdown) { 796 this.countryListItems.removeClass("active").attr("aria-selected", "false"); 797 if (countryCode) { 798 var listItem = this.countryListItems.find(".iti-flag." + countryCode).first().closest(".country"); 799 listItem.addClass("active").attr("aria-selected", "true"); 800 this.countryList.attr("aria-activedescendant", listItem.attr("id")); 801 } 802 } 803 // return if the flag has changed or not 804 return prevCountry.iso2 !== countryCode; 805 }, 806 // update the input placeholder to an example number from the currently selected country 807 _updatePlaceholder: function() { 808 var shouldSetPlaceholder = this.options.autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && (this.options.autoPlaceholder === true || this.options.autoPlaceholder === "polite"); 809 if (window.intlTelInputUtils && shouldSetPlaceholder) { 810 var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType], placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, this.options.nationalMode, numberType) : ""; 811 placeholder = this._beforeSetNumber(placeholder); 812 if (typeof this.options.customPlaceholder === "function") { 813 placeholder = this.options.customPlaceholder(placeholder, this.selectedCountryData); 814 } 815 this.telInput.attr("placeholder", placeholder); 816 } 817 }, 818 // called when the user selects a list item from the dropdown 819 _selectListItem: function(listItem) { 820 // update selected flag and active list item 821 var flagChanged = this._setFlag(listItem.attr("data-country-code")); 822 this._closeDropdown(); 823 this._updateDialCode(listItem.attr("data-dial-code"), true); 824 // focus the input 825 this.telInput.focus(); 826 // put cursor at end - this fix is required for FF and IE11 (with nationalMode=false i.e. auto inserting dial code), who try to put the cursor at the beginning the first time 827 if (this.isGoodBrowser) { 828 var len = this.telInput.val().length; 829 this.telInput[0].setSelectionRange(len, len); 830 } 831 if (flagChanged) { 832 this._triggerCountryChange(); 833 } 834 }, 835 // close the dropdown and unbind any listeners 836 _closeDropdown: function() { 837 this.countryList.addClass("hide"); 838 this.countryList.attr("aria-expanded", "false"); 839 // update the arrow 840 this.selectedFlagInner.children(".iti-arrow").removeClass("up"); 841 // unbind key events 842 $(document).off(this.ns); 843 // unbind click-off-to-close 844 $("html").off(this.ns); 845 // unbind hover and click listeners 846 this.countryList.off(this.ns); 847 // remove menu from container 848 if (this.options.dropdownContainer) { 849 if (!this.isMobile) { 850 $(window).off("scroll" + this.ns); 851 } 852 this.dropdown.detach(); 853 } 854 this.telInput.trigger("close:countrydropdown"); 855 }, 856 // check if an element is visible within it's container, else scroll until it is 857 _scrollTo: function(element, middle) { 858 var container = this.countryList, containerHeight = container.height(), containerTop = container.offset().top, containerBottom = containerTop + containerHeight, elementHeight = element.outerHeight(), elementTop = element.offset().top, elementBottom = elementTop + elementHeight, newScrollTop = elementTop - containerTop + container.scrollTop(), middleOffset = containerHeight / 2 - elementHeight / 2;
859 if (elementTop < containerTop) { 860 // scroll up 861 if (middle) { 862 newScrollTop -= middleOffset; 863 } 864 container.scrollTop(newScrollTop); 865 } else if (elementBottom > containerBottom) { 866 // scroll down 867 if (middle) { 868 newScrollTop += middleOffset; 869 } 870 var heightDifference = containerHeight - elementHeight; 871 container.scrollTop(newScrollTop - heightDifference); 872 } 873 }, 874 // replace any existing dial code with the new one 875 // Note: called from _selectListItem and setCountry 876 _updateDialCode: function(newDialCode, hasSelectedListItem) { 877 var inputVal = this.telInput.val(), newNumber; 878 // save having to pass this every time 879 newDialCode = "+" + newDialCode; 880 if (inputVal.charAt(0) == "+") { 881 // there's a plus so we're dealing with a replacement (doesn't matter if nationalMode or not) 882 var prevDialCode = this._getDialCode(inputVal); 883 if (prevDialCode) { 884 // current number contains a valid dial code, so replace it 885 newNumber = inputVal.replace(prevDialCode, newDialCode); 886 } else { 887 // current number contains an invalid dial code, so ditch it 888 // (no way to determine where the invalid dial code ends and the rest of the number begins) 889 newNumber = newDialCode; 890 } 891 } else if (this.options.nationalMode || this.options.separateDialCode) { 892 // don't do anything 893 return; 894 } else { 895 // nationalMode is disabled 896 if (inputVal) { 897 // there is an existing value with no dial code: prefix the new dial code 898 newNumber = newDialCode + inputVal; 899 } else if (hasSelectedListItem || !this.options.autoHideDialCode) { 900 // no existing value and either they've just selected a list item, or autoHideDialCode is disabled: insert new dial code 901 newNumber = newDialCode; 902 } else { 903 return; 904 } 905 } 906 this.telInput.val(newNumber); 907 }, 908 // try and extract a valid international dial code from a full telephone number 909 // Note: returns the raw string inc plus character and any whitespace/dots etc 910 _getDialCode: function(number) { 911 var dialCode = ""; 912 // only interested in international numbers (starting with a plus) 913 if (number.charAt(0) == "+") { 914 var numericChars = ""; 915 // iterate over chars 916 for (var i = 0; i < number.length; i++) { 917 var c = number.charAt(i); 918 // if char is number 919 if ($.isNumeric(c)) { 920 numericChars += c; 921 // if current numericChars make a valid dial code 922 if (this.countryCodes[numericChars]) { 923 // store the actual raw string (useful for matching later) 924 dialCode = number.substr(0, i + 1); 925 } 926 // longest dial code is 4 chars 927 if (numericChars.length == 4) { 928 break; 929 } 930 } 931 } 932 } 933 return dialCode; 934 }, 935 // get the input val, adding the dial code if separateDialCode is enabled 936 _getFullNumber: function() { 937 var val = $.trim(this.telInput.val()), dialCode = this.selectedCountryData.dialCode, prefix, numericVal = this._getNumeric(val), // normalized means ensure starts with a 1, so we can match against the full dial code 938 normalizedVal = numericVal.charAt(0) == "1" ? numericVal : "1" + numericVal; 939 if (this.options.separateDialCode) { 940 // when using separateDialCode, it is visible so is effectively part of the typed number 941 prefix = "+" + dialCode; 942 } else if (val && val.charAt(0) != "+" && val.charAt(0) != "1" && dialCode && dialCode.charAt(0) == "1" && dialCode.length == 4 && dialCode != normalizedVal.substr(0, 4)) { 943 // ensure national NANP numbers contain the area code 944 prefix = dialCode.substr(1); 945 } else { 946 prefix = ""; 947 } 948 return prefix + val; 949 }, 950 // remove the dial code if separateDialCode is enabled 951 _beforeSetNumber: function(number) { 952 if (this.options.separateDialCode) { 953 var dialCode = this._getDialCode(number); 954 if (dialCode) { 955 // US dialCode is "+1", which is what we want 956 // CA dialCode is "+1 123", which is wrong - should be "+1" (as it has multiple area codes) 957 // AS dialCode is "+1 684", which is what we want 958 // Solution: if the country has area codes, then revert to just the dial code 959 if (this.selectedCountryData.areaCodes !== null) { 960 dialCode = "+" + this.selectedCountryData.dialCode; 961 } 962 // a lot of numbers will have a space separating the dial code and the main number, and some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get rid of it 963 // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc 964 var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length; 965 number = number.substr(start); 966 } 967 } 968 return this._cap(number); 969 }, 970 // trigger the 'countrychange' event 971 _triggerCountryChange: function() { 972 this.telInput.trigger("countrychange", this.selectedCountryData); 973 }, 974 /************************** 975 * SECRET PUBLIC METHODS 976 **************************/ 977 // this is called when the geoip call returns 978 handleAutoCountry: function() { 979 if (this.options.initialCountry === "auto") { 980 // we must set this even if there is an initial val in the input: in case the initial val is invali
980d and they delete it - they should see their auto country 981 this.defaultCountry = $.fn[pluginName].autoCountry; 982 // if there's no initial value in the input, then update the flag 983 if (!this.telInput.val()) { 984 this.setCountry(this.defaultCountry); 985 } 986 this.autoCountryDeferred.resolve(); 987 } 988 }, 989 // this is called when the utils request completes 990 handleUtils: function() { 991 // if the request was successful 992 if (window.intlTelInputUtils) { 993 // if there's an initial value in the input, then format it 994 if (this.telInput.val()) { 995 this._updateValFromNumber(this.telInput.val()); 996 } 997 this._updatePlaceholder(); 998 } 999 this.utilsScriptDeferred.resolve(); 1000 }, 1001 /******************** 1002 * PUBLIC METHODS 1003 ********************/ 1004 // remove plugin 1005 destroy: function() { 1006 if (this.options.allowDropdown) { 1007 // make sure the dropdown is closed (and unbind listeners) 1008 this._closeDropdown(); 1009 // click event to open dropdown 1010 this.selectedFlagInner.parent().off(this.ns); 1011 // label click hack 1012 this.telInput.closest("label").off(this.ns); 1013 } 1014 // unbind submit event handler on form 1015 if (this.options.autoHideDialCode) { 1016 var form = this.telInput.prop("form"); 1017 if (form) { 1018 $(form).off(this.ns); 1019 } 1020 } 1021 // unbind all events: key events, and focus/blur events if autoHideDialCode=true 1022 this.telInput.off(this.ns); 1023 // remove markup (but leave the original input) 1024 var container = this.telInput.parent(); 1025 container.before(this.telInput).remove(); 1026 }, 1027 // get the extension from the current number 1028 getExtension: function() { 1029 if (window.intlTelInputUtils) { 1030 return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2); 1031 } 1032 return ""; 1033 }, 1034 // format the number to the given format 1035 getNumber: function(format) { 1036 if (window.intlTelInputUtils) { 1037 return intlTelInputUtils.formatNumber(this._getFullNumber(), this.selectedCountryData.iso2, format); 1038 } 1039 return ""; 1040 }, 1041 // get the type of the entered number e.g. landline/mobile 1042 getNumberType: function() { 1043 if (window.intlTelInputUtils) { 1044 return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2); 1045 } 1046 return -99; 1047 }, 1048 // get the country data for the currently selected flag 1049 getSelectedCountryData: function() { 1050 return this.selectedCountryData; 1051 }, 1052 // get the validation error 1053 getValidationError: function() { 1054 if (window.intlTelInputUtils) { 1055 return intlTelInputUtils.getValidationError(this._getFullNumber(), this.selectedCountryData.iso2); 1056 } 1057 return -99; 1058 }, 1059 // validate the input val - assumes the global function isValidNumber (from utilsScript) 1060 isValidNumber: function() { 1061 var val = $.trim(this._getFullNumber()), countryCode = this.options.nationalMode ? this.selectedCountryData.iso2 : ""; 1062 return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, countryCode) : null; 1063 }, 1064 // update the selected flag, and update the input val accordingly 1065 setCountry: function(countryCode) { 1066 countryCode = countryCode.toLowerCase(); 1067 // check if already selected 1068 if (!this.selectedFlagInner.hasClass(countryCode)) { 1069 this._setFlag(countryCode); 1070 this._updateDialCode(this.selectedCountryData.dialCode, false); 1071 this._triggerCountryChange(); 1072 } 1073 }, 1074 // set the input value and update the flag 1075 setNumber: function(number) { 1076 // we must update the flag first, which updates this.selectedCountryData, which is used for formatting the number before displaying it 1077 var flagChanged = this._updateFlagFromNumber(number);
1078 this._updateValFromNumber(number); 1079 if (flagChanged) { 1080 this._triggerCountryChange(); 1081 } 1082 }, 1083 // set the placeholder number typ 1084 setPlaceholderNumberType: function(type) { 1085 this.options.placeholderNumberType = type; 1086 this._updatePlaceholder(); 1087 } 1088 }; 1089 // using https://github.com/jquery-boilerplate/jquery-boilerplate/wiki/Extending-jQuery-Boilerplate 1090 // (adapted to allow public functions) 1091 $.fn[pluginName] = function(options) { 1092 var args = arguments; 1093 // Is the first parameter an object (options), or was omitted, 1094 // instantiate a new instance of the plugin. 1095 if (options === undefined || typeof options === "object") { 1096 // collect all of the deferred objects for all instances created with this selector 1097 var deferreds = []; 1098 this.each(function() { 1099 if (!$.data(this, "plugin_" + pluginName)) { 1100 var instance = new Plugin(this, options); 1101 var instanceDeferreds = instance._init(); 1102 // we now have 2 deffereds: 1 for auto country, 1 for utils script 1103 deferreds.push(instanceDeferreds[0]); 1104 deferreds.push(instanceDeferreds[1]); 1105 $.data(this, "plugin_" + pluginName, instance); 1106 } 1107 }); 1108 // return the promise from the "master" deferred object that tracks all the others 1109 return $.when.apply(null, deferreds); 1110 } else if (typeof options === "string" && options[0] !== "_") { 1111 // If the first parameter is a string and it doesn't start 1112 // with an underscore or "contains" the `init`-function, 1113 // treat this as a call to a public method. 1114 // Cache the method call to make it possible to return a value 1115 var returns; 1116 this.each(function() { 1117 var instance = $.data(this, "plugin_" + pluginName); 1118 // Tests that there's already a plugin-instance 1119 // and checks that the requested public method exists 1120 if (instance instanceof Plugin && typeof instance[options] === "function") { 1121 // Call the method of our plugin instance, 1122 // and pass it the supplied arguments. 1123 returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1)); 1124 } 1125 // Allow instances to be destroyed via the 'destroy' method 1126 if (options === "destroy") { 1127 $.data(this, "plugin_" + pluginName, null); 1128 } 1129 }); 1130 // If the earlier cached method gives a value back return the value, 1131 // otherwise return this to preserve chainability. 1132 return returns !== undefined ? returns : this; 1133 } 1134 }; 1135 /******************** 1136 * STATIC METHODS 1137 ********************/ 1138 // get the country data object 1139 $.fn[pluginName].getCountryData = function() { 1140 return allCountries; 1141 }; 1142 // load the utils script 1143 // (assumes it has not already loaded - we check this before calling this internally) 1144 // (also assumes that if it is called manually, it will only be once per page) 1145 $.fn[pluginName].loadUtils = function(path) { 1146 // 2 options: 1147 // 1) not already started loading (start) 1148 // 2) already started loading (do nothing - just wait for loading callback to fire, which will trigger handleUtils on all instances, resolving each of their utilsScriptDeferred objects) 1149 if (!$.fn[pluginName].startedLoadingUtilsScript) { 1150 // don't do this twice! 1151 $.fn[pluginName].startedLoadingUtilsScript = true; 1152 // dont use $.getScript as it prevents caching 1153 // return the ajax Deferred object, so manual calls can be chained with .then(callback) 1154 return $.ajax({ 1155 type: "GET", 1156 url: path, 1157 complete: function() { 1158 // tell all instances that the utils request is complete 1159 $(".intl-tel-input input").intlTelInput("handleUtils"); 1160 }, 1161 dataType: "script", 1162 cache: true 1163 }); 1164 } 1165 return null; 1166 }; 1167 // default options 1168 $.fn[pluginName].defaults = defaults; 1169 // version 1170 $.fn[pluginName].version = "13.0.4"; 1171 // Array of country objects for the flag dropdown. 1172 // Here is the criteria for the plugin to support a given country/territory 1173 // - It has an iso2 code: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 1174 // - It has it's own country calling code (it is not a sub-region of another country): https://en.wikipedia.org/wiki/List_of_country_calling_codes 1175 // - It has a flag in the region-flags project: https://github.com/behdad/region-flags/tree/gh-pages/png 1176 // - It is supported by libphonenumber (it must be listed on this page): https://github.com/googlei18n/libphonenumber/blob/master/resources/ShortNumberMetadata.xml 1177 // Each country array has the following information: 1178 // [ 1179 // Country name, 1180 // iso2 code, 1181 // International dial code, 1182 // Order (if >1 country with same dial code), 1183 // Area codes 1184 // ] 1185 var allCountries = [ [ "Afghanistan (â«Ø§ÙØºØ§ÙØ³ØªØ§Ùâ¬â)", "af", "93" ], [ "Albania (Shqipëri)", "al", "355" ], [ "Algeria (â«Ø§Ùجزائرâ¬â)", "dz", "213" ], [ "American Samoa", "as", "1684" ], [ "Andorra", "ad", "376" ], [ "Angola", "ao", "244" ], [ "Anguilla", "ai", "1264" ], [ "Antigua and Barbuda", "ag", "1268" ], [ "Argentina", "ar", "54" ], [ "Armenia (ÕÕ¡ÕµÕ¡Õ½Õ¿Õ¡Õ¶)", "am", "374" ], [ "Aruba", "aw", "297" ], [ "Australia", "au", "61", 0 ], [ "Austria (Ãsterreich)", "at", "43" ], [ "Azerbaijan (AzÉrbaycan)", "az", "994" ], [ "Bahamas", "bs", "1242" ], [ "Bahrain (â«Ø§ÙØ¨ØØ±ÙÙâ¬â)", "bh", "973" ], [ "Bangladesh (বাà¦à¦²à¦¾à¦¦à§à¦¶)", "bd", "880" ], [ "Barbados", "bb", "1246" ], [ "Belarus (ÐелаÑÑÑÑ)", "by", "375" ], [ "Belgium (België)", "be", "32" ], [ "Belize", "bz", "501" ], [ "Benin (Bénin)", "bj", "229" ], [ "Bermuda", "bm", "1441" ], [ "Bhutan (འà½à¾²à½´à½)", "bt", "975" ], [ "Bolivia", "bo", "591" ], [ "Bosnia and Herzegovina (ÐоÑна и ХеÑÑеговина)", "ba", "387" ], [ "Botswana", "bw", "267" ], [ "Brazil (Brasil)", "br", "55" ], [ "British Indian Ocean Territory", "io", "246" ], [ "British Virgin Islands", "vg", "1284" ], [ "Brunei", "bn", "673" ], [ "Bulgaria (ÐÑлгаÑиÑ)", "bg", "359" ], [ "Burkina Faso", "bf", "226" ], [ "Burundi (Uburundi)", "bi", "257" ], [ "Cambodia (ááááá»áá¶)", "kh", "855" ], [ "Cameroon (Cameroun)", "cm", "237" ], [ "Canada", "ca", "1", 1, [ "204", "226", "236", "249", "250", "289", "306", "343", "365", "387", "403", "416", "418", "431", "437", "438", "450", "506", "514", "519", "548", "579", "581", "587", "604", "613", "639", "647", "672", "705", "709", "742", "778", "780", "782", "807", "819", "825", "867", "873", "902", "905" ] ], [ "Cape Verde (Kabu Verdi)", "cv", "238" ], [ "Caribbean Netherlands", "bq", "599", 1 ], [ "Cayman Islands", "ky", "1345" ], [ "Central African Republic (République centrafricaine)", "cf", "236" ], [ "Chad (Tchad)", "td", "235" ], [ "Chile", "cl", "56" ], [ "China (ä¸å½)", "cn", "86" ], [ "Christmas Island", "cx", "61", 2 ], [ "Cocos (Keeling) Islands", "cc", "61", 1 ], [ "Colombia", "co", "57" ], [ "Comoros (â«Ø¬Ø²Ø± اÙÙ٠رâ¬â)", "km", "269" ], [ "Congo (DRC) (Jamhuri ya Kidemokrasia ya Kongo)", "cd", "243" ], [ "Congo (Republic) (Congo-Brazzaville)", "cg", "242" ], [ "Cook Islands", "ck", "682" ], [ "Costa Rica", "cr", "506" ], [ "Côte dâIvoire", "ci", "225" ], [ "Croatia (Hrvatska)", "hr", "385" ], [ "Cuba", "cu", "53" ], [ "Curaçao", "cw", "599", 0 ], [ "Cyprus (ÎÏÏÏοÏ)", "cy", "357" ], [ "Czech Republic (Äeská republika)", "cz", "420" ], [ "Denmark (Danmark)", "dk", "45" ], [ "Djibouti", "dj", "253" ], [ "Dominica", "dm", "1767" ], [ "Dominican Republic (República Dominicana)", "do", "1", 2, [ "809", "829", "849" ] ], [ "Ecuador", "ec", "593" ], [ "Egypt (â«Ù صرâ¬â)", "eg", "20" ], [ "El Salvador", "sv", "503" ], [ "Equatorial Guinea (Guinea Ecuatorial)", "gq", "240" ], [ "Eritrea", "er", "291" ], [ "Estonia (Eesti)", "ee", "372" ], [ "Ethiopia", "et", "251" ], [ "Falkland Islands (Islas Malvinas)", "fk", "500" ], [ "Faroe Islands (Føroyar)", "fo", "298" ], [ "Fiji", "fj", "679" ], [ "Finland (Suomi)", "fi", "358", 0 ], [ "France", "fr", "33" ], [ "French Guiana (Guyane française)", "gf", "594" ], [ "French Polynesia (Polynésie française)", "pf", "689" ], [ "Gabon", "ga", "241" ], [ "Gambia", "gm", "220" ], [ "Georgia (á¡áá¥áá ááááá)", "ge", "995" ], [ "Germany (Deutschland)", "de", "49" ], [ "Ghana (Gaana)", "gh", "233" ], [ "Gibraltar", "gi", "350" ], [ "Greece (Îλλάδα)", "gr", "30" ], [ "Greenland (Kalaallit Nun
1185aat)", "gl", "299" ], [ "Grenada", "gd", "1473" ], [ "Guadeloupe", "gp", "590", 0 ], [ "Guam", "gu", "1671" ], [ "Guatemala", "gt", "502" ], [ "Guernsey", "gg", "44", 1 ], [ "Guinea (Guinée)", "gn", "224" ], [ "Guinea-Bissau (Guiné Bissau)", "gw", "245" ], [ "Guyana", "gy", "592" ], [ "Haiti", "ht", "509" ], [ "Honduras", "hn", "504" ], [ "Hong Kong (馿¸¯)", "hk", "852" ], [ "Hungary (Magyarország)", "hu", "36" ], [ "Iceland (Ãsland)", "is", "354" ], [ "India (à¤à¤¾à¤°à¤¤)", "in", "91" ], [ "Indonesia", "id", "62" ], [ "Iran (â«Ø§ÛراÙâ¬â)", "ir", "98" ], [ "Iraq (â«Ø§ÙعراÙâ¬â)", "iq", "964" ], [ "Ireland", "ie", "353" ], [ "Isle of Man", "im", "44", 2 ], [ "Israel (â«×שר××â¬â)", "il", "972" ], [ "Italy (Italia)", "it", "39", 0 ], [ "Jamaica", "jm", "1", 4, [ "876", "658" ] ], [ "Japan (æ¥æ¬)", "jp", "81" ], [ "Jersey", "je", "44", 3 ], [ "Jordan (â«Ø§ÙأردÙâ¬â)", "jo", "962" ], [ "Kazakhstan (ÐÐ°Ð·Ð°Ñ ÑÑан)", "kz", "7", 1 ], [ "Kenya", "ke", "254" ], [ "Kiribati", "ki", "686" ], [ "Kosovo", "xk", "383" ], [ "Kuwait (â«Ø§ÙÙÙÙØªâ¬â)", "kw", "965" ], [ "Kyrgyzstan (ÐÑÑгÑзÑÑан)", "kg", "996" ], [ "Laos (ລາວ)", "la", "856" ], [ "Latvia (Latvija)", "lv", "371" ], [ "Lebanon (â«ÙØ¨ÙØ§Ùâ¬â)", "lb", "961" ], [ "Lesotho", "ls", "266" ], [ "Liberia", "lr", "231" ], [ "Libya (â«ÙÙØ¨Ùاâ¬â)", "ly", "218" ], [ "Liechtenstein", "li", "423" ], [ "Lithuania (Lietuva)", "lt", "370" ], [ "Luxembourg", "lu", "352" ], [ "Macau (æ¾³é)", "mo", "853" ], [ "Macedonia (FYROM) (ÐакедониÑа)", "mk", "389" ], [ "Madagascar (Madagasikara)", "mg", "261" ], [ "Malawi", "mw", "265" ], [ "Malaysia", "my", "60" ], [ "Maldives", "mv", "960" ], [ "Mali", "ml", "223" ], [ "Malta", "mt", "356" ], [ "Marshall Islands", "mh", "692" ], [ "Martinique", "mq", "596" ], [ "Mauritania (â«Ù ÙØ±ÙتاÙÙØ§â¬â)", "mr", "222" ], [ "Mauritius (Moris)", "mu", "230" ], [ "Mayotte", "yt", "262", 1 ], [ "Mexico (México)", "mx", "52" ], [ "Micronesia", "fm", "691" ], [ "Moldova (Republica Moldova)", "md", "373" ], [ "Monaco", "mc", "377" ], [ "Mongolia (Ðонгол)", "mn", "976" ], [ "Montenegro (Crna Gora)", "me", "382" ], [ "Montserrat", "ms", "1664" ], [ "Morocco (â«Ø§Ù٠غربâ¬â)", "ma", "212", 0 ], [ "Mozambique (Moçambique)", "mz", "258" ], [ "Myanmar (Burma) (áá¼ááºáá¬)", "mm", "95" ], [ "Namibia (Namibië)", "na", "264" ], [ "Nauru", "nr", "674" ], [ "Nepal (नà¥à¤ªà¤¾à¤²)", "np", "977" ], [ "Netherlands (Nederland)", "nl", "31" ], [ "New Caledonia (Nouvelle-Calédonie)", "nc", "687" ], [ "New Zealand", "nz", "64" ], [ "Nicaragua", "ni", "505" ], [ "Niger (Nijar)", "ne", "227" ], [ "Nigeria", "ng", "234" ], [ "Niue", "nu", "683" ], [ "Norfolk Island", "nf", "672" ], [ "North Korea (ì¡°ì 민주주ì ì¸ë¯¼ ê³µíêµ)", "kp", "850" ], [ "Northern Mariana Islands", "mp", "1670" ], [ "Norway (Norge)", "no", "47", 0 ], [ "Oman (â«Ø¹Ù٠اÙâ¬â)", "om", "968" ], [ "Pakistan (â«Ù¾Ø§Ú©Ø³ØªØ§Ùâ¬â)", "pk", "92" ], [ "Palau", "pw", "680" ], [ "Palestine (â«ÙÙØ³Ø·ÙÙâ¬â)", "ps", "970" ], [ "Panama (Panamá)", "pa", "507" ], [ "Papua New Guinea", "pg", "675" ], [ "Paraguay", "py", "595" ], [ "Peru (Perú)", "pe", "51" ], [ "Philippines", "ph", "63" ], [ "Poland (Polska)", "pl", "48" ], [ "Portugal", "pt", "351" ], [ "Puerto Rico", "pr", "1", 3, [ "787", "939" ] ], [ "Qatar (â«Ùطرâ¬â)", "qa", "974" ], [ "Réunion (La Réunion)", "re", "262", 0 ], [ "Romania (România)", "ro", "40" ], [ "Russia (РоÑÑиÑ)", "ru", "7", 0 ], [ "Rwanda", "rw", "250" ], [ "Saint Barthélemy", "bl", "590", 1 ], [ "Saint Helena", "sh", "290" ], [ "Saint Kitts and Nevis", "kn", "1869" ], [ "Saint Lucia", "lc", "1758" ], [ "Saint Martin (Saint-Martin (partie française))", "mf", "590", 2 ], [ "Saint Pierre and Miquelon (Saint-Pierre-et-Miquelon)", "pm", "508" ], [ "Saint Vincent and the Grenadines", "vc", "1784" ], [ "Samoa", "ws", "685" ], [ "San Marino", "sm", "378" ], [ "São Tomé and PrÃncipe (São Tomé e PrÃncipe)", "st", "239" ], [ "Saudi Arabia (â«Ø§ÙÙ Ù ÙÙØ© Ø§ÙØ¹Ø±Ø¨ÙØ© Ø§ÙØ³Ø¹ÙØ¯ÙØ©â¬â)", "sa", "966" ], [ "Senegal (Sénégal)", "sn", "221" ], [ "Serbia (СÑбиÑа)", "rs", "381" ], [ "Seychelles", "sc", "248" ], [ "Sierra Leone", "sl", "232" ], [ "Singapore", "sg", "65" ], [ "Sint Maarten", "sx", "1721" ], [ "Slovakia (Slovensko)", "sk", "421" ], [ "Slovenia (Slovenija)", "si", "386" ], [ "Solomon Islands", "sb", "677" ], [ "Somalia (Soomaaliya)", "so", "252" ], [ "South Africa", "za", "27" ], [ "South Korea (ëí민êµ)", "kr", "82" ], [ "South Sudan (â«Ø¬ÙÙØ¨ Ø§ÙØ³ÙداÙâ¬â)", "ss", "211" ], [ "Spain (España)", "es", "34" ], [ "Sri Lanka (à·à·âර෠ලà¶à¶à·à·)", "lk", "94" ], [ "Sudan (â«Ø§ÙØ³ÙØ¯Ø§Ùâ¬â)", "sd", "249" ], [ "Suriname", "sr", "597" ], [ "Svalbard and Jan Mayen", "sj", "47", 1 ], [ "Swaziland", "sz", "268" ], [ "Sweden (Sverige)", "se", "46" ], [ "Switzerland (Schweiz)", "ch", "41" ], [ "Syria (â«Ø³ÙØ±ÙØ§â¬â)", "sy", "963" ], [ "Taiwan (å°ç£)", "tw", "886" ], [ "Tajikistan", "tj", "992" ], [ "Tanzania", "tz", "255" ], [ "Thailand (à¹à¸à¸¢)", "th", "66" ], [ "Timor-Leste", "tl", "670" ], [ "Togo", "tg", "228" ], [ "Tokelau", "tk", "690" ], [ "Tonga", "to", "676" ], [ "Trinidad and Tobago", "tt", "1868" ], [ "Tunisia (â«ØªÙÙØ³â¬â)", "tn", "216" ], [ "Turkey (Türkiye)", "tr", "90" ], [ "Turkmenistan", "tm", "993" ], [ "Turks and Caicos Islands", "tc", "1649" ], [ "Tuvalu", "tv", "688" ], [ "U.S. Virgin Islands", "vi", "1340" ], [ "Uganda", "ug", "256" ], [ "Ukraine (УкÑаÑна)", "ua", "380" ], [ "United Arab Emirates (â«Ø§Ùإ٠ارات Ø§ÙØ¹Ø±Ø¨ÙØ© اÙÙ ØªØØ¯Ø©â¬â)", "ae", "971" ], [ "United Kingdom", "gb", "44", 0 ], [ "United States", "us", "1", 0 ], [ "Uruguay", "uy", "598" ], [ "Uzbekistan (OÊ»zbekiston)", "uz", "998" ], [ "Vanuatu", "vu", "678" ], [ "Vatican City (Città del Vaticano)", "va", "39", 1 ], [ "Venezuela", "ve", "58" ], [ "Vietnam (Viá»t Nam)", "vn", "84" ], [ "Wallis and Futuna (Wallis-et-Futuna)", "wf", "681" ], [ "Western Sahara (â«Ø§ÙØµØØ±Ø§Ø¡ Ø§ÙØºØ±Ø¨ÙØ©â¬â)", "eh", "212", 1 ], [ "Yemen (â«Ø§ÙÙÙ Ùâ¬â)", "ye", "967" ], [ "Zambia", "zm", "260" ], [ "Zimbabwe", "zw", "263" ], [ "à land Islands", "ax", "358", 1 ] ]; 1186 // loop over all of the countries above 1187 for (var i = 0; i < allCountries.length; i++) { 1188 var c = allCountries[i]; 1189 allCountries[i] = { 1190 name: c[0], 1191 iso2: c[1], 1192 dialCode: c[2], 1193 priority: c[3] || 0, 1194 areaCodes: c[4] || null 1195 }; 1196 } 1197});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.