1/* 2 * International Telephone Input v16.0.8 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", "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: [], 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
vendor: 12,493 bytes, lines 114-332
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'>
vendor: 2,696 bytes, lines 427-467
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", "Up", "ArrowDown", "Down", " ", "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,890 bytes, lines 637-715
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.options.dropdownContainer.appendChild(this.dropdown); 671 } 672 if (!this.isMobile) { 673 var pos = this.telInput.getBoundingClientRect(); 674 // windowTop from https://stackoverflow.com/a/14384091/217866 675 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 676 var inputTop = pos.top + windowTop; 677 var dropdownHeight = this.countryList.offsetHeight; 678 // dropdownFitsBelow = (dropdownBottom < windowBottom) 679 var dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight; 680 var dropdownFitsAbove = inputTop - dropdownHeight > windowTop; 681 // by default, the dropdown will be below the input. If we want to position it above the 682 // input, we add the dropup class. 683 this._toggleClass(this.countryList, "iti__country-list--dropup", !dropdownFitsBelow && dropdownFitsAbove); 684 // if dropdownContainer is enabled, calculate postion 685 if (this.options.dropdownContainer) { 686 // by default the dropdown will be directly over the input because it's not in the flow. 687 // If we want to position it below, we need to add some extra top value. 688 var extraTop = !dropdownFitsBelow && dropdownFitsAbove ? 0 : this.telInput.offsetHeight; 689 // calculate placement 690 this.dropdown.style.top = "".concat(inputTop + extraTop, "px"); 691 this.dropdown.style.left = "".concat(pos.left + document.body.scrollLeft, "px"); 692 // close menu on window scroll 693 this._handleWindowScroll = function() { 694 return _this8._closeDropdown(); 695 }; 696 window.addEventListener("scroll", this._handleWindowScroll); 697 } 698 } 699 } 700 }, { 701 key: "_getClosestListItem", 702 value: function _getClosestListItem(target) { 703 var el = target; 704 while (el && el !== this.countryList && !el.classList.contains("iti__country")) { 705 el = el.parentNode; 706 } 707 // if we reached the countryList element, then return null 708 return el === this.countryList ? null : el; 709 } 710 }, { 711 key: "_bindDropdownListeners", 712 value: function _bindDropdownListeners() { 713 var _this9 = this; 714 // when mouse over a list item, just highlight that one 715 // we add the class "highlight", so if they hit "enter" we know
715which one to select 716 this._handleMouseoverCountryList = function(e) { 717 // handle event delegation, as we're listening for this event on the countryList 718 var listItem = _this9._getClosestListItem(e.target); 719 if (listItem) _this9._highlightListItem(listItem, false); 720 }; 721 this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList); 722 // listen for country selection 723 this._handleClickCountryList = function(e) { 724 var listItem = _this9._getClosestListItem(e.target); 725 if (listItem) _this9._selectListItem(listItem); 726 }; 727 this.countryList.addEventListener("click", this._handleClickCountryList); 728 // click off to close 729 // (except when this initial opening click is bubbling up) 730 // we cannot just stopPropagation as it may be needed to close another instance 731 var isOpening = true; 732 this._handleClickOffToClose = function() { 733 if (!isOpening) _this9._closeDropdown(); 734 isOpening = false; 735 }; 736 document.documentElement.addEventListener("click", this._handleClickOffToClose); 737 // listen for up/down scrolling, enter to select, or letters to jump to country name. 738 // use keydown as keypress doesn't fire for non-char keys and we want to catch if they 739 // just hit down and hold it to scroll down (no keyup event). 740 // listen on the document because that's where key events are triggered if no input has focus 741 var query = ""; 742 var queryTimer = null; 743 this._handleKeydownOnDropdown = function(e) { 744 // prevent down key from scrolling the whole page, 745 // and enter key from submitting a form etc 746 e.preventDefault(); 747 // up and down to navigate 748 if (e.key === "ArrowUp" || e.key === "Up" || e.key === "ArrowDown" || e.key === "Down") _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)) { 749 // jump to countries that start with the query string 750 if (queryTimer) clearTimeout(queryTimer); 751 query += e.key.toLowerCase(); 752 _this9._searchForCountry(query); 753 // if the timer hits 1 second, reset the query 754 queryTimer = setTimeout(function() { 755 query = ""; 756 }, 1e3); 757 } 758 }; 759 document.addEventListener("keydown", this._handleKeydownOnDropdown); 760 } 761 }, { 762 key: "_handleUpDownKey", 763 value: function _handleUpDownKey(key) { 764 var next = key === "ArrowUp" || key === "Up" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling; 765 if (next) { 766 // skip the divider 767 if (next.classList.contains("iti__divider")) { 768 next = key === "ArrowUp" || key === "Up" ? next.previousElementSibling : next.nextElementSibling; 769 } 770 this._highlightListItem(next, true); 771 } 772 } 773 }, { 774 key: "_handleEnterKey", 775 value: function _handleEnterKey() { 776 if (this.highlightedItem) this._selectListItem(this.highlightedItem); 777 } 778 }, { 779 key: "_searchForCountry", 780 value: function _searchForCountry(query) { 781 for (var i = 0; i < this.countries.length; i++) { 782 if (this._startsWith(this.countries[i].name, query)) { 783 var listItem = this.countryList.querySelector("#iti-item-".concat(this.countries[i].iso2)); 784 // update highlighting and scroll 785 this._highlightListItem(listItem, false); 786 this._scrollTo(listItem, true); 787 break; 788 } 789 } 790 } 791 }, { 792 key: "_startsWith", 793 value: function _startsWith(a, b) { 794 return a.substr(0, b.length).toLowerCase() === b; 795 } 796 }, {
vendor: 4,876 bytes, lines 797-866
797 key: "_updateValFromNumber", 798 value: function _updateValFromNumber(originalNumber) { 799 var number = originalNumber; 800 if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) { 801 var useNational = !this.options.separateDialCode && (this.options.nationalMode || number.charAt(0) !== "+"); 802 var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL; 803 var format = useNational ? NATIONAL : INTERNATIONAL; 804 number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format); 805 } 806 number = this._beforeSetNumber(number); 807 this.telInput.value = number; 808 } 809 }, { 810 key: "_updateFlagFromNumber", 811 value: function _updateFlagFromNumber(originalNumber) { 812 // if we're in nationalMode and we already have US/Canada selected, make sure the number starts 813 // with a +1 so _getDialCode will be able to extract the area code 814 // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag 815 // from the number), that means we're initialising the plugin with a number that already has a 816 // dial code, so fine to ignore this bit 817 var number = originalNumber; 818 var selectedDialCode = this.selectedCountryData.dialCode; 819 var isNanp = selectedDialCode === "1"; 820 if (number && this.options.nationalMode && isNanp && number.charAt(0) !== "+") { 821 if (number.charAt(0) !== "1") number = "1".concat(number); 822 number = "+".concat(number); 823 } 824 // update flag if user types area code for another country 825 if (this.options.separateDialCode && selectedDialCode && number.charAt(0) !== "+") { 826 number = "+".concat(selectedDialCode).concat(number); 827 } 828 // try and extract valid dial code from input 829 var dialCode = this._getDialCode(number); 830 var numeric = this._getNumeric(number); 831 var countryCode = null; 832 if (dialCode) { 833 var countryCodes = this.countryCodes[this._getNumeric(dialCode)]; 834 // check if the right country is already selected. this should be false if the number is 835 // longer than the matched dial code because in this case we need to make sure that if 836 // there are multiple country matches, that the first one is selected (note: we could 837 // just check that here, but it requires the same loop that we already have later) 838 var alreadySelected = countryCodes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1; 839 var isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric); 840 // only update the flag if: 841 // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp) 842 // AND 843 // B) the right country is not already selected 844 if (!isRegionlessNanpNumber && !alreadySelected) { 845 // if using onlyCountries option, countryCodes[0] may be empty, so we must find the first 846 // non-empty index 847 for (var j = 0; j < countryCodes.length; j++) { 848 if (countryCodes[j]) { 849 countryCode = countryCodes[j]; 850 break; 851 } 852 } 853 } 854 } else if (number.charAt(0) === "+" && numeric.length) { 855 // invalid dial code, so empty 856 // Note: use getNumeric here because the number has not been formatted yet, so could contain 857 // bad chars 858 countryCode = ""; 859 } else if (!number || number === "+") { 860 // empty, or just a plus, so default 861 countryCode = this.defaultCountry; 862 } 863 if (countryCode !== null) { 864 return this._setFlag(countryCode); 865 } 866 return false;
867 } 868 }, { 869 key: "_isRegionlessNanp", 870 value: function _isRegionlessNanp(number) { 871 var numeric = this._getNumeric(number); 872 if (numeric.charAt(0) === "1") { 873 var areaCode = numeric.substr(1, 3); 874 return regionlessNanpNumbers.indexOf(areaCode) !== -1; 875 } 876 return false; 877 } 878 }, { 879 key: "_highlightListItem", 880 value: function _highlightListItem(listItem, shouldFocus) { 881 var prevItem = this.highlightedItem; 882 if (prevItem) prevItem.classList.remove("iti__highlight"); 883 this.highlightedItem = listItem; 884 this.highlightedItem.classList.add("iti__highlight"); 885 if (shouldFocus) this.highlightedItem.focus(); 886 } 887 }, { 888 key: "_getCountryData", 889 value: function _getCountryData(countryCode, ignoreOnlyCountriesOption, allowFail) { 890 var countryList = ignoreOnlyCountriesOption ? allCountries : this.countries; 891 for (var i = 0; i < countryList.length; i++) { 892 if (countryList[i].iso2 === countryCode) { 893 return countryList[i]; 894 } 895 } 896 if (allowFail) { 897 return null; 898 } 899 throw new Error("No country data for '".concat(countryCode, "'")); 900 } 901 }, { 902 key: "_setFlag", 903 value: function _setFlag(countryCode) { 904 var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {}; 905 // do this first as it will throw an error and stop if countryCode is invalid 906 this.selectedCountryData = countryCode ? this._getCountryData(countryCode, false, false) : {}; 907 // update the defaultCountry - we only need the iso2 from now on, so just store that 908 if (this.selectedCountryData.iso2) { 909 this.defaultCountry = this.selectedCountryData.iso2; 910 } 911 this.selectedFlagInner.setAttribute("class", "iti__flag iti__".concat(countryCode)); 912 // update the selected country's title attribute 913 var title = countryCode ? "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode) : "Unknown"; 914 this.selectedFlag.setAttribute("title", title); 915 if (this.options.separateDialCode) { 916 var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : ""; 917 this.selectedDialCode.innerHTML = dialCode; 918 // offsetWidth is zero if input is in a hidden container during initialisation 919 var selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth(); 920 // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css 921 this.telInput.style.paddingLeft = "".concat(selectedFlagWidth + 6, "px"); 922 } 923 // and the input's placeholder 924 this._updatePlaceholder(); 925 // update the active list item 926 if (this.options.allowDropdown) { 927 var prevItem = this.activeItem; 928 if (prevItem) { 929 prevItem.classList.remove("iti__active"); 930 prevItem.setAttribute("aria-selected", "false"); 931 } 932 if (countryCode) { 933 var nextItem = this.countryList.querySelector("#iti-item-".concat(countryCode)); 934 nextItem.setAttribute("aria-selected", "true"); 935 nextItem.classList.add("iti__active"); 936 this.activeItem = nextItem; 937 this.countryList.setAttribute("aria-activedescendant", nextItem.getAttribute("id")); 938 } 939 } 940 // return if the flag has changed or not 941 return prevCountry.iso2 !== countryCode; 942 } 943 }, { 944 key: "_getHiddenSelectedFlagWidth", 945 value: function _getHiddenSelectedFlagWidth() { 946 // to get the right styling to apply, all we need is a shallow clone of the container, 947 // and then to inject a deep clone of the selectedFlag element 948 var containerClone = this.telInput.parentNode.cloneNode(); 949 containerClone.style.visibility = "hidden"; 950 document.body.appendChild(containerClone); 951 var selectedFlagClone = this.selectedFlag.cloneNode(true); 952 containerClone.appendChild(selectedFlagClone); 953 var width = selectedFlagClone.offsetWidth; 954 containerClone.parentNode.removeChild(containerClone); 955 return width; 956 } 957 }, { 958 key: "_updatePlaceholder", 959 value: function _updatePlaceholder() { 960 var shouldSetPlaceholder = this.options.autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && this.options.autoPlaceholder === "polite"; 961 if (window.intlTelInputUtils && shouldSetPlaceholder) { 962 var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType]; 963 var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, this.options.nationalMode, numberType) : ""; 964 placeholder = this._beforeSetNumber(placeholder); 965 if (typeof this.options.customPlaceholder === "function") { 966 placeholder = this.options.customPlaceholder(placeholder, this.selectedCountryData); 967 } 968 this.telInput.setAttribute("placeholder", placeholder); 969 } 970 } 971 }, { 972 key: "_selectListItem", 973 value: function _selectListItem(listItem) { 974 // update selected flag and active list item 975 var flagChanged = this._setFlag(listItem.getAttribute("data-country-code")); 976 this._closeDropdown(); 977 this._updateDialCode(listItem.getAttribute("data-dial-code"), true); 978 // focus the input 979 this.telInput.focus(); 980 // put cursor at end - this fix is required for FF and IE11 (with nationalMode=false i.e. auto 981 // inserting dial code), who try to put the cursor at the beginning the first time 982 var len = this.telInput.value.length; 983 this.telInput.setSelectionRange(len, len); 984 if (flagChanged) { 985 this._triggerCountryChange(); 986 } 987 } 988 }, { 989 key: "_closeDropdown", 990 value: function _closeDropdown() { 991 this.countryList.classList.add("iti__hide"); 992 this.countryList.setAttribute("aria-expanded", "false"); 993 // update the arrow 994 this.dropdownArrow.classList.remove("iti__arrow--up"); 995 // unbind key events 996 document.removeEventListener("keydown", this._handleKeydownOnDropdown); 997 document.documentElement.removeEventListener("click", this._handleClickOffToClose); 998 this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList); 999 this.countryList.removeEventListener("click", this._handleClickCountryList); 1000 // remove menu from container 1001 if (this.options.dropdownContainer) { 1002 if (!this.isMobile) window.removeEventListener("scroll", this._handleWindowScroll); 1003 if (this.dropdown.parentNode) this.dropdown.parentNode.removeChild(this.dropdown); 1004 } 1005 this._trigger("close:countrydropdown"); 1006 } 1007 }, { 1008 key: "_scrollTo", 1009 value: function _scrollTo(element, middle) { 1010 var container = this.countryList; 1011 // windowTop from https://stackoverflow.com/a/14384091/2
101117866 1012 var windowTop = window.pageYOffset || document.documentElement.scrollTop; 1013 var containerHeight = container.offsetHeight; 1014 var containerTop = container.getBoundingClientRect().top + windowTop; 1015 var containerBottom = containerTop + containerHeight; 1016 var elementHeight = element.offsetHeight; 1017 var elementTop = element.getBoundingClientRect().top + windowTop; 1018 var elementBottom = elementTop + elementHeight; 1019 var newScrollTop = elementTop - containerTop + container.scrollTop; 1020 var middleOffset = containerHeight / 2 - elementHeight / 2; 1021 if (elementTop < containerTop) { 1022 // scroll up 1023 if (middle) newScrollTop -= middleOffset; 1024 container.scrollTop = newScrollTop; 1025 } else if (elementBottom > containerBottom) { 1026 // scroll down 1027 if (middle) newScrollTop += middleOffset; 1028 var heightDifference = containerHeight - elementHeight; 1029 container.scrollTop = newScrollTop - heightDifference; 1030 } 1031 } 1032 }, { 1033 key: "_updateDialCode", 1034 value: function _updateDialCode(newDialCodeBare, hasSelectedListItem) { 1035 var inputVal = this.telInput.value; 1036 // save having to pass this every time 1037 var newDialCode = "+".concat(newDialCodeBare); 1038 var newNumber; 1039 if (inputVal.charAt(0) === "+") { 1040 // there's a plus so we're dealing with a replacement (doesn't matter if nationalMode or not) 1041 var prevDialCode = this._getDialCode(inputVal); 1042 if (prevDialCode) { 1043 // current number contains a valid dial code, so replace it 1044 newNumber = inputVal.replace(prevDialCode, newDialCode); 1045 } else { 1046 // current number contains an invalid dial code, so ditch it 1047 // (no way to determine where the invalid dial code ends and the rest of the number begins) 1048 newNumber = newDialCode; 1049 } 1050 } else if (this.options.nationalMode || this.options.separateDialCode) { 1051 // don't do anything 1052 return; 1053 } else { 1054 // nationalMode is disabled 1055 if (inputVal) { 1056 // there is an existing value with no dial code: prefix the new dial code 1057 newNumber = newDialCode + inputVal; 1058 } else if (hasSelectedListItem || !this.options.autoHideDialCode) { 1059 // no existing value and either they've just selected a list item, or autoHideDialCode is 1060 // disabled: insert new dial code 1061 newNumber = newDialCode; 1062 } else { 1063 return; 1064 } 1065 } 1066 this.telInput.value = newNumber; 1067 } 1068 }, { 1069 key: "_getDialCode", 1070 value: function _getDialCode(number) { 1071 var dialCode = ""; 1072 // only interested in international numbers (starting with a plus) 1073 if (number.charAt(0) === "+") { 1074 var numericChars = ""; 1075 // iterate over chars 1076 for (var i = 0; i < number.length; i++) { 1077 var c = number.charAt(i); 1078 // if char is number (https://stackoverflow.com/a/8935649/217866) 1079 if (!isNaN(parseInt(c, 10))) { 1080 numericChars += c; 1081 // if current numericChars make a valid dial code 1082 if (this.countryCodes[numericChars]) { 1083 // store the actual raw string (useful for matching later) 1084 dialCode = number.substr(0, i + 1); 1085 } 1086 if (numericChars.length === this.dialCodeMaxLen) { 1087 break; 1088 } 1089 } 1090 } 1091 } 1092 return dialCode; 1093 } 1094 }, { 1095 key: "_getFullNumber", 1096 value: function _getFullNumber() { 1097 var val = this.telInput.value.trim(); 1098 var dialCode = this.selectedCountryData.dialCode; 1099 var prefix; 1100 var numericVal = this._getNumeric(val); 1101 if (this.options.separateDialCode && val.charAt(0) !== "+" && dialCode && numericVal) { 1102 // when using separateDialCode, it is visible so is effectively part of the typed number 1103 prefix = "+".concat(dialCode); 1104 } else { 1105 prefix = ""; 1106 } 1107 return prefix + val; 1108 } 1109 }, { 1110 key: "_beforeSetNumber", 1111 value: function _beforeSetNumber(originalNumber) { 1112 var number = originalNumber; 1113 if (this.options.separateDialCode) { 1114 var dialCode = this._getDialCode(number); 1115 // if there is a valid dial code 1116 if (dialCode) { 1117 // in case _getDialCode returned an area code as well 1118 dialCode = "+".concat(this.selectedCountryData.dialCode); 1119 // a lot of numbers will have a space separating the dial code and the main number, and 1120 // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get 1121 // rid of it 1122 // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc 1123 var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length; 1124 number = number.substr(start); 1125 } 1126 } 1127 return this._cap(number); 1128 } 1129 }, { 1130 key: "_triggerCountryChange", 1131 value: function _triggerCountryChange() { 1132 this._trigger("countrychange"); 1133 } 1134 }, { 1135 key: "handleAutoCountry", 1136 value: function handleAutoCountry() { 1137 if (this.options.initialCountry === "auto") { 1138 // we must set this even if there is an initial val in the input: in case the initial val is 1139 // invalid and they delete it - they should see their auto country 1140 this.defaultCountry = window.intlTelInputGlobals.autoCountry; 1141 // if there's no initial value in the input, then update the flag 1142 if (!this.telInput.value) { 1143 this.setCountry(this.defaultCountry); 1144 } 1145 this.resolveAutoCountryPromise(); 1146 } 1147 } 1148 }, { 1149 key: "handleUtils", 1150 value: function handleUtils() { 1151 // if the request was successful 1152 if (window.intlTelInputUtils) { 1153 // if there's an initial value in the input, then format it 1154 if (this.telInput.value) {
1155 this._updateValFromNumber(this.telInput.value); 1156 } 1157 this._updatePlaceholder(); 1158 } 1159 this.resolveUtilsScriptPromise(); 1160 } 1161 }, { 1162 key: "destroy", 1163 value: function destroy() { 1164 var form = this.telInput.form; 1165 if (this.options.allowDropdown) { 1166 // make sure the dropdown is closed (and unbind listeners) 1167 this._closeDropdown(); 1168 this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag); 1169 this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown); 1170 // label click hack 1171 var label = this._getClosestLabel(); 1172 if (label) label.removeEventListener("click", this._handleLabelClick); 1173 } 1174 // unbind hiddenInput listeners 1175 if (this.hiddenInput && form) form.removeEventListener("submit", this._handleHiddenInputSubmit); 1176 // unbind autoHideDialCode listeners 1177 if (this.options.autoHideDialCode) { 1178 if (form) form.removeEventListener("submit", this._handleSubmitOrBlurEvent); 1179 this.telInput.removeEventListener("blur", this._handleSubmitOrBlurEvent); 1180 } 1181 // unbind key events, and cut/paste events 1182 this.telInput.removeEventListener("keyup", this._handleKeyupEvent); 1183 this.telInput.removeEventListener("cut", this._handleClipboardEvent); 1184 this.telInput.removeEventListener("paste", this._handleClipboardEvent); 1185 // remove attribute of id instance: data-intl-tel-input-id 1186 this.telInput.removeAttribute("data-intl-tel-input-id"); 1187 // remove markup (but leave the original input) 1188 var wrapper = this.telInput.parentNode; 1189 wrapper.parentNode.insertBefore(this.telInput, wrapper); 1190 wrapper.parentNode.removeChild(wrapper); 1191 delete window.intlTelInputGlobals.instances[this.id]; 1192 } 1193 }, { 1194 key: "getExtension", 1195 value: function getExtension() { 1196 if (window.intlTelInputUtils) { 1197 return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2); 1198 } 1199 return ""; 1200 } 1201 }, { 1202 key: "getNumber", 1203 value: function getNumber(format) { 1204 if (window.intlTelInputUtils) { 1205 var iso2 = this.selectedCountryData.iso2; 1206 return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format); 1207 } 1208 return ""; 1209 } 1210 }, { 1211 key: "getNumberType", 1212 value: function getNumberType() { 1213 if (window.intlTelInputUtils) { 1214 return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2); 1215 } 1216 return -99; 1217 } 1218 }, { 1219 key: "getSelectedCountryData", 1220 value: function getSelectedCountryData() { 1221 return this.selectedCountryData; 1222 } 1223 }, { 1224 key: "getValidationError", 1225 value: function getValidationError() { 1226 if (window.intlTelInputUtils) { 1227 var iso2 = this.selectedCountryData.iso2; 1228 return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2); 1229 } 1230 return -99; 1231 } 1232 }, { 1233 key: "isValidNumber", 1234 value: function isValidNumber() { 1235 var val = this._getFullNumber().trim(); 1236 var countryCode = this.options.nationalMode ? this.selectedCountryData.iso2 : ""; 1237 return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, countryCode) : null; 1238 } 1239 }, { 1240 key: "setCountry", 1241 value: function setCountry(originalCountryCode) { 1242 var countryCode = originalCountryCode.toLowerCase(); 1243 // check if already selected 1244 if (!this.selectedFlagInner.classList.contains("iti__".concat(countryCode))) { 1245 this._setFlag(countryCode); 1246 this._updateDialCode(this.selectedCountryData.dialCode, false); 1247 this._triggerCountryChange(); 1248 } 1249 } 1250 }, { 1251 key: "setNumber", 1252 value: function setNumber(number) { 1253 // we must update the flag first, which updates this.selectedCountryData, which is used for 1254 // formatting the number before displaying it 1255 var flagChanged = this._updateFlagFromNumber(number);
1256 this._updateValFromNumber(number); 1257 if (flagChanged) { 1258 this._triggerCountryChange(); 1259 } 1260 } 1261 }, { 1262 key: "setPlaceholderNumberType", 1263 value: function setPlaceholderNumberType(type) { 1264 this.options.placeholderNumberType = type; 1265 this._updatePlaceholder(); 1266 } 1267 } ]); 1268 return Iti; 1269 }(); 1270 /******************** 1271 * STATIC METHODS 1272 ********************/ 1273 // get the country data object 1274 window.intlTelInputGlobals.getCountryData = function() { 1275 return allCountries; 1276 }; 1277 // inject a <script> element to load utils.js 1278 var injectScript = function injectScript(path, handleSuccess, handleFailure) { 1279 // inject a new script element into the page 1280 var script = document.createElement("script"); 1281 script.onload = function() { 1282 forEachInstance("handleUtils"); 1283 if (handleSuccess) handleSuccess(); 1284 }; 1285 script.onerror = function() { 1286 forEachInstance("rejectUtilsScriptPromise"); 1287 if (handleFailure) handleFailure(); 1288 }; 1289 script.className = "iti-load-utils"; 1290 script.async = true; 1291 script.src = path; 1292 document.body.appendChild(script); 1293 }; 1294 // load the utils script 1295 window.intlTelInputGlobals.loadUtils = function(path) { 1296 // 2 options: 1297 // 1) not already started loading (start) 1298 // 2) already started loading (do nothing - just wait for the onload callback to fire, which will 1299 // trigger handleUtils on all instances, invoking their resolveUtilsScriptPromise functions) 1300 if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) { 1301 // only do this once 1302 window.intlTelInputGlobals.startedLoadingUtilsScript = true; 1303 // if we have promises, then return a promise 1304 if (typeof Promise !== "undefined") { 1305 return new Promise(function(resolve, reject) { 1306 return injectScript(path, resolve, reject); 1307 }); 1308 } 1309 injectScript(path); 1310 } 1311 return null; 1312 }; 1313 // default options 1314 window.intlTelInputGlobals.defaults = defaults; 1315 // version 1316 window.intlTelInputGlobals.version = "16.0.8"; 1317 // convenience wrapper 1318 return function(input, options) { 1319 var iti = new Iti(input, options); 1320 iti._init(); 1321 input.setAttribute("data-intl-tel-input-id", iti.id); 1322 window.intlTelInputGlobals.instances[iti.id] = iti; 1323 return iti; 1324 }; 1325 }(); 1326});
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.