1/* 2 * International Telephone Input v15.0.2 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 "use strict"; 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", "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
28aat)", "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 ] ]; 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: [], 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: [], 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", "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 (!this.countryCodes.hasOwnProperty(dialCode)) { 224 this.countryCodes[dialCode] = []; 225 } 226 var index = priority || 0; 227 this.countryCodes[dialCode][index] = iso2; 228 } 229 }, { 230 key: "_processAllCountries", 231 value: function _processAllCountries() { 232 if (this.options.onlyCountries.length) { 233 var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) { 234 return country.toLowerCase(); 235 }); 236 this.countries = allCountries.filter(function(country) { 237 return lowerCaseOnlyCountries.indexOf(country.iso2) > -1; 238 }); 239 } else if (this.options.excludeCountries.length) { 240 var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) { 241 return country.toLowerCase(); 242 }); 243 this.countries = allCountries.filter(function(country) { 244 return lowerCaseExcludeCountries.indexOf(country.iso2) === -1; 245 }); 246 } else { 247 this.countries = allCountries; 248 } 249 } 250 }, { 251 key: "_translateCountriesByLocale", 252 value: function _translateCountriesByLocale() { 253 for (var i = 0; i < this.countries.length; i++) { 254 var iso = this.countries[i].iso2.toLowerCase(); 255 if (this.options.localizedCountries.hasOwnProperty(iso)) { 256 this.countries[i].name = this.options.localizedCountries[iso]; 257 } 258 } 259 } 260 }, { 261 key: "_countryNameSort", 262 value: function _countryNameSort(a, b) { 263 return a.name.localeCompare(b.name); 264 } 265 }, { 266 key: "_processCountryCodes", 267 value: function _processCountryCodes() { 268 this.countryCodes = {}; 269 for (var i = 0; i < this.countries.length; i++) { 270 var c = this.countries[i]; 271 this._addCountryCode(c.iso2, c.dialCode, c.priority); 272 // area codes 273 if (c.areaCodes) { 274 for (var j = 0; j < c.areaCodes.length; j++) { 275 // full dial code is country code + dial code 276 this._addCountryCode(c.iso2, c.dialCode + c.areaCodes[j]); 277 } 278 } 279 } 280 } 281 }, { 282 key: "_processPreferredCountries", 283 value: function _processPreferredCountries() { 284 this.preferredCountries = []; 285 for (var i = 0; i < this.options.preferredCountries.length; i++) { 286 var countryCode = this.options.preferredCountries[i].toLowerCase(); 287 var countryData = this._getCountryData(countryCode, false, true); 288 if (countryData) this.preferredCountries.push(countryData); 289 } 290 } 291 }, { 292 key: "_createEl", 293 value: function _createEl(name, attrs, container) { 294 var el = document.createElement(name); 295 if (attrs) forEachProp(attrs, function(key, value) { 296 return el.setAttribute(key, value); 297 }); 298 if (container) container.appendChild(el); 299 return el; 300 } 301 }, { 302 key: "_generateMarkup", 303 value: function _generateMarkup() { 304 // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can 305 // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
306 // autocompleted number, which on submit could mean wrong number is saved (esp in nationalMode) 307 this.telInput.setAttribute("autocomplete", "off"); 308 // containers (mostly for positioning) 309 var parentClass = "intl-tel-input"; 310 if (this.options.allowDropdown) parentClass += " allow-dropdown"; 311 if (this.options.separateDialCode) parentClass += " separate-dial-code"; 312 if (this.options.customContainer) { 313 parentClass += " "; 314 parentClass += this.options.customContainer; 315 } 316 var wrapper = this._createEl("div", { 317 "class": parentClass 318 }); 319 this.telInput.parentNode.insertBefore(wrapper, this.telInput); 320 this.flagsContainer = this._createEl("div", { 321 "class": "flag-container" 322 }, wrapper); 323 wrapper.appendChild(this.telInput); 324 // selected flag (displayed to left of input) 325 this.selectedFlag = this._createEl("div", { 326 "class": "selected-flag", 327 role: "combobox", 328 "aria-owns": "country-listbox" 329 }, this.flagsContainer); 330 this.selectedFlagInner = this._createEl("div", { 331 "class": "iti-flag" 332 }, this.selectedFlag); 333 if (this.options.separateDialCode) { 334 this.selectedDialCode = this._createEl("div", { 335 "class": "selected-dial-code" 336 }, this.selectedFlag); 337 } 338 if (this.options.allowDropdown) { 339 // make element focusable and tab navigable 340 this.selectedFlag.setAttribute("tabindex", "0"); 341 this.dropdownArrow = this._createEl("div", { 342 "class": "iti-arrow" 343 }, this.selectedFlag); 344 // country dropdown: preferred countries, then divider, then all countries 345 this.countryList = this._createEl("ul", { 346 "class": "country-list hide", 347 id: "country-listbox", 348 "aria-expanded": "false", 349 role: "listbox" 350 }); 351 if (this.preferredCountries.length) { 352 this._appendListItems(this.preferredCountries, "preferred"); 353 this._createEl("li", { 354 "class": "divider", 355 role: "separator", 356 "aria-disabled": "true" 357 }, this.countryList); 358 } 359 this._appendListItems(this.countries, "standard"); 360 // create dropdownContainer markup 361 if (this.options.dropdownContainer) { 362 this.dropdown = this._createEl("div", { 363 "class": "intl-tel-input iti-container" 364 }); 365 this.dropdown.appendChild(this.countryList); 366 } else { 367 this.flagsContainer.appendChild(this.countryList); 368 } 369 } 370 if (this.options.hiddenInput) { 371 var hiddenInputName = this.options.hiddenInput; 372 var name = this.telInput.getAttribute("name"); 373 if (name) { 374 var i = name.lastIndexOf("["); 375 // if input name contains square brackets, then give the hidden input the same name, 376 // replacing the contents of the last set of brackets with the given hiddenInput name 377 if (i !== -1) hiddenInputName = "".concat(name.substr(0, i), "[").concat(hiddenInputName, "]"); 378 } 379 this.hiddenInput = this._createEl("input", { 380 type: "hidden", 381 name: hiddenInputName 382 }); 383 wrapper.appendChild(this.hiddenInput); 384 } 385 } 386 }, { 387 key: "_appendListItems", 388 value: function _appendListItems(countries, className) { 389 // we create so many DOM elements, it is faster to build a temp string 390 // and then add everything to the DOM in one go at the end 391 var tmp = ""; 392 // for each country 393 for (var i = 0; i < countries.length; i++) { 394 var c = countries[i]; 395 // open the list item 396 tmp += "<li class='country ".concat(className, "' id='iti-item-").concat(c.iso2, "' role='option' data-dial-code='").concat(c.dialCode, "' data-country-code='").concat(c.iso2, "'>"); 397 // add the flag 398 tmp += "<div class='flag-box'><div class='iti-flag ".concat(c.iso2, "'></div></div>"); 399 // and the country name and dial code 400 tmp += "<span class='country-name'>
vendor: 2,691 bytes, lines 400-440
400".concat(c.name, "</span>"); 401 tmp += "<span class='dial-code'>+".concat(c.dialCode, "</span>"); 402 // close the list item 403 tmp += "</li>"; 404 } 405 this.countryList.insertAdjacentHTML("beforeend", tmp); 406 } 407 }, { 408 key: "_setInitialState", 409 value: function _setInitialState() { 410 var val = this.telInput.value; 411 var dialCode = this._getDialCode(val); 412 var isRegionlessNanp = this._isRegionlessNanp(val); 413 var _this$options = this.options, initialCountry = _this$options.initialCountry, nationalMode = _this$options.nationalMode, autoHideDialCode = _this$options.autoHideDialCode, separateDialCode = _this$options.separateDialCode; 414 // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the 415 // flag, else fall back to the default country 416 if (dialCode && !isRegionlessNanp) { 417 this._updateFlagFromNumber(val); 418 } else if (initialCountry !== "auto") { 419 // see if we should select a flag 420 if (initialCountry) { 421 this._setFlag(initialCountry.toLowerCase()); 422 } else { 423 if (dialCode && isRegionlessNanp) { 424 // has intl dial code, is regionless nanp, and no initialCountry, so default to US 425 this._setFlag("us"); 426 } else { 427 // no dial code and no initialCountry, so default to first in list 428 this.defaultCountry = this.preferredCountries.length ? this.preferredCountries[0].iso2 : this.countries[0].iso2; 429 if (!val) { 430 this._setFlag(this.defaultCountry); 431 } 432 } 433 } 434 // if empty and no nationalMode and no autoHideDialCode then insert the default dial code 435 if (!val && !nationalMode && !autoHideDialCode && !separateDialCode) { 436 this.telInput.value = "+".concat(this.selectedCountryData.dialCode); 437 } 438 } 439 // NOTE: if initialCountry is set to auto, that will be handled separately 440 // format - note this wont be run after _updateDialCode as that's only called
440if no val 441 if (val) this._updateValFromNumber(val); 442 } 443 }, { 444 key: "_initListeners", 445 value: function _initListeners() { 446 this._initKeyListeners(); 447 if (this.options.autoHideDialCode) this._initBlurListeners(); 448 if (this.options.allowDropdown) this._initDropdownListeners(); 449 if (this.hiddenInput) this._initHiddenInputListener(); 450 } 451 }, { 452 key: "_initHiddenInputListener", 453 value: function _initHiddenInputListener() { 454 var _this3 = this; 455 this._handleHiddenInputSubmit = function() { 456 _this3.hiddenInput.value = _this3.getNumber(); 457 }; 458 if (this.telInput.form) this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit); 459 } 460 }, { 461 key: "_getClosestLabel", 462 value: function _getClosestLabel() { 463 var el = this.telInput; 464 while (el && el.tagName !== "LABEL") { 465 el = el.parentNode; 466 } 467 return el; 468 } 469 }, { 470 key: "_initDropdownListeners", 471 value: function _initDropdownListeners() { 472 var _this4 = this; 473 // hack for input nested inside label (which is valid markup): clicking the selected-flag to 474 // open the dropdown would then automatically trigger a 2nd click on the input which would 475 // close it again 476 this._handleLabelClick = function(e) { 477 // if the dropdown is closed, then focus the input, else ignore the click 478 if (_this4.countryList.classList.contains("hide")) _this4.telInput.focus(); else e.preventDefault(); 479 }; 480 var label = this._getClosestLabel(); 481 if (label) label.addEventListener("click", this._handleLabelClick); 482 // toggle country dropdown on click 483 this._handleClickSelectedFlag = function() { 484 // only intercept this event if we're opening the dropdown 485 // else let it bubble up to the top ("click-off-to-close" listener) 486 // we cannot just stopPropagation as it may be needed to close another instance 487 if (_this4.countryList.classList.contains("hide") && !_this4.telInput.disabled && !_this4.telInput.readOnly) { 488 _this4._showDropdown(); 489 } 490 }; 491 this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag); 492 // open dropdown list if currently focused 493 this._handleFlagsContainerKeydown = function(e) { 494 var isDropdownHidden = _this4.countryList.classList.contains("hide"); 495 if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].indexOf(e.key) !== -1) { 496 // prevent form from being submitted if "ENTER" was pressed 497 e.preventDefault(); 498 // prevent event from being handled again by document 499 e.stopPropagation(); 500 _this4._showDropdown(); 501 } 502 // allow navigation from dropdown to input on TAB 503 if (e.key === "Tab") _this4._closeDropdown(); 504 }; 505 this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown); 506 } 507 }, { 508 key: "_initRequests", 509 value: function _initRequests() { 510 var _this5 = this; 511 // if the user has specified the path to the utils script, fetch it on window.load, else resolve 512 if (this.options.utilsScript && !window.intlTelInputUtils) { 513 // if the plugin is being initialised after the window.load event has already been fired 514 if (window.intlTelInputGlobals.windowLoaded) { 515 window.intlTelInputGlobals.loadUtils(this.options.utilsScript); 516 } else { 517 // wait until the load event so we don't block any other requests e.g. the flags image 518 window.addEventListener("load", function() { 519 window.intlTelInputGlobals.loadUtils(_this5.options.utilsScript); 520 }); 521 } 522 } else this.resolveUtilsScriptPromise(); 523 if (this.options.initialCountry === "auto") this._loadAutoCountry(); else this.resolveAutoCountryPromise(); 524 } 525 }, { 526 key: "_loadAutoCountry", 527 value: function _loadAutoCountry() { 528 // 3 options: 529 // 1) already loaded (we're done) 530 // 2) not already started loading (start) 531 // 3) already started loading (do nothing - just wait for loading callback to fire) 532 if (window.intlTelInputGlobals.autoCountry) { 533 this.handleAutoCountry(); 534 } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) { 535 // don't do this twice! 536 window.intlTelInputGlobals.startedLoadingAutoCountry = true; 537 if (typeof this.options.geoIpLookup === "function") { 538 this.options.geoIpLookup(function(countryCode) { 539 window.intlTelInputGlobals.autoCountry = countryCode.toLowerCase(); 540 // tell all instances the auto country is ready 541 // TODO: this should just be the current instances 542 // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight 543 // away (e.g. if they have already done the geo ip lookup somewhere else). Using 544 // setTimeout means that the current thread of execution will finish before executing 545 // this, which allows the plugin to finish initialising. 546 setTimeout(function() { 547 return forEachInstance("handleAutoCountry"); 548 }); 549 }, function() { 550 return forEachInstance("rejectAutoCountryPromise"); 551 }); 552 } 553 } 554 } 555 }, { 556 key: "_initKeyListeners", 557 value: function _initKeyListeners() { 558 var _this6 = this; 559 // update flag on keyup 560 this._handleKeyupEvent = function() { 561 if (_this6._updateFlagFromNumber(_this6.telInput.value)) { 562 _this6._triggerCountryChange(); 563 } 564 }; 565 this.telInput.addEventListener("keyup", this._handleKeyupEvent); 566 // update flag on cut/paste events (now supported in all major browsers) 567 this._handleClipboardEvent = function() { 568 // hack because "paste" event is fired before input is updated 569 setTimeout(_this6._handleKeyupEvent); 570 }; 571 this.telInput.addEventListener("cut", this._handleClipboardEvent); 572 this.telInput.addEventListener("paste", this._handleClipboardEvent); 573 } 574 }, { 575 key: "_cap", 576 value: function _cap(number) { 577 var max = this.telInput.getAttribute("maxlength"); 578 return max && number.length > max ? number.substr(0, max) : number; 579 } 580 }, { 581 key: "_initBlurListeners", 582 value: function _initBlurListeners() { 583 var _this7 = this; 584 // on blur or form submit: if just a dial code then remove it 585 this._handleSubmitOrBlurEvent = function() { 586 _this7._removeEmptyDialCode(); 587 }; 588 if (this.telInput.form) this.telInput.form.addEventListener("submit", this._handleSubmitOrBlurEvent); 589 this.telInput.addEventListener("blur", this._handleSubmitOrBlurEvent); 590 } 591 }, { 592 key: "_removeEmptyDialCode", 593 value: function _removeEmptyDialCode() { 594 if (this.telInput.value.charAt(0) === "+") { 595 var numeric = this._getNumeric(this.telInput.value); 596 // if just a plus, or if just a dial code 597 if (!numeric || this.selectedCountryData.dialCode === numeric) { 598 this.telInput.value = ""; 599 } 600 } 601 } 602 }, { 603 key: "_getNumeric", 604 value: function _getNumeric(s) { 605 return s.replace(/\D/g, ""); 606 } 607 }, { 608 key: "_trigger", 609 value: function _trigger(name) {
vendor: 4,836 bytes, lines 610-688
610 // have to use old school document.createEvent as IE11 doesn't support `new Event()` syntax 611 var e = document.createEvent("Event"); 612 e.initEvent(name, true, true); 613 // can bubble, and is cancellable 614 this.telInput.dispatchEvent(e); 615 } 616 }, { 617 key: "_showDropdown", 618 value: function _showDropdown() { 619 this.countryList.classList.remove("hide"); 620 this.countryList.setAttribute("aria-expanded", "true"); 621 this._setDropdownPosition(); 622 // update highlighting and scroll to active list item 623 if (this.activeItem) { 624 this._highlightListItem(this.activeItem); 625 this._scrollTo(this.activeItem); 626 } 627 // bind all the dropdown-related listeners: mouseover, click, click-off, keydown 628 this._bindDropdownListeners(); 629 // update the arrow 630 this.dropdownArrow.classList.add("up"); 631 this._trigger("open:countrydropdown"); 632 } 633 }, { 634 key: "_toggleClass", 635 value: function _toggleClass(el, className, shouldHaveClass) { 636 if (shouldHaveClass && !el.classList.contains(className)) el.classList.add(className); else if (!shouldHaveClass && el.classList.contains(className)) el.classList.remove(className); 637 } 638 }, { 639 key: "_setDropdownPosition", 640 value: function _setDropdownPosition() { 641 var _this8 = this; 642 if (this.options.dropdownContainer) { 643 this.options.dropdownContainer.appendChild(this.dropdown); 644 } 645 if (!this.isMobile) { 646 var pos = this.telInput.getBoundingClientRect(); 647 // windowTop from https://stackoverflow.com/a/14384091/217866 648 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 649 var inputTop = pos.top + windowTop; 650 var dropdownHeight = this.countryList.offsetHeight; 651 // dropdownFitsBelow = (dropdownBottom < windowBottom) 652 var dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight; 653 var dropdownFitsAbove = inputTop - dropdownHeight > windowTop; 654 // by default, the dropdown will be below the input. If we want to position it above the 655 // input, we add the dropup class. 656 this._toggleClass(this.countryList, "dropup", !dropdownFitsBelow && dropdownFitsAbove); 657 // if dropdownContainer is enabled, calculate postion 658 if (this.options.dropdownContainer) { 659 // by default the dropdown will be directly over the input because it's not in the flow. 660 // If we want to position it below, we need to add some extra top value. 661 var extraTop = !dropdownFitsBelow && dropdownFitsAbove ? 0 : this.telInput.offsetHeight; 662 // calculate placement 663 this.dropdown.style.top = "".concat(inputTop + extraTop, "px"); 664 this.dropdown.style.left = "".concat(pos.left + document.body.scrollLeft, "px"); 665 // close menu on window scroll 666 this._handleWindowScroll = function() { 667 return _this8._closeDropdown(); 668 }; 669 window.addEventListener("scroll", this._handleWindowScroll); 670 } 671 } 672 } 673 }, { 674 key: "_getClosestListItem", 675 value: function _getClosestListItem(target) { 676 var el = target; 677 while (el && el !== this.countryList && !el.classList.contains("country")) { 678 el = el.parentNode; 679 } 680 // if we reached the countryList element, then return null 681 return el === this.countryList ? null : el; 682 } 683 }, { 684 key: "_bindDropdownListeners", 685 value: function _bindDropdownListeners() { 686 var _this9 = this; 687 // when mouse over a list item, just highlight that one 688 // we add the class "highlight", so if they hit "enter" we know
688which one to select 689 this._handleMouseoverCountryList = function(e) { 690 // handle event delegation, as we're listening for this event on the countryList 691 var listItem = _this9._getClosestListItem(e.target); 692 if (listItem) _this9._highlightListItem(listItem); 693 }; 694 this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList); 695 // listen for country selection 696 this._handleClickCountryList = function(e) { 697 var listItem = _this9._getClosestListItem(e.target); 698 if (listItem) _this9._selectListItem(listItem); 699 }; 700 this.countryList.addEventListener("click", this._handleClickCountryList); 701 // click off to close 702 // (except when this initial opening click is bubbling up) 703 // we cannot just stopPropagation as it may be needed to close another instance 704 var isOpening = true; 705 this._handleClickOffToClose = function() { 706 if (!isOpening) _this9._closeDropdown(); 707 isOpening = false; 708 }; 709 document.documentElement.addEventListener("click", this._handleClickOffToClose); 710 // listen for up/down scrolling, enter to select, or letters to jump to country name. 711 // use keydown as keypress doesn't fire for non-char keys and we want to catch if they 712 // just hit down and hold it to scroll down (no keyup event). 713 // listen on the document because that's where key events are triggered if no input has focus 714 var query = ""; 715 var queryTimer = null; 716 this._handleKeydownOnDropdown = function(e) { 717 // prevent down key from scrolling the whole page, 718 // and enter key from submitting a form etc 719 e.preventDefault(); 720 // up and down to navigate 721 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)) { 722 // jump to countries that start with the query string 723 if (queryTimer) clearTimeout(queryTimer); 724 query += e.key.toLowerCase(); 725 _this9._searchForCountry(query); 726 // if the timer hits 1 second, reset the query 727 queryTimer = setTimeout(function() { 728 query = ""; 729 }, 1e3); 730 } 731 }; 732 document.addEventListener("keydown", this._handleKeydownOnDropdown); 733 } 734 }, { 735 key: "_handleUpDownKey", 736 value: function _handleUpDownKey(key) { 737 var next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling; 738 if (next) { 739 // skip the divider 740 if (next.classList.contains("divider")) { 741 next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling; 742 } 743 this._highlightListItem(next); 744 this._scrollTo(next); 745 } 746 } 747 }, { 748 key: "_handleEnterKey", 749 value: function _handleEnterKey() { 750 if (this.highlightedItem) this._selectListItem(this.highlightedItem); 751 } 752 }, { 753 key: "_searchForCountry", 754 value: function _searchForCountry(query) { 755 for (var i = 0; i < this.countries.length; i++) { 756 if (this._startsWith(this.countries[i].name, query)) { 757 var listItem = this.countryList.querySelector("#iti-item-".concat(this.countries[i].iso2)); 758 // update highlighting and scroll 759 this._highlightListItem(listItem); 760 this._scrollTo(listItem, true); 761 break; 762 } 763 } 764 } 765 }, { 766 key: "_startsWith", 767 value: function _startsWith(a, b) { 768 return a.substr(0, b.length).toLowerCase() === b; 769 } 770 }, {
771 key: "_updateValFromNumber", 772 value: function _updateValFromNumber(originalNumber) { 773 var number = originalNumber; 774 if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) { 775 var useNational = !this.options.separateDialCode && (this.options.nationalMode || number.charAt(0) !== "+"); 776 var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL; 777 var format = useNational ? NATIONAL : INTERNATIONAL; 778 number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format); 779 } 780 number = this._beforeSetNumber(number); 781 this.telInput.value = number; 782 } 783 }, { 784 key: "_updateFlagFromNumber", 785 value: function _updateFlagFromNumber(originalNumber) { 786 // if we're in nationalMode and we already have US/Canada selected, make sure the number starts 787 // with a +1 so _getDialCode will be able to extract the area code 788 // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag 789 // from the number), that means we're initialising the plugin with a number that already has a 790 // dial code, so fine to ignore this bit 791 var number = originalNumber; 792 var isNanp = this.selectedCountryData.dialCode === "1"; 793 if (number && this.options.nationalMode && isNanp && number.charAt(0) !== "+") { 794 if (number.charAt(0) !== "1") number = "1".concat(number); 795 number = "+".concat(number); 796 } 797 // try and extract valid dial code from input 798 var dialCode = this._getDialCode(number); 799 var numeric = this._getNumeric(number); 800 var countryCode = null; 801 if (dialCode) { 802 // check if one of the matching countries is already selected 803 var countryCodes = this.countryCodes[this._getNumeric(dialCode)]; 804 var alreadySelected = countryCodes.indexOf(this.selectedCountryData.iso2) !== -1; 805 // check if the given number contains a NANP area code i.e. the only dialCode that could be 806 // extracted was +1 (instead of say +1204) and the actual number's length is >=4 807 var isNanpAreaCode = dialCode === "+1" && numeric.length >= 4; 808 var isRegionlessNanpNumber = this.selectedCountryData.dialCode === "1" && this._isRegionlessNanp(numeric); 809 // only update the flag if: 810 // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp) 811 // AND 812 // B) either a matching country is not already selected OR the number contains a NANP area 813 // code (ensure the flag is set to the first matching country) 814 if (!isRegionlessNanpNumber && (!alreadySelected || isNanpAreaCode)) { 815 // if using onlyCountries option, countryCodes[0] may be empty, so we must find the first 816 // non-empty index 817 for (var j = 0; j < countryCodes.length; j++) { 818 if (countryCodes[j]) { 819 countryCode = countryCodes[j]; 820 break; 821 } 822 } 823 } 824 } else if (number.charAt(0) === "+" && numeric.length) { 825 // invalid dial code, so empty 826 // Note: use getNumeric here because the number has not been formatted yet, so could contain 827 // bad chars 828 countryCode = ""; 829 } else if (!number || number === "+") { 830 // empty, or just a plus, so default 831 countryCode = this.defaultCountry; 832 } 833 if (countryCode !== null) { 834 return this._setFlag(countryCode); 835 } 836 return false;
837 } 838 }, { 839 key: "_isRegionlessNanp", 840 value: function _isRegionlessNanp(number) { 841 var numeric = this._getNumeric(number); 842 if (numeric.charAt(0) === "1") { 843 var areaCode = numeric.substr(1, 3); 844 return regionlessNanpNumbers.indexOf(areaCode) !== -1; 845 } 846 return false; 847 } 848 }, { 849 key: "_highlightListItem", 850 value: function _highlightListItem(listItem) { 851 var prevItem = this.highlightedItem; 852 if (prevItem) prevItem.classList.remove("highlight"); 853 this.highlightedItem = listItem; 854 this.highlightedItem.classList.add("highlight"); 855 } 856 }, { 857 key: "_getCountryData", 858 value: function _getCountryData(countryCode, ignoreOnlyCountriesOption, allowFail) { 859 var countryList = ignoreOnlyCountriesOption ? allCountries : this.countries; 860 for (var i = 0; i < countryList.length; i++) { 861 if (countryList[i].iso2 === countryCode) { 862 return countryList[i]; 863 } 864 } 865 if (allowFail) { 866 return null; 867 } 868 throw new Error("No country data for '".concat(countryCode, "'")); 869 } 870 }, { 871 key: "_setFlag", 872 value: function _setFlag(countryCode) { 873 var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {}; 874 // do this first as it will throw an error and stop if countryCode is invalid 875 this.selectedCountryData = countryCode ? this._getCountryData(countryCode, false, false) : {}; 876 // update the defaultCountry - we only need the iso2 from now on, so just store that 877 if (this.selectedCountryData.iso2) { 878 this.defaultCountry = this.selectedCountryData.iso2; 879 } 880 this.selectedFlagInner.setAttribute("class", "iti-flag ".concat(countryCode)); 881 // update the selected country's title attribute 882 var title = countryCode ? "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode) : "Unknown"; 883 this.selectedFlag.setAttribute("title", title); 884 if (this.options.separateDialCode) { 885 var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : ""; 886 this.selectedDialCode.innerHTML = dialCode; 887 // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css 888 this.telInput.style.paddingLeft = "".concat(this.selectedFlag.offsetWidth + 6, "px"); 889 } 890 // and the input's placeholder 891 this._updatePlaceholder(); 892 // update the active list item 893 if (this.options.allowDropdown) { 894 var prevItem = this.activeItem; 895 if (prevItem) { 896 prevItem.classList.remove("active"); 897 prevItem.setAttribute("aria-selected", "false"); 898 } 899 if (countryCode) { 900 var nextItem = this.countryList.querySelector("#iti-item-".concat(countryCode)); 901 nextItem.setAttribute("aria-selected", "true"); 902 nextItem.classList.add("active"); 903 this.activeItem = nextItem; 904 this.countryList.setAttribute("aria-activedescendant", nextItem.getAttribute("id")); 905 } 906 } 907 // return if the flag has changed or not 908 return prevCountry.iso2 !== countryCode; 909 } 910 }, { 911 key: "_updatePlaceholder", 912 value: function _updatePlaceholder() { 913 var shouldSetPlaceholder = this.options.autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && this.options.autoPlaceholder === "polite"; 914 if (window.intlTelInputUtils && shouldSetPlaceholder) { 915 var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType]; 916 var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, this.options.nationalMode, numberType) : ""; 917 placeholder = this._beforeSetNumber(placeholder); 918 if (typeof this.options.customPlaceholder === "function") { 919 placeholder = this.options.customPlaceholder(placeholder, this.selectedCountryData); 920 } 921 this.telInput.setAttribute("placeholder", placeholder); 922 } 923 } 924 }, { 925 key: "_selectListItem", 926 value: function _selectListItem(listItem) { 927 // update selected flag and active list item 928 var flagChanged = this._setFlag(listItem.getAttribute("data-country-code")); 929 this._closeDropdown(); 930 this._updateDialCode(listItem.getAttribute("data-dial-code"), true); 931 // focus the input 932 this.telInput.focus(); 933 // put cursor at end - this fix is required for FF and IE11 (with nationalMode=false i.e. auto 934 // inserting dial code), who try to put the cursor at the beginning the first time 935 var len = this.telInput.value.length; 936 this.telInput.setSelectionRange(len, len); 937 if (flagChanged) { 938 this._triggerCountryChange(); 939 } 940 } 941 }, { 942 key: "_closeDropdown", 943 value: function _closeDropdown() { 944 this.countryList.classList.add("hide"); 945 this.countryList.setAttribute("aria-expanded", "false"); 946 // update the arrow 947 this.dropdownArrow.classList.remove("up"); 948 // unbind key events 949 document.removeEventListener("keydown", this._handleKeydownOnDropdown); 950 document.documentElement.removeEventListener("click", this._handleClickOffToClose); 951 this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList); 952 this.countryList.removeEventListener("click", this._handleClickCountryList); 953 // remove menu from container 954 if (this.options.dropdownContainer) { 955 if (!this.isMobile) window.removeEventListener("scroll", this._handleWindowScroll); 956 if (this.dropdown.parentNode) this.dropdown.parentNode.removeChild(this.dropdown); 957 } 958 this._trigger("close:countrydropdown"); 959 } 960 }, { 961 key: "_scrollTo", 962 value: function _scrollTo(element, middle) { 963 var container = this.countryList; 964 // windowTop from https://stackoverflow.com/a/14384091/2
96417866 965 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 966 var containerHeight = container.offsetHeight; 967 var containerTop = container.getBoundingClientRect().top + windowTop; 968 var containerBottom = containerTop + containerHeight; 969 var elementHeight = element.offsetHeight; 970 var elementTop = element.getBoundingClientRect().top + windowTop; 971 var elementBottom = elementTop + elementHeight; 972 var newScrollTop = elementTop - containerTop + container.scrollTop; 973 var middleOffset = containerHeight / 2 - elementHeight / 2; 974 if (elementTop < containerTop) { 975 // scroll up 976 if (middle) newScrollTop -= middleOffset; 977 container.scrollTop = newScrollTop; 978 } else if (elementBottom > containerBottom) { 979 // scroll down 980 if (middle) newScrollTop += middleOffset; 981 var heightDifference = containerHeight - elementHeight; 982 container.scrollTop = newScrollTop - heightDifference; 983 } 984 } 985 }, { 986 key: "_updateDialCode", 987 value: function _updateDialCode(newDialCodeBare, hasSelectedListItem) { 988 var inputVal = this.telInput.value; 989 // save having to pass this every time 990 var newDialCode = "+".concat(newDialCodeBare); 991 var newNumber; 992 if (inputVal.charAt(0) === "+") { 993 // there's a plus so we're dealing with a replacement (doesn't matter if nationalMode or not) 994 var prevDialCode = this._getDialCode(inputVal); 995 if (prevDialCode) { 996 // current number contains a valid dial code, so replace it 997 newNumber = inputVal.replace(prevDialCode, newDialCode); 998 } else { 999 // current number contains an invalid dial code, so ditch it 1000 // (no way to determine where the invalid dial code ends and the rest of the number begins) 1001 newNumber = newDialCode; 1002 } 1003 } else if (this.options.nationalMode || this.options.separateDialCode) { 1004 // don't do anything 1005 return; 1006 } else { 1007 // nationalMode is disabled 1008 if (inputVal) { 1009 // there is an existing value with no dial code: prefix the new dial code 1010 newNumber = newDialCode + inputVal; 1011 } else if (hasSelectedListItem || !this.options.autoHideDialCode) { 1012 // no existing value and either they've just selected a list item, or autoHideDialCode is 1013 // disabled: insert new dial code 1014 newNumber = newDialCode; 1015 } else { 1016 return; 1017 } 1018 } 1019 this.telInput.value = newNumber; 1020 } 1021 }, { 1022 key: "_getDialCode", 1023 value: function _getDialCode(number) { 1024 var dialCode = ""; 1025 // only interested in international numbers (starting with a plus) 1026 if (number.charAt(0) === "+") { 1027 var numericChars = ""; 1028 // iterate over chars 1029 for (var i = 0; i < number.length; i++) { 1030 var c = number.charAt(i); 1031 // if char is number (https://stackoverflow.com/a/8935649/217866) 1032 if (!isNaN(parseInt(c, 10))) { 1033 numericChars += c; 1034 // if current numericChars make a valid dial code 1035 if (this.countryCodes[numericChars]) { 1036 // store the actual raw string (useful for matching later) 1037 dialCode = number.substr(0, i + 1); 1038 } 1039 // longest dial code is 4 chars 1040 if (numericChars.length === 4) { 1041 break; 1042 } 1043 } 1044 } 1045 } 1046 return dialCode; 1047 } 1048 }, { 1049 key: "_getFullNumber", 1050 value: function _getFullNumber() { 1051 var val = this.telInput.value.trim(); 1052 var dialCode = this.selectedCountryData.dialCode; 1053 var prefix; 1054 var numericVal = this._getNumeric(val); 1055 // normalized means ensure starts with a 1, so we can match against the full dial code 1056 var normalizedVal = numericVal.charAt(0) === "1" ? numericVal : "1".concat(numericVal); 1057 if (this.options.separateDialCode && val.charAt(0) !== "+") { 1058 // when using separateDialCode, it is visible so is effectively part of the typed number 1059 prefix = "+".concat(dialCode); 1060 } else if (val && val.charAt(0) !== "+" && val.charAt(0) !== "1" && dialCode && dialCode.charAt(0) === "1" && dialCode.length === 4 && dialCode !== normalizedVal.substr(0, 4)) { 1061 // ensure national NANP numbers contain the area code 1062 prefix = dialCode.substr(1); 1063 } else { 1064 prefix = ""; 1065 } 1066 return prefix + val; 1067 } 1068 }, { 1069 key: "_beforeSetNumber", 1070 value: function _beforeSetNumber(originalNumber) { 1071 var number = originalNumber; 1072 if (this.options.separateDialCode) { 1073 var dialCode = this._getDialCode(number); 1074 if (dialCode) { 1075 // US dialCode is "+1", which is what we want 1076 // CA dialCode is "+1 123", which is wrong - should be "+1" (as it has multiple area codes) 1077 // AS dialCode is "+1 684", which is what we want (as it doesn't have area codes) 1078 // Solution: if the country has area codes, then revert to just the dial code 1079 if (this.selectedCountryData.areaCodes !== null) { 1080 dialCode = "+".concat(this.selectedCountryData.dialCode); 1081 } 1082 // a lot of numbers will have a space separating the dial code and the main number, and 1083 // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get 1084 // rid of it 1085 // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc 1086 var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length; 1087 number = number.substr(start); 1088 } 1089 } 1090 return this._cap(number); 1091 } 1092 }, { 1093 key: "_triggerCountryChange", 1094 value: function _triggerCountryChange() { 1095 this._trigger("countrychange"); 1096 } 1097 }, { 1098 key: "handleAutoCountry", 1099 value: function handleAutoCountry() { 1100 if (this.options.initialCountry === "auto") { 1101 // we must set this even if there is an initial val in the input: in case the initial val is 1102 // invalid and they delete it - they should see their auto country 1103 this.defaultCountry = window.intlTelInputGlobals.autoCountry; 1104 // if there's no initial value in the input, then update the flag 1105 if (!this.telInput.value) { 1106 this.setCountry(this.defaultCountry); 1107 } 1108 this.resolveAutoCountryPromise(); 1109 } 1110 } 1111 }, { 1112 key: "handleUtils", 1113 value: function handleUtils() { 1114 // if the request was successful 1115 if (window.intlTelInputUtils) { 1116 // if there's an initial value in the input, then format it 1117 if (this.telInput.value) {
1118 this._updateValFromNumber(this.telInput.value); 1119 } 1120 this._updatePlaceholder(); 1121 } 1122 this.resolveUtilsScriptPromise(); 1123 } 1124 }, { 1125 key: "destroy", 1126 value: function destroy() { 1127 var form = this.telInput.form; 1128 if (this.options.allowDropdown) { 1129 // make sure the dropdown is closed (and unbind listeners) 1130 this._closeDropdown(); 1131 this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag); 1132 this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown); 1133 // label click hack 1134 var label = this._getClosestLabel(); 1135 if (label) label.removeEventListener("click", this._handleLabelClick); 1136 } 1137 // unbind hiddenInput listeners 1138 if (this.hiddenInput && form) form.removeEventListener("submit", this._handleHiddenInputSubmit); 1139 // unbind autoHideDialCode listeners 1140 if (this.options.autoHideDialCode) { 1141 if (form) form.removeEventListener("submit", this._handleSubmitOrBlurEvent); 1142 this.telInput.removeEventListener("blur", this._handleSubmitOrBlurEvent); 1143 } 1144 // unbind key events, and cut/paste events 1145 this.telInput.removeEventListener("keyup", this._handleKeyupEvent); 1146 this.telInput.removeEventListener("cut", this._handleClipboardEvent); 1147 this.telInput.removeEventListener("paste", this._handleClipboardEvent); 1148 // remove attribute of id instance: data-intl-tel-input-id 1149 this.telInput.removeAttribute("data-intl-tel-input-id"); 1150 // remove markup (but leave the original input) 1151 var wrapper = this.telInput.parentNode; 1152 wrapper.parentNode.insertBefore(this.telInput, wrapper); 1153 wrapper.parentNode.removeChild(wrapper); 1154 delete window.intlTelInputGlobals.instances[this.id]; 1155 } 1156 }, { 1157 key: "getExtension", 1158 value: function getExtension() { 1159 if (window.intlTelInputUtils) { 1160 return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2); 1161 } 1162 return ""; 1163 } 1164 }, { 1165 key: "getNumber", 1166 value: function getNumber(format) { 1167 if (window.intlTelInputUtils) { 1168 var iso2 = this.selectedCountryData.iso2; 1169 return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format); 1170 } 1171 return ""; 1172 } 1173 }, { 1174 key: "getNumberType", 1175 value: function getNumberType() { 1176 if (window.intlTelInputUtils) { 1177 return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2); 1178 } 1179 return -99; 1180 } 1181 }, { 1182 key: "getSelectedCountryData", 1183 value: function getSelectedCountryData() { 1184 return this.selectedCountryData; 1185 } 1186 }, { 1187 key: "getValidationError", 1188 value: function getValidationError() { 1189 if (window.intlTelInputUtils) { 1190 var iso2 = this.selectedCountryData.iso2; 1191 return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2); 1192 } 1193 return -99; 1194 } 1195 }, { 1196 key: "isValidNumber", 1197 value: function isValidNumber() { 1198 var val = this._getFullNumber().trim(); 1199 var countryCode = this.options.nationalMode ? this.selectedCountryData.iso2 : ""; 1200 return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, countryCode) : null; 1201 } 1202 }, { 1203 key: "setCountry", 1204 value: function setCountry(originalCountryCode) { 1205 var countryCode = originalCountryCode.toLowerCase(); 1206 // check if already selected 1207 if (!this.selectedFlagInner.classList.contains(countryCode)) { 1208 this._setFlag(countryCode); 1209 this._updateDialCode(this.selectedCountryData.dialCode, false); 1210 this._triggerCountryChange(); 1211 } 1212 } 1213 }, { 1214 key: "setNumber", 1215 value: function setNumber(number) { 1216 // we must update the flag first, which updates this.selectedCountryData, which is used for 1217 // formatting the number before displaying it 1218 var flagChanged = this._updateFlagFromNumber(number);
1219 this._updateValFromNumber(number); 1220 if (flagChanged) { 1221 this._triggerCountryChange(); 1222 } 1223 } 1224 }, { 1225 key: "setPlaceholderNumberType", 1226 value: function setPlaceholderNumberType(type) { 1227 this.options.placeholderNumberType = type; 1228 this._updatePlaceholder(); 1229 } 1230 } ]); 1231 return Iti; 1232 }(); 1233 /******************** 1234 * STATIC METHODS 1235 ********************/ 1236 // get the country data object 1237 window.intlTelInputGlobals.getCountryData = function() { 1238 return allCountries; 1239 }; 1240 // inject a <script> element to load utils.js 1241 var injectScript = function injectScript(path, handleSuccess, handleFailure) { 1242 // inject a new script element into the page 1243 var script = document.createElement("script"); 1244 script.onload = function() { 1245 forEachInstance("handleUtils"); 1246 if (handleSuccess) handleSuccess(); 1247 }; 1248 script.onerror = function() { 1249 forEachInstance("rejectUtilsScriptPromise"); 1250 if (handleFailure) handleFailure(); 1251 }; 1252 script.className = "iti-load-utils"; 1253 script.async = true; 1254 script.src = path; 1255 document.body.appendChild(script); 1256 }; 1257 // load the utils script 1258 window.intlTelInputGlobals.loadUtils = function(path) { 1259 // 2 options: 1260 // 1) not already started loading (start) 1261 // 2) already started loading (do nothing - just wait for the onload callback to fire, which will 1262 // trigger handleUtils on all instances, invoking their resolveUtilsScriptPromise functions) 1263 if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) { 1264 // only do this once 1265 window.intlTelInputGlobals.startedLoadingUtilsScript = true; 1266 // if we have promises, then return a promise 1267 if (typeof Promise !== "undefined") { 1268 return new Promise(function(resolve, reject) { 1269 return injectScript(path, resolve, reject); 1270 }); 1271 } 1272 injectScript(path); 1273 } 1274 return null; 1275 }; 1276 // default options 1277 window.intlTelInputGlobals.defaults = defaults; 1278 // version 1279 window.intlTelInputGlobals.version = "15.0.2"; 1280 // convenience wrapper 1281 return function(input, options) { 1282 var iti = new Iti(input, options); 1283 iti._init(); 1284 input.setAttribute("data-intl-tel-input-id", iti.id); 1285 window.intlTelInputGlobals.instances[iti.id] = iti; 1286 return iti; 1287 }; 1288 }(); 1289});
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.