1/* 2 * International Telephone Input v16.0.5 3 * https://github.com/jackocnr/intl-tel-input.git 4 * Licensed under the MIT license 5 */ 6 7// wrap in UMD 8(function(factory) { 9 var intlTelInput = factory(window, document); 10 if (typeof module === "object" && module.exports) module.exports = intlTelInput; else window.intlTelInput = intlTelInput; 11})(function(window, document, undefined) { 12 13 return function() { 14 // Array of country objects for the flag dropdown. 15 // Here is the criteria for the plugin to support a given country/territory 16 // - It has an iso2 code: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 17 // - 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 18 // - It has a flag in the region-flags project: https://github.com/behdad/region-flags/tree/gh-pages/png 19 // - It is supported by libphonenumber (it must be listed on this page): https://github.com/googlei18n/libphonenumber/blob/master/resources/ShortNumberMetadata.xml 20 // Each country array has the following information: 21 // [ 22 // Country name, 23 // iso2 code, 24 // International dial code, 25 // Order (if >1 country with same dial code), 26 // Area codes 27 // ] 28 var allCountries = [ [ "Afghanistan (â«Ø§ÙØºØ§ÙØ³ØªØ§Ùâ¬â)", "af", "93" ], [ "Albania (Shqipëri)", "al", "355" ], [ "Algeria (â«Ø§Ùجزائرâ¬â)", "dz", "213" ], [ "American Samoa", "as", "1", 5, [ "684" ] ], [ "Andorra", "ad", "376" ], [ "Angola", "ao", "244" ], [ "Anguilla", "ai", "1", 6, [ "264" ] ], [ "Antigua and Barbuda", "ag", "1", 7, [ "268" ] ], [ "Argentina", "ar", "54" ], [ "Armenia (ÕÕ¡ÕµÕ¡Õ½Õ¿Õ¡Õ¶)", "am", "374" ], [ "Aruba", "aw", "297" ], [ "Australia", "au", "61", 0 ], [ "Austria (Ãsterreich)", "at", "43" ], [ "Azerbaijan (AzÉrbaycan)", "az", "994" ], [ "Bahamas", "bs", "1", 8, [ "242" ] ], [ "Bahrain (â«Ø§ÙØ¨ØØ±ÙÙâ¬â)", "bh", "973" ], [ "Bangladesh (বাà¦à¦²à¦¾à¦¦à§à¦¶)", "bd", "880" ], [ "Barbados", "bb", "1", 9, [ "246" ] ], [ "Belarus (ÐелаÑÑÑÑ)", "by", "375" ], [ "Belgium (België)", "be", "32" ], [ "Belize", "bz", "501" ], [ "Benin (Bénin)", "bj", "229" ], [ "Bermuda", "bm", "1", 10, [ "441" ] ], [ "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", "1", 11, [ "284" ] ], [ "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, [ "3", "4", "7" ] ], [ "Cayman Islands", "ky", "1", 12, [ "345" ] ], [ "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", "1", 13, [ "767" ] ], [ "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
28aat)", "gl", "299" ], [ "Grenada", "gd", "1", 14, [ "473" ] ], [ "Guadeloupe", "gp", "590", 0 ], [ "Guam", "gu", "1", 15, [ "671" ] ], [ "Guatemala", "gt", "502" ], [ "Guernsey", "gg", "44", 1, [ "1481", "7781", "7839", "7911" ] ], [ "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, [ "1624", "74576", "7524", "7924", "7624" ] ], [ "Israel (â«×שר××â¬â)", "il", "972" ], [ "Italy (Italia)", "it", "39", 0 ], [ "Jamaica", "jm", "1", 4, [ "876", "658" ] ], [ "Japan (æ¥æ¬)", "jp", "81" ], [ "Jersey", "je", "44", 3, [ "1534", "7509", "7700", "7797", "7829", "7937" ] ], [ "Jordan (â«Ø§ÙأردÙâ¬â)", "jo", "962" ], [ "Kazakhstan (ÐÐ°Ð·Ð°Ñ ÑÑан)", "kz", "7", 1, [ "33", "7" ] ], [ "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, [ "269", "639" ] ], [ "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", "1", 16, [ "664" ] ], [ "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", "1", 17, [ "670" ] ], [ "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", "1", 18, [ "869" ] ], [ "Saint Lucia", "lc", "1", 19, [ "758" ] ], [ "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", "1", 20, [ "784" ] ], [ "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", "1", 21, [ "721" ] ], [ "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, [ "79" ] ], [ "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", "1", 22, [ "868" ] ], [ "Tunisia (â«ØªÙÙØ³â¬â)", "tn", "216" ], [ "Turkey (Türkiye)", "tr", "90" ], [ "Turkmenistan", "tm", "993" ], [ "Turks and Caicos Islands", "tc", "1", 23, [ "649" ] ], [ "Tuvalu", "tv", "688" ], [ "U.S. Virgin Islands", "vi", "1", 24, [ "340" ] ], [ "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, [ "06698" ] ], [ "Venezuela", "ve", "58" ], [ "Vietnam (Viá»t Nam)", "vn", "84" ], [ "Wallis and Futuna (Wallis-et-Futuna)", "wf", "681" ], [ "Western Sahara (â«Ø§ÙØµØØ±Ø§Ø¡ Ø§ÙØºØ±Ø¨ÙØ©â¬â)", "eh", "212", 1, [ "5288", "5289" ] ], [ "Yemen (â«Ø§ÙÙÙ Ùâ¬â)", "ye", "967" ], [ "Zambia", "zm", "260" ], [ "Zimbabwe", "zw", "263" ], [ "à land Islands", "ax", "358", 1, [ "18" ] ] ]; 29 // loop over all of the countries above, restructuring the data to be objects with named keys 30 for (var i = 0; i < allCountries.length; i++) { 31 var c = allCountries[i]; 32 allCountries[i] = { 33 name: c[0], 34 iso2: c[1], 35 dialCode: c[2], 36 priority: c[3] || 0, 37 areaCodes: c[4] || null 38 }; 39 } 40 "use strict"; 41 function _classCallCheck(instance, Constructor) { 42 if (!(instance instanceof Constructor)) { 43 throw new TypeError("Cannot call a class as a function"); 44 } 45 } 46 function _defineProperties(target, props) { 47 for (var i = 0; i < props.length; i++) { 48 var descriptor = props[i]; 49 descriptor.enumerable = descriptor.enumerable || false; 50 descriptor.configurable = true; 51 if ("value" in descriptor) descriptor.writable = true; 52 Object.defineProperty(target, descriptor.key, descriptor); 53 } 54 } 55 function _createClass(Constructor, protoProps, staticProps) { 56 if (protoProps) _defineProperties(Constructor.prototype, protoProps); 57 if (staticProps) _defineProperties(Constructor, staticProps); 58 return Constructor; 59 } 60 window.intlTelInputGlobals = { 61 getInstance: function getInstance(input) { 62 var id = input.getAttribute("data-intl-tel-input-id"); 63 return window.intlTelInputGlobals.instances[id]; 64 }, 65 instances: {} 66 }; 67 // these vars persist through all instances of the plugin 68 var id = 0; 69 var defaults = { 70 // whether or not to allow the dropdown 71 allowDropdown: true, 72 // if there is just a dial code in the input: remove it on blur 73 autoHideDialCode: true, 74 // add a placeholder in the input with an example number for the selected country 75 autoPlaceholder: "polite", 76 // modify the parentClass 77 customContainer: "", 78 // modify the auto placeholder 79 customPlaceholder: null, 80 // append menu to specified element 81 dropdownContainer: null, 82 // don't display these countries 83 excludeCountries: ['ir', 'kp', 'mm', 'cu', 'sd', 'lb'], // 1. Iran 2. North Korea. 3. Burma 4. Cuba 5. Sudan 6. Lebanon 84 // format the input value during initialisation and on setNumber 85 formatOnDisplay: true, 86 // geoIp lookup function 87 geoIpLookup: null, 88 // inject a hidden input with this name, and on submit, populate it with the result of getNumber 89 hiddenInput: "", 90 // initial country 91 initialCountry: "", 92 // localized country names e.g. { 'de': 'Deutschland' } 93 localizedCountries: null, 94 // don't insert international dial codes 95 nationalMode: true, 96 // display only these countries 97 onlyCountries: ['us', 'gb', 'ca', 'au'], //mobile phones we collect in order step 2 - email 98 // number type to use for placeholders 99 placeholderNumberType: "MOBILE", 100 // the countries at the top of the list. defaults to united states and united kingdom 101 preferredCountries: ["us"], //[ "us", "gb" ], 102 // display the country dial code next to the selected flag so it's not part of the typed number 103 separateDialCode: false, 104 // specify the path to the libphonenumber script to enable validation/formatting 105 utilsScript: "" 106 }; 107 // https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes 108 var regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ]; 109 // keep track of if the window.load event has fired as impossible to check after the fact 110 window.addEventListener("load", function() { 111 // UPDATE: use a public static field so we can fudge it in the tests 112 window.intlTelInputGlobals.windowLoaded = true; 113 }); 114 // utility function to iterate over an object. can't use Object.entries or native forEac
114h because 115 // of IE11 116 var forEachProp = function forEachProp(obj, callback) { 117 var keys = Object.keys(obj); 118 for (var i = 0; i < keys.length; i++) { 119 callback(keys[i], obj[keys[i]]); 120 } 121 }; 122 // run a method on each instance of the plugin 123 var forEachInstance = function forEachInstance(method) { 124 forEachProp(window.intlTelInputGlobals.instances, function(key) { 125 window.intlTelInputGlobals.instances[key][method](); 126 }); 127 }; 128 // this is our plugin class that we will create an instance of 129 // eslint-disable-next-line no-unused-vars 130 var Iti = /*#__PURE__*/ 131 function() { 132 function Iti(input, options) { 133 var _this = this; 134 _classCallCheck(this, Iti); 135 this.id = id++; 136 this.telInput = input; 137 this.activeItem = null; 138 this.highlightedItem = null; 139 // process specified options / defaults 140 // alternative to Object.assign, which isn't supported by IE11 141 var customOptions = options || {}; 142 this.options = {}; 143 forEachProp(defaults, function(key, value) { 144 _this.options[key] = customOptions.hasOwnProperty(key) ? customOptions[key] : value; 145 }); 146 this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder")); 147 } 148 _createClass(Iti, [ { 149 key: "_init", 150 value: function _init() { 151 var _this2 = this; 152 // if in nationalMode, disable options relating to dial codes 153 if (this.options.nationalMode) this.options.autoHideDialCode = false; 154 // if separateDialCode then doesn't make sense to A) insert dial code into input 155 // (autoHideDialCode), and B) display national numbers (because we're displaying the country 156 // dial code next to them) 157 if (this.options.separateDialCode) { 158 this.options.autoHideDialCode = this.options.nationalMode = false; 159 } 160 // we cannot just test screen size as some smartphones/website meta tags will report desktop 161 // resolutions 162 // Note: for some reason jasmine breaks if you put this in the main Plugin function with the 163 // rest of these declarations 164 // Note: to target Android Mobiles (and not Tablets), we must find 'Android' and 'Mobile' 165 this.isMobile = /Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); 166 if (this.isMobile) { 167 // trigger the mobile dropdown css 168 document.body.classList.add("iti-mobile"); 169 // on mobile, we want a full screen dropdown, so we must append it to the body 170 if (!this.options.dropdownContainer) this.options.dropdownContainer = document.body; 171 } 172 // these promises get resolved when their individual requests complete 173 // this way the dev can do something like iti.promise.then(...) to know when all requests are 174 // complete 175 if (typeof Promise !== "undefined") { 176 var autoCountryPromise = new Promise(function(resolve, reject) { 177 _this2.resolveAutoCountryPromise = resolve; 178 _this2.rejectAutoCountryPromise = reject; 179 }); 180 var utilsScriptPromise = new Promise(function(resolve, reject) { 181 _this2.resolveUtilsScriptPromise = resolve; 182 _this2.rejectUtilsScriptPromise = reject; 183 }); 184 this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]); 185 } else { 186 // prevent errors when Promise doesn't exist 187 this.resolveAutoCountryPromise = this.rejectAutoCountryPromise = function() {}; 188 this.resolveUtilsScriptPromise = this.rejectUtilsScriptPromise = function() {}; 189 } 190 // in various situations there could be no country selected initially, but we need to be able 191 // to assume this variable exists 192 this.selectedCountryData = {}; 193 // process all the data: onlyCountries, excludeCountries, preferredCountries etc 194 this._processCountryData(); 195 // generate the markup 196 this._generateMarkup(); 197 // set the initial state of the input value and the selected flag 198 this._setInitialState(); 199 // start all of the event listeners: autoHideDialCode, input keydown, selectedFlag click 200 this._initListeners(); 201 // utils script, and auto country 202 this._initRequests(); 203 } 204 }, { 205 key: "_processCountryData", 206 value: function _processCountryData() { 207 // process onlyCountries or excludeCountries array if present 208 this._processAllCountries(); 209 // process the countryCodes map 210 this._processCountryCodes(); 211 // process the preferredCountries 212 this._processPreferredCountries(); 213 // translate countries according to localizedCountries option 214 if (this.options.localizedCountries) this._translateCountriesByLocale(); 215 // sort countries by name 216 if (this.options.onlyCountries.length || this.options.localizedCountries) { 217 this.countries.sort(this._countryNameSort); 218 } 219 } 220 }, { 221 key: "_addCountryCode", 222 value: function _addCountryCode(iso2, dialCode, priority) { 223 if (dialCode.length > this.dialCodeMaxLen) { 224 this.dialCodeMaxLen = dialCode.length; 225 } 226 if (!this.countryCodes.hasOwnProperty(dialCode)) { 227 this.countryCodes[dialCode] = []; 228 } 229 // bail if we already have this country for this dialCode 230 for (var i = 0; i < this.countryCodes[dialCode].length; i++) { 231 if (this.countryCodes[dialCode][i] === iso2) return; 232 } 233 // check for undefined as 0 is falsy 234 var index = priority !== undefined ? priority : this.countryCodes[dialCode].length; 235 this.countryCodes[dialCode][index] = iso2; 236 } 237 }, { 238 key: "_processAllCountries", 239 value: function _processAllCountries() { 240 if (this.options.onlyCountries.length) { 241 var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) { 242 return country.toLowerCase(); 243 }); 244 this.countries = allCountries.filter(function(country) { 245 return lowerCaseOnlyCountries.indexOf(country.iso2) > -1; 246 }); 247 } else if (this.options.excludeCountries.length) { 248 var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) { 249 return country.toLowerCase(); 250 }); 251 this.countries = allCountries.filter(function(country) { 252 return lowerCaseExcludeCountries.indexOf(country.iso2) === -1; 253 }); 254 } else { 255 this.countries = allCountries; 256 } 257 } 258 }, { 259 key: "_translateCountriesByLocale", 260 value: function _translateCountriesByLocale() { 261 for (var i = 0; i < this.countries.length; i++) { 262 var iso = this.countries[i].iso2.toLowerCase(); 263 if (this.options.localizedCountries.hasOwnProperty(iso)) { 264 this.countries[i].name = this.options.localizedCountries[iso]; 265 } 266 } 267 } 268 }, { 269 key: "_countryNameSort", 270 value: function _countryNameSort(a, b) { 271 return a.name.localeCompare(b.name); 272 } 273 }, { 274 key: "_processCountryCodes", 275 value: function _processCountryCodes() { 276 this.dialCodeMaxLen = 0; 277 this.countryCodes = {}; 278 // first: add dial codes 279 for (var i = 0; i < this.countries.length; i++) { 280 var c = this.countries[i]; 281 this._addCountryCode(c.iso2, c.dialCode, c.priority); 282 } 283 // next: add area codes 284 // this is a second loop over countries, to make sure we have all of the "root" countries 285 // already in the map, so that we can access them, as each time we add an area code substring 286 // to the map, we also need to include the "root" country's code, as that also matches 287 for (var _i = 0; _i < this.countries.length; _i++) { 288 var _c = this.countries[_i]; 289 // area codes 290 if (_c.areaCodes) { 291 var rootCountryCode = this.countryCodes[_c.dialCode][0]; 292 // for each area code 293 for (var j = 0; j < _c.areaCodes.length; j++) { 294 var areaCode = _c.areaCodes[j]; 295 // for each digit in the area code to add all partial matches as well 296 for (var k = 1; k < areaCode.length; k++) { 297 var partialDialCode = _c.dialCode + areaCode.substr(0, k); 298 // start with the root country, as that also matches this dial code 299 this._addCountryCode(rootCountryCode, partialDialCode); 300 this._addCountryCode(_c.iso2, partialDialCode); 301 } 302 // add the full area code 303 this._addCountryCode(_c.iso2, _c.dialCode + areaCode); 304 } 305 } 306 } 307 } 308 }, { 309 key: "_processPreferredCountries", 310 value: function _processPreferredCountries() { 311 this.preferredCountries = []; 312 for (var i = 0; i < this.options.preferredCountries.length; i++) { 313 var countryCode = this.options.preferredCountries[i].toLowerCase(); 314 var countryData = this._getCountryData(countryCode, false, true); 315 if (countryData) this.preferredCountries.push(countryData); 316 } 317 } 318 }, { 319 key: "_createEl", 320 value: function _createEl(name, attrs, container) { 321 var el = document.createElement(name); 322 if (attrs) forEachProp(attrs, function(key, value) { 323 return el.setAttribute(key, value); 324 }); 325 if (container) container.appendChild(el); 326 return el; 327 } 328 }, { 329 key: "_generateMarkup", 330 value: function _generateMarkup() { 331 // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can 332 // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
333 // autocompleted number, which on submit could mean wrong number is saved (esp in nationalMode) 334 this.telInput.setAttribute("autocomplete", "off"); 335 // containers (mostly for positioning) 336 var parentClass = "iti"; 337 if (this.options.allowDropdown) parentClass += " iti--allow-dropdown"; 338 if (this.options.separateDialCode) parentClass += " iti--separate-dial-code"; 339 if (this.options.customContainer) { 340 parentClass += " "; 341 parentClass += this.options.customContainer; 342 } 343 var wrapper = this._createEl("div", { 344 "class": parentClass 345 }); 346 this.telInput.parentNode.insertBefore(wrapper, this.telInput); 347 this.flagsContainer = this._createEl("div", { 348 "class": "iti__flag-container" 349 }, wrapper); 350 wrapper.appendChild(this.telInput); 351 // selected flag (displayed to left of input) 352 this.selectedFlag = this._createEl("div", { 353 "class": "iti__selected-flag", 354 role: "combobox", 355 "aria-owns": "country-listbox" 356 }, this.flagsContainer); 357 this.selectedFlagInner = this._createEl("div", { 358 "class": "iti__flag" 359 }, this.selectedFlag); 360 if (this.options.separateDialCode) { 361 this.selectedDialCode = this._createEl("div", { 362 "class": "iti__selected-dial-code" 363 }, this.selectedFlag); 364 } 365 if (this.options.allowDropdown) { 366 // make element focusable and tab navigable 367 this.selectedFlag.setAttribute("tabindex", "0"); 368 this.dropdownArrow = this._createEl("div", { 369 "class": "iti__arrow" 370 }, this.selectedFlag); 371 // country dropdown: preferred countries, then divider, then all countries 372 this.countryList = this._createEl("ul", { 373 "class": "iti__country-list iti__hide", 374 id: "country-listbox", 375 "aria-expanded": "false", 376 role: "listbox" 377 }); 378 if (this.preferredCountries.length) { 379 this._appendListItems(this.preferredCountries, "iti__preferred"); 380 this._createEl("li", { 381 "class": "iti__divider", 382 role: "separator", 383 "aria-disabled": "true" 384 }, this.countryList); 385 } 386 this._appendListItems(this.countries, "iti__standard"); 387 // create dropdownContainer markup 388 if (this.options.dropdownContainer) { 389 this.dropdown = this._createEl("div", { 390 "class": "iti iti--container" 391 }); 392 this.dropdown.appendChild(this.countryList); 393 } else { 394 this.flagsContainer.appendChild(this.countryList); 395 } 396 } 397 if (this.options.hiddenInput) { 398 var hiddenInputName = this.options.hiddenInput; 399 var name = this.telInput.getAttribute("name"); 400 if (name) { 401 var i = name.lastIndexOf("["); 402 // if input name contains square brackets, then give the hidden input the same name, 403 // replacing the contents of the last set of brackets with the given hiddenInput name 404 if (i !== -1) hiddenInputName = "".concat(name.substr(0, i), "[").concat(hiddenInputName, "]"); 405 } 406 this.hiddenInput = this._createEl("input", { 407 type: "hidden", 408 name: hiddenInputName 409 }); 410 wrapper.appendChild(this.hiddenInput); 411 } 412 } 413 }, { 414 key: "_appendListItems", 415 value: function _appendListItems(countries, className) { 416 // we create so many DOM elements, it is faster to build a temp string 417 // and then add everything to the DOM in one go at the end 418 var tmp = ""; 419 // for each country 420 for (var i = 0; i < countries.length; i++) { 421 var c = countries[i]; 422 // open the list item 423 tmp += "<li class='iti__country ".concat(className, "' tabIndex='-1' id='iti-item-").concat(c.iso2, "' role='option' data-dial-code='").concat(c.dialCode, "' data-country-code='").concat(c.iso2, "'>"); 424 // add the flag 425 tmp += "<div class='iti__flag-box'><div class='iti__flag iti__".concat(c.iso2, "'></div></div>"); 426 // and the country name and dial code 427 tmp += "<span class='iti__country-name'>
427".concat(c.name, "</span>"); 428 tmp += "<span class='iti__dial-code'>+".concat(c.dialCode, "</span>"); 429 // close the list item 430 tmp += "</li>"; 431 } 432 this.countryList.insertAdjacentHTML("beforeend", tmp); 433 } 434 }, { 435 key: "_setInitialState", 436 value: function _setInitialState() { 437 var val = this.telInput.value; 438 var dialCode = this._getDialCode(val); 439 var isRegionlessNanp = this._isRegionlessNanp(val); 440 var _this$options = this.options, initialCountry = _this$options.initialCountry, nationalMode = _this$options.nationalMode, autoHideDialCode = _this$options.autoHideDialCode, separateDialCode = _this$options.separateDialCode; 441 // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the 442 // flag, else fall back to the default country 443 if (dialCode && !isRegionlessNanp) { 444 this._updateFlagFromNumber(val); 445 } else if (initialCountry !== "auto") { 446 // see if we should select a flag 447 if (initialCountry) { 448 this._setFlag(initialCountry.toLowerCase()); 449 } else { 450 if (dialCode && isRegionlessNanp) { 451 // has intl dial code, is regionless nanp, and no initialCountry, so default to US 452 this._setFlag("us"); 453 } else { 454 // no dial code and no initialCountry, so default to first in list 455 this.defaultCountry = this.preferredCountries.length ? this.preferredCountries[0].iso2 : this.countries[0].iso2; 456 if (!val) { 457 this._setFlag(this.defaultCountry); 458 } 459 } 460 } 461 // if empty and no nationalMode and no autoHideDialCode then insert the default dial code 462 if (!val && !nationalMode && !autoHideDialCode && !separateDialCode) { 463 this.telInput.value = "+".concat(this.selectedCountryData.dialCode); 464 } 465 } 466 // NOTE: if initialCountry is set to auto, that will be handled separately 467 // format - note this wont be run after _updateDialCode as that's only called
467if no val 468 if (val) this._updateValFromNumber(val); 469 } 470 }, { 471 key: "_initListeners", 472 value: function _initListeners() { 473 this._initKeyListeners(); 474 if (this.options.autoHideDialCode) this._initBlurListeners(); 475 if (this.options.allowDropdown) this._initDropdownListeners(); 476 if (this.hiddenInput) this._initHiddenInputListener(); 477 } 478 }, { 479 key: "_initHiddenInputListener", 480 value: function _initHiddenInputListener() { 481 var _this3 = this; 482 this._handleHiddenInputSubmit = function() { 483 _this3.hiddenInput.value = _this3.getNumber(); 484 }; 485 if (this.telInput.form) this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit); 486 } 487 }, { 488 key: "_getClosestLabel", 489 value: function _getClosestLabel() { 490 var el = this.telInput; 491 while (el && el.tagName !== "LABEL") { 492 el = el.parentNode; 493 } 494 return el; 495 } 496 }, { 497 key: "_initDropdownListeners", 498 value: function _initDropdownListeners() { 499 var _this4 = this; 500 // hack for input nested inside label (which is valid markup): clicking the selected-flag to 501 // open the dropdown would then automatically trigger a 2nd click on the input which would 502 // close it again 503 this._handleLabelClick = function(e) { 504 // if the dropdown is closed, then focus the input, else ignore the click 505 if (_this4.countryList.classList.contains("iti__hide")) _this4.telInput.focus(); else e.preventDefault(); 506 }; 507 var label = this._getClosestLabel(); 508 if (label) label.addEventListener("click", this._handleLabelClick); 509 // toggle country dropdown on click 510 this._handleClickSelectedFlag = function() { 511 // only intercept this event if we're opening the dropdown 512 // else let it bubble up to the top ("click-off-to-close" listener) 513 // we cannot just stopPropagation as it may be needed to close another instance 514 if (_this4.countryList.classList.contains("iti__hide") && !_this4.telInput.disabled && !_this4.telInput.readOnly) { 515 _this4._showDropdown(); 516 } 517 }; 518 this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag); 519 // open dropdown list if currently focused 520 this._handleFlagsContainerKeydown = function(e) { 521 var isDropdownHidden = _this4.countryList.classList.contains("iti__hide"); 522 if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].indexOf(e.key) !== -1) { 523 // prevent form from being submitted if "ENTER" was pressed 524 e.preventDefault(); 525 // prevent event from being handled again by document 526 e.stopPropagation(); 527 _this4._showDropdown(); 528 } 529 // allow navigation from dropdown to input on TAB 530 if (e.key === "Tab") _this4._closeDropdown(); 531 }; 532 this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown); 533 } 534 }, { 535 key: "_initRequests", 536 value: function _initRequests() { 537 var _this5 = this; 538 // if the user has specified the path to the utils script, fetch it on window.load, else resolve 539 if (this.options.utilsScript && !window.intlTelInputUtils) { 540 // if the plugin is being initialised after the window.load event has already been fired 541 if (window.intlTelInputGlobals.windowLoaded) { 542 window.intlTelInputGlobals.loadUtils(this.options.utilsScript); 543 } else { 544 // wait until the load event so we don't block any other requests e.g. the flags image 545 window.addEventListener("load", function() { 546 window.intlTelInputGlobals.loadUtils(_this5.options.utilsScript); 547 }); 548 } 549 } else this.resolveUtilsScriptPromise(); 550 if (this.options.initialCountry === "auto") this._loadAutoCountry(); else this.resolveAutoCountryPromise(); 551 } 552 }, { 553 key: "_loadAutoCountry", 554 value: function _loadAutoCountry() { 555 // 3 options: 556 // 1) already loaded (we're done) 557 // 2) not already started loading (start) 558 // 3) already started loading (do nothing - just wait for loading callback to fire) 559 if (window.intlTelInputGlobals.autoCountry) { 560 this.handleAutoCountry(); 561 } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) { 562 // don't do this twice! 563 window.intlTelInputGlobals.startedLoadingAutoCountry = true; 564 if (typeof this.options.geoIpLookup === "function") { 565 this.options.geoIpLookup(function(countryCode) { 566 window.intlTelInputGlobals.autoCountry = countryCode.toLowerCase(); 567 // tell all instances the auto country is ready 568 // TODO: this should just be the current instances 569 // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight 570 // away (e.g. if they have already done the geo ip lookup somewhere else). Using 571 // setTimeout means that the current thread of execution will finish before executing 572 // this, which allows the plugin to finish initialising. 573 setTimeout(function() { 574 return forEachInstance("handleAutoCountry"); 575 }); 576 }, function() { 577 return forEachInstance("rejectAutoCountryPromise"); 578 }); 579 } 580 } 581 } 582 }, { 583 key: "_initKeyListeners", 584 value: function _initKeyListeners() { 585 var _this6 = this; 586 // update flag on keyup 587 this._handleKeyupEvent = function() { 588 if (_this6._updateFlagFromNumber(_this6.telInput.value)) { 589 _this6._triggerCountryChange(); 590 } 591 }; 592 this.telInput.addEventListener("keyup", this._handleKeyupEvent); 593 // update flag on cut/paste events (now supported in all major browsers) 594 this._handleClipboardEvent = function() { 595 // hack because "paste" event is fired before input is updated 596 setTimeout(_this6._handleKeyupEvent); 597 }; 598 this.telInput.addEventListener("cut", this._handleClipboardEvent); 599 this.telInput.addEventListener("paste", this._handleClipboardEvent); 600 } 601 }, { 602 key: "_cap", 603 value: function _cap(number) { 604 var max = this.telInput.getAttribute("maxlength"); 605 return max && number.length > max ? number.substr(0, max) : number; 606 } 607 }, { 608 key: "_initBlurListeners", 609 value: function _initBlurListeners() { 610 var _this7 = this; 611 // on blur or form submit: if just a dial code then remove it 612 this._handleSubmitOrBlurEvent = function() { 613 _this7._removeEmptyDialCode(); 614 }; 615 if (this.telInput.form) this.telInput.form.addEventListener("submit", this._handleSubmitOrBlurEvent); 616 this.telInput.addEventListener("blur", this._handleSubmitOrBlurEvent); 617 } 618 }, { 619 key: "_removeEmptyDialCode", 620 value: function _removeEmptyDialCode() { 621 if (this.telInput.value.charAt(0) === "+") { 622 var numeric = this._getNumeric(this.telInput.value); 623 // if just a plus, or if just a dial code 624 if (!numeric || this.selectedCountryData.dialCode === numeric) { 625 this.telInput.value = ""; 626 } 627 } 628 } 629 }, { 630 key: "_getNumeric", 631 value: function _getNumeric(s) { 632 return s.replace(/\D/g, ""); 633 } 634 }, { 635 key: "_trigger", 636 value: function _trigger(name) {
vendor: 4,970 bytes, lines 637-716
637 // have to use old school document.createEvent as IE11 doesn't support `new Event()` syntax 638 var e = document.createEvent("Event"); 639 e.initEvent(name, true, true); 640 // can bubble, and is cancellable 641 this.telInput.dispatchEvent(e); 642 } 643 }, { 644 key: "_showDropdown", 645 value: function _showDropdown() { 646 this.countryList.classList.remove("iti__hide"); 647 this.countryList.setAttribute("aria-expanded", "true"); 648 this._setDropdownPosition(); 649 // update highlighting and scroll to active list item 650 if (this.activeItem) { 651 this._highlightListItem(this.activeItem, false); 652 this._scrollTo(this.activeItem, true); 653 } 654 // bind all the dropdown-related listeners: mouseover, click, click-off, keydown 655 this._bindDropdownListeners(); 656 // update the arrow 657 this.dropdownArrow.classList.add("iti__arrow--up"); 658 this._trigger("open:countrydropdown"); 659 } 660 }, { 661 key: "_toggleClass", 662 value: function _toggleClass(el, className, shouldHaveClass) { 663 if (shouldHaveClass && !el.classList.contains(className)) el.classList.add(className); else if (!shouldHaveClass && el.classList.contains(className)) el.classList.remove(className); 664 } 665 }, { 666 key: "_setDropdownPosition", 667 value: function _setDropdownPosition() { 668 var _this8 = this; 669 if (this.options.dropdownContainer) { 670 this.telInput.parentNode.appendChild(this.dropdown); 671 // this.options.dropdownContainer.appendChild(this.dropdown); 672 } 673 if (!this.isMobile) { 674 var pos = this.telInput.getBoundingClientRect(); 675 // windowTop from https://stackoverflow.com/a/14384091/217866 676 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 677 var inputTop = pos.top + windowTop; 678 var dropdownHeight = this.countryList.offsetHeight; 679 // dropdownFitsBelow = (dropdownBottom < windowBottom) 680 var dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight; 681 var dropdownFitsAbove = inputTop - dropdownHeight > windowTop; 682 // by default, the dropdown will be below the input. If we want to position it above the 683 // input, we add the dropup class. 684 this._toggleClass(this.countryList, "iti__country-list--dropup", !dropdownFitsBelow && dropdownFitsAbove); 685 // if dropdownContainer is enabled, calculate postion 686 if (this.options.dropdownContainer) { 687 // by default the dropdown will be directly over the input because it's not in the flow. 688 // If we want to position it below, we need to add some extra top value. 689 var extraTop = !dropdownFitsBelow && dropdownFitsAbove ? 0 : this.telInput.offsetHeight; 690 // calculate placement 691 this.dropdown.style.top = "".concat(inputTop + extraTop, "px"); 692 this.dropdown.style.left = "".concat(pos.left + document.body.scrollLeft, "px"); 693 // close menu on window scroll 694 this._handleWindowScroll = function() { 695 return _this8._closeDropdown(); 696 }; 697 window.addEventListener("scroll", this._handleWindowScroll); 698 } 699 } 700 } 701 }, { 702 key: "_getClosestListItem", 703 value: function _getClosestListItem(target) { 704 var el = target; 705 while (el && el !== this.countryList && !el.classList.contains("iti__country")) { 706 el = el.parentNode; 707 } 708 // if we reached the countryList element, then return null 709 return el === this.countryList ? null : el; 710 } 711 }, { 712 key: "_bindDropdownListeners", 713 value: function _bindDropdownListeners() { 714 var _this9 = this; 715 // when mouse over a list item, just highlight that one 716 // we add the class "highlight", so if they hit "enter" we know
716which one to select 717 this._handleMouseoverCountryList = function(e) { 718 // handle event delegation, as we're listening for this event on the countryList 719 var listItem = _this9._getClosestListItem(e.target); 720 if (listItem) _this9._highlightListItem(listItem, false); 721 }; 722 this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList); 723 // listen for country selection 724 this._handleClickCountryList = function(e) { 725 var listItem = _this9._getClosestListItem(e.target); 726 if (listItem) _this9._selectListItem(listItem); 727 }; 728 this.countryList.addEventListener("click", this._handleClickCountryList); 729 // click off to close 730 // (except when this initial opening click is bubbling up) 731 // we cannot just stopPropagation as it may be needed to close another instance 732 var isOpening = true; 733 this._handleClickOffToClose = function() { 734 if (!isOpening) _this9._closeDropdown(); 735 isOpening = false; 736 }; 737 document.documentElement.addEventListener("click", this._handleClickOffToClose); 738 // listen for up/down scrolling, enter to select, or letters to jump to country name. 739 // use keydown as keypress doesn't fire for non-char keys and we want to catch if they 740 // just hit down and hold it to scroll down (no keyup event). 741 // listen on the document because that's where key events are triggered if no input has focus 742 var query = ""; 743 var queryTimer = null; 744 this._handleKeydownOnDropdown = function(e) { 745 // prevent down key from scrolling the whole page, 746 // and enter key from submitting a form etc 747 e.preventDefault(); 748 // up and down to navigate 749 if (e.key === "ArrowUp" || e.key === "ArrowDown") _this9._handleUpDownKey(e.key); else if (e.key === "Enter") _this9._handleEnterKey(); else if (e.key === "Escape") _this9._closeDropdown(); else if (/^[a-zA-ZÃ-ÿ ]$/.test(e.key)) { 750 // jump to countries that start with the query string 751 if (queryTimer) clearTimeout(queryTimer); 752 query += e.key.toLowerCase(); 753 _this9._searchForCountry(query); 754 // if the timer hits 1 second, reset the query 755 queryTimer = setTimeout(function() { 756 query = ""; 757 }, 1e3); 758 } 759 }; 760 document.addEventListener("keydown", this._handleKeydownOnDropdown); 761 } 762 }, { 763 key: "_handleUpDownKey", 764 value: function _handleUpDownKey(key) { 765 var next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling; 766 if (next) { 767 // skip the divider 768 if (next.classList.contains("iti__divider")) { 769 next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling; 770 } 771 this._highlightListItem(next, true); 772 } 773 } 774 }, { 775 key: "_handleEnterKey", 776 value: function _handleEnterKey() { 777 if (this.highlightedItem) this._selectListItem(this.highlightedItem); 778 } 779 }, { 780 key: "_searchForCountry", 781 value: function _searchForCountry(query) { 782 for (var i = 0; i < this.countries.length; i++) { 783 if (this._startsWith(this.countries[i].name, query)) { 784 var listItem = this.countryList.querySelector("#iti-item-".concat(this.countries[i].iso2)); 785 // update highlighting and scroll 786 this._highlightListItem(listItem, false); 787 this._scrollTo(listItem, true); 788 break; 789 } 790 } 791 } 792 }, { 793 key: "_startsWith", 794 value: function _startsWith(a, b) { 795 return a.substr(0, b.length).toLowerCase() === b; 796 } 797 }, {
798 key: "_updateValFromNumber", 799 value: function _updateValFromNumber(originalNumber) { 800 var number = originalNumber; 801 if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) { 802 var useNational = !this.options.separateDialCode && (this.options.nationalMode || number.charAt(0) !== "+"); 803 var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL; 804 var format = useNational ? NATIONAL : INTERNATIONAL; 805 number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format); 806 } 807 number = this._beforeSetNumber(number); 808 this.telInput.value = number; 809 } 810 }, { 811 key: "_updateFlagFromNumber", 812 value: function _updateFlagFromNumber(originalNumber) { 813 // if we're in nationalMode and we already have US/Canada selected, make sure the number starts 814 // with a +1 so _getDialCode will be able to extract the area code 815 // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag 816 // from the number), that means we're initialising the plugin with a number that already has a 817 // dial code, so fine to ignore this bit 818 var number = originalNumber; 819 var selectedDialCode = this.selectedCountryData.dialCode; 820 var isNanp = selectedDialCode === "1"; 821 if (number && this.options.nationalMode && isNanp && number.charAt(0) !== "+") { 822 if (number.charAt(0) !== "1") number = "1".concat(number); 823 number = "+".concat(number); 824 } 825 // update flag if user types area code for another country 826 if (this.options.separateDialCode && selectedDialCode && number.charAt(0) !== "+") { 827 number = "+".concat(selectedDialCode).concat(number); 828 } 829 // try and extract valid dial code from input 830 var dialCode = this._getDialCode(number); 831 var numeric = this._getNumeric(number); 832 var countryCode = null; 833 if (dialCode) { 834 var countryCodes = this.countryCodes[this._getNumeric(dialCode)]; 835 // check if the right country is already selected. this should be false if the number is 836 // longer than the matched dial code because in this case we need to make sure that if 837 // there are multiple country matches, that the first one is selected (note: we could 838 // just check that here, but it requires the same loop that we already have later) 839 var alreadySelected = countryCodes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1; 840 var isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric); 841 // only update the flag if: 842 // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp) 843 // AND 844 // B) the right country is not already selected 845 if (!isRegionlessNanpNumber && !alreadySelected) { 846 // if using onlyCountries option, countryCodes[0] may be empty, so we must find the first 847 // non-empty index 848 for (var j = 0; j < countryCodes.length; j++) { 849 if (countryCodes[j]) { 850 countryCode = countryCodes[j]; 851 break; 852 } 853 } 854 } 855 } else if (number.charAt(0) === "+" && numeric.length) { 856 // invalid dial code, so empty 857 // Note: use getNumeric here because the number has not been formatted yet, so could contain 858 // bad chars 859 countryCode = ""; 860 } else if (!number || number === "+") { 861 // empty, or just a plus, so default 862 countryCode = this.defaultCountry; 863 } 864 if (countryCode !== null) { 865 return this._setFlag(countryCode); 866 } 867 return false;
868 } 869 }, { 870 key: "_isRegionlessNanp", 871 value: function _isRegionlessNanp(number) { 872 var numeric = this._getNumeric(number); 873 if (numeric.charAt(0) === "1") { 874 var areaCode = numeric.substr(1, 3); 875 return regionlessNanpNumbers.indexOf(areaCode) !== -1; 876 } 877 return false; 878 } 879 }, { 880 key: "_highlightListItem", 881 value: function _highlightListItem(listItem, shouldFocus) { 882 var prevItem = this.highlightedItem; 883 if (prevItem) prevItem.classList.remove("iti__highlight"); 884 this.highlightedItem = listItem; 885 this.highlightedItem.classList.add("iti__highlight"); 886 if (shouldFocus) this.highlightedItem.focus(); 887 } 888 }, { 889 key: "_getCountryData", 890 value: function _getCountryData(countryCode, ignoreOnlyCountriesOption, allowFail) { 891 var countryList = ignoreOnlyCountriesOption ? allCountries : this.countries; 892 for (var i = 0; i < countryList.length; i++) { 893 if (countryList[i].iso2 === countryCode) { 894 return countryList[i]; 895 } 896 } 897 if (allowFail) { 898 return null; 899 } 900 throw new Error("No country data for '".concat(countryCode, "'")); 901 } 902 }, { 903 key: "_setFlag", 904 value: function _setFlag(countryCode) { 905 //mobile phones we collect in order step 2 (set default)- email 906 if ('-1' == $.inArray(countryCode, this.options.onlyCountries)) { 907 countryCode = 'us'; 908 } 909 910 var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {}; 911 // do this first as it will throw an error and stop if countryCode is invalid 912 this.selectedCountryData = countryCode ? this._getCountryData(countryCode, false, false) : {}; 913 // update the defaultCountry - we only need the iso2 from now on, so just store that 914 if (this.selectedCountryData.iso2) { 915 this.defaultCountry = this.selectedCountryData.iso2; 916 } 917 this.selectedFlagInner.setAttribute("class", "iti__flag iti__".concat(countryCode)); 918 // update the selected country's title attribute 919 var title = countryCode ? "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode) : "Unknown"; 920 this.selectedFlag.setAttribute("title", title); 921 if (this.options.separateDialCode) { 922 var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : ""; 923 this.selectedDialCode.innerHTML = dialCode; 924 // offsetWidth is zero if input is in a hidden container during initialisation 925 var selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth(); 926 // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css 927 this.telInput.style.paddingLeft = "".concat(selectedFlagWidth + 6, "px"); 928 } 929 // and the input's placeholder 930 this._updatePlaceholder(); 931 // update the active list item 932 if (this.options.allowDropdown) { 933 var prevItem = this.activeItem; 934 if (prevItem) { 935 prevItem.classList.remove("iti__active"); 936 prevItem.setAttribute("aria-selected", "false"); 937 } 938 if (countryCode) { 939 var nextItem = this.countryList.querySelector("#iti-item-".concat(countryCode)); 940 nextItem.setAttribute("aria-selected", "true"); 941 nextItem.classList.add("iti__active"); 942 this.activeItem = nextItem; 943 this.countryList.setAttribute("aria-activedescendant", nextItem.getAttribute("id")); 944 } 945 } 946 // return if the flag has changed or not 947 return prevCountry.iso2 !== countryCode; 948 } 949 }, { 950 key: "_getHiddenSelectedFlagWidth", 951 value: function _getHiddenSelectedFlagWidth() { 952 // to get the right styling to apply, all we need is a shallow clone of the container, 953 // and then to inject a deep clone of the selectedFlag element 954 var containerClone = this.telInput.parentNode.cloneNode(); 955 containerClone.style.visibility = "hidden"; 956 document.body.appendChild(containerClone); 957 var selectedFlagClone = this.selectedFlag.cloneNode(true); 958 containerClone.appendChild(selectedFlagClone); 959 var width = selectedFlagClone.offsetWidth; 960 containerClone.parentNode.removeChild(containerClone); 961 return width; 962 } 963 }, { 964 key: "_updatePlaceholder", 965 value: function _updatePlaceholder() { 966 var shouldSetPlaceholder = this.options.autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && this.options.autoPlaceholder === "polite"; 967 if (window.intlTelInputUtils && shouldSetPlaceholder) { 968 var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType]; 969 var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, this.options.nationalMode, numberType) : ""; 970 placeholder = this._beforeSetNumber(placeholder); 971 if (typeof this.options.customPlaceholder === "function") { 972 placeholder = this.options.customPlaceholder(placeholder, this.selectedCountryData); 973 } 974 this.telInput.setAttribute("placeholder", placeholder); 975 } 976 } 977 }, { 978 key: "_selectListItem", 979 value: function _selectListItem(listItem) { 980 // update selected flag and active list item 981 var flagChanged = this._setFlag(listItem.getAttribute("data-country-code")); 982 this._closeDropdown(); 983 this._updateDialCode(listItem.getAttribute("data-dial-code"), true); 984 // focus the input 985 this.telInput.focus(); 986 // put cursor at end - this fix is required for FF and IE11 (with nationalMode=false i.e. auto 987 // inserting dial code), who try to put the cursor at the beginning the first time 988 var len = this.telInput.value.length; 989 this.telInput.setSelectionRange(len, len); 990 if (flagChanged) { 991 this._triggerCountryChange(); 992 } 993 } 994 }, { 995 key: "_closeDropdown", 996 value: function _closeDropdown() { 997 this.countryList.classList.add("iti__hide"); 998 this.countryList.setAttribute("aria-expanded", "false"); 999 // update the arrow 1000 this.dropdownArrow.classList.remove("iti__arrow--up"); 1001 // unbind key events 1002 document.removeEventListener("keydown", this._handleKeydownOnDropdown); 1003 document.documentElement.removeEventListener("click", this._handleClickOffToClose); 1004 this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList); 1005 this.countryList.removeEventListener("click", this._handleClickCountryList); 1006 // remove menu from container 1007 if (this.options.dropdownContainer) { 1008 if (!this.isMobile) window.removeEventListener("scroll", this._handleWindowScroll); 1009 if (this.dropdown.parentNode) this.dropdown.parentNode.removeChild(this.dropdown); 1010 } 1011 this._trigger("close:countrydropdown"); 1012 } 1013 }, { 1014 key: "_scrollTo", 1015 value: function _scrollTo(element, middle) { 1016 var container = this.countryList; 1017 // windowTop from https://stackoverflow.com/a/14384091/2
101717866 1018 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 1019 var containerHeight = container.offsetHeight; 1020 var containerTop = container.getBoundingClientRect().top + windowTop; 1021 var containerBottom = containerTop + containerHeight; 1022 var elementHeight = element.offsetHeight; 1023 var elementTop = element.getBoundingClientRect().top + windowTop; 1024 var elementBottom = elementTop + elementHeight; 1025 var newScrollTop = elementTop - containerTop + container.scrollTop; 1026 var middleOffset = containerHeight / 2 - elementHeight / 2; 1027 if (elementTop < containerTop) { 1028 // scroll up 1029 if (middle) newScrollTop -= middleOffset; 1030 container.scrollTop = newScrollTop; 1031 } else if (elementBottom > containerBottom) { 1032 // scroll down 1033 if (middle) newScrollTop += middleOffset; 1034 var heightDifference = containerHeight - elementHeight; 1035 container.scrollTop = newScrollTop - heightDifference; 1036 } 1037 } 1038 }, { 1039 key: "_updateDialCode", 1040 value: function _updateDialCode(newDialCodeBare, hasSelectedListItem) { 1041 var inputVal = this.telInput.value; 1042 // save having to pass this every time 1043 var newDialCode = "+".concat(newDialCodeBare); 1044 var newNumber; 1045 if (inputVal.charAt(0) === "+") { 1046 // there's a plus so we're dealing with a replacement (doesn't matter if nationalMode or not) 1047 var prevDialCode = this._getDialCode(inputVal); 1048 if (prevDialCode) { 1049 // current number contains a valid dial code, so replace it 1050 newNumber = inputVal.replace(prevDialCode, newDialCode); 1051 } else { 1052 // current number contains an invalid dial code, so ditch it 1053 // (no way to determine where the invalid dial code ends and the rest of the number begins) 1054 newNumber = newDialCode; 1055 } 1056 } else if (this.options.nationalMode || this.options.separateDialCode) { 1057 // don't do anything 1058 return; 1059 } else { 1060 // nationalMode is disabled 1061 if (inputVal) { 1062 // there is an existing value with no dial code: prefix the new dial code 1063 newNumber = newDialCode + inputVal; 1064 } else if (hasSelectedListItem || !this.options.autoHideDialCode) { 1065 // no existing value and either they've just selected a list item, or autoHideDialCode is 1066 // disabled: insert new dial code 1067 newNumber = newDialCode; 1068 } else { 1069 return; 1070 } 1071 } 1072 this.telInput.value = newNumber; 1073 } 1074 }, { 1075 key: "_getDialCode", 1076 value: function _getDialCode(number) { 1077 var dialCode = ""; 1078 // only interested in international numbers (starting with a plus) 1079 if (number.charAt(0) === "+") { 1080 var numericChars = ""; 1081 // iterate over chars 1082 for (var i = 0; i < number.length; i++) { 1083 var c = number.charAt(i); 1084 // if char is number (https://stackoverflow.com/a/8935649/217866) 1085 if (!isNaN(parseInt(c, 10))) { 1086 numericChars += c; 1087 // if current numericChars make a valid dial code 1088 if (this.countryCodes[numericChars]) { 1089 // store the actual raw string (useful for matching later) 1090 dialCode = number.substr(0, i + 1); 1091 } 1092 if (numericChars.length === this.dialCodeMaxLen) { 1093 break; 1094 } 1095 } 1096 } 1097 } 1098 return dialCode; 1099 } 1100 }, { 1101 key: "_getFullNumber", 1102 value: function _getFullNumber() { 1103 var val = this.telInput.value.trim(); 1104 var dialCode = this.selectedCountryData.dialCode; 1105 var prefix; 1106 var numericVal = this._getNumeric(val); 1107 if (this.options.separateDialCode && val.charAt(0) !== "+" && dialCode && numericVal) { 1108 // when using separateDialCode, it is visible so is effectively part of the typed number 1109 prefix = "+".concat(dialCode); 1110 } else { 1111 prefix = ""; 1112 } 1113 return prefix + val; 1114 } 1115 }, { 1116 key: "_beforeSetNumber", 1117 value: function _beforeSetNumber(originalNumber) { 1118 var number = originalNumber; 1119 if (this.options.separateDialCode) { 1120 var dialCode = this._getDialCode(number); 1121 // if there is a valid dial code 1122 if (dialCode) { 1123 // in case _getDialCode returned an area code as well 1124 dialCode = "+".concat(this.selectedCountryData.dialCode); 1125 // a lot of numbers will have a space separating the dial code and the main number, and 1126 // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get 1127 // rid of it 1128 // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc 1129 var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length; 1130 number = number.substr(start); 1131 } 1132 } 1133 return this._cap(number); 1134 } 1135 }, { 1136 key: "_triggerCountryChange", 1137 value: function _triggerCountryChange() { 1138 this._trigger("countrychange"); 1139 } 1140 }, { 1141 key: "handleAutoCountry", 1142 value: function handleAutoCountry() { 1143 if (this.options.initialCountry === "auto") { 1144 // we must set this even if there is an initial val in the input: in case the initial val is 1145 // invalid and they delete it - they should see their auto country 1146 this.defaultCountry = window.intlTelInputGlobals.autoCountry; 1147 // if there's no initial value in the input, then update the flag 1148 if (!this.telInput.value) { 1149 this.setCountry(this.defaultCountry); 1150 } 1151 this.resolveAutoCountryPromise(); 1152 } 1153 } 1154 }, { 1155 key: "handleUtils", 1156 value: function handleUtils() { 1157 // if the request was successful 1158 if (window.intlTelInputUtils) { 1159 // if there's an initial value in the input, then format it 1160 if (this.telInput.value) {
1161 this._updateValFromNumber(this.telInput.value); 1162 } 1163 this._updatePlaceholder(); 1164 } 1165 this.resolveUtilsScriptPromise(); 1166 } 1167 }, { 1168 key: "destroy", 1169 value: function destroy() { 1170 var form = this.telInput.form; 1171 if (this.options.allowDropdown) { 1172 // make sure the dropdown is closed (and unbind listeners) 1173 this._closeDropdown(); 1174 this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag); 1175 this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown); 1176 // label click hack 1177 var label = this._getClosestLabel(); 1178 if (label) label.removeEventListener("click", this._handleLabelClick); 1179 } 1180 // unbind hiddenInput listeners 1181 if (this.hiddenInput && form) form.removeEventListener("submit", this._handleHiddenInputSubmit); 1182 // unbind autoHideDialCode listeners 1183 if (this.options.autoHideDialCode) { 1184 if (form) form.removeEventListener("submit", this._handleSubmitOrBlurEvent); 1185 this.telInput.removeEventListener("blur", this._handleSubmitOrBlurEvent); 1186 } 1187 // unbind key events, and cut/paste events 1188 this.telInput.removeEventListener("keyup", this._handleKeyupEvent); 1189 this.telInput.removeEventListener("cut", this._handleClipboardEvent); 1190 this.telInput.removeEventListener("paste", this._handleClipboardEvent); 1191 // remove attribute of id instance: data-intl-tel-input-id 1192 this.telInput.removeAttribute("data-intl-tel-input-id"); 1193 // remove markup (but leave the original input) 1194 var wrapper = this.telInput.parentNode; 1195 wrapper.parentNode.insertBefore(this.telInput, wrapper); 1196 wrapper.parentNode.removeChild(wrapper); 1197 delete window.intlTelInputGlobals.instances[this.id]; 1198 } 1199 }, { 1200 key: "getExtension", 1201 value: function getExtension() { 1202 if (window.intlTelInputUtils) { 1203 return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2); 1204 } 1205 return ""; 1206 } 1207 }, { 1208 key: "getNumber", 1209 value: function getNumber(format) { 1210 if (window.intlTelInputUtils) { 1211 var iso2 = this.selectedCountryData.iso2; 1212 return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format); 1213 } 1214 return ""; 1215 } 1216 }, { 1217 key: "getNumberType", 1218 value: function getNumberType() { 1219 if (window.intlTelInputUtils) { 1220 return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2); 1221 } 1222 return -99; 1223 } 1224 }, { 1225 key: "getSelectedCountryData", 1226 value: function getSelectedCountryData() { 1227 return this.selectedCountryData; 1228 } 1229 }, { 1230 key: "getValidationError", 1231 value: function getValidationError() { 1232 if (window.intlTelInputUtils) { 1233 var iso2 = this.selectedCountryData.iso2; 1234 return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2); 1235 } 1236 return -99; 1237 } 1238 }, { 1239 key: "isValidNumber", 1240 value: function isValidNumber() { 1241 var val = this._getFullNumber().trim(); 1242 var countryCode = this.options.nationalMode ? this.selectedCountryData.iso2 : ""; 1243 return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, countryCode) : null; 1244 } 1245 }, { 1246 key: "setCountry", 1247 value: function setCountry(originalCountryCode) { 1248 var countryCode = originalCountryCode.toLowerCase(); 1249 // check if already selected 1250 if (!this.selectedFlagInner.classList.contains("iti__".concat(countryCode))) { 1251 this._setFlag(countryCode); 1252 this._updateDialCode(this.selectedCountryData.dialCode, false); 1253 this._triggerCountryChange(); 1254 } 1255 } 1256 }, { 1257 key: "setNumber", 1258 value: function setNumber(number) { 1259 // we must update the flag first, which updates this.selectedCountryData, which is used for 1260 // formatting the number before displaying it 1261 var flagChanged = this._updateFlagFromNumber(number);
1262 this._updateValFromNumber(number); 1263 if (flagChanged) { 1264 this._triggerCountryChange(); 1265 } 1266 } 1267 }, { 1268 key: "setPlaceholderNumberType", 1269 value: function setPlaceholderNumberType(type) { 1270 this.options.placeholderNumberType = type; 1271 this._updatePlaceholder(); 1272 } 1273 } ]); 1274 return Iti; 1275 }(); 1276 /******************** 1277 * STATIC METHODS 1278 ********************/ 1279 // get the country data object 1280 window.intlTelInputGlobals.getCountryData = function() { 1281 return allCountries; 1282 }; 1283 // inject a <script> element to load utils.js 1284 var injectScript = function injectScript(path, handleSuccess, handleFailure) { 1285 // inject a new script element into the page 1286 var script = document.createElement("script"); 1287 script.onload = function() { 1288 forEachInstance("handleUtils"); 1289 if (handleSuccess) handleSuccess(); 1290 }; 1291 script.onerror = function() { 1292 forEachInstance("rejectUtilsScriptPromise"); 1293 if (handleFailure) handleFailure(); 1294 }; 1295 script.className = "iti-load-utils"; 1296 script.async = true; 1297 script.src = path; 1298 document.body.appendChild(script); 1299 }; 1300 // load the utils script 1301 window.intlTelInputGlobals.loadUtils = function(path) { 1302 // 2 options: 1303 // 1) not already started loading (start) 1304 // 2) already started loading (do nothing - just wait for the onload callback to fire, which will 1305 // trigger handleUtils on all instances, invoking their resolveUtilsScriptPromise functions) 1306 if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) { 1307 // only do this once 1308 window.intlTelInputGlobals.startedLoadingUtilsScript = true; 1309 // if we have promises, then return a promise 1310 if (typeof Promise !== "undefined") { 1311 return new Promise(function(resolve, reject) { 1312 return injectScript(path, resolve, reject); 1313 }); 1314 } 1315 injectScript(path); 1316 } 1317 return null; 1318 }; 1319 // default options 1320 window.intlTelInputGlobals.defaults = defaults; 1321 // version 1322 window.intlTelInputGlobals.version = "16.0.5"; 1323 // convenience wrapper 1324 return function(input, options) { 1325 var iti = new Iti(input, options); 1326 iti._init(); 1327 input.setAttribute("data-intl-tel-input-id", iti.id); 1328 window.intlTelInputGlobals.instances[iti.id] = iti; 1329 return iti; 1330 }; 1331 }(); 1332});
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.