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