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