intl-tel-input 20.3.0 → 21.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1691 +1,2878 @@
1
1
  /*
2
- * International Telephone Input v20.3.0
2
+ * International Telephone Input v21.0.0
3
3
  * https://github.com/jackocnr/intl-tel-input.git
4
4
  * Licensed under the MIT license
5
5
  */
6
- // wrap in UMD
6
+
7
+ // UMD
7
8
  (function(factory) {
8
- if (typeof module === "object" && module.exports) module.exports = factory(); else window.intlTelInput = factory();
9
- })(function(undefined) {
10
- "use strict";
11
- return function() {
12
- // Array of country objects for the flag dropdown.
13
- // Here is the criteria for the plugin to support a given country/territory
14
- // - It has an iso2 code: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
15
- // - 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
16
- // - It has a flag in the region-flags project: https://github.com/behdad/region-flags/tree/gh-pages/png
17
- // - It is supported by libphonenumber (it must be listed on this page): https://github.com/googlei18n/libphonenumber/blob/master/resources/ShortNumberMetadata.xml
18
- // Each country array has the following information:
19
- // [
20
- // Country name,
21
- // iso2 code,
22
- // International dial code,
23
- // Order (if >1 country with same dial code),
24
- // Area codes
25
- // ]
26
- var allCountries = [ [ "Afghanistan", "af", "93" ], [ "Albania", "al", "355" ], [ "Algeria", "dz", "213" ], [ "American Samoa", "as", "1", 5, [ "684" ] ], [ "Andorra", "ad", "376" ], [ "Angola", "ao", "244" ], [ "Anguilla", "ai", "1", 6, [ "264" ] ], [ "Antigua & Barbuda", "ag", "1", 7, [ "268" ] ], [ "Argentina", "ar", "54" ], [ "Armenia", "am", "374" ], [ "Aruba", "aw", "297" ], [ "Ascension Island", "ac", "247" ], [ "Australia", "au", "61", 0 ], [ "Austria", "at", "43" ], [ "Azerbaijan", "az", "994" ], [ "Bahamas", "bs", "1", 8, [ "242" ] ], [ "Bahrain", "bh", "973" ], [ "Bangladesh", "bd", "880" ], [ "Barbados", "bb", "1", 9, [ "246" ] ], [ "Belarus", "by", "375" ], [ "Belgium", "be", "32" ], [ "Belize", "bz", "501" ], [ "Benin", "bj", "229" ], [ "Bermuda", "bm", "1", 10, [ "441" ] ], [ "Bhutan", "bt", "975" ], [ "Bolivia", "bo", "591" ], [ "Bosnia & Herzegovina", "ba", "387" ], [ "Botswana", "bw", "267" ], [ "Brazil", "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", "bi", "257" ], [ "Cambodia", "kh", "855" ], [ "Cameroon", "cm", "237" ], [ "Canada", "ca", "1", 1, [ "204", "226", "236", "249", "250", "263", "289", "306", "343", "354", "365", "367", "368", "382", "387", "403", "416", "418", "428", "431", "437", "438", "450", "584", "468", "474", "506", "514", "519", "548", "579", "581", "584", "587", "604", "613", "639", "647", "672", "683", "705", "709", "742", "753", "778", "780", "782", "807", "819", "825", "867", "873", "902", "905" ] ], [ "Cape Verde", "cv", "238" ], [ "Caribbean Netherlands", "bq", "599", 1, [ "3", "4", "7" ] ], [ "Cayman Islands", "ky", "1", 12, [ "345" ] ], [ "Central African Republic", "cf", "236" ], [ "Chad", "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 - Brazzaville", "cg", "242" ], [ "Congo - Kinshasa", "cd", "243" ], [ "Cook Islands", "ck", "682" ], [ "Costa Rica", "cr", "506" ], [ "Côte d’Ivoire", "ci", "225" ], [ "Croatia", "hr", "385" ], [ "Cuba", "cu", "53" ], [ "Curaçao", "cw", "599", 0 ], [ "Cyprus", "cy", "357" ], [ "Czech Republic", "cz", "420" ], [ "Denmark", "dk", "45" ], [ "Djibouti", "dj", "253" ], [ "Dominica", "dm", "1", 13, [ "767" ] ], [ "Dominican Republic", "do", "1", 2, [ "809", "829", "849" ] ], [ "Ecuador", "ec", "593" ], [ "Egypt", "eg", "20" ], [ "El Salvador", "sv", "503" ], [ "Equatorial Guinea", "gq", "240" ], [ "Eritrea", "er", "291" ], [ "Estonia", "ee", "372" ], [ "Eswatini", "sz", "268" ], [ "Ethiopia", "et", "251" ], [ "Falkland Islands", "fk", "500" ], [ "Faroe Islands", "fo", "298" ], [ "Fiji", "fj", "679" ], [ "Finland", "fi", "358", 0 ], [ "France", "fr", "33" ], [ "French Guiana", "gf", "594" ], [ "French Polynesia", "pf", "689" ], [ "Gabon", "ga", "241" ], [ "Gambia", "gm", "220" ], [ "Georgia", "ge", "995" ], [ "Germany", "de", "49" ], [ "Ghana", "gh", "233" ], [ "Gibraltar", "gi", "350" ], [ "Greece", "gr", "30" ], [ "Greenland", "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", "gn", "224" ], [ "Guinea-Bissau", "gw", "245" ], [ "Guyana", "gy", "592" ], [ "Haiti", "ht", "509" ], [ "Honduras", "hn", "504" ], [ "Hong Kong", "hk", "852" ], [ "Hungary", "hu", "36" ], [ "Iceland", "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", "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", "lv", "371" ], [ "Lebanon", "lb", "961" ], [ "Lesotho", "ls", "266" ], [ "Liberia", "lr", "231" ], [ "Libya", "ly", "218" ], [ "Liechtenstein", "li", "423" ], [ "Lithuania", "lt", "370" ], [ "Luxembourg", "lu", "352" ], [ "Macau", "mo", "853" ], [ "Madagascar", "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", "mu", "230" ], [ "Mayotte", "yt", "262", 1, [ "269", "639" ] ], [ "Mexico", "mx", "52" ], [ "Micronesia", "fm", "691" ], [ "Moldova", "md", "373" ], [ "Monaco", "mc", "377" ], [ "Mongolia", "mn", "976" ], [ "Montenegro", "me", "382" ], [ "Montserrat", "ms", "1", 16, [ "664" ] ], [ "Morocco", "ma", "212", 0 ], [ "Mozambique", "mz", "258" ], [ "Myanmar (Burma)", "mm", "95" ], [ "Namibia", "na", "264" ], [ "Nauru", "nr", "674" ], [ "Nepal", "np", "977" ], [ "Netherlands", "nl", "31" ], [ "New Caledonia", "nc", "687" ], [ "New Zealand", "nz", "64" ], [ "Nicaragua", "ni", "505" ], [ "Niger", "ne", "227" ], [ "Nigeria", "ng", "234" ], [ "Niue", "nu", "683" ], [ "Norfolk Island", "nf", "672" ], [ "North Korea", "kp", "850" ], [ "North Macedonia", "mk", "389" ], [ "Northern Mariana Islands", "mp", "1", 17, [ "670" ] ], [ "Norway", "no", "47", 0 ], [ "Oman", "om", "968" ], [ "Pakistan", "pk", "92" ], [ "Palau", "pw", "680" ], [ "Palestine", "ps", "970" ], [ "Panama", "pa", "507" ], [ "Papua New Guinea", "pg", "675" ], [ "Paraguay", "py", "595" ], [ "Peru", "pe", "51" ], [ "Philippines", "ph", "63" ], [ "Poland", "pl", "48" ], [ "Portugal", "pt", "351" ], [ "Puerto Rico", "pr", "1", 3, [ "787", "939" ] ], [ "Qatar", "qa", "974" ], [ "Réunion", "re", "262", 0 ], [ "Romania", "ro", "40" ], [ "Russia", "ru", "7", 0 ], [ "Rwanda", "rw", "250" ], [ "Samoa", "ws", "685" ], [ "San Marino", "sm", "378" ], [ "São Tomé & Príncipe", "st", "239" ], [ "Saudi Arabia", "sa", "966" ], [ "Senegal", "sn", "221" ], [ "Serbia", "rs", "381" ], [ "Seychelles", "sc", "248" ], [ "Sierra Leone", "sl", "232" ], [ "Singapore", "sg", "65" ], [ "Sint Maarten", "sx", "1", 21, [ "721" ] ], [ "Slovakia", "sk", "421" ], [ "Slovenia", "si", "386" ], [ "Solomon Islands", "sb", "677" ], [ "Somalia", "so", "252" ], [ "South Africa", "za", "27" ], [ "South Korea", "kr", "82" ], [ "South Sudan", "ss", "211" ], [ "Spain", "es", "34" ], [ "Sri Lanka", "lk", "94" ], [ "St Barthélemy", "bl", "590", 1 ], [ "St Helena", "sh", "290" ], [ "St Kitts & Nevis", "kn", "1", 18, [ "869" ] ], [ "St Lucia", "lc", "1", 19, [ "758" ] ], [ "St Martin", "mf", "590", 2 ], [ "St Pierre & Miquelon", "pm", "508" ], [ "St Vincent & Grenadines", "vc", "1", 20, [ "784" ] ], [ "Sudan", "sd", "249" ], [ "Suriname", "sr", "597" ], [ "Svalbard & Jan Mayen", "sj", "47", 1, [ "79" ] ], [ "Sweden", "se", "46" ], [ "Switzerland", "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 & Tobago", "tt", "1", 22, [ "868" ] ], [ "Tunisia", "tn", "216" ], [ "Turkey", "tr", "90" ], [ "Turkmenistan", "tm", "993" ], [ "Turks & Caicos Islands", "tc", "1", 23, [ "649" ] ], [ "Tuvalu", "tv", "688" ], [ "Uganda", "ug", "256" ], [ "Ukraine", "ua", "380" ], [ "United Arab Emirates", "ae", "971" ], [ "United Kingdom", "gb", "44", 0 ], [ "United States", "us", "1", 0 ], [ "Uruguay", "uy", "598" ], [ "US Virgin Islands", "vi", "1", 24, [ "340" ] ], [ "Uzbekistan", "uz", "998" ], [ "Vanuatu", "vu", "678" ], [ "Vatican City", "va", "39", 1, [ "06698" ] ], [ "Venezuela", "ve", "58" ], [ "Vietnam", "vn", "84" ], [ "Wallis & 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" ] ] ];
27
- // loop over all of the countries above, restructuring the data to be objects with named keys
28
- for (var i = 0; i < allCountries.length; i++) {
29
- var c = allCountries[i];
30
- allCountries[i] = {
31
- name: c[0],
32
- iso2: c[1],
33
- dialCode: c[2],
34
- priority: c[3] || 0,
35
- areaCodes: c[4] || null,
36
- nodeById: {}
37
- };
9
+ if (typeof module === 'object' && module.exports) {
10
+ module.exports = factory();
11
+ } else {
12
+ window.intlTelInput = factory();
13
+ }
14
+ }(() => {
15
+
16
+ var factoryOutput = (() => {
17
+ var __defProp = Object.defineProperty;
18
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
19
+ var __getOwnPropNames = Object.getOwnPropertyNames;
20
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
34
+
35
+ // src/js/intlTelInput.ts
36
+ var intlTelInput_exports = {};
37
+ __export(intlTelInput_exports, {
38
+ Iti: () => Iti,
39
+ default: () => intlTelInput_default
40
+ });
41
+
42
+ // src/js/data.ts
43
+ var rawCountryData = [
44
+ [
45
+ "Afghanistan",
46
+ "af",
47
+ "93"
48
+ ],
49
+ [
50
+ "Albania",
51
+ "al",
52
+ "355"
53
+ ],
54
+ [
55
+ "Algeria",
56
+ "dz",
57
+ "213"
58
+ ],
59
+ [
60
+ "American Samoa",
61
+ "as",
62
+ "1",
63
+ 5,
64
+ ["684"]
65
+ ],
66
+ [
67
+ "Andorra",
68
+ "ad",
69
+ "376"
70
+ ],
71
+ [
72
+ "Angola",
73
+ "ao",
74
+ "244"
75
+ ],
76
+ [
77
+ "Anguilla",
78
+ "ai",
79
+ "1",
80
+ 6,
81
+ ["264"]
82
+ ],
83
+ [
84
+ "Antigua & Barbuda",
85
+ "ag",
86
+ "1",
87
+ 7,
88
+ ["268"]
89
+ ],
90
+ [
91
+ "Argentina",
92
+ "ar",
93
+ "54"
94
+ ],
95
+ [
96
+ "Armenia",
97
+ "am",
98
+ "374"
99
+ ],
100
+ [
101
+ "Aruba",
102
+ "aw",
103
+ "297"
104
+ ],
105
+ [
106
+ "Ascension Island",
107
+ "ac",
108
+ "247"
109
+ ],
110
+ [
111
+ "Australia",
112
+ "au",
113
+ "61",
114
+ 0
115
+ ],
116
+ [
117
+ "Austria",
118
+ "at",
119
+ "43"
120
+ ],
121
+ [
122
+ "Azerbaijan",
123
+ "az",
124
+ "994"
125
+ ],
126
+ [
127
+ "Bahamas",
128
+ "bs",
129
+ "1",
130
+ 8,
131
+ ["242"]
132
+ ],
133
+ [
134
+ "Bahrain",
135
+ "bh",
136
+ "973"
137
+ ],
138
+ [
139
+ "Bangladesh",
140
+ "bd",
141
+ "880"
142
+ ],
143
+ [
144
+ "Barbados",
145
+ "bb",
146
+ "1",
147
+ 9,
148
+ ["246"]
149
+ ],
150
+ [
151
+ "Belarus",
152
+ "by",
153
+ "375"
154
+ ],
155
+ [
156
+ "Belgium",
157
+ "be",
158
+ "32"
159
+ ],
160
+ [
161
+ "Belize",
162
+ "bz",
163
+ "501"
164
+ ],
165
+ [
166
+ "Benin",
167
+ "bj",
168
+ "229"
169
+ ],
170
+ [
171
+ "Bermuda",
172
+ "bm",
173
+ "1",
174
+ 10,
175
+ ["441"]
176
+ ],
177
+ [
178
+ "Bhutan",
179
+ "bt",
180
+ "975"
181
+ ],
182
+ [
183
+ "Bolivia",
184
+ "bo",
185
+ "591"
186
+ ],
187
+ [
188
+ "Bosnia & Herzegovina",
189
+ "ba",
190
+ "387"
191
+ ],
192
+ [
193
+ "Botswana",
194
+ "bw",
195
+ "267"
196
+ ],
197
+ [
198
+ "Brazil",
199
+ "br",
200
+ "55"
201
+ ],
202
+ [
203
+ "British Indian Ocean Territory",
204
+ "io",
205
+ "246"
206
+ ],
207
+ [
208
+ "British Virgin Islands",
209
+ "vg",
210
+ "1",
211
+ 11,
212
+ ["284"]
213
+ ],
214
+ [
215
+ "Brunei",
216
+ "bn",
217
+ "673"
218
+ ],
219
+ [
220
+ "Bulgaria",
221
+ "bg",
222
+ "359"
223
+ ],
224
+ [
225
+ "Burkina Faso",
226
+ "bf",
227
+ "226"
228
+ ],
229
+ [
230
+ "Burundi",
231
+ "bi",
232
+ "257"
233
+ ],
234
+ [
235
+ "Cambodia",
236
+ "kh",
237
+ "855"
238
+ ],
239
+ [
240
+ "Cameroon",
241
+ "cm",
242
+ "237"
243
+ ],
244
+ [
245
+ "Canada",
246
+ "ca",
247
+ "1",
248
+ 1,
249
+ ["204", "226", "236", "249", "250", "263", "289", "306", "343", "354", "365", "367", "368", "382", "387", "403", "416", "418", "428", "431", "437", "438", "450", "584", "468", "474", "506", "514", "519", "548", "579", "581", "584", "587", "604", "613", "639", "647", "672", "683", "705", "709", "742", "753", "778", "780", "782", "807", "819", "825", "867", "873", "902", "905"]
250
+ ],
251
+ [
252
+ "Cape Verde",
253
+ "cv",
254
+ "238"
255
+ ],
256
+ [
257
+ "Caribbean Netherlands",
258
+ "bq",
259
+ "599",
260
+ 1,
261
+ ["3", "4", "7"]
262
+ ],
263
+ [
264
+ "Cayman Islands",
265
+ "ky",
266
+ "1",
267
+ 12,
268
+ ["345"]
269
+ ],
270
+ [
271
+ "Central African Republic",
272
+ "cf",
273
+ "236"
274
+ ],
275
+ [
276
+ "Chad",
277
+ "td",
278
+ "235"
279
+ ],
280
+ [
281
+ "Chile",
282
+ "cl",
283
+ "56"
284
+ ],
285
+ [
286
+ "China",
287
+ "cn",
288
+ "86"
289
+ ],
290
+ [
291
+ "Christmas Island",
292
+ "cx",
293
+ "61",
294
+ 2,
295
+ ["89164"]
296
+ ],
297
+ [
298
+ "Cocos (Keeling) Islands",
299
+ "cc",
300
+ "61",
301
+ 1,
302
+ ["89162"]
303
+ ],
304
+ [
305
+ "Colombia",
306
+ "co",
307
+ "57"
308
+ ],
309
+ [
310
+ "Comoros",
311
+ "km",
312
+ "269"
313
+ ],
314
+ [
315
+ "Congo - Brazzaville",
316
+ "cg",
317
+ "242"
318
+ ],
319
+ [
320
+ "Congo - Kinshasa",
321
+ "cd",
322
+ "243"
323
+ ],
324
+ [
325
+ "Cook Islands",
326
+ "ck",
327
+ "682"
328
+ ],
329
+ [
330
+ "Costa Rica",
331
+ "cr",
332
+ "506"
333
+ ],
334
+ [
335
+ "C\xF4te d\u2019Ivoire",
336
+ "ci",
337
+ "225"
338
+ ],
339
+ [
340
+ "Croatia",
341
+ "hr",
342
+ "385"
343
+ ],
344
+ [
345
+ "Cuba",
346
+ "cu",
347
+ "53"
348
+ ],
349
+ [
350
+ "Cura\xE7ao",
351
+ "cw",
352
+ "599",
353
+ 0
354
+ ],
355
+ [
356
+ "Cyprus",
357
+ "cy",
358
+ "357"
359
+ ],
360
+ [
361
+ "Czech Republic",
362
+ "cz",
363
+ "420"
364
+ ],
365
+ [
366
+ "Denmark",
367
+ "dk",
368
+ "45"
369
+ ],
370
+ [
371
+ "Djibouti",
372
+ "dj",
373
+ "253"
374
+ ],
375
+ [
376
+ "Dominica",
377
+ "dm",
378
+ "1",
379
+ 13,
380
+ ["767"]
381
+ ],
382
+ [
383
+ "Dominican Republic",
384
+ "do",
385
+ "1",
386
+ 2,
387
+ ["809", "829", "849"]
388
+ ],
389
+ [
390
+ "Ecuador",
391
+ "ec",
392
+ "593"
393
+ ],
394
+ [
395
+ "Egypt",
396
+ "eg",
397
+ "20"
398
+ ],
399
+ [
400
+ "El Salvador",
401
+ "sv",
402
+ "503"
403
+ ],
404
+ [
405
+ "Equatorial Guinea",
406
+ "gq",
407
+ "240"
408
+ ],
409
+ [
410
+ "Eritrea",
411
+ "er",
412
+ "291"
413
+ ],
414
+ [
415
+ "Estonia",
416
+ "ee",
417
+ "372"
418
+ ],
419
+ [
420
+ "Eswatini",
421
+ "sz",
422
+ "268"
423
+ ],
424
+ [
425
+ "Ethiopia",
426
+ "et",
427
+ "251"
428
+ ],
429
+ [
430
+ "Falkland Islands",
431
+ "fk",
432
+ "500"
433
+ ],
434
+ [
435
+ "Faroe Islands",
436
+ "fo",
437
+ "298"
438
+ ],
439
+ [
440
+ "Fiji",
441
+ "fj",
442
+ "679"
443
+ ],
444
+ [
445
+ "Finland",
446
+ "fi",
447
+ "358",
448
+ 0
449
+ ],
450
+ [
451
+ "France",
452
+ "fr",
453
+ "33"
454
+ ],
455
+ [
456
+ "French Guiana",
457
+ "gf",
458
+ "594"
459
+ ],
460
+ [
461
+ "French Polynesia",
462
+ "pf",
463
+ "689"
464
+ ],
465
+ [
466
+ "Gabon",
467
+ "ga",
468
+ "241"
469
+ ],
470
+ [
471
+ "Gambia",
472
+ "gm",
473
+ "220"
474
+ ],
475
+ [
476
+ "Georgia",
477
+ "ge",
478
+ "995"
479
+ ],
480
+ [
481
+ "Germany",
482
+ "de",
483
+ "49"
484
+ ],
485
+ [
486
+ "Ghana",
487
+ "gh",
488
+ "233"
489
+ ],
490
+ [
491
+ "Gibraltar",
492
+ "gi",
493
+ "350"
494
+ ],
495
+ [
496
+ "Greece",
497
+ "gr",
498
+ "30"
499
+ ],
500
+ [
501
+ "Greenland",
502
+ "gl",
503
+ "299"
504
+ ],
505
+ [
506
+ "Grenada",
507
+ "gd",
508
+ "1",
509
+ 14,
510
+ ["473"]
511
+ ],
512
+ [
513
+ "Guadeloupe",
514
+ "gp",
515
+ "590",
516
+ 0
517
+ ],
518
+ [
519
+ "Guam",
520
+ "gu",
521
+ "1",
522
+ 15,
523
+ ["671"]
524
+ ],
525
+ [
526
+ "Guatemala",
527
+ "gt",
528
+ "502"
529
+ ],
530
+ [
531
+ "Guernsey",
532
+ "gg",
533
+ "44",
534
+ 1,
535
+ ["1481", "7781", "7839", "7911"]
536
+ ],
537
+ [
538
+ "Guinea",
539
+ "gn",
540
+ "224"
541
+ ],
542
+ [
543
+ "Guinea-Bissau",
544
+ "gw",
545
+ "245"
546
+ ],
547
+ [
548
+ "Guyana",
549
+ "gy",
550
+ "592"
551
+ ],
552
+ [
553
+ "Haiti",
554
+ "ht",
555
+ "509"
556
+ ],
557
+ [
558
+ "Honduras",
559
+ "hn",
560
+ "504"
561
+ ],
562
+ [
563
+ "Hong Kong",
564
+ "hk",
565
+ "852"
566
+ ],
567
+ [
568
+ "Hungary",
569
+ "hu",
570
+ "36"
571
+ ],
572
+ [
573
+ "Iceland",
574
+ "is",
575
+ "354"
576
+ ],
577
+ [
578
+ "India",
579
+ "in",
580
+ "91"
581
+ ],
582
+ [
583
+ "Indonesia",
584
+ "id",
585
+ "62"
586
+ ],
587
+ [
588
+ "Iran",
589
+ "ir",
590
+ "98"
591
+ ],
592
+ [
593
+ "Iraq",
594
+ "iq",
595
+ "964"
596
+ ],
597
+ [
598
+ "Ireland",
599
+ "ie",
600
+ "353"
601
+ ],
602
+ [
603
+ "Isle of Man",
604
+ "im",
605
+ "44",
606
+ 2,
607
+ ["1624", "74576", "7524", "7924", "7624"]
608
+ ],
609
+ [
610
+ "Israel",
611
+ "il",
612
+ "972"
613
+ ],
614
+ [
615
+ "Italy",
616
+ "it",
617
+ "39",
618
+ 0
619
+ ],
620
+ [
621
+ "Jamaica",
622
+ "jm",
623
+ "1",
624
+ 4,
625
+ ["876", "658"]
626
+ ],
627
+ [
628
+ "Japan",
629
+ "jp",
630
+ "81"
631
+ ],
632
+ [
633
+ "Jersey",
634
+ "je",
635
+ "44",
636
+ 3,
637
+ ["1534", "7509", "7700", "7797", "7829", "7937"]
638
+ ],
639
+ [
640
+ "Jordan",
641
+ "jo",
642
+ "962"
643
+ ],
644
+ [
645
+ "Kazakhstan",
646
+ "kz",
647
+ "7",
648
+ 1,
649
+ ["33", "7"]
650
+ ],
651
+ [
652
+ "Kenya",
653
+ "ke",
654
+ "254"
655
+ ],
656
+ [
657
+ "Kiribati",
658
+ "ki",
659
+ "686"
660
+ ],
661
+ [
662
+ "Kosovo",
663
+ "xk",
664
+ "383"
665
+ ],
666
+ [
667
+ "Kuwait",
668
+ "kw",
669
+ "965"
670
+ ],
671
+ [
672
+ "Kyrgyzstan",
673
+ "kg",
674
+ "996"
675
+ ],
676
+ [
677
+ "Laos",
678
+ "la",
679
+ "856"
680
+ ],
681
+ [
682
+ "Latvia",
683
+ "lv",
684
+ "371"
685
+ ],
686
+ [
687
+ "Lebanon",
688
+ "lb",
689
+ "961"
690
+ ],
691
+ [
692
+ "Lesotho",
693
+ "ls",
694
+ "266"
695
+ ],
696
+ [
697
+ "Liberia",
698
+ "lr",
699
+ "231"
700
+ ],
701
+ [
702
+ "Libya",
703
+ "ly",
704
+ "218"
705
+ ],
706
+ [
707
+ "Liechtenstein",
708
+ "li",
709
+ "423"
710
+ ],
711
+ [
712
+ "Lithuania",
713
+ "lt",
714
+ "370"
715
+ ],
716
+ [
717
+ "Luxembourg",
718
+ "lu",
719
+ "352"
720
+ ],
721
+ [
722
+ "Macau",
723
+ "mo",
724
+ "853"
725
+ ],
726
+ [
727
+ "Madagascar",
728
+ "mg",
729
+ "261"
730
+ ],
731
+ [
732
+ "Malawi",
733
+ "mw",
734
+ "265"
735
+ ],
736
+ [
737
+ "Malaysia",
738
+ "my",
739
+ "60"
740
+ ],
741
+ [
742
+ "Maldives",
743
+ "mv",
744
+ "960"
745
+ ],
746
+ [
747
+ "Mali",
748
+ "ml",
749
+ "223"
750
+ ],
751
+ [
752
+ "Malta",
753
+ "mt",
754
+ "356"
755
+ ],
756
+ [
757
+ "Marshall Islands",
758
+ "mh",
759
+ "692"
760
+ ],
761
+ [
762
+ "Martinique",
763
+ "mq",
764
+ "596"
765
+ ],
766
+ [
767
+ "Mauritania",
768
+ "mr",
769
+ "222"
770
+ ],
771
+ [
772
+ "Mauritius",
773
+ "mu",
774
+ "230"
775
+ ],
776
+ [
777
+ "Mayotte",
778
+ "yt",
779
+ "262",
780
+ 1,
781
+ ["269", "639"]
782
+ ],
783
+ [
784
+ "Mexico",
785
+ "mx",
786
+ "52"
787
+ ],
788
+ [
789
+ "Micronesia",
790
+ "fm",
791
+ "691"
792
+ ],
793
+ [
794
+ "Moldova",
795
+ "md",
796
+ "373"
797
+ ],
798
+ [
799
+ "Monaco",
800
+ "mc",
801
+ "377"
802
+ ],
803
+ [
804
+ "Mongolia",
805
+ "mn",
806
+ "976"
807
+ ],
808
+ [
809
+ "Montenegro",
810
+ "me",
811
+ "382"
812
+ ],
813
+ [
814
+ "Montserrat",
815
+ "ms",
816
+ "1",
817
+ 16,
818
+ ["664"]
819
+ ],
820
+ [
821
+ "Morocco",
822
+ "ma",
823
+ "212",
824
+ 0
825
+ ],
826
+ [
827
+ "Mozambique",
828
+ "mz",
829
+ "258"
830
+ ],
831
+ [
832
+ "Myanmar (Burma)",
833
+ "mm",
834
+ "95"
835
+ ],
836
+ [
837
+ "Namibia",
838
+ "na",
839
+ "264"
840
+ ],
841
+ [
842
+ "Nauru",
843
+ "nr",
844
+ "674"
845
+ ],
846
+ [
847
+ "Nepal",
848
+ "np",
849
+ "977"
850
+ ],
851
+ [
852
+ "Netherlands",
853
+ "nl",
854
+ "31"
855
+ ],
856
+ [
857
+ "New Caledonia",
858
+ "nc",
859
+ "687"
860
+ ],
861
+ [
862
+ "New Zealand",
863
+ "nz",
864
+ "64"
865
+ ],
866
+ [
867
+ "Nicaragua",
868
+ "ni",
869
+ "505"
870
+ ],
871
+ [
872
+ "Niger",
873
+ "ne",
874
+ "227"
875
+ ],
876
+ [
877
+ "Nigeria",
878
+ "ng",
879
+ "234"
880
+ ],
881
+ [
882
+ "Niue",
883
+ "nu",
884
+ "683"
885
+ ],
886
+ [
887
+ "Norfolk Island",
888
+ "nf",
889
+ "672"
890
+ ],
891
+ [
892
+ "North Korea",
893
+ "kp",
894
+ "850"
895
+ ],
896
+ [
897
+ "North Macedonia",
898
+ "mk",
899
+ "389"
900
+ ],
901
+ [
902
+ "Northern Mariana Islands",
903
+ "mp",
904
+ "1",
905
+ 17,
906
+ ["670"]
907
+ ],
908
+ [
909
+ "Norway",
910
+ "no",
911
+ "47",
912
+ 0
913
+ ],
914
+ [
915
+ "Oman",
916
+ "om",
917
+ "968"
918
+ ],
919
+ [
920
+ "Pakistan",
921
+ "pk",
922
+ "92"
923
+ ],
924
+ [
925
+ "Palau",
926
+ "pw",
927
+ "680"
928
+ ],
929
+ [
930
+ "Palestine",
931
+ "ps",
932
+ "970"
933
+ ],
934
+ [
935
+ "Panama",
936
+ "pa",
937
+ "507"
938
+ ],
939
+ [
940
+ "Papua New Guinea",
941
+ "pg",
942
+ "675"
943
+ ],
944
+ [
945
+ "Paraguay",
946
+ "py",
947
+ "595"
948
+ ],
949
+ [
950
+ "Peru",
951
+ "pe",
952
+ "51"
953
+ ],
954
+ [
955
+ "Philippines",
956
+ "ph",
957
+ "63"
958
+ ],
959
+ [
960
+ "Poland",
961
+ "pl",
962
+ "48"
963
+ ],
964
+ [
965
+ "Portugal",
966
+ "pt",
967
+ "351"
968
+ ],
969
+ [
970
+ "Puerto Rico",
971
+ "pr",
972
+ "1",
973
+ 3,
974
+ ["787", "939"]
975
+ ],
976
+ [
977
+ "Qatar",
978
+ "qa",
979
+ "974"
980
+ ],
981
+ [
982
+ "R\xE9union",
983
+ "re",
984
+ "262",
985
+ 0
986
+ ],
987
+ [
988
+ "Romania",
989
+ "ro",
990
+ "40"
991
+ ],
992
+ [
993
+ "Russia",
994
+ "ru",
995
+ "7",
996
+ 0
997
+ ],
998
+ [
999
+ "Rwanda",
1000
+ "rw",
1001
+ "250"
1002
+ ],
1003
+ [
1004
+ "Samoa",
1005
+ "ws",
1006
+ "685"
1007
+ ],
1008
+ [
1009
+ "San Marino",
1010
+ "sm",
1011
+ "378"
1012
+ ],
1013
+ [
1014
+ "S\xE3o Tom\xE9 & Pr\xEDncipe",
1015
+ "st",
1016
+ "239"
1017
+ ],
1018
+ [
1019
+ "Saudi Arabia",
1020
+ "sa",
1021
+ "966"
1022
+ ],
1023
+ [
1024
+ "Senegal",
1025
+ "sn",
1026
+ "221"
1027
+ ],
1028
+ [
1029
+ "Serbia",
1030
+ "rs",
1031
+ "381"
1032
+ ],
1033
+ [
1034
+ "Seychelles",
1035
+ "sc",
1036
+ "248"
1037
+ ],
1038
+ [
1039
+ "Sierra Leone",
1040
+ "sl",
1041
+ "232"
1042
+ ],
1043
+ [
1044
+ "Singapore",
1045
+ "sg",
1046
+ "65"
1047
+ ],
1048
+ [
1049
+ "Sint Maarten",
1050
+ "sx",
1051
+ "1",
1052
+ 21,
1053
+ ["721"]
1054
+ ],
1055
+ [
1056
+ "Slovakia",
1057
+ "sk",
1058
+ "421"
1059
+ ],
1060
+ [
1061
+ "Slovenia",
1062
+ "si",
1063
+ "386"
1064
+ ],
1065
+ [
1066
+ "Solomon Islands",
1067
+ "sb",
1068
+ "677"
1069
+ ],
1070
+ [
1071
+ "Somalia",
1072
+ "so",
1073
+ "252"
1074
+ ],
1075
+ [
1076
+ "South Africa",
1077
+ "za",
1078
+ "27"
1079
+ ],
1080
+ [
1081
+ "South Korea",
1082
+ "kr",
1083
+ "82"
1084
+ ],
1085
+ [
1086
+ "South Sudan",
1087
+ "ss",
1088
+ "211"
1089
+ ],
1090
+ [
1091
+ "Spain",
1092
+ "es",
1093
+ "34"
1094
+ ],
1095
+ [
1096
+ "Sri Lanka",
1097
+ "lk",
1098
+ "94"
1099
+ ],
1100
+ [
1101
+ "St Barth\xE9lemy",
1102
+ "bl",
1103
+ "590",
1104
+ 1
1105
+ ],
1106
+ [
1107
+ "St Helena",
1108
+ "sh",
1109
+ "290"
1110
+ ],
1111
+ [
1112
+ "St Kitts & Nevis",
1113
+ "kn",
1114
+ "1",
1115
+ 18,
1116
+ ["869"]
1117
+ ],
1118
+ [
1119
+ "St Lucia",
1120
+ "lc",
1121
+ "1",
1122
+ 19,
1123
+ ["758"]
1124
+ ],
1125
+ [
1126
+ "St Martin",
1127
+ "mf",
1128
+ "590",
1129
+ 2
1130
+ ],
1131
+ [
1132
+ "St Pierre & Miquelon",
1133
+ "pm",
1134
+ "508"
1135
+ ],
1136
+ [
1137
+ "St Vincent & Grenadines",
1138
+ "vc",
1139
+ "1",
1140
+ 20,
1141
+ ["784"]
1142
+ ],
1143
+ [
1144
+ "Sudan",
1145
+ "sd",
1146
+ "249"
1147
+ ],
1148
+ [
1149
+ "Suriname",
1150
+ "sr",
1151
+ "597"
1152
+ ],
1153
+ [
1154
+ "Svalbard & Jan Mayen",
1155
+ "sj",
1156
+ "47",
1157
+ 1,
1158
+ ["79"]
1159
+ ],
1160
+ [
1161
+ "Sweden",
1162
+ "se",
1163
+ "46"
1164
+ ],
1165
+ [
1166
+ "Switzerland",
1167
+ "ch",
1168
+ "41"
1169
+ ],
1170
+ [
1171
+ "Syria",
1172
+ "sy",
1173
+ "963"
1174
+ ],
1175
+ [
1176
+ "Taiwan",
1177
+ "tw",
1178
+ "886"
1179
+ ],
1180
+ [
1181
+ "Tajikistan",
1182
+ "tj",
1183
+ "992"
1184
+ ],
1185
+ [
1186
+ "Tanzania",
1187
+ "tz",
1188
+ "255"
1189
+ ],
1190
+ [
1191
+ "Thailand",
1192
+ "th",
1193
+ "66"
1194
+ ],
1195
+ [
1196
+ "Timor-Leste",
1197
+ "tl",
1198
+ "670"
1199
+ ],
1200
+ [
1201
+ "Togo",
1202
+ "tg",
1203
+ "228"
1204
+ ],
1205
+ [
1206
+ "Tokelau",
1207
+ "tk",
1208
+ "690"
1209
+ ],
1210
+ [
1211
+ "Tonga",
1212
+ "to",
1213
+ "676"
1214
+ ],
1215
+ [
1216
+ "Trinidad & Tobago",
1217
+ "tt",
1218
+ "1",
1219
+ 22,
1220
+ ["868"]
1221
+ ],
1222
+ [
1223
+ "Tunisia",
1224
+ "tn",
1225
+ "216"
1226
+ ],
1227
+ [
1228
+ "Turkey",
1229
+ "tr",
1230
+ "90"
1231
+ ],
1232
+ [
1233
+ "Turkmenistan",
1234
+ "tm",
1235
+ "993"
1236
+ ],
1237
+ [
1238
+ "Turks & Caicos Islands",
1239
+ "tc",
1240
+ "1",
1241
+ 23,
1242
+ ["649"]
1243
+ ],
1244
+ [
1245
+ "Tuvalu",
1246
+ "tv",
1247
+ "688"
1248
+ ],
1249
+ [
1250
+ "Uganda",
1251
+ "ug",
1252
+ "256"
1253
+ ],
1254
+ [
1255
+ "Ukraine",
1256
+ "ua",
1257
+ "380"
1258
+ ],
1259
+ [
1260
+ "United Arab Emirates",
1261
+ "ae",
1262
+ "971"
1263
+ ],
1264
+ [
1265
+ "United Kingdom",
1266
+ "gb",
1267
+ "44",
1268
+ 0
1269
+ ],
1270
+ [
1271
+ "United States",
1272
+ "us",
1273
+ "1",
1274
+ 0
1275
+ ],
1276
+ [
1277
+ "Uruguay",
1278
+ "uy",
1279
+ "598"
1280
+ ],
1281
+ [
1282
+ "US Virgin Islands",
1283
+ "vi",
1284
+ "1",
1285
+ 24,
1286
+ ["340"]
1287
+ ],
1288
+ [
1289
+ "Uzbekistan",
1290
+ "uz",
1291
+ "998"
1292
+ ],
1293
+ [
1294
+ "Vanuatu",
1295
+ "vu",
1296
+ "678"
1297
+ ],
1298
+ [
1299
+ "Vatican City",
1300
+ "va",
1301
+ "39",
1302
+ 1,
1303
+ ["06698"]
1304
+ ],
1305
+ [
1306
+ "Venezuela",
1307
+ "ve",
1308
+ "58"
1309
+ ],
1310
+ [
1311
+ "Vietnam",
1312
+ "vn",
1313
+ "84"
1314
+ ],
1315
+ [
1316
+ "Wallis & Futuna",
1317
+ "wf",
1318
+ "681"
1319
+ ],
1320
+ [
1321
+ "Western Sahara",
1322
+ "eh",
1323
+ "212",
1324
+ 1,
1325
+ ["5288", "5289"]
1326
+ ],
1327
+ [
1328
+ "Yemen",
1329
+ "ye",
1330
+ "967"
1331
+ ],
1332
+ [
1333
+ "Zambia",
1334
+ "zm",
1335
+ "260"
1336
+ ],
1337
+ [
1338
+ "Zimbabwe",
1339
+ "zw",
1340
+ "263"
1341
+ ],
1342
+ [
1343
+ "\xC5land Islands",
1344
+ "ax",
1345
+ "358",
1346
+ 1,
1347
+ ["18"]
1348
+ ]
1349
+ ];
1350
+ var allCountries = [];
1351
+ for (let i = 0; i < rawCountryData.length; i++) {
1352
+ const c = rawCountryData[i];
1353
+ allCountries[i] = {
1354
+ name: c[0],
1355
+ iso2: c[1],
1356
+ dialCode: c[2],
1357
+ priority: c[3] || 0,
1358
+ areaCodes: c[4] || null,
1359
+ nodeById: {}
1360
+ };
1361
+ }
1362
+ var data_default = allCountries;
1363
+
1364
+ // src/js/intlTelInput.ts
1365
+ var id = 0;
1366
+ var defaults = {
1367
+ // whether or not to allow the dropdown
1368
+ allowDropdown: true,
1369
+ // add a placeholder in the input with an example number for the selected country
1370
+ autoPlaceholder: "polite",
1371
+ // add a country search input at the top of the dropdown
1372
+ countrySearch: true,
1373
+ // modify the parentClass
1374
+ containerClass: "",
1375
+ // modify the auto placeholder
1376
+ customPlaceholder: null,
1377
+ // append menu to specified element
1378
+ dropdownContainer: null,
1379
+ // don't display these countries
1380
+ excludeCountries: [],
1381
+ // fix the dropdown width to the input width (rather than being as wide as the longest country name)
1382
+ fixDropdownWidth: true,
1383
+ // format the number as the user types
1384
+ formatAsYouType: true,
1385
+ // format the input value during initialisation and on setNumber
1386
+ formatOnDisplay: true,
1387
+ // geoIp lookup function
1388
+ geoIpLookup: null,
1389
+ // inject a hidden input with the name returned from this function, and on submit, populate it with the result of getNumber
1390
+ hiddenInput: null,
1391
+ // internationalise the plugin text e.g. search input placeholder, country names
1392
+ i18n: {},
1393
+ // initial country
1394
+ initialCountry: "",
1395
+ // national vs international formatting for numbers e.g. placeholders and displaying existing numbers
1396
+ nationalMode: true,
1397
+ // display only these countries
1398
+ onlyCountries: [],
1399
+ // number type to use for placeholders
1400
+ placeholderNumberType: "MOBILE",
1401
+ // the countries at the top of the list
1402
+ preferredCountries: [],
1403
+ // option to hide the flags - must be used with showSelectedDialCode, or allowDropdown=false
1404
+ showFlags: true,
1405
+ // display the international dial code next to the selected flag
1406
+ showSelectedDialCode: false,
1407
+ // only allow certain chars e.g. a plus followed by numeric digits, and cap at max valid length
1408
+ strictMode: false,
1409
+ // use full screen popup instead of dropdown for country list
1410
+ useFullscreenPopup: typeof navigator !== "undefined" && typeof window !== "undefined" ? (
1411
+ // we cannot just test screen size as some smartphones/website meta tags will report desktop
1412
+ // resolutions
1413
+ // Note: to target Android Mobiles (and not Tablets), we must find 'Android' and 'Mobile'
1414
+ /Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
1415
+ navigator.userAgent
1416
+ ) || window.innerWidth <= 500
1417
+ ) : false,
1418
+ // specify the path to the libphonenumber script to enable validation/formatting
1419
+ utilsScript: ""
1420
+ };
1421
+ var regionlessNanpNumbers = [
1422
+ "800",
1423
+ "822",
1424
+ "833",
1425
+ "844",
1426
+ "855",
1427
+ "866",
1428
+ "877",
1429
+ "880",
1430
+ "881",
1431
+ "882",
1432
+ "883",
1433
+ "884",
1434
+ "885",
1435
+ "886",
1436
+ "887",
1437
+ "888",
1438
+ "889"
1439
+ ];
1440
+ var getNumeric = (s) => s.replace(/\D/g, "");
1441
+ var normaliseString = (s = "") => s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
1442
+ var toggleClass = (el, className, shouldHaveClass) => {
1443
+ if (shouldHaveClass && !el.classList.contains(className)) {
1444
+ el.classList.add(className);
1445
+ } else if (!shouldHaveClass && el.classList.contains(className)) {
1446
+ el.classList.remove(className);
1447
+ }
1448
+ };
1449
+ var isRegionlessNanp = (number) => {
1450
+ const numeric = getNumeric(number);
1451
+ if (numeric.charAt(0) === "1") {
1452
+ const areaCode = numeric.substr(1, 3);
1453
+ return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1454
+ }
1455
+ return false;
1456
+ };
1457
+ var countryNameSort = (a, b) => {
1458
+ if (a.name < b.name) {
1459
+ return -1;
1460
+ }
1461
+ if (a.name > b.name) {
1462
+ return 1;
1463
+ }
1464
+ return 0;
1465
+ };
1466
+ var translateCursorPosition = (relevantChars, formattedValue, prevCaretPos, isDeleteForwards) => {
1467
+ if (prevCaretPos === 0 && !isDeleteForwards) {
1468
+ return 0;
1469
+ }
1470
+ let count = 0;
1471
+ for (let i = 0; i < formattedValue.length; i++) {
1472
+ if (/[+0-9]/.test(formattedValue[i])) {
1473
+ count++;
1474
+ }
1475
+ if (count === relevantChars && !isDeleteForwards) {
1476
+ return i + 1;
1477
+ }
1478
+ if (isDeleteForwards && count === relevantChars + 1) {
1479
+ return i;
1480
+ }
1481
+ }
1482
+ return formattedValue.length;
1483
+ };
1484
+ var createEl = (name, attrs, container) => {
1485
+ const el = document.createElement(name);
1486
+ if (attrs) {
1487
+ Object.entries(attrs).forEach(([key, value]) => el.setAttribute(key, value));
1488
+ }
1489
+ if (container) {
1490
+ container.appendChild(el);
1491
+ }
1492
+ return el;
1493
+ };
1494
+ var forEachInstance = (method) => {
1495
+ const { instances } = window.intlTelInputGlobals;
1496
+ Object.values(instances).forEach((instance) => instance[method]());
1497
+ };
1498
+ var Iti = class {
1499
+ // can't be private as it's called from intlTelInput convenience wrapper
1500
+ id;
1501
+ // not private!
1502
+ promise;
1503
+ // private
1504
+ telInput;
1505
+ activeItem;
1506
+ highlightedItem;
1507
+ options;
1508
+ hadInitialPlaceholder;
1509
+ isRTL;
1510
+ selectedCountryData;
1511
+ countries;
1512
+ dialCodeMaxLen;
1513
+ dialCodeToIso2Map;
1514
+ dialCodes;
1515
+ preferredCountries;
1516
+ countryContainer;
1517
+ selectedCountry;
1518
+ selectedCountryInner;
1519
+ selectedCountryA11yText;
1520
+ selectedDialCode;
1521
+ dropdownArrow;
1522
+ dropdownContent;
1523
+ searchInput;
1524
+ searchResultsA11yText;
1525
+ countryList;
1526
+ dropdown;
1527
+ hiddenInput;
1528
+ hiddenInputCountry;
1529
+ maxCoreNumberLength;
1530
+ defaultCountry;
1531
+ _handleHiddenInputSubmit;
1532
+ _handleLabelClick;
1533
+ _handleClickSelectedCountry;
1534
+ _handleCountryContainerKeydown;
1535
+ _handleInputEvent;
1536
+ _handleKeydownEvent;
1537
+ _handleWindowScroll;
1538
+ _handleMouseoverCountryList;
1539
+ _handleClickCountryList;
1540
+ _handleClickOffToClose;
1541
+ _handleKeydownOnDropdown;
1542
+ _handleSearchChange;
1543
+ resolveAutoCountryPromise;
1544
+ rejectAutoCountryPromise;
1545
+ resolveUtilsScriptPromise;
1546
+ rejectUtilsScriptPromise;
1547
+ constructor(input, customOptions = {}) {
1548
+ this.id = id++;
1549
+ this.telInput = input;
1550
+ this.activeItem = null;
1551
+ this.highlightedItem = null;
1552
+ this.options = Object.assign({}, defaults, customOptions);
1553
+ this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
1554
+ }
1555
+ // can't be private as it's called from intlTelInput convenience wrapper
1556
+ _init() {
1557
+ if (this.options.useFullscreenPopup) {
1558
+ this.options.fixDropdownWidth = false;
1559
+ }
1560
+ if (this.options.countrySearch && !this.options.useFullscreenPopup) {
1561
+ this.options.fixDropdownWidth = true;
1562
+ }
1563
+ const forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
1564
+ if (!this.options.showFlags && forceShowFlags) {
1565
+ this.options.showFlags = true;
1566
+ }
1567
+ if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
1568
+ this.options.dropdownContainer = document.body;
1569
+ }
1570
+ this.isRTL = !!this.telInput.closest("[dir=rtl]");
1571
+ const autoCountryPromise = new Promise((resolve, reject) => {
1572
+ this.resolveAutoCountryPromise = resolve;
1573
+ this.rejectAutoCountryPromise = reject;
1574
+ });
1575
+ const utilsScriptPromise = new Promise((resolve, reject) => {
1576
+ this.resolveUtilsScriptPromise = resolve;
1577
+ this.rejectUtilsScriptPromise = reject;
1578
+ });
1579
+ this.promise = Promise.all([autoCountryPromise, utilsScriptPromise]);
1580
+ this.selectedCountryData = {};
1581
+ this._processCountryData();
1582
+ this._generateMarkup();
1583
+ this._setInitialState();
1584
+ this._initListeners();
1585
+ this._initRequests();
1586
+ }
1587
+ //********************
1588
+ //* PRIVATE METHODS
1589
+ //********************
1590
+ // prepare all of the country data, including onlyCountries, excludeCountries and
1591
+ // preferredCountries options
1592
+ _processCountryData() {
1593
+ this._processAllCountries();
1594
+ this._processDialCodes();
1595
+ this._processPreferredCountries();
1596
+ this._translateCountryNames();
1597
+ if (this.options.onlyCountries.length || this.options.i18n) {
1598
+ this.countries.sort(countryNameSort);
1599
+ }
1600
+ }
1601
+ // add a dial code to this.dialCodeToIso2Map
1602
+ _addToDialCodeMap(iso2, dialCode, priority) {
1603
+ if (dialCode.length > this.dialCodeMaxLen) {
1604
+ this.dialCodeMaxLen = dialCode.length;
1605
+ }
1606
+ if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
1607
+ this.dialCodeToIso2Map[dialCode] = [];
1608
+ }
1609
+ for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
1610
+ if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
1611
+ return;
38
1612
  }
39
- const intlTelInputGlobals = {
40
- getInstance: input => {
41
- const id = input.getAttribute("data-intl-tel-input-id");
42
- return window.intlTelInputGlobals.instances[id];
1613
+ }
1614
+ const index = priority !== void 0 ? priority : this.dialCodeToIso2Map[dialCode].length;
1615
+ this.dialCodeToIso2Map[dialCode][index] = iso2;
1616
+ }
1617
+ // process onlyCountries or excludeCountries array if present
1618
+ _processAllCountries() {
1619
+ const { onlyCountries, excludeCountries } = this.options;
1620
+ if (onlyCountries.length) {
1621
+ const lowerCaseOnlyCountries = onlyCountries.map(
1622
+ (country) => country.toLowerCase()
1623
+ );
1624
+ this.countries = data_default.filter(
1625
+ (country) => lowerCaseOnlyCountries.indexOf(country.iso2) > -1
1626
+ );
1627
+ } else if (excludeCountries.length) {
1628
+ const lowerCaseExcludeCountries = excludeCountries.map(
1629
+ (country) => country.toLowerCase()
1630
+ );
1631
+ this.countries = data_default.filter(
1632
+ (country) => lowerCaseExcludeCountries.indexOf(country.iso2) === -1
1633
+ );
1634
+ } else {
1635
+ this.countries = data_default;
1636
+ }
1637
+ }
1638
+ // Translate Countries by object literal provided on config
1639
+ _translateCountryNames() {
1640
+ for (let i = 0; i < this.countries.length; i++) {
1641
+ const iso2 = this.countries[i].iso2.toLowerCase();
1642
+ if (this.options.i18n.hasOwnProperty(iso2)) {
1643
+ this.countries[i].name = this.options.i18n[iso2];
1644
+ }
1645
+ }
1646
+ }
1647
+ // generate this.dialCodes and this.dialCodeToIso2Map
1648
+ _processDialCodes() {
1649
+ this.dialCodes = {};
1650
+ this.dialCodeMaxLen = 0;
1651
+ this.dialCodeToIso2Map = {};
1652
+ for (let i = 0; i < this.countries.length; i++) {
1653
+ const c = this.countries[i];
1654
+ if (!this.dialCodes[c.dialCode]) {
1655
+ this.dialCodes[c.dialCode] = true;
1656
+ }
1657
+ this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
1658
+ }
1659
+ for (let i = 0; i < this.countries.length; i++) {
1660
+ const c = this.countries[i];
1661
+ if (c.areaCodes) {
1662
+ const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
1663
+ for (let j = 0; j < c.areaCodes.length; j++) {
1664
+ const areaCode = c.areaCodes[j];
1665
+ for (let k = 1; k < areaCode.length; k++) {
1666
+ const partialDialCode = c.dialCode + areaCode.substr(0, k);
1667
+ this._addToDialCodeMap(rootIso2Code, partialDialCode);
1668
+ this._addToDialCodeMap(c.iso2, partialDialCode);
1669
+ }
1670
+ this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
1671
+ }
1672
+ }
1673
+ }
1674
+ }
1675
+ // process preferred countries - iterate through the preferences, fetching the country data for
1676
+ // each one
1677
+ _processPreferredCountries() {
1678
+ this.preferredCountries = [];
1679
+ for (let i = 0; i < this.options.preferredCountries.length; i++) {
1680
+ const iso2 = this.options.preferredCountries[i].toLowerCase();
1681
+ const countryData = this._getCountryData(iso2, true);
1682
+ if (countryData) {
1683
+ this.preferredCountries.push(countryData);
1684
+ }
1685
+ }
1686
+ }
1687
+ // generate all of the markup for the plugin: the selected country overlay, and the dropdown
1688
+ _generateMarkup() {
1689
+ this.telInput.classList.add("iti__tel-input");
1690
+ if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
1691
+ this.telInput.setAttribute("autocomplete", "off");
1692
+ }
1693
+ const {
1694
+ allowDropdown,
1695
+ showSelectedDialCode,
1696
+ showFlags,
1697
+ containerClass,
1698
+ hiddenInput,
1699
+ dropdownContainer,
1700
+ fixDropdownWidth,
1701
+ useFullscreenPopup,
1702
+ countrySearch,
1703
+ i18n
1704
+ } = this.options;
1705
+ let parentClass = "iti";
1706
+ if (allowDropdown) {
1707
+ parentClass += " iti--allow-dropdown";
1708
+ }
1709
+ if (showSelectedDialCode) {
1710
+ parentClass += " iti--show-selected-dial-code";
1711
+ }
1712
+ if (showFlags) {
1713
+ parentClass += " iti--show-flags";
1714
+ }
1715
+ if (containerClass) {
1716
+ parentClass += ` ${containerClass}`;
1717
+ }
1718
+ if (!useFullscreenPopup) {
1719
+ parentClass += " iti--inline-dropdown";
1720
+ }
1721
+ const wrapper = createEl("div", { class: parentClass });
1722
+ this.telInput.parentNode?.insertBefore(wrapper, this.telInput);
1723
+ if (showFlags || showSelectedDialCode) {
1724
+ this.countryContainer = createEl(
1725
+ "div",
1726
+ { class: "iti__country-container" },
1727
+ wrapper
1728
+ );
1729
+ this.selectedCountry = createEl(
1730
+ "button",
1731
+ {
1732
+ type: "button",
1733
+ class: "iti__selected-country",
1734
+ ...allowDropdown && {
1735
+ "aria-expanded": "false",
1736
+ "aria-label": this.options.i18n.selectedCountryAriaLabel || "Selected country",
1737
+ "aria-haspopup": countrySearch ? "true" : "listbox",
1738
+ "aria-controls": countrySearch ? `iti-${this.id}__dropdown-content` : `iti-${this.id}__country-listbox`,
1739
+ ...countrySearch ? { role: "combobox" } : {}
1740
+ }
1741
+ },
1742
+ this.countryContainer
1743
+ );
1744
+ this.selectedCountryInner = createEl("div", null, this.selectedCountry);
1745
+ this.selectedCountryA11yText = createEl(
1746
+ "span",
1747
+ { class: "iti__a11y-text" },
1748
+ this.selectedCountryInner
1749
+ );
1750
+ }
1751
+ wrapper.appendChild(this.telInput);
1752
+ if (this.selectedCountry && this.telInput.disabled) {
1753
+ this.selectedCountry.setAttribute("aria-disabled", "true");
1754
+ }
1755
+ if (showSelectedDialCode) {
1756
+ this.selectedDialCode = createEl(
1757
+ "div",
1758
+ { class: "iti__selected-dial-code" },
1759
+ this.selectedCountry
1760
+ );
1761
+ }
1762
+ if (allowDropdown) {
1763
+ if (!this.telInput.disabled) {
1764
+ this.selectedCountry.setAttribute("tabindex", "0");
1765
+ }
1766
+ this.dropdownArrow = createEl(
1767
+ "div",
1768
+ { class: "iti__arrow", "aria-hidden": "true" },
1769
+ this.selectedCountry
1770
+ );
1771
+ const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
1772
+ this.dropdownContent = createEl("div", {
1773
+ id: `iti-${this.id}__dropdown-content`,
1774
+ class: `iti__dropdown-content iti__hide ${extraClasses}`
1775
+ });
1776
+ if (countrySearch) {
1777
+ this.searchInput = createEl(
1778
+ "input",
1779
+ {
1780
+ type: "text",
1781
+ class: "iti__search-input",
1782
+ placeholder: i18n.searchPlaceholder || "Search",
1783
+ role: "combobox",
1784
+ "aria-expanded": "true",
1785
+ "aria-label": i18n.searchPlaceholder || "Search",
1786
+ "aria-controls": `iti-${this.id}__country-listbox`,
1787
+ "aria-autocomplete": "list",
1788
+ "autocomplete": "off"
43
1789
  },
44
- instances: {},
45
- // using a global like this allows us to mock it in the tests
46
- documentReady: () => document.readyState === "complete"
47
- };
48
- if (typeof window === "object") {
49
- window.intlTelInputGlobals = intlTelInputGlobals;
1790
+ this.dropdownContent
1791
+ );
1792
+ this.searchResultsA11yText = createEl(
1793
+ "span",
1794
+ { class: "iti__a11y-text" },
1795
+ this.dropdownContent
1796
+ );
50
1797
  }
51
- // these vars persist through all instances of the plugin
52
- let id = 0;
53
- const defaults = {
54
- // whether or not to allow the dropdown
55
- allowDropdown: true,
56
- // add a placeholder in the input with an example number for the selected country
57
- autoPlaceholder: "polite",
58
- // add a country search input at the top of the dropdown
59
- countrySearch: true,
60
- // modify the parentClass
61
- containerClass: "",
62
- // modify the auto placeholder
63
- customPlaceholder: null,
64
- // append menu to specified element
65
- dropdownContainer: null,
66
- // don't display these countries
67
- excludeCountries: [],
68
- // fix the dropdown width to the input width (rather than being as wide as the longest country name)
69
- fixDropdownWidth: true,
70
- // format the number as the user types
71
- formatAsYouType: true,
72
- // format the input value during initialisation and on setNumber
73
- formatOnDisplay: true,
74
- // geoIp lookup function
75
- geoIpLookup: null,
76
- // inject a hidden input with the name returned from this function, and on submit, populate it with the result of getNumber
77
- hiddenInput: null,
78
- // internationalise the plugin text e.g. search input placeholder, country names
79
- i18n: {},
80
- // initial country
81
- initialCountry: "",
82
- // national vs international formatting for numbers e.g. placeholders and displaying existing numbers
83
- nationalMode: true,
84
- // display only these countries
85
- onlyCountries: [],
86
- // number type to use for placeholders
87
- placeholderNumberType: "MOBILE",
88
- // the countries at the top of the list
89
- preferredCountries: [],
90
- // option to hide the flags - must be used with showSelectedDialCode, or allowDropdown=false
91
- showFlags: true,
92
- // display the international dial code next to the selected flag
93
- showSelectedDialCode: false,
94
- // only allow certain chars e.g. a plus followed by numeric digits, and cap at max valid length
95
- strictMode: false,
96
- // use full screen popup instead of dropdown for country list
97
- useFullscreenPopup: typeof navigator !== "undefined" && typeof window !== "undefined" ? // we cannot just test screen size as some smartphones/website meta tags will report desktop
98
- // resolutions
99
- // Note: to target Android Mobiles (and not Tablets), we must find 'Android' and 'Mobile'
100
- /Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth <= 500 : false,
101
- // specify the path to the libphonenumber script to enable validation/formatting
102
- utilsScript: ""
103
- };
104
- // https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
105
- const regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
106
- // run a method on each instance of the plugin
107
- const forEachInstance = method => {
108
- const {
109
- instances
110
- } = window.intlTelInputGlobals;
111
- Object.values(instances).forEach(instance => instance[method]());
112
- };
113
- // this is our plugin class that we will create an instance of
114
- // eslint-disable-next-line no-unused-vars
115
- class Iti {
116
- constructor(input, customOptions = {}) {
117
- this.id = id++;
118
- this.telInput = input;
119
- this.activeItem = null;
120
- this.highlightedItem = null;
121
- // process specified options / defaults
122
- this.options = Object.assign({}, defaults, customOptions);
123
- this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
124
- }
125
- _init() {
126
- // if showing fullscreen popup, do not fix the width
127
- if (this.options.useFullscreenPopup) {
128
- this.options.fixDropdownWidth = false;
129
- }
130
- // when search enabled, we must fix the width else it would change with different results
131
- if (this.options.countrySearch && !this.options.useFullscreenPopup) {
132
- this.options.fixDropdownWidth = true;
133
- }
134
- // force showFlags=true if there's a dropdown and we're not displaying the dial code,
135
- // as otherwise you just have a down arrow on it's own which doesn't make sense
136
- const forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
137
- if (!this.options.showFlags && forceShowFlags) {
138
- this.options.showFlags = true;
139
- }
140
- // on mobile, we want a full screen dropdown, so we must append it to the body
141
- if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
142
- this.options.dropdownContainer = document.body;
143
- }
144
- // check if input has one parent with RTL
145
- this.isRTL = !!this.telInput.closest("[dir=rtl]");
146
- // these promises get resolved when their individual requests complete
147
- // this way the dev can do something like iti.promise.then(...) to know when all requests are
148
- // complete
149
- const autoCountryPromise = new Promise((resolve, reject) => {
150
- this.resolveAutoCountryPromise = resolve;
151
- this.rejectAutoCountryPromise = reject;
152
- });
153
- const utilsScriptPromise = new Promise((resolve, reject) => {
154
- this.resolveUtilsScriptPromise = resolve;
155
- this.rejectUtilsScriptPromise = reject;
156
- });
157
- this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
158
- // in various situations there could be no country selected initially, but we need to be able
159
- // to assume this variable exists
160
- this.selectedCountryData = {};
161
- // process all the data: onlyCountries, excludeCountries, preferredCountries etc
162
- this._processCountryData();
163
- // generate the markup
164
- this._generateMarkup();
165
- // set the initial state of the input value and the selected flag
166
- this._setInitialState();
167
- // start all of the event listeners: input keydown, selectedFlag click
168
- this._initListeners();
169
- // utils script, and auto country
170
- this._initRequests();
171
- }
172
- /********************
173
- * PRIVATE METHODS
174
- ********************/
175
- // prepare all of the country data, including onlyCountries, excludeCountries and
176
- // preferredCountries options
177
- _processCountryData() {
178
- // process onlyCountries or excludeCountries array if present
179
- this._processAllCountries();
180
- // generate this.dialCodes and this.dialCodeToIso2Map
181
- this._processDialCodes();
182
- // process the preferredCountries
183
- this._processPreferredCountries();
184
- // translate country names according to i18n option
185
- this._translateCountryNames();
186
- // sort countries by name
187
- if (this.options.onlyCountries.length || this.options.i18n) {
188
- this.countries.sort(this._countryNameSort);
189
- }
190
- }
191
- // add a dial code to this.dialCodeToIso2Map
192
- _addToDialCodeMap(iso2, dialCode, priority) {
193
- if (dialCode.length > this.dialCodeMaxLen) {
194
- this.dialCodeMaxLen = dialCode.length;
195
- }
196
- if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
197
- this.dialCodeToIso2Map[dialCode] = [];
198
- }
199
- // bail if we already have this country for this dialCode
200
- for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
201
- if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
202
- return;
203
- }
204
- }
205
- // check for undefined as 0 is falsy
206
- const index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
207
- this.dialCodeToIso2Map[dialCode][index] = iso2;
208
- }
209
- // process onlyCountries or excludeCountries array if present
210
- _processAllCountries() {
211
- if (this.options.onlyCountries.length) {
212
- const lowerCaseOnlyCountries = this.options.onlyCountries.map(country => country.toLowerCase());
213
- this.countries = allCountries.filter(country => lowerCaseOnlyCountries.indexOf(country.iso2) > -1);
214
- } else if (this.options.excludeCountries.length) {
215
- const lowerCaseExcludeCountries = this.options.excludeCountries.map(country => country.toLowerCase());
216
- this.countries = allCountries.filter(country => lowerCaseExcludeCountries.indexOf(country.iso2) === -1);
217
- } else {
218
- this.countries = allCountries;
219
- }
220
- }
221
- // Translate Countries by object literal provided on config
222
- _translateCountryNames() {
223
- for (let i = 0; i < this.countries.length; i++) {
224
- const iso2 = this.countries[i].iso2.toLowerCase();
225
- if (this.options.i18n.hasOwnProperty(iso2)) {
226
- this.countries[i].name = this.options.i18n[iso2];
227
- }
228
- }
229
- }
230
- // sort by country name
231
- _countryNameSort(a, b) {
232
- if (a.name < b.name) {
233
- return -1;
234
- }
235
- if (a.name > b.name) {
236
- return 1;
237
- }
238
- return 0;
239
- }
240
- // generate this.dialCodes and this.dialCodeToIso2Map
241
- _processDialCodes() {
242
- // here we store just dial codes, where the key is the dial code, and the value is true
243
- // e.g. { 1: true, 7: true, 20: true, ... }
244
- this.dialCodes = {};
245
- this.dialCodeMaxLen = 0;
246
- // here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
247
- /* e.g.
248
- * {
249
- * 1: [ 'us', 'ca', ... ], # all NANP countries
250
- * 12: [ 'us', 'ca', ... ], # subset of NANP countries
251
- * 120: [ 'us', 'ca' ], # just US and Canada
252
- * 1204: [ 'ca' ], # only Canada
253
- * ...
254
- * }
255
- */
256
- this.dialCodeToIso2Map = {};
257
- // first: add dial codes
258
- for (let i = 0; i < this.countries.length; i++) {
259
- const c = this.countries[i];
260
- if (!this.dialCodes[c.dialCode]) {
261
- this.dialCodes[c.dialCode] = true;
262
- }
263
- this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
264
- }
265
- // next: add area codes
266
- // this is a second loop over countries, to make sure we have all of the "root" countries
267
- // already in the map, so that we can access them, as each time we add an area code substring
268
- // to the map, we also need to include the "root" country's code, as that also matches
269
- for (let i = 0; i < this.countries.length; i++) {
270
- const c = this.countries[i];
271
- // area codes
272
- if (c.areaCodes) {
273
- const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
274
- // for each area code
275
- for (let j = 0; j < c.areaCodes.length; j++) {
276
- const areaCode = c.areaCodes[j];
277
- // for each digit in the area code to add all partial matches as well
278
- for (let k = 1; k < areaCode.length; k++) {
279
- const partialDialCode = c.dialCode + areaCode.substr(0, k);
280
- // start with the root country, as that also matches this dial code
281
- this._addToDialCodeMap(rootIso2Code, partialDialCode);
282
- this._addToDialCodeMap(c.iso2, partialDialCode);
283
- }
284
- // add the full area code
285
- this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
286
- }
287
- }
288
- }
289
- }
290
- // process preferred countries - iterate through the preferences, fetching the country data for
291
- // each one
292
- _processPreferredCountries() {
293
- this.preferredCountries = [];
294
- for (let i = 0; i < this.options.preferredCountries.length; i++) {
295
- const iso2 = this.options.preferredCountries[i].toLowerCase();
296
- const countryData = this._getCountryData(iso2, true);
297
- if (countryData) {
298
- this.preferredCountries.push(countryData);
299
- }
300
- }
301
- }
302
- // create a DOM element
303
- _createEl(name, attrs, container) {
304
- const el = document.createElement(name);
305
- if (attrs) {
306
- Object.entries(attrs).forEach(([ key, value ]) => el.setAttribute(key, value));
307
- }
308
- if (container) {
309
- container.appendChild(el);
310
- }
311
- return el;
312
- }
313
- // generate all of the markup for the plugin: the selected flag overlay, and the dropdown
314
- _generateMarkup() {
315
- this.telInput.classList.add("iti__tel-input");
316
- // if autocomplete does not exist on the element and its form, then
317
- // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
318
- // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
319
- // autocompleted number, which on submit could mean wrong number is saved
320
- if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
321
- this.telInput.setAttribute("autocomplete", "off");
322
- }
323
- const {
324
- allowDropdown,
325
- showSelectedDialCode,
326
- showFlags,
327
- containerClass,
328
- hiddenInput,
329
- dropdownContainer,
330
- fixDropdownWidth,
331
- useFullscreenPopup,
332
- countrySearch,
333
- i18n
334
- } = this.options;
335
- // containers (mostly for positioning)
336
- let parentClass = "iti";
337
- if (allowDropdown) {
338
- parentClass += " iti--allow-dropdown";
339
- }
340
- if (showSelectedDialCode) {
341
- parentClass += " iti--show-selected-dial-code";
342
- }
343
- if (showFlags) {
344
- parentClass += " iti--show-flags";
345
- }
346
- if (containerClass) {
347
- parentClass += ` ${containerClass}`;
348
- }
349
- if (!useFullscreenPopup) {
350
- parentClass += " iti--inline-dropdown";
351
- }
352
- const wrapper = this._createEl("div", {
353
- class: parentClass
354
- });
355
- this.telInput.parentNode.insertBefore(wrapper, this.telInput);
356
- // if we're showing flags or dial codes, we need the flags container etc
357
- if (showFlags || showSelectedDialCode) {
358
- this.flagsContainer = this._createEl("div", {
359
- class: "iti__flag-container"
360
- }, wrapper);
361
- // selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
362
- // when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
363
- // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
364
- this.selectedFlag = this._createEl("div", {
365
- class: "iti__selected-flag",
366
- ...allowDropdown && {
367
- role: "button",
368
- "aria-expanded": "false",
369
- "aria-label": this.options.i18n.selectedCountryAriaLabel || "Selected country",
370
- "aria-haspopup": countrySearch ? "true" : "listbox",
371
- "aria-controls": countrySearch ? `iti-${this.id}__dropdown-content` : `iti-${this.id}__country-listbox`,
372
- ...countrySearch || {
373
- role: "combobox"
374
- }
375
- }
376
- }, this.flagsContainer);
377
- // we now include the selected flag element even when showFlags is disabled,
378
- // as need to show globe icon for showSelectedDialCode empty state
379
- this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
380
- this.selectedFlagA11yText = this._createEl("span", {
381
- class: "iti__a11y-text"
382
- }, this.selectedFlagInner);
383
- }
384
- wrapper.appendChild(this.telInput);
385
- if (this.selectedFlag && this.telInput.disabled) {
386
- this.selectedFlag.setAttribute("aria-disabled", "true");
387
- }
388
- if (showSelectedDialCode) {
389
- this.selectedDialCode = this._createEl("div", {
390
- class: "iti__selected-dial-code"
391
- }, this.selectedFlag);
392
- }
393
- if (allowDropdown) {
394
- if (!this.telInput.disabled) {
395
- // make element focusable and tab navigable
396
- this.selectedFlag.setAttribute("tabindex", "0");
397
- }
398
- this.dropdownArrow = this._createEl("div", {
399
- class: "iti__arrow",
400
- "aria-hidden": "true"
401
- }, this.selectedFlag);
402
- const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
403
- this.dropdownContent = this._createEl("div", {
404
- id: `iti-${this.id}__dropdown-content`,
405
- class: `iti__dropdown-content iti__hide ${extraClasses}`
406
- });
407
- if (countrySearch) {
408
- this.searchInput = this._createEl("input", {
409
- type: "text",
410
- class: "iti__search-input",
411
- placeholder: i18n.searchPlaceholder || "Search",
412
- role: "combobox",
413
- "aria-expanded": "true",
414
- "aria-label": i18n.searchPlaceholder || "Search",
415
- "aria-controls": `iti-${this.id}__country-listbox`,
416
- "aria-autocomplete": "list",
417
- autocomplete: "off"
418
- }, this.dropdownContent);
419
- this.searchResultsA11yText = this._createEl("span", {
420
- class: "iti__a11y-text"
421
- }, this.dropdownContent);
422
- }
423
- // country list: preferred countries, then divider, then all countries
424
- this.countryList = this._createEl("ul", {
425
- class: "iti__country-list",
426
- id: `iti-${this.id}__country-listbox`,
427
- role: "listbox",
428
- "aria-label": i18n.countryListAriaLabel || "List of countries"
429
- }, this.dropdownContent);
430
- if (this.preferredCountries.length && !countrySearch) {
431
- this._appendListItems(this.preferredCountries, "iti__preferred", true);
432
- this._createEl("li", {
433
- class: "iti__divider",
434
- "aria-hidden": "true"
435
- }, this.countryList);
436
- }
437
- this._appendListItems(this.countries, "iti__standard");
438
- if (countrySearch) {
439
- this._updateSearchResultsText();
440
- }
441
- // create dropdownContainer markup
442
- if (dropdownContainer) {
443
- let dropdownClasses = "iti iti--container";
444
- if (useFullscreenPopup) {
445
- dropdownClasses += " iti--fullscreen-popup";
446
- } else {
447
- dropdownClasses += " iti--inline-dropdown";
448
- }
449
- if (countrySearch) {
450
- dropdownClasses += " iti--country-search";
451
- }
452
- this.dropdown = this._createEl("div", {
453
- class: dropdownClasses
454
- });
455
- this.dropdown.appendChild(this.dropdownContent);
456
- } else {
457
- this.flagsContainer.appendChild(this.dropdownContent);
458
- }
459
- }
460
- if (hiddenInput) {
461
- const telInputName = this.telInput.getAttribute("name");
462
- const names = hiddenInput(telInputName);
463
- if (names.phone) {
464
- // Create hidden input for the full international number
465
- this.hiddenInput = this._createEl("input", {
466
- type: "hidden",
467
- name: names.phone
468
- });
469
- wrapper.appendChild(this.hiddenInput);
470
- }
471
- if (names.country) {
472
- // Create hidden input for the selected country iso2 code
473
- this.hiddenInputCountry = this._createEl("input", {
474
- type: "hidden",
475
- name: names.country
476
- });
477
- wrapper.appendChild(this.hiddenInputCountry);
478
- }
479
- }
480
- }
481
- // for each of the passed countries: add a country <li> to the countryList <ul> container
482
- _appendListItems(countries, className, preferred) {
483
- for (let i = 0; i < countries.length; i++) {
484
- const c = countries[i];
485
- const idSuffix = preferred ? "-preferred" : "";
486
- const listItem = this._createEl("li", {
487
- id: `iti-${this.id}__item-${c.iso2}${idSuffix}`,
488
- class: `iti__country ${className}`,
489
- tabindex: "-1",
490
- role: "option",
491
- "data-dial-code": c.dialCode,
492
- "data-country-code": c.iso2,
493
- "aria-selected": "false"
494
- }, this.countryList);
495
- // store this for later use e.g. country search filtering
496
- c.nodeById[this.id] = listItem;
497
- let content = "";
498
- // add the flag
499
- if (this.options.showFlags) {
500
- content += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
501
- }
502
- // and the country name and dial code
503
- content += `<span class='iti__country-name'>${c.name}</span>`;
504
- content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
505
- listItem.insertAdjacentHTML("beforeend", content);
506
- }
507
- }
508
- // set the initial state of the input value and the selected flag by:
509
- // 1. extracting a dial code from the given number
510
- // 2. using explicit initialCountry
511
- // 3. picking the first preferred country
512
- // 4. picking the first country
513
- _setInitialState(overrideAutoCountry = false) {
514
- // fix firefox bug: when first load page (with input with value set to number with intl dial
515
- // code) and initialising plugin removes the dial code from the input, then refresh page,
516
- // and we try to init plugin again but this time on number without dial code so get grey flag
517
- const attributeValue = this.telInput.getAttribute("value");
518
- const inputValue = this.telInput.value;
519
- const useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
520
- const val = useAttribute ? attributeValue : inputValue;
521
- const dialCode = this._getDialCode(val);
522
- const isRegionlessNanp = this._isRegionlessNanp(val);
523
- const {
524
- initialCountry
525
- } = this.options;
526
- // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
527
- // flag, else fall back to the default country
528
- if (dialCode && !isRegionlessNanp) {
529
- this._updateFlagFromNumber(val);
530
- } else if (initialCountry !== "auto" || overrideAutoCountry) {
531
- const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
532
- const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
533
- // see if we should select a flag
534
- if (isValidInitialCountry) {
535
- this._setCountry(lowerInitialCountry);
536
- } else {
537
- if (dialCode && isRegionlessNanp) {
538
- // has intl dial code, is regionless nanp, and no initialCountry, so default to US
539
- this._setCountry("us");
540
- } else {
541
- // display the empty state (globe icon)
542
- this._setCountry();
543
- }
544
- }
545
- }
546
- // NOTE: if initialCountry is set to auto, that will be handled separately
547
- // format - note this wont be run after _updateDialCode as that's only called if no val
548
- if (val) {
549
- this._updateValFromNumber(val);
550
- }
551
- }
552
- // initialise the main event listeners: input keyup, and click selected flag
553
- _initListeners() {
554
- this._initTelInputListeners();
555
- if (this.options.allowDropdown) {
556
- this._initDropdownListeners();
557
- }
558
- if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
559
- this._initHiddenInputListener();
560
- }
561
- }
562
- // update hidden input on form submit
563
- _initHiddenInputListener() {
564
- this._handleHiddenInputSubmit = () => {
565
- if (this.hiddenInput) {
566
- this.hiddenInput.value = this.getNumber();
567
- }
568
- if (this.hiddenInputCountry) {
569
- this.hiddenInputCountry.value = this.getSelectedCountryData().iso2;
570
- }
571
- };
572
- this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
573
- }
574
- // initialise the dropdown listeners
575
- _initDropdownListeners() {
576
- // hack for input nested inside label (which is valid markup): clicking the selected-flag to
577
- // open the dropdown would then automatically trigger a 2nd click on the input which would
578
- // close it again
579
- this._handleLabelClick = e => {
580
- // if the dropdown is closed, then focus the input, else ignore the click
581
- if (this.dropdownContent.classList.contains("iti__hide")) {
582
- this.telInput.focus();
583
- } else {
584
- e.preventDefault();
585
- }
586
- };
587
- const label = this.telInput.closest("label");
588
- if (label) {
589
- label.addEventListener("click", this._handleLabelClick);
590
- }
591
- // toggle country dropdown on click
592
- this._handleClickSelectedFlag = () => {
593
- // only intercept this event if we're opening the dropdown
594
- // else let it bubble up to the top ("click-off-to-close" listener)
595
- // we cannot just stopPropagation as it may be needed to close another instance
596
- if (this.dropdownContent.classList.contains("iti__hide") && !this.telInput.disabled && !this.telInput.readOnly) {
597
- this._openDropdown();
598
- }
599
- };
600
- this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
601
- // open dropdown if selected flag is focused and they press up/down/space/enter
602
- this._handleFlagsContainerKeydown = e => {
603
- const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
604
- if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
605
- // prevent form from being submitted if "ENTER" was pressed
606
- e.preventDefault();
607
- // prevent event from being handled again by document
608
- e.stopPropagation();
609
- this._openDropdown();
610
- }
611
- // allow navigation from dropdown to input on TAB
612
- if (e.key === "Tab") {
613
- this._closeDropdown();
614
- }
615
- };
616
- this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
617
- }
618
- // init many requests: utils script / geo ip lookup
619
- _initRequests() {
620
- // if the user has specified the path to the utils script, fetch it on window.load, else resolve
621
- if (this.options.utilsScript && !window.intlTelInputUtils) {
622
- // if the plugin is being initialised after the window.load event has already been fired
623
- if (window.intlTelInputGlobals.documentReady()) {
624
- window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
625
- } else {
626
- // wait until the load event so we don't block any other requests e.g. the flags image
627
- window.addEventListener("load", () => {
628
- window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
629
- });
630
- }
631
- } else {
632
- this.resolveUtilsScriptPromise();
633
- }
634
- // dont bother with IP lookup if we already have a selected country
635
- if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
636
- this._loadAutoCountry();
637
- } else {
638
- this.resolveAutoCountryPromise();
639
- }
640
- }
641
- // perform the geo ip lookup
642
- _loadAutoCountry() {
643
- // 3 options:
644
- // 1) already loaded (we're done)
645
- // 2) not already started loading (start)
646
- // 3) already started loading (do nothing - just wait for loading callback to fire)
647
- if (window.intlTelInputGlobals.autoCountry) {
648
- this.handleAutoCountry();
649
- } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
650
- // don't do this twice!
651
- window.intlTelInputGlobals.startedLoadingAutoCountry = true;
652
- if (typeof this.options.geoIpLookup === "function") {
653
- this.options.geoIpLookup((iso2 = "") => {
654
- const iso2Lower = iso2.toLowerCase();
655
- const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
656
- if (isValidIso2) {
657
- window.intlTelInputGlobals.autoCountry = iso2Lower;
658
- // tell all instances the auto country is ready
659
- // TODO: this should just be the current instances
660
- // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
661
- // away (e.g. if they have already done the geo ip lookup somewhere else). Using
662
- // setTimeout means that the current thread of execution will finish before executing
663
- // this, which allows the plugin to finish initialising.
664
- setTimeout(() => forEachInstance("handleAutoCountry"));
665
- } else {
666
- this._setInitialState(true);
667
- forEachInstance("rejectAutoCountryPromise");
668
- }
669
- }, () => forEachInstance("rejectAutoCountryPromise"));
670
- }
671
- }
672
- }
673
- // initialize the tel input listeners
674
- _initTelInputListeners() {
675
- const {
676
- strictMode,
677
- formatAsYouType
678
- } = this.options;
679
- let userOverrideFormatting = false;
680
- // update flag on input event
681
- this._handleInputEvent = e => {
682
- if (this._updateFlagFromNumber(this.telInput.value)) {
683
- this._triggerCountryChange();
684
- }
685
- // if user types their own formatting char (not a plus or a numeric), or they paste something, then set the override
686
- const isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
687
- const isPaste = e && e.inputType === "insertFromPaste" && this.telInput.value;
688
- if (isFormattingChar || isPaste && !strictMode) {
689
- userOverrideFormatting = true;
690
- }
691
- // if user removes all formatting chars, then reset the override
692
- else if (!/[^+0-9]/.test(this.telInput.value)) {
693
- userOverrideFormatting = false;
694
- }
695
- // handle FAYT, unless userOverrideFormatting
696
- if (formatAsYouType && !userOverrideFormatting) {
697
- // maintain caret position after reformatting
698
- const currentCaretPos = this.telInput.selectionStart;
699
- const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
700
- const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
701
- const isDeleteForwards = e && e.inputType === "deleteContentForward";
702
- const formattedValue = this._formatNumberAsYouType();
703
- const newCaretPos = this._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
704
- this.telInput.value = formattedValue;
705
- this.telInput.setSelectionRange(newCaretPos, newCaretPos);
706
- }
707
- };
708
- // this handles individual key presses as well as cut/paste events
709
- // the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
710
- // whereas "keyup" fires even for shift key, arrow key presses etc
711
- this.telInput.addEventListener("input", this._handleInputEvent);
712
- if (strictMode) {
713
- this._handleKeydownEvent = e => {
714
- // only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
715
- if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
716
- const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
717
- const isNumeric = /^[0-9]$/.test(e.key);
718
- const isAllowedChar = isInitialPlus || isNumeric;
719
- const fullNumber = this._getFullNumber();
720
- const coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
721
- const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
722
- if (!isAllowedChar || hasReachedMaxLength) {
723
- e.preventDefault();
724
- }
725
- }
726
- };
727
- this.telInput.addEventListener("keydown", this._handleKeydownEvent);
728
- }
729
- }
730
- // iterate through the formattedValue until hit the right number of relevant chars
731
- _translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
732
- // if the first char is a formatting char, and they backspace delete it:
733
- // cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
734
- if (prevCaretPos === 0 && !isDeleteForwards) {
735
- return 0;
736
- }
737
- let count = 0;
738
- for (let i = 0; i < formattedValue.length; i++) {
739
- if (/[+0-9]/.test(formattedValue[i])) {
740
- count++;
741
- }
742
- // normal case: stop when you hit the right number of relevant chars
743
- // (cursor will be just after the final relevant char)
744
- if (count === relevantChars && !isDeleteForwards) {
745
- return i + 1;
746
- }
747
- // spacial case: delete forwards (fn + delete on a mac):
748
- // wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
749
- if (isDeleteForwards && count === relevantChars + 1) {
750
- return i;
751
- }
752
- }
753
- return formattedValue.length;
754
- }
755
- // adhere to the input's maxlength attr
756
- _cap(number) {
757
- const max = this.telInput.getAttribute("maxlength");
758
- return max && number.length > max ? number.substr(0, max) : number;
759
- }
760
- // clear the input if it just contains a dial code
761
- _removeEmptyDialCode() {
762
- if (this.telInput.value.charAt(0) === "+") {
763
- const numeric = this._getNumeric(this.telInput.value);
764
- // if just a plus, or if just a dial code
765
- if (!numeric || this.selectedCountryData.dialCode === numeric) {
766
- this.telInput.value = "";
767
- }
768
- }
769
- }
770
- // extract the numeric digits from the given string
771
- _getNumeric(s) {
772
- return s.replace(/\D/g, "");
773
- }
774
- // trigger a custom event on the input
775
- _trigger(name) {
776
- const e = new Event(name, {
777
- bubbles: true,
778
- cancelable: true
779
- });
780
- this.telInput.dispatchEvent(e);
781
- }
782
- // open the dropdown
783
- _openDropdown() {
784
- const {
785
- fixDropdownWidth,
786
- countrySearch
787
- } = this.options;
788
- if (fixDropdownWidth) {
789
- this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
790
- }
791
- this.dropdownContent.classList.remove("iti__hide");
792
- this.selectedFlag.setAttribute("aria-expanded", "true");
793
- this._setDropdownPosition();
794
- // if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
795
- // else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
796
- if (this.activeItem && !countrySearch) {
797
- // update highlighting and scroll to active list item
798
- this._highlightListItem(this.activeItem, false);
799
- this._scrollTo(this.activeItem, true);
800
- } else {
801
- // start by highlighting the first item in the list
802
- const {
803
- firstElementChild
804
- } = this.countryList;
805
- if (firstElementChild) {
806
- this._highlightListItem(firstElementChild, false);
807
- this.countryList.scrollTop = 0;
808
- }
809
- if (countrySearch) {
810
- this.searchInput.focus();
811
- }
812
- }
813
- // bind all the dropdown-related listeners: mouseover, click, click-off, keydown
814
- this._bindDropdownListeners();
815
- // update the arrow
816
- this.dropdownArrow.classList.add("iti__arrow--up");
817
- this._trigger("open:countrydropdown");
818
- }
819
- // make sure the el has the className or not, depending on the value of shouldHaveClass
820
- _toggleClass(el, className, shouldHaveClass) {
821
- if (shouldHaveClass && !el.classList.contains(className)) {
822
- el.classList.add(className);
823
- } else if (!shouldHaveClass && el.classList.contains(className)) {
824
- el.classList.remove(className);
825
- }
826
- }
827
- // decide if should position dropdown above or below input (depends on position within viewport, and scroll)
828
- _setDropdownPosition() {
829
- if (this.options.dropdownContainer) {
830
- this.options.dropdownContainer.appendChild(this.dropdown);
831
- }
832
- if (!this.options.useFullscreenPopup) {
833
- const pos = this.telInput.getBoundingClientRect();
834
- // windowTop from https://stackoverflow.com/a/14384091/217866
835
- const windowTop = document.documentElement.scrollTop;
836
- const inputTop = pos.top + windowTop;
837
- const dropdownHeight = this.dropdownContent.offsetHeight;
838
- // dropdownFitsBelow = (dropdownBottom < windowBottom)
839
- const dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
840
- const dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
841
- // dont allow positioning above when country search enabled as the search box jumps around as you filter countries
842
- const positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
843
- // by default, the dropdown will be below the input. If we want to position it above the
844
- // input, we add the dropup class.
845
- this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
846
- // if dropdownContainer is enabled, calculate postion
847
- if (this.options.dropdownContainer) {
848
- // if we want to position the dropdown below the input, we need to add the input height to the top value
849
- const extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
850
- // calculate placement
851
- this.dropdown.style.top = `${inputTop + extraTop}px`;
852
- this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
853
- // close menu on window scroll
854
- this._handleWindowScroll = () => this._closeDropdown();
855
- window.addEventListener("scroll", this._handleWindowScroll);
856
- }
857
- }
858
- }
859
- // we only bind dropdown listeners when the dropdown is open
860
- _bindDropdownListeners() {
861
- // when mouse over a list item, just highlight that one
862
- // we add the class "highlight", so if they hit "enter" we know which one to select
863
- this._handleMouseoverCountryList = e => {
864
- // handle event delegation, as we're listening for this event on the countryList
865
- const listItem = e.target.closest(".iti__country");
866
- if (listItem) {
867
- this._highlightListItem(listItem, false);
868
- }
869
- };
870
- this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
871
- // listen for country selection
872
- this._handleClickCountryList = e => {
873
- const listItem = e.target.closest(".iti__country");
874
- if (listItem) {
875
- this._selectListItem(listItem);
876
- }
877
- };
878
- this.countryList.addEventListener("click", this._handleClickCountryList);
879
- // click off to close
880
- // (except when this initial opening click is bubbling up)
881
- // we cannot just stopPropagation as it may be needed to close another instance
882
- let isOpening = true;
883
- this._handleClickOffToClose = () => {
884
- if (!isOpening) {
885
- this._closeDropdown();
886
- }
887
- isOpening = false;
888
- };
889
- document.documentElement.addEventListener("click", this._handleClickOffToClose);
890
- // listen for up/down scrolling, enter to select, or escape to close
891
- // use keydown as keypress doesn't fire for non-char keys and we want to catch if they
892
- // just hit down and hold it to scroll down (no keyup event).
893
- // listen on the document because that's where key events are triggered if no input has focus
894
- let query = "";
895
- let queryTimer = null;
896
- this._handleKeydownOnDropdown = e => {
897
- // prevent down key from scrolling the whole page,
898
- // and enter key from submitting a form etc
899
- if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
900
- e.preventDefault();
901
- e.stopPropagation();
902
- // up and down to navigate
903
- if (e.key === "ArrowUp" || e.key === "ArrowDown") {
904
- this._handleUpDownKey(e.key);
905
- }
906
- // enter to select
907
- else if (e.key === "Enter") {
908
- this._handleEnterKey();
909
- }
910
- // esc to close
911
- else if (e.key === "Escape") {
912
- this._closeDropdown();
913
- }
914
- }
915
- // alpha chars to perform search
916
- // regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
917
- if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
918
- e.stopPropagation();
919
- // jump to countries that start with the query string
920
- if (queryTimer) {
921
- clearTimeout(queryTimer);
922
- }
923
- query += e.key.toLowerCase();
924
- this._searchForCountry(query);
925
- // if the timer hits 1 second, reset the query
926
- queryTimer = setTimeout(() => {
927
- query = "";
928
- }, 1e3);
929
- }
930
- };
931
- document.addEventListener("keydown", this._handleKeydownOnDropdown);
932
- if (this.options.countrySearch) {
933
- const doFilter = () => {
934
- const inputQuery = this.searchInput.value.trim();
935
- if (inputQuery) {
936
- this._filterCountries(inputQuery);
937
- } else {
938
- this._filterCountries("", true);
939
- }
940
- };
941
- let keyupTimer = null;
942
- this._handleSearchChange = () => {
943
- // filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
944
- if (keyupTimer) {
945
- clearTimeout(keyupTimer);
946
- }
947
- keyupTimer = setTimeout(() => {
948
- doFilter();
949
- keyupTimer = null;
950
- }, 100);
951
- };
952
- this.searchInput.addEventListener("input", this._handleSearchChange);
953
- // stop propagation on search input click, so doesn't trigger click-off-to-close listener
954
- this.searchInput.addEventListener("click", e => e.stopPropagation());
955
- }
956
- }
957
- // turns "Réunion" into "Reunion"
958
- // from https://stackoverflow.com/a/37511463
959
- _normaliseString(s = "") {
960
- return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
961
- }
962
- _filterCountries(query, isReset = false) {
963
- let isFirst = true;
964
- this.countryList.innerHTML = "";
965
- const normalisedQuery = this._normaliseString(query);
966
- for (let i = 0; i < this.countries.length; i++) {
967
- const c = this.countries[i];
968
- const normalisedCountryName = this._normaliseString(c.name);
969
- const fullDialCode = `+${c.dialCode}`;
970
- if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
971
- this.countryList.appendChild(c.nodeById[this.id]);
972
- // highlight the first item
973
- if (isFirst) {
974
- this._highlightListItem(c.nodeById[this.id], false);
975
- isFirst = false;
976
- }
977
- }
978
- }
979
- // scroll to top (useful if user had previously scrolled down)
980
- this.countryList.scrollTop = 0;
981
- this._updateSearchResultsText();
982
- }
983
- // update search results text (for a11y)
984
- _updateSearchResultsText() {
985
- const {
986
- i18n
987
- } = this.options;
988
- const count = this.countryList.childElementCount;
989
- let searchText;
990
- if (count === 0) {
991
- searchText = i18n.zeroSearchResults || "No results found";
992
- } else if (count === 1) {
993
- searchText = i18n.oneSearchResult || "1 result found";
994
- } else {
995
- // eslint-disable-next-line no-template-curly-in-string
996
- searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : `${count} results found`;
997
- }
998
- this.searchResultsA11yText.textContent = searchText;
999
- }
1000
- // highlight the next/prev item in the list (and ensure it is visible)
1001
- _handleUpDownKey(key) {
1002
- let next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
1003
- if (next) {
1004
- // skip the divider
1005
- if (next.classList.contains("iti__divider")) {
1006
- next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
1007
- }
1008
- } else if (this.countryList.childElementCount > 1) {
1009
- // otherwise, we must be at the end, so loop round again
1010
- next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
1011
- }
1012
- if (next) {
1013
- // make sure the next item is visible
1014
- // (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
1015
- this._scrollTo(next, false);
1016
- // if country search enabled, dont lose focus from the search input on up/down
1017
- const doFocus = !this.options.countrySearch;
1018
- this._highlightListItem(next, doFocus);
1019
- }
1020
- }
1021
- // select the currently highlighted item
1022
- _handleEnterKey() {
1023
- if (this.highlightedItem) {
1024
- this._selectListItem(this.highlightedItem);
1025
- }
1026
- }
1027
- // find the first list item whose name starts with the query string
1028
- _searchForCountry(query) {
1029
- for (let i = 0; i < this.countries.length; i++) {
1030
- if (this._startsWith(this.countries[i].name, query)) {
1031
- const listItem = this.countries[i].nodeById[this.id];
1032
- // update highlighting and scroll
1033
- this._highlightListItem(listItem, false);
1034
- this._scrollTo(listItem, true);
1035
- break;
1036
- }
1037
- }
1038
- }
1039
- // check if string a starts with string b
1040
- _startsWith(a, b) {
1041
- return a.substr(0, b.length).toLowerCase() === b;
1042
- }
1043
- // update the input's value to the given val (format first if possible)
1044
- // NOTE: this is called from _setInitialState, handleUtils and setNumber
1045
- _updateValFromNumber(fullNumber) {
1046
- let number = fullNumber;
1047
- if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
1048
- const useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
1049
- const {
1050
- NATIONAL,
1051
- INTERNATIONAL
1052
- } = intlTelInputUtils.numberFormat;
1053
- const format = useNational ? NATIONAL : INTERNATIONAL;
1054
- number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
1055
- }
1056
- number = this._beforeSetNumber(number);
1057
- this.telInput.value = number;
1058
- }
1059
- // check if need to select a new flag based on the given number
1060
- // Note: called from _setInitialState, keyup handler, setNumber
1061
- _updateFlagFromNumber(fullNumber) {
1062
- const plusIndex = fullNumber.indexOf("+");
1063
- // if it contains a plus, discard any chars before it e.g. accidental space char.
1064
- // this keeps the selected country auto-updating correctly, which we want as
1065
- // libphonenumber's validation/getNumber methods will ignore these chars anyway
1066
- let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
1067
- // if we already have US/Canada selected, make sure the number starts
1068
- // with a +1 so _getDialCode will be able to extract the area code
1069
- // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
1070
- // from the number), that means we're initialising the plugin with a number that already has a
1071
- // dial code, so fine to ignore this bit
1072
- const selectedDialCode = this.selectedCountryData.dialCode;
1073
- const isNanp = selectedDialCode === "1";
1074
- if (number && isNanp && number.charAt(0) !== "+") {
1075
- if (number.charAt(0) !== "1") {
1076
- number = `1${number}`;
1077
- }
1078
- number = `+${number}`;
1079
- }
1080
- // if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
1081
- if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
1082
- number = `+${selectedDialCode}${number}`;
1083
- }
1084
- // try and extract valid dial code from input
1085
- const dialCode = this._getDialCode(number, true);
1086
- const numeric = this._getNumeric(number);
1087
- let iso2 = null;
1088
- if (dialCode) {
1089
- const iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
1090
- // check if the right country is already selected. this should be false if the number is
1091
- // longer than the matched dial code because in this case we need to make sure that if
1092
- // there are multiple country matches, that the first one is selected (note: we could
1093
- // just check that here, but it requires the same loop that we already have later)
1094
- const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
1095
- const isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
1096
- // only update the flag if:
1097
- // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
1098
- // AND
1099
- // B) the right country is not already selected
1100
- if (!isRegionlessNanpNumber && !alreadySelected) {
1101
- // if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
1102
- // non-empty index
1103
- for (let j = 0; j < iso2Codes.length; j++) {
1104
- if (iso2Codes[j]) {
1105
- iso2 = iso2Codes[j];
1106
- break;
1107
- }
1108
- }
1109
- }
1110
- } else if (number.charAt(0) === "+" && numeric.length) {
1111
- // invalid dial code, so empty
1112
- // Note: use getNumeric here because the number has not been formatted yet, so could contain
1113
- // bad chars
1114
- iso2 = "";
1115
- } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1116
- // if no selected flag, and user either clears the input, or just types a plus, then show default
1117
- iso2 = this.defaultCountry;
1118
- }
1119
- if (iso2 !== null) {
1120
- return this._setCountry(iso2);
1121
- }
1122
- return false;
1123
- }
1124
- // check if the given number is a regionless NANP number (expects the number to contain an
1125
- // international dial code)
1126
- _isRegionlessNanp(number) {
1127
- const numeric = this._getNumeric(number);
1128
- if (numeric.charAt(0) === "1") {
1129
- const areaCode = numeric.substr(1, 3);
1130
- return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1131
- }
1132
- return false;
1133
- }
1134
- // remove highlighting from other list items and highlight the given item
1135
- _highlightListItem(listItem, shouldFocus) {
1136
- const prevItem = this.highlightedItem;
1137
- if (prevItem) {
1138
- prevItem.classList.remove("iti__highlight");
1139
- prevItem.setAttribute("aria-selected", "false");
1140
- }
1141
- this.highlightedItem = listItem;
1142
- this.highlightedItem.classList.add("iti__highlight");
1143
- this.highlightedItem.setAttribute("aria-selected", "true");
1144
- this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1145
- if (this.options.countrySearch) {
1146
- this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1147
- }
1148
- if (shouldFocus) {
1149
- this.highlightedItem.focus();
1150
- }
1151
- }
1152
- // find the country data for the given iso2 code
1153
- // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
1154
- _getCountryData(iso2, allowFail) {
1155
- for (let i = 0; i < this.countries.length; i++) {
1156
- if (this.countries[i].iso2 === iso2) {
1157
- return this.countries[i];
1158
- }
1159
- }
1160
- if (allowFail) {
1161
- return null;
1162
- }
1163
- throw new Error(`No country data for '${iso2}'`);
1164
- }
1165
- // update the selected flag, dial code (if showSelectedDialCode), placeholder, title, and active list item
1166
- // Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry
1167
- _setCountry(iso2) {
1168
- const {
1169
- allowDropdown,
1170
- showSelectedDialCode,
1171
- showFlags,
1172
- countrySearch,
1173
- i18n
1174
- } = this.options;
1175
- const prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
1176
- // do this first as it will throw an error and stop if iso2 is invalid
1177
- this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
1178
- // update the defaultCountry - we only need the iso2 from now on, so just store that
1179
- if (this.selectedCountryData.iso2) {
1180
- this.defaultCountry = this.selectedCountryData.iso2;
1181
- }
1182
- // update the flag class and the a11y text
1183
- if (this.selectedFlagInner) {
1184
- let flagClass = "";
1185
- let a11yText = "";
1186
- if (iso2) {
1187
- if (showFlags) {
1188
- flagClass = `iti__flag iti__${iso2}`;
1189
- a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
1190
- }
1191
- // if showFlags disabled and showSelectedDialCode is enabled, we dont show a flag or have any a11y text, as the displayed dial code is enough
1192
- } else {
1193
- flagClass = "iti__flag iti__globe";
1194
- a11yText = i18n.noCountrySelected || "No country selected";
1195
- }
1196
- this.selectedFlagInner.className = flagClass;
1197
- this.selectedFlagA11yText.textContent = a11yText;
1198
- }
1199
- this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
1200
- // update the selected dial code
1201
- if (showSelectedDialCode) {
1202
- const dialCode = this.selectedCountryData.dialCode ? `+${this.selectedCountryData.dialCode}` : "";
1203
- this.selectedDialCode.innerHTML = dialCode;
1204
- // offsetWidth is zero if input is in a hidden container during initialisation
1205
- const selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1206
- // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1207
- if (this.isRTL) {
1208
- this.telInput.style.paddingRight = `${selectedFlagWidth + 6}px`;
1209
- } else {
1210
- this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
1211
- }
1212
- }
1213
- // and the input's placeholder
1214
- this._updatePlaceholder();
1215
- // update the maximum valid number length
1216
- this._updateMaxLength();
1217
- // update the active list item (only if country search disabled, as country search doesn't store the active item)
1218
- if (allowDropdown && !countrySearch) {
1219
- const prevItem = this.activeItem;
1220
- if (prevItem) {
1221
- prevItem.classList.remove("iti__active");
1222
- prevItem.setAttribute("aria-selected", "false");
1223
- }
1224
- if (iso2) {
1225
- // check if there is a preferred item first, else fall back to standard
1226
- const nextItem = this.countryList.querySelector(`#iti-${this.id}__item-${iso2}-preferred`) || this.countryList.querySelector(`#iti-${this.id}__item-${iso2}`);
1227
- nextItem.setAttribute("aria-selected", "true");
1228
- nextItem.classList.add("iti__active");
1229
- this.activeItem = nextItem;
1230
- }
1231
- }
1232
- // return if the flag has changed or not
1233
- return prevCountry.iso2 !== iso2;
1234
- }
1235
- // update the maximum valid number length for the currently selected country
1236
- _updateMaxLength() {
1237
- if (this.options.strictMode && window.intlTelInputUtils) {
1238
- if (this.selectedCountryData.iso2) {
1239
- const numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
1240
- let exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
1241
- // see if adding more digits is still valid to get the true maximum valid length
1242
- let validNumber = exampleNumber;
1243
- while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
1244
- validNumber = exampleNumber;
1245
- exampleNumber += "0";
1246
- }
1247
- const coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
1248
- this.maxCoreNumberLength = coreNumber.length;
1249
- } else {
1250
- this.maxCoreNumberLength = null;
1251
- }
1252
- }
1253
- }
1254
- _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
1255
- if (!this.selectedFlag) {
1256
- return;
1257
- }
1258
- let title;
1259
- if (iso2 && !showSelectedDialCode) {
1260
- title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
1261
- } else if (iso2) {
1262
- // For screen reader output, we don't want to include the dial code in the reader output twice
1263
- // so just use the selected country name here:
1264
- title = this.selectedCountryData.name;
1265
- } else {
1266
- title = "Unknown";
1267
- }
1268
- this.selectedFlag.setAttribute("title", title);
1269
- }
1270
- // when the input is in a hidden container during initialisation, we must inject some markup
1271
- // into the end of the DOM to calculate the correct offsetWidth
1272
- // NOTE: this is only used when showSelectedDialCode is enabled, so flagsContainer and selectedFlag
1273
- // will definitely exist
1274
- _getHiddenSelectedFlagWidth() {
1275
- // to get the right styling to apply, all we need is a shallow clone of the container,
1276
- // and then to inject a deep clone of the selectedFlag element
1277
- const containerClone = this.telInput.parentNode.cloneNode();
1278
- containerClone.style.visibility = "hidden";
1279
- document.body.appendChild(containerClone);
1280
- const flagsContainerClone = this.flagsContainer.cloneNode();
1281
- containerClone.appendChild(flagsContainerClone);
1282
- const selectedFlagClone = this.selectedFlag.cloneNode(true);
1283
- flagsContainerClone.appendChild(selectedFlagClone);
1284
- const width = selectedFlagClone.offsetWidth;
1285
- containerClone.parentNode.removeChild(containerClone);
1286
- return width;
1287
- }
1288
- // update the input placeholder to an example number from the currently selected country
1289
- _updatePlaceholder() {
1290
- const {
1291
- autoPlaceholder,
1292
- placeholderNumberType,
1293
- nationalMode,
1294
- customPlaceholder
1295
- } = this.options;
1296
- const shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
1297
- if (window.intlTelInputUtils && shouldSetPlaceholder) {
1298
- const numberType = intlTelInputUtils.numberType[placeholderNumberType];
1299
- // note: must set placeholder to empty string if no country selected (globe icon showing)
1300
- let placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
1301
- placeholder = this._beforeSetNumber(placeholder);
1302
- if (typeof customPlaceholder === "function") {
1303
- placeholder = customPlaceholder(placeholder, this.selectedCountryData);
1304
- }
1305
- this.telInput.setAttribute("placeholder", placeholder);
1306
- }
1307
- }
1308
- // called when the user selects a list item from the dropdown
1309
- _selectListItem(listItem) {
1310
- // update selected flag and active list item
1311
- const flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
1312
- this._closeDropdown();
1313
- this._updateDialCode(listItem.getAttribute("data-dial-code"));
1314
- // focus the input
1315
- this.telInput.focus();
1316
- if (flagChanged) {
1317
- this._triggerCountryChange();
1318
- }
1319
- }
1320
- // close the dropdown and unbind any listeners
1321
- _closeDropdown() {
1322
- this.dropdownContent.classList.add("iti__hide");
1323
- this.selectedFlag.setAttribute("aria-expanded", "false");
1324
- this.selectedFlag.removeAttribute("aria-activedescendant");
1325
- if (this.highlightedItem) {
1326
- this.highlightedItem.setAttribute("aria-selected", "false");
1327
- }
1328
- if (this.options.countrySearch) {
1329
- this.searchInput.removeAttribute("aria-activedescendant");
1330
- }
1331
- // update the arrow
1332
- this.dropdownArrow.classList.remove("iti__arrow--up");
1333
- // unbind key events
1334
- document.removeEventListener("keydown", this._handleKeydownOnDropdown);
1335
- if (this.options.countrySearch) {
1336
- this.searchInput.removeEventListener("input", this._handleSearchChange);
1337
- }
1338
- document.documentElement.removeEventListener("click", this._handleClickOffToClose);
1339
- this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
1340
- this.countryList.removeEventListener("click", this._handleClickCountryList);
1341
- // remove menu from container
1342
- if (this.options.dropdownContainer) {
1343
- if (!this.options.useFullscreenPopup) {
1344
- window.removeEventListener("scroll", this._handleWindowScroll);
1345
- }
1346
- if (this.dropdown.parentNode) {
1347
- this.dropdown.parentNode.removeChild(this.dropdown);
1348
- }
1349
- }
1350
- this._trigger("close:countrydropdown");
1351
- }
1352
- // check if an element is visible within it's container, else scroll until it is
1353
- _scrollTo(element, middle) {
1354
- const container = this.countryList;
1355
- // windowTop from https://stackoverflow.com/a/14384091/217866
1356
- const windowTop = document.documentElement.scrollTop;
1357
- const containerHeight = container.offsetHeight;
1358
- const containerTop = container.getBoundingClientRect().top + windowTop;
1359
- const containerBottom = containerTop + containerHeight;
1360
- const elementHeight = element.offsetHeight;
1361
- const elementTop = element.getBoundingClientRect().top + windowTop;
1362
- const elementBottom = elementTop + elementHeight;
1363
- let newScrollTop = elementTop - containerTop + container.scrollTop;
1364
- const middleOffset = containerHeight / 2 - elementHeight / 2;
1365
- if (elementTop < containerTop) {
1366
- // scroll up
1367
- if (middle) {
1368
- newScrollTop -= middleOffset;
1369
- }
1370
- container.scrollTop = newScrollTop;
1371
- } else if (elementBottom > containerBottom) {
1372
- // scroll down
1373
- if (middle) {
1374
- newScrollTop += middleOffset;
1375
- }
1376
- const heightDifference = containerHeight - elementHeight;
1377
- container.scrollTop = newScrollTop - heightDifference;
1378
- }
1379
- }
1380
- // replace any existing dial code with the new one
1381
- // Note: called from _selectListItem and setCountry
1382
- _updateDialCode(newDialCodeBare) {
1383
- const inputVal = this.telInput.value;
1384
- // save having to pass this every time
1385
- const newDialCode = `+${newDialCodeBare}`;
1386
- let newNumber;
1387
- if (inputVal.charAt(0) === "+") {
1388
- // there's a plus so we're dealing with a replacement
1389
- const prevDialCode = this._getDialCode(inputVal);
1390
- if (prevDialCode) {
1391
- // current number contains a valid dial code, so replace it
1392
- newNumber = inputVal.replace(prevDialCode, newDialCode);
1393
- } else {
1394
- // current number contains an invalid dial code, so ditch it
1395
- // (no way to determine where the invalid dial code ends and the rest of the number begins)
1396
- newNumber = newDialCode;
1397
- }
1398
- this.telInput.value = newNumber;
1399
- }
1400
- }
1401
- // try and extract a valid international dial code from a full telephone number
1402
- // Note: returns the raw string inc plus character and any whitespace/dots etc
1403
- _getDialCode(number, includeAreaCode) {
1404
- let dialCode = "";
1405
- // only interested in international numbers (starting with a plus)
1406
- if (number.charAt(0) === "+") {
1407
- let numericChars = "";
1408
- // iterate over chars
1409
- for (let i = 0; i < number.length; i++) {
1410
- const c = number.charAt(i);
1411
- // if char is number (https://stackoverflow.com/a/8935649/217866)
1412
- if (!isNaN(parseInt(c, 10))) {
1413
- numericChars += c;
1414
- // if current numericChars make a valid dial code
1415
- if (includeAreaCode) {
1416
- if (this.dialCodeToIso2Map[numericChars]) {
1417
- // store the actual raw string (useful for matching later)
1418
- dialCode = number.substr(0, i + 1);
1419
- }
1420
- } else {
1421
- if (this.dialCodes[numericChars]) {
1422
- dialCode = number.substr(0, i + 1);
1423
- // if we're just looking for a dial code, we can break as soon as we find one
1424
- break;
1425
- }
1426
- }
1427
- // stop searching as soon as we can - in this case when we hit max len
1428
- if (numericChars.length === this.dialCodeMaxLen) {
1429
- break;
1430
- }
1431
- }
1432
- }
1433
- }
1434
- return dialCode;
1435
- }
1436
- // get the input val, adding the dial code if showSelectedDialCode is enabled
1437
- _getFullNumber() {
1438
- const val = this.telInput.value.trim();
1439
- const {
1440
- dialCode
1441
- } = this.selectedCountryData;
1442
- let prefix;
1443
- const numericVal = this._getNumeric(val);
1444
- if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
1445
- // when using showSelectedDialCode, it is visible so is effectively part of the typed number
1446
- prefix = `+${dialCode}`;
1447
- } else {
1448
- prefix = "";
1449
- }
1450
- return prefix + val;
1451
- }
1452
- // remove the dial code if showSelectedDialCode is enabled
1453
- // also cap the length if the input has a maxlength attribute
1454
- _beforeSetNumber(fullNumber) {
1455
- let number = fullNumber;
1456
- if (this.options.showSelectedDialCode) {
1457
- let dialCode = this._getDialCode(number);
1458
- // if there is a valid dial code
1459
- if (dialCode) {
1460
- // in case _getDialCode returned an area code as well
1461
- dialCode = `+${this.selectedCountryData.dialCode}`;
1462
- // a lot of numbers will have a space separating the dial code and the main number, and
1463
- // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
1464
- // rid of it
1465
- // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
1466
- const start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
1467
- number = number.substr(start);
1468
- }
1469
- }
1470
- return this._cap(number);
1471
- }
1472
- // trigger the 'countrychange' event
1473
- _triggerCountryChange() {
1474
- this._trigger("countrychange");
1475
- }
1476
- // format the number as the user types
1477
- _formatNumberAsYouType() {
1478
- const val = this._getFullNumber();
1479
- const result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
1480
- // if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
1481
- const {
1482
- dialCode
1483
- } = this.selectedCountryData;
1484
- if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes(`+${dialCode}`)) {
1485
- const afterDialCode = result.split(`+${dialCode}`)[1] || "";
1486
- return afterDialCode.trim();
1487
- }
1488
- return result;
1489
- }
1490
- /**************************
1491
- * SECRET PUBLIC METHODS
1492
- **************************/
1493
- // this is called when the geoip call returns
1494
- handleAutoCountry() {
1495
- if (this.options.initialCountry === "auto") {
1496
- // we must set this even if there is an initial val in the input: in case the initial val is
1497
- // invalid and they delete it - they should see their auto country
1498
- this.defaultCountry = window.intlTelInputGlobals.autoCountry;
1499
- // if there's no initial value in the input, then update the flag
1500
- if (!this.telInput.value) {
1501
- this.setCountry(this.defaultCountry);
1502
- }
1503
- this.resolveAutoCountryPromise();
1504
- }
1505
- }
1506
- // this is called when the utils request completes
1507
- handleUtils() {
1508
- // if the request was successful
1509
- if (window.intlTelInputUtils) {
1510
- // if there's an initial value in the input, then format it
1511
- if (this.telInput.value) {
1512
- this._updateValFromNumber(this.telInput.value);
1513
- }
1514
- if (this.selectedCountryData.iso2) {
1515
- this._updatePlaceholder();
1516
- this._updateMaxLength();
1517
- }
1518
- }
1519
- this.resolveUtilsScriptPromise();
1520
- }
1521
- /********************
1522
- * PUBLIC METHODS
1523
- ********************/
1524
- // remove plugin
1525
- destroy() {
1526
- if (this.options.allowDropdown) {
1527
- // make sure the dropdown is closed (and unbind listeners)
1528
- this._closeDropdown();
1529
- this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
1530
- this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
1531
- // label click hack
1532
- const label = this.telInput.closest("label");
1533
- if (label) {
1534
- label.removeEventListener("click", this._handleLabelClick);
1535
- }
1536
- }
1537
- // unbind hiddenInput listeners
1538
- const {
1539
- form
1540
- } = this.telInput;
1541
- if (this._handleHiddenInputSubmit && form) {
1542
- form.removeEventListener("submit", this._handleHiddenInputSubmit);
1543
- }
1544
- // unbind key events, and cut/paste events
1545
- this.telInput.removeEventListener("input", this._handleInputEvent);
1546
- if (this._handleKeydownEvent) {
1547
- this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
1548
- }
1549
- // remove attribute of id instance: data-intl-tel-input-id
1550
- this.telInput.removeAttribute("data-intl-tel-input-id");
1551
- // remove markup (but leave the original input)
1552
- const wrapper = this.telInput.parentNode;
1553
- wrapper.parentNode.insertBefore(this.telInput, wrapper);
1554
- wrapper.parentNode.removeChild(wrapper);
1555
- delete window.intlTelInputGlobals.instances[this.id];
1556
- }
1557
- // get the extension from the current number
1558
- getExtension() {
1559
- if (window.intlTelInputUtils) {
1560
- return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
1561
- }
1562
- return "";
1563
- }
1564
- // format the number to the given format
1565
- getNumber(format) {
1566
- if (window.intlTelInputUtils) {
1567
- const {
1568
- iso2
1569
- } = this.selectedCountryData;
1570
- return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
1571
- }
1572
- return "";
1573
- }
1574
- // get the type of the entered number e.g. landline/mobile
1575
- getNumberType() {
1576
- if (window.intlTelInputUtils) {
1577
- return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
1578
- }
1579
- return -99;
1580
- }
1581
- // get the country data for the currently selected flag
1582
- getSelectedCountryData() {
1583
- return this.selectedCountryData;
1584
- }
1585
- // get the validation error
1586
- getValidationError() {
1587
- if (window.intlTelInputUtils) {
1588
- const {
1589
- iso2
1590
- } = this.selectedCountryData;
1591
- return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
1592
- }
1593
- return -99;
1594
- }
1595
- // validate the input val - assumes the global function isPossibleNumber (from utilsScript)
1596
- isValidNumber(mobileOnly = true) {
1597
- const val = this._getFullNumber();
1598
- // return false for any alpha chars
1599
- if (/\p{L}/u.test(val)) {
1600
- return false;
1601
- }
1602
- return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
1603
- }
1604
- // validate the input val (precise) - assumes the global function isValidNumber (from utilsScript)
1605
- isValidNumberPrecise() {
1606
- const val = this._getFullNumber();
1607
- // return false for any alpha chars
1608
- if (/\p{L}/u.test(val)) {
1609
- return false;
1610
- }
1611
- return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
1612
- }
1613
- // update the selected flag, and update the input val accordingly
1614
- setCountry(iso2) {
1615
- const iso2Lower = iso2.toLowerCase();
1616
- // check if already selected
1617
- if (this.selectedCountryData.iso2 !== iso2Lower) {
1618
- this._setCountry(iso2Lower);
1619
- this._updateDialCode(this.selectedCountryData.dialCode);
1620
- this._triggerCountryChange();
1621
- }
1622
- }
1623
- // set the input value and update the flag
1624
- setNumber(number) {
1625
- // we must update the flag first, which updates this.selectedCountryData, which is used for
1626
- // formatting the number before displaying it
1627
- const flagChanged = this._updateFlagFromNumber(number);
1628
- this._updateValFromNumber(number);
1629
- if (flagChanged) {
1630
- this._triggerCountryChange();
1631
- }
1632
- }
1633
- // set the placeholder number typ
1634
- setPlaceholderNumberType(type) {
1635
- this.options.placeholderNumberType = type;
1636
- this._updatePlaceholder();
1798
+ this.countryList = createEl(
1799
+ "ul",
1800
+ {
1801
+ class: "iti__country-list",
1802
+ id: `iti-${this.id}__country-listbox`,
1803
+ role: "listbox",
1804
+ "aria-label": i18n.countryListAriaLabel || "List of countries"
1805
+ },
1806
+ this.dropdownContent
1807
+ );
1808
+ if (this.preferredCountries.length && !countrySearch) {
1809
+ this._appendListItems(this.preferredCountries, "iti__preferred", true);
1810
+ createEl(
1811
+ "li",
1812
+ {
1813
+ class: "iti__divider",
1814
+ "aria-hidden": "true"
1815
+ },
1816
+ this.countryList
1817
+ );
1818
+ }
1819
+ this._appendListItems(this.countries, "iti__standard");
1820
+ if (countrySearch) {
1821
+ this._updateSearchResultsText();
1822
+ }
1823
+ if (dropdownContainer) {
1824
+ let dropdownClasses = "iti iti--container";
1825
+ if (useFullscreenPopup) {
1826
+ dropdownClasses += " iti--fullscreen-popup";
1827
+ } else {
1828
+ dropdownClasses += " iti--inline-dropdown";
1829
+ }
1830
+ if (countrySearch) {
1831
+ dropdownClasses += " iti--country-search";
1832
+ }
1833
+ this.dropdown = createEl("div", { class: dropdownClasses });
1834
+ this.dropdown.appendChild(this.dropdownContent);
1835
+ } else {
1836
+ this.countryContainer.appendChild(this.dropdownContent);
1837
+ }
1838
+ }
1839
+ if (hiddenInput) {
1840
+ const telInputName = this.telInput.getAttribute("name") || "";
1841
+ const names = hiddenInput(telInputName);
1842
+ if (names.phone) {
1843
+ this.hiddenInput = createEl("input", {
1844
+ type: "hidden",
1845
+ name: names.phone
1846
+ });
1847
+ wrapper.appendChild(this.hiddenInput);
1848
+ }
1849
+ if (names.country) {
1850
+ this.hiddenInputCountry = createEl("input", {
1851
+ type: "hidden",
1852
+ name: names.country
1853
+ });
1854
+ wrapper.appendChild(this.hiddenInputCountry);
1855
+ }
1856
+ }
1857
+ }
1858
+ // for each of the passed countries: add a country <li> to the countryList <ul> container
1859
+ _appendListItems(countries, className, preferred) {
1860
+ for (let i = 0; i < countries.length; i++) {
1861
+ const c = countries[i];
1862
+ const idSuffix = preferred ? "-preferred" : "";
1863
+ const listItem = createEl(
1864
+ "li",
1865
+ {
1866
+ id: `iti-${this.id}__item-${c.iso2}${idSuffix}`,
1867
+ class: `iti__country ${className}`,
1868
+ tabindex: "-1",
1869
+ role: "option",
1870
+ "data-dial-code": c.dialCode,
1871
+ "data-country-code": c.iso2,
1872
+ "aria-selected": "false"
1873
+ },
1874
+ this.countryList
1875
+ );
1876
+ c.nodeById[this.id] = listItem;
1877
+ let content = "";
1878
+ if (this.options.showFlags) {
1879
+ content += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
1880
+ }
1881
+ content += `<span class='iti__country-name'>${c.name}</span>`;
1882
+ content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
1883
+ listItem.insertAdjacentHTML("beforeend", content);
1884
+ }
1885
+ }
1886
+ // set the initial state of the input value and the selected country by:
1887
+ // 1. extracting a dial code from the given number
1888
+ // 2. using explicit initialCountry
1889
+ // 3. picking the first preferred country
1890
+ // 4. picking the first country
1891
+ _setInitialState(overrideAutoCountry = false) {
1892
+ const attributeValue = this.telInput.getAttribute("value");
1893
+ const inputValue = this.telInput.value;
1894
+ const useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
1895
+ const val = useAttribute ? attributeValue : inputValue;
1896
+ const dialCode = this._getDialCode(val);
1897
+ const isRegionlessNanpNumber = isRegionlessNanp(val);
1898
+ const { initialCountry } = this.options;
1899
+ if (dialCode && !isRegionlessNanpNumber) {
1900
+ this._updateCountryFromNumber(val);
1901
+ } else if (initialCountry !== "auto" || overrideAutoCountry) {
1902
+ const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
1903
+ const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
1904
+ if (isValidInitialCountry) {
1905
+ this._setCountry(lowerInitialCountry);
1906
+ } else {
1907
+ if (dialCode && isRegionlessNanpNumber) {
1908
+ this._setCountry("us");
1909
+ } else {
1910
+ this._setCountry();
1911
+ }
1912
+ }
1913
+ }
1914
+ if (val) {
1915
+ this._updateValFromNumber(val);
1916
+ }
1917
+ }
1918
+ // initialise the main event listeners: input keyup, and click selected country
1919
+ _initListeners() {
1920
+ this._initTelInputListeners();
1921
+ if (this.options.allowDropdown) {
1922
+ this._initDropdownListeners();
1923
+ }
1924
+ if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
1925
+ this._initHiddenInputListener();
1926
+ }
1927
+ }
1928
+ // update hidden input on form submit
1929
+ _initHiddenInputListener() {
1930
+ this._handleHiddenInputSubmit = () => {
1931
+ if (this.hiddenInput) {
1932
+ this.hiddenInput.value = this.getNumber();
1933
+ }
1934
+ if (this.hiddenInputCountry) {
1935
+ this.hiddenInputCountry.value = this.getSelectedCountryData().iso2 || "";
1936
+ }
1937
+ };
1938
+ this.telInput.form?.addEventListener(
1939
+ "submit",
1940
+ this._handleHiddenInputSubmit
1941
+ );
1942
+ }
1943
+ // initialise the dropdown listeners
1944
+ _initDropdownListeners() {
1945
+ this._handleLabelClick = (e) => {
1946
+ if (this.dropdownContent.classList.contains("iti__hide")) {
1947
+ this.telInput.focus();
1948
+ } else {
1949
+ e.preventDefault();
1950
+ }
1951
+ };
1952
+ const label = this.telInput.closest("label");
1953
+ if (label) {
1954
+ label.addEventListener("click", this._handleLabelClick);
1955
+ }
1956
+ this._handleClickSelectedCountry = () => {
1957
+ if (this.dropdownContent.classList.contains("iti__hide") && !this.telInput.disabled && !this.telInput.readOnly) {
1958
+ this._openDropdown();
1959
+ }
1960
+ };
1961
+ this.selectedCountry.addEventListener("click", this._handleClickSelectedCountry);
1962
+ this._handleCountryContainerKeydown = (e) => {
1963
+ const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
1964
+ if (isDropdownHidden && ["ArrowUp", "ArrowDown", " ", "Enter"].includes(e.key)) {
1965
+ e.preventDefault();
1966
+ e.stopPropagation();
1967
+ this._openDropdown();
1968
+ }
1969
+ if (e.key === "Tab") {
1970
+ this._closeDropdown();
1971
+ }
1972
+ };
1973
+ this.countryContainer.addEventListener(
1974
+ "keydown",
1975
+ this._handleCountryContainerKeydown
1976
+ );
1977
+ }
1978
+ // init many requests: utils script / geo ip lookup
1979
+ _initRequests() {
1980
+ if (this.options.utilsScript && !window.intlTelInputUtils) {
1981
+ if (window.intlTelInputGlobals.documentReady()) {
1982
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
1983
+ } else {
1984
+ window.addEventListener("load", () => {
1985
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
1986
+ });
1987
+ }
1988
+ } else {
1989
+ this.resolveUtilsScriptPromise();
1990
+ }
1991
+ if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
1992
+ this._loadAutoCountry();
1993
+ } else {
1994
+ this.resolveAutoCountryPromise();
1995
+ }
1996
+ }
1997
+ // perform the geo ip lookup
1998
+ _loadAutoCountry() {
1999
+ if (window.intlTelInputGlobals.autoCountry) {
2000
+ this.handleAutoCountry();
2001
+ } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
2002
+ window.intlTelInputGlobals.startedLoadingAutoCountry = true;
2003
+ if (typeof this.options.geoIpLookup === "function") {
2004
+ this.options.geoIpLookup(
2005
+ (iso2 = "") => {
2006
+ const iso2Lower = iso2.toLowerCase();
2007
+ const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
2008
+ if (isValidIso2) {
2009
+ window.intlTelInputGlobals.autoCountry = iso2Lower;
2010
+ setTimeout(() => forEachInstance("handleAutoCountry"));
2011
+ } else {
2012
+ this._setInitialState(true);
2013
+ forEachInstance("rejectAutoCountryPromise");
2014
+ }
2015
+ },
2016
+ () => {
2017
+ this._setInitialState(true);
2018
+ forEachInstance("rejectAutoCountryPromise");
1637
2019
  }
2020
+ );
2021
+ }
2022
+ }
2023
+ }
2024
+ // initialize the tel input listeners
2025
+ _initTelInputListeners() {
2026
+ const { strictMode, formatAsYouType } = this.options;
2027
+ let userOverrideFormatting = false;
2028
+ this._handleInputEvent = (e) => {
2029
+ if (this._updateCountryFromNumber(this.telInput.value)) {
2030
+ this._triggerCountryChange();
1638
2031
  }
1639
- /********************
1640
- * STATIC METHODS
1641
- ********************/
1642
- // get the country data object
1643
- intlTelInputGlobals.getCountryData = () => allCountries;
1644
- // inject a <script> element to load utils.js
1645
- const injectScript = (path, handleSuccess, handleFailure) => {
1646
- // inject a new script element into the page
1647
- const script = document.createElement("script");
1648
- script.onload = () => {
1649
- forEachInstance("handleUtils");
1650
- if (handleSuccess) {
1651
- handleSuccess();
1652
- }
1653
- };
1654
- script.onerror = () => {
1655
- forEachInstance("rejectUtilsScriptPromise");
1656
- if (handleFailure) {
1657
- handleFailure();
1658
- }
1659
- };
1660
- script.className = "iti-load-utils";
1661
- script.async = true;
1662
- script.src = path;
1663
- document.body.appendChild(script);
2032
+ const isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
2033
+ const isPaste = e && e.inputType === "insertFromPaste" && this.telInput.value;
2034
+ if (isFormattingChar || isPaste && !strictMode) {
2035
+ userOverrideFormatting = true;
2036
+ } else if (!/[^+0-9]/.test(this.telInput.value)) {
2037
+ userOverrideFormatting = false;
2038
+ }
2039
+ if (formatAsYouType && !userOverrideFormatting) {
2040
+ const currentCaretPos = this.telInput.selectionStart || 0;
2041
+ const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
2042
+ const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
2043
+ const isDeleteForwards = e && e.inputType === "deleteContentForward";
2044
+ const formattedValue = this._formatNumberAsYouType();
2045
+ const newCaretPos = translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
2046
+ this.telInput.value = formattedValue;
2047
+ this.telInput.setSelectionRange(newCaretPos, newCaretPos);
2048
+ }
2049
+ };
2050
+ this.telInput.addEventListener("input", this._handleInputEvent);
2051
+ if (strictMode) {
2052
+ this._handleKeydownEvent = (e) => {
2053
+ if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
2054
+ const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
2055
+ const isNumeric = /^[0-9]$/.test(e.key);
2056
+ const isAllowedChar = isInitialPlus || isNumeric;
2057
+ const fullNumber = this._getFullNumber();
2058
+ const coreNumber = window.intlTelInputUtils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
2059
+ const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
2060
+ if (!isAllowedChar || hasReachedMaxLength) {
2061
+ e.preventDefault();
2062
+ }
2063
+ }
1664
2064
  };
1665
- // load the utils script
1666
- intlTelInputGlobals.loadUtils = path => {
1667
- // 2 options:
1668
- // 1) not already started loading (start)
1669
- // 2) already started loading (do nothing - just wait for the onload callback to fire, which will
1670
- // trigger handleUtils on all instances, invoking their resolveUtilsScriptPromise functions)
1671
- if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) {
1672
- // only do this once
1673
- window.intlTelInputGlobals.startedLoadingUtilsScript = true;
1674
- return new Promise((resolve, reject) => injectScript(path, resolve, reject));
1675
- }
1676
- return null;
2065
+ this.telInput.addEventListener("keydown", this._handleKeydownEvent);
2066
+ }
2067
+ }
2068
+ // adhere to the input's maxlength attr
2069
+ _cap(number) {
2070
+ const max = parseInt(this.telInput.getAttribute("maxlength") || "", 10);
2071
+ return max && number.length > max ? number.substr(0, max) : number;
2072
+ }
2073
+ // trigger a custom event on the input
2074
+ _trigger(name) {
2075
+ const e = new Event(name, {
2076
+ bubbles: true,
2077
+ cancelable: true
2078
+ });
2079
+ this.telInput.dispatchEvent(e);
2080
+ }
2081
+ // open the dropdown
2082
+ _openDropdown() {
2083
+ const { fixDropdownWidth, countrySearch } = this.options;
2084
+ if (fixDropdownWidth) {
2085
+ this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
2086
+ }
2087
+ this.dropdownContent.classList.remove("iti__hide");
2088
+ this.selectedCountry.setAttribute("aria-expanded", "true");
2089
+ this._setDropdownPosition();
2090
+ if (this.activeItem && !countrySearch) {
2091
+ this._highlightListItem(this.activeItem, false);
2092
+ this._scrollTo(this.activeItem, true);
2093
+ } else {
2094
+ const firstCountryItem = this.countryList.firstElementChild;
2095
+ if (firstCountryItem) {
2096
+ this._highlightListItem(firstCountryItem, false);
2097
+ this.countryList.scrollTop = 0;
2098
+ }
2099
+ if (countrySearch) {
2100
+ this.searchInput.focus();
2101
+ }
2102
+ }
2103
+ this._bindDropdownListeners();
2104
+ this.dropdownArrow.classList.add("iti__arrow--up");
2105
+ this._trigger("open:countrydropdown");
2106
+ }
2107
+ // decide if should position dropdown above or below input (depends on position within viewport, and scroll)
2108
+ _setDropdownPosition() {
2109
+ if (this.options.dropdownContainer) {
2110
+ this.options.dropdownContainer.appendChild(this.dropdown);
2111
+ }
2112
+ if (!this.options.useFullscreenPopup) {
2113
+ const pos = this.telInput.getBoundingClientRect();
2114
+ const windowTop = document.documentElement.scrollTop;
2115
+ const inputTop = pos.top + windowTop;
2116
+ const dropdownHeight = this.dropdownContent.offsetHeight;
2117
+ const dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
2118
+ const dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
2119
+ const positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
2120
+ toggleClass(
2121
+ this.dropdownContent,
2122
+ "iti__dropdown-content--dropup",
2123
+ positionDropdownAboveInput
2124
+ );
2125
+ if (this.options.dropdownContainer) {
2126
+ const extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
2127
+ this.dropdown.style.top = `${inputTop + extraTop}px`;
2128
+ this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
2129
+ this._handleWindowScroll = () => this._closeDropdown();
2130
+ window.addEventListener("scroll", this._handleWindowScroll);
2131
+ }
2132
+ }
2133
+ }
2134
+ // we only bind dropdown listeners when the dropdown is open
2135
+ _bindDropdownListeners() {
2136
+ this._handleMouseoverCountryList = (e) => {
2137
+ const listItem = e.target?.closest(".iti__country");
2138
+ if (listItem) {
2139
+ this._highlightListItem(listItem, false);
2140
+ }
2141
+ };
2142
+ this.countryList.addEventListener(
2143
+ "mouseover",
2144
+ this._handleMouseoverCountryList
2145
+ );
2146
+ this._handleClickCountryList = (e) => {
2147
+ const listItem = e.target?.closest(".iti__country");
2148
+ if (listItem) {
2149
+ this._selectListItem(listItem);
2150
+ }
2151
+ };
2152
+ this.countryList.addEventListener("click", this._handleClickCountryList);
2153
+ let isOpening = true;
2154
+ this._handleClickOffToClose = () => {
2155
+ if (!isOpening) {
2156
+ this._closeDropdown();
2157
+ }
2158
+ isOpening = false;
2159
+ };
2160
+ document.documentElement.addEventListener(
2161
+ "click",
2162
+ this._handleClickOffToClose
2163
+ );
2164
+ let query = "";
2165
+ let queryTimer = null;
2166
+ this._handleKeydownOnDropdown = (e) => {
2167
+ if (["ArrowUp", "ArrowDown", "Enter", "Escape"].includes(e.key)) {
2168
+ e.preventDefault();
2169
+ e.stopPropagation();
2170
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
2171
+ this._handleUpDownKey(e.key);
2172
+ } else if (e.key === "Enter") {
2173
+ this._handleEnterKey();
2174
+ } else if (e.key === "Escape") {
2175
+ this._closeDropdown();
2176
+ }
2177
+ }
2178
+ if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
2179
+ e.stopPropagation();
2180
+ if (queryTimer) {
2181
+ clearTimeout(queryTimer);
2182
+ }
2183
+ query += e.key.toLowerCase();
2184
+ this._searchForCountry(query);
2185
+ queryTimer = setTimeout(() => {
2186
+ query = "";
2187
+ }, 1e3);
2188
+ }
2189
+ };
2190
+ document.addEventListener("keydown", this._handleKeydownOnDropdown);
2191
+ if (this.options.countrySearch) {
2192
+ const doFilter = () => {
2193
+ const inputQuery = this.searchInput.value.trim();
2194
+ if (inputQuery) {
2195
+ this._filterCountries(inputQuery);
2196
+ } else {
2197
+ this._filterCountries("", true);
2198
+ }
1677
2199
  };
1678
- // default options
1679
- intlTelInputGlobals.defaults = defaults;
1680
- // version
1681
- intlTelInputGlobals.version = "20.3.0";
1682
- // convenience wrapper
1683
- return function(input, options) {
1684
- var iti = new Iti(input, options);
1685
- iti._init();
1686
- input.setAttribute("data-intl-tel-input-id", iti.id);
1687
- window.intlTelInputGlobals.instances[iti.id] = iti;
1688
- return iti;
2200
+ let keyupTimer = null;
2201
+ this._handleSearchChange = () => {
2202
+ if (keyupTimer) {
2203
+ clearTimeout(keyupTimer);
2204
+ }
2205
+ keyupTimer = setTimeout(() => {
2206
+ doFilter();
2207
+ keyupTimer = null;
2208
+ }, 100);
1689
2209
  };
1690
- }();
1691
- });
2210
+ this.searchInput.addEventListener("input", this._handleSearchChange);
2211
+ this.searchInput.addEventListener("click", (e) => e.stopPropagation());
2212
+ }
2213
+ }
2214
+ _filterCountries(query, isReset = false) {
2215
+ let isFirst = true;
2216
+ this.countryList.innerHTML = "";
2217
+ const normalisedQuery = normaliseString(query);
2218
+ for (let i = 0; i < this.countries.length; i++) {
2219
+ const c = this.countries[i];
2220
+ const normalisedCountryName = normaliseString(c.name);
2221
+ const fullDialCode = `+${c.dialCode}`;
2222
+ if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
2223
+ const listItem = c.nodeById[this.id];
2224
+ if (listItem) {
2225
+ this.countryList.appendChild(listItem);
2226
+ }
2227
+ if (isFirst) {
2228
+ this._highlightListItem(listItem, false);
2229
+ isFirst = false;
2230
+ }
2231
+ }
2232
+ }
2233
+ this.countryList.scrollTop = 0;
2234
+ this._updateSearchResultsText();
2235
+ }
2236
+ // update search results text (for a11y)
2237
+ _updateSearchResultsText() {
2238
+ const { i18n } = this.options;
2239
+ const count = this.countryList.childElementCount;
2240
+ let searchText;
2241
+ if (count === 0) {
2242
+ searchText = i18n.zeroSearchResults || "No results found";
2243
+ } else if (count === 1) {
2244
+ searchText = i18n.oneSearchResult || "1 result found";
2245
+ } else {
2246
+ searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count.toString()) : `${count} results found`;
2247
+ }
2248
+ this.searchResultsA11yText.textContent = searchText;
2249
+ }
2250
+ // highlight the next/prev item in the list (and ensure it is visible)
2251
+ _handleUpDownKey(key) {
2252
+ let next = key === "ArrowUp" ? this.highlightedItem?.previousElementSibling : this.highlightedItem?.nextElementSibling;
2253
+ if (next) {
2254
+ if (next.classList.contains("iti__divider")) {
2255
+ next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
2256
+ }
2257
+ } else if (this.countryList.childElementCount > 1) {
2258
+ next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
2259
+ }
2260
+ if (next) {
2261
+ this._scrollTo(next, false);
2262
+ const doFocus = !this.options.countrySearch;
2263
+ this._highlightListItem(next, doFocus);
2264
+ }
2265
+ }
2266
+ // select the currently highlighted item
2267
+ _handleEnterKey() {
2268
+ if (this.highlightedItem) {
2269
+ this._selectListItem(this.highlightedItem);
2270
+ }
2271
+ }
2272
+ // find the first list item whose name starts with the query string
2273
+ _searchForCountry(query) {
2274
+ for (let i = 0; i < this.countries.length; i++) {
2275
+ const c = this.countries[i];
2276
+ const startsWith = c.name.substr(0, query.length).toLowerCase() === query;
2277
+ if (startsWith) {
2278
+ const listItem = c.nodeById[this.id];
2279
+ this._highlightListItem(listItem, false);
2280
+ this._scrollTo(listItem, true);
2281
+ break;
2282
+ }
2283
+ }
2284
+ }
2285
+ // update the input's value to the given val (format first if possible)
2286
+ // NOTE: this is called from _setInitialState, handleUtils and setNumber
2287
+ _updateValFromNumber(fullNumber) {
2288
+ let number = fullNumber;
2289
+ if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
2290
+ const useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
2291
+ const { NATIONAL, INTERNATIONAL } = window.intlTelInputUtils.numberFormat;
2292
+ const format = useNational ? NATIONAL : INTERNATIONAL;
2293
+ number = window.intlTelInputUtils.formatNumber(
2294
+ number,
2295
+ this.selectedCountryData.iso2,
2296
+ format
2297
+ );
2298
+ }
2299
+ number = this._beforeSetNumber(number);
2300
+ this.telInput.value = number;
2301
+ }
2302
+ // check if need to select a new country based on the given number
2303
+ // Note: called from _setInitialState, keyup handler, setNumber
2304
+ _updateCountryFromNumber(fullNumber) {
2305
+ const plusIndex = fullNumber.indexOf("+");
2306
+ let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
2307
+ const selectedDialCode = this.selectedCountryData.dialCode;
2308
+ const isNanp = selectedDialCode === "1";
2309
+ if (number && isNanp && number.charAt(0) !== "+") {
2310
+ if (number.charAt(0) !== "1") {
2311
+ number = `1${number}`;
2312
+ }
2313
+ number = `+${number}`;
2314
+ }
2315
+ if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
2316
+ number = `+${selectedDialCode}${number}`;
2317
+ }
2318
+ const dialCode = this._getDialCode(number, true);
2319
+ const numeric = getNumeric(number);
2320
+ let iso2 = null;
2321
+ if (dialCode) {
2322
+ const iso2Codes = this.dialCodeToIso2Map[getNumeric(dialCode)];
2323
+ const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
2324
+ const isRegionlessNanpNumber = selectedDialCode === "1" && isRegionlessNanp(numeric);
2325
+ if (!isRegionlessNanpNumber && !alreadySelected) {
2326
+ for (let j = 0; j < iso2Codes.length; j++) {
2327
+ if (iso2Codes[j]) {
2328
+ iso2 = iso2Codes[j];
2329
+ break;
2330
+ }
2331
+ }
2332
+ }
2333
+ } else if (number.charAt(0) === "+" && numeric.length) {
2334
+ iso2 = "";
2335
+ } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
2336
+ iso2 = this.defaultCountry;
2337
+ }
2338
+ if (iso2 !== null) {
2339
+ return this._setCountry(iso2);
2340
+ }
2341
+ return false;
2342
+ }
2343
+ // remove highlighting from other list items and highlight the given item
2344
+ _highlightListItem(listItem, shouldFocus) {
2345
+ const prevItem = this.highlightedItem;
2346
+ if (prevItem) {
2347
+ prevItem.classList.remove("iti__highlight");
2348
+ prevItem.setAttribute("aria-selected", "false");
2349
+ }
2350
+ this.highlightedItem = listItem;
2351
+ this.highlightedItem.classList.add("iti__highlight");
2352
+ this.highlightedItem.setAttribute("aria-selected", "true");
2353
+ this.selectedCountry.setAttribute(
2354
+ "aria-activedescendant",
2355
+ listItem.getAttribute("id") || ""
2356
+ );
2357
+ if (this.options.countrySearch) {
2358
+ this.searchInput.setAttribute(
2359
+ "aria-activedescendant",
2360
+ listItem.getAttribute("id") || ""
2361
+ );
2362
+ }
2363
+ if (shouldFocus) {
2364
+ this.highlightedItem.focus();
2365
+ }
2366
+ }
2367
+ // find the country data for the given iso2 code
2368
+ // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
2369
+ _getCountryData(iso2, allowFail) {
2370
+ for (let i = 0; i < this.countries.length; i++) {
2371
+ if (this.countries[i].iso2 === iso2) {
2372
+ return this.countries[i];
2373
+ }
2374
+ }
2375
+ if (allowFail) {
2376
+ return null;
2377
+ }
2378
+ throw new Error(`No country data for '${iso2}'`);
2379
+ }
2380
+ // update the selected country, dial code (if showSelectedDialCode), placeholder, title, and active list item
2381
+ // Note: called from _setInitialState, _updateCountryFromNumber, _selectListItem, setCountry
2382
+ _setCountry(iso2) {
2383
+ const { allowDropdown, showSelectedDialCode, showFlags, countrySearch, i18n } = this.options;
2384
+ const prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
2385
+ this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) || {} : {};
2386
+ if (this.selectedCountryData.iso2) {
2387
+ this.defaultCountry = this.selectedCountryData.iso2;
2388
+ }
2389
+ if (this.selectedCountryInner) {
2390
+ let flagClass = "";
2391
+ let a11yText = "";
2392
+ if (iso2) {
2393
+ if (showFlags) {
2394
+ flagClass = `iti__flag iti__${iso2}`;
2395
+ a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
2396
+ }
2397
+ } else {
2398
+ flagClass = "iti__flag iti__globe";
2399
+ a11yText = i18n.noCountrySelected || "No country selected";
2400
+ }
2401
+ this.selectedCountryInner.className = flagClass;
2402
+ this.selectedCountryA11yText.textContent = a11yText;
2403
+ }
2404
+ this._setSelectedCountryTitleAttribute(iso2, showSelectedDialCode);
2405
+ if (showSelectedDialCode) {
2406
+ const dialCode = this.selectedCountryData.dialCode ? `+${this.selectedCountryData.dialCode}` : "";
2407
+ this.selectedDialCode.innerHTML = dialCode;
2408
+ const selectedCountryWidth = this.selectedCountry.offsetWidth || this._getHiddenSelectedCountryWidth();
2409
+ if (this.isRTL) {
2410
+ this.telInput.style.paddingRight = `${selectedCountryWidth + 6}px`;
2411
+ } else {
2412
+ this.telInput.style.paddingLeft = `${selectedCountryWidth + 6}px`;
2413
+ }
2414
+ }
2415
+ this._updatePlaceholder();
2416
+ this._updateMaxLength();
2417
+ if (allowDropdown && !countrySearch) {
2418
+ const prevItem = this.activeItem;
2419
+ if (prevItem) {
2420
+ prevItem.classList.remove("iti__active");
2421
+ prevItem.setAttribute("aria-selected", "false");
2422
+ }
2423
+ if (iso2) {
2424
+ const nextItem = this.countryList.querySelector(
2425
+ `#iti-${this.id}__item-${iso2}-preferred`
2426
+ ) || this.countryList.querySelector(
2427
+ `#iti-${this.id}__item-${iso2}`
2428
+ );
2429
+ if (nextItem) {
2430
+ nextItem.setAttribute("aria-selected", "true");
2431
+ nextItem.classList.add("iti__active");
2432
+ this.activeItem = nextItem;
2433
+ }
2434
+ }
2435
+ }
2436
+ return prevCountry.iso2 !== iso2;
2437
+ }
2438
+ // update the maximum valid number length for the currently selected country
2439
+ _updateMaxLength() {
2440
+ if (this.options.strictMode && window.intlTelInputUtils) {
2441
+ if (this.selectedCountryData.iso2) {
2442
+ const numberType = window.intlTelInputUtils.numberType[this.options.placeholderNumberType];
2443
+ let exampleNumber = window.intlTelInputUtils.getExampleNumber(
2444
+ this.selectedCountryData.iso2,
2445
+ false,
2446
+ numberType,
2447
+ true
2448
+ );
2449
+ let validNumber = exampleNumber;
2450
+ while (window.intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
2451
+ validNumber = exampleNumber;
2452
+ exampleNumber += "0";
2453
+ }
2454
+ const coreNumber = window.intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
2455
+ this.maxCoreNumberLength = coreNumber.length;
2456
+ } else {
2457
+ this.maxCoreNumberLength = null;
2458
+ }
2459
+ }
2460
+ }
2461
+ _setSelectedCountryTitleAttribute(iso2 = null, showSelectedDialCode) {
2462
+ if (!this.selectedCountry) {
2463
+ return;
2464
+ }
2465
+ let title;
2466
+ if (iso2 && !showSelectedDialCode) {
2467
+ title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
2468
+ } else if (iso2) {
2469
+ title = this.selectedCountryData.name;
2470
+ } else {
2471
+ title = "Unknown";
2472
+ }
2473
+ this.selectedCountry.setAttribute("title", title);
2474
+ }
2475
+ // when the input is in a hidden container during initialisation, we must inject some markup
2476
+ // into the end of the DOM to calculate the correct offsetWidth
2477
+ // NOTE: this is only used when showSelectedDialCode is enabled, so countryContainer and selectedCountry
2478
+ // will definitely exist
2479
+ _getHiddenSelectedCountryWidth() {
2480
+ if (this.telInput.parentNode) {
2481
+ const containerClone = this.telInput.parentNode.cloneNode(false);
2482
+ containerClone.style.visibility = "hidden";
2483
+ document.body.appendChild(containerClone);
2484
+ const countryContainerClone = this.countryContainer.cloneNode();
2485
+ containerClone.appendChild(countryContainerClone);
2486
+ const selectedCountryClone = this.selectedCountry.cloneNode(true);
2487
+ countryContainerClone.appendChild(selectedCountryClone);
2488
+ const width = selectedCountryClone.offsetWidth;
2489
+ document.body.removeChild(containerClone);
2490
+ return width;
2491
+ }
2492
+ return 0;
2493
+ }
2494
+ // update the input placeholder to an example number from the currently selected country
2495
+ _updatePlaceholder() {
2496
+ const {
2497
+ autoPlaceholder,
2498
+ placeholderNumberType,
2499
+ nationalMode,
2500
+ customPlaceholder
2501
+ } = this.options;
2502
+ const shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
2503
+ if (window.intlTelInputUtils && shouldSetPlaceholder) {
2504
+ const numberType = window.intlTelInputUtils.numberType[placeholderNumberType];
2505
+ let placeholder = this.selectedCountryData.iso2 ? window.intlTelInputUtils.getExampleNumber(
2506
+ this.selectedCountryData.iso2,
2507
+ nationalMode,
2508
+ numberType
2509
+ ) : "";
2510
+ placeholder = this._beforeSetNumber(placeholder);
2511
+ if (typeof customPlaceholder === "function") {
2512
+ placeholder = customPlaceholder(placeholder, this.selectedCountryData);
2513
+ }
2514
+ this.telInput.setAttribute("placeholder", placeholder);
2515
+ }
2516
+ }
2517
+ // called when the user selects a list item from the dropdown
2518
+ _selectListItem(listItem) {
2519
+ const countryChanged = this._setCountry(
2520
+ listItem.getAttribute("data-country-code")
2521
+ );
2522
+ this._closeDropdown();
2523
+ this._updateDialCode(listItem.getAttribute("data-dial-code"));
2524
+ this.telInput.focus();
2525
+ if (countryChanged) {
2526
+ this._triggerCountryChange();
2527
+ }
2528
+ }
2529
+ // close the dropdown and unbind any listeners
2530
+ _closeDropdown() {
2531
+ this.dropdownContent.classList.add("iti__hide");
2532
+ this.selectedCountry.setAttribute("aria-expanded", "false");
2533
+ this.selectedCountry.removeAttribute("aria-activedescendant");
2534
+ if (this.highlightedItem) {
2535
+ this.highlightedItem.setAttribute("aria-selected", "false");
2536
+ }
2537
+ if (this.options.countrySearch) {
2538
+ this.searchInput.removeAttribute("aria-activedescendant");
2539
+ }
2540
+ this.dropdownArrow.classList.remove("iti__arrow--up");
2541
+ document.removeEventListener("keydown", this._handleKeydownOnDropdown);
2542
+ if (this.options.countrySearch) {
2543
+ this.searchInput.removeEventListener("input", this._handleSearchChange);
2544
+ }
2545
+ document.documentElement.removeEventListener(
2546
+ "click",
2547
+ this._handleClickOffToClose
2548
+ );
2549
+ this.countryList.removeEventListener(
2550
+ "mouseover",
2551
+ this._handleMouseoverCountryList
2552
+ );
2553
+ this.countryList.removeEventListener("click", this._handleClickCountryList);
2554
+ if (this.options.dropdownContainer) {
2555
+ if (!this.options.useFullscreenPopup) {
2556
+ window.removeEventListener("scroll", this._handleWindowScroll);
2557
+ }
2558
+ if (this.dropdown.parentNode) {
2559
+ this.dropdown.parentNode.removeChild(this.dropdown);
2560
+ }
2561
+ }
2562
+ this._trigger("close:countrydropdown");
2563
+ }
2564
+ // check if an element is visible within it's container, else scroll until it is
2565
+ _scrollTo(element, middle) {
2566
+ const container = this.countryList;
2567
+ const windowTop = document.documentElement.scrollTop;
2568
+ const containerHeight = container.offsetHeight;
2569
+ const containerTop = container.getBoundingClientRect().top + windowTop;
2570
+ const containerBottom = containerTop + containerHeight;
2571
+ const elementHeight = element.offsetHeight;
2572
+ const elementTop = element.getBoundingClientRect().top + windowTop;
2573
+ const elementBottom = elementTop + elementHeight;
2574
+ let newScrollTop = elementTop - containerTop + container.scrollTop;
2575
+ const middleOffset = containerHeight / 2 - elementHeight / 2;
2576
+ if (elementTop < containerTop) {
2577
+ if (middle) {
2578
+ newScrollTop -= middleOffset;
2579
+ }
2580
+ container.scrollTop = newScrollTop;
2581
+ } else if (elementBottom > containerBottom) {
2582
+ if (middle) {
2583
+ newScrollTop += middleOffset;
2584
+ }
2585
+ const heightDifference = containerHeight - elementHeight;
2586
+ container.scrollTop = newScrollTop - heightDifference;
2587
+ }
2588
+ }
2589
+ // replace any existing dial code with the new one
2590
+ // Note: called from _selectListItem and setCountry
2591
+ _updateDialCode(newDialCodeBare) {
2592
+ const inputVal = this.telInput.value;
2593
+ const newDialCode = `+${newDialCodeBare}`;
2594
+ let newNumber;
2595
+ if (inputVal.charAt(0) === "+") {
2596
+ const prevDialCode = this._getDialCode(inputVal);
2597
+ if (prevDialCode) {
2598
+ newNumber = inputVal.replace(prevDialCode, newDialCode);
2599
+ } else {
2600
+ newNumber = newDialCode;
2601
+ }
2602
+ this.telInput.value = newNumber;
2603
+ }
2604
+ }
2605
+ // try and extract a valid international dial code from a full telephone number
2606
+ // Note: returns the raw string inc plus character and any whitespace/dots etc
2607
+ _getDialCode(number, includeAreaCode) {
2608
+ let dialCode = "";
2609
+ if (number.charAt(0) === "+") {
2610
+ let numericChars = "";
2611
+ for (let i = 0; i < number.length; i++) {
2612
+ const c = number.charAt(i);
2613
+ if (!isNaN(parseInt(c, 10))) {
2614
+ numericChars += c;
2615
+ if (includeAreaCode) {
2616
+ if (this.dialCodeToIso2Map[numericChars]) {
2617
+ dialCode = number.substr(0, i + 1);
2618
+ }
2619
+ } else {
2620
+ if (this.dialCodes[numericChars]) {
2621
+ dialCode = number.substr(0, i + 1);
2622
+ break;
2623
+ }
2624
+ }
2625
+ if (numericChars.length === this.dialCodeMaxLen) {
2626
+ break;
2627
+ }
2628
+ }
2629
+ }
2630
+ }
2631
+ return dialCode;
2632
+ }
2633
+ // get the input val, adding the dial code if showSelectedDialCode is enabled
2634
+ _getFullNumber() {
2635
+ const val = this.telInput.value.trim();
2636
+ const { dialCode } = this.selectedCountryData;
2637
+ let prefix;
2638
+ const numericVal = getNumeric(val);
2639
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
2640
+ prefix = `+${dialCode}`;
2641
+ } else {
2642
+ prefix = "";
2643
+ }
2644
+ return prefix + val;
2645
+ }
2646
+ // remove the dial code if showSelectedDialCode is enabled
2647
+ // also cap the length if the input has a maxlength attribute
2648
+ _beforeSetNumber(fullNumber) {
2649
+ let number = fullNumber;
2650
+ if (this.options.showSelectedDialCode) {
2651
+ let dialCode = this._getDialCode(number);
2652
+ if (dialCode) {
2653
+ dialCode = `+${this.selectedCountryData.dialCode}`;
2654
+ const start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
2655
+ number = number.substr(start);
2656
+ }
2657
+ }
2658
+ return this._cap(number);
2659
+ }
2660
+ // trigger the 'countrychange' event
2661
+ _triggerCountryChange() {
2662
+ this._trigger("countrychange");
2663
+ }
2664
+ // format the number as the user types
2665
+ _formatNumberAsYouType() {
2666
+ const val = this._getFullNumber();
2667
+ const result = window.intlTelInputUtils ? window.intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
2668
+ const { dialCode } = this.selectedCountryData;
2669
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes(`+${dialCode}`)) {
2670
+ const afterDialCode = result.split(`+${dialCode}`)[1] || "";
2671
+ return afterDialCode.trim();
2672
+ }
2673
+ return result;
2674
+ }
2675
+ //**************************
2676
+ //* SECRET PUBLIC METHODS
2677
+ //**************************
2678
+ // this is called when the geoip call returns
2679
+ handleAutoCountry() {
2680
+ if (this.options.initialCountry === "auto" && window.intlTelInputGlobals.autoCountry) {
2681
+ this.defaultCountry = window.intlTelInputGlobals.autoCountry;
2682
+ if (!this.telInput.value) {
2683
+ this.setCountry(this.defaultCountry);
2684
+ }
2685
+ this.resolveAutoCountryPromise();
2686
+ }
2687
+ }
2688
+ // this is called when the utils request completes
2689
+ handleUtils() {
2690
+ if (window.intlTelInputUtils) {
2691
+ if (this.telInput.value) {
2692
+ this._updateValFromNumber(this.telInput.value);
2693
+ }
2694
+ if (this.selectedCountryData.iso2) {
2695
+ this._updatePlaceholder();
2696
+ this._updateMaxLength();
2697
+ }
2698
+ }
2699
+ this.resolveUtilsScriptPromise();
2700
+ }
2701
+ //********************
2702
+ //* PUBLIC METHODS
2703
+ //********************
2704
+ // remove plugin
2705
+ destroy() {
2706
+ if (this.options.allowDropdown) {
2707
+ this._closeDropdown();
2708
+ this.selectedCountry.removeEventListener(
2709
+ "click",
2710
+ this._handleClickSelectedCountry
2711
+ );
2712
+ this.countryContainer.removeEventListener(
2713
+ "keydown",
2714
+ this._handleCountryContainerKeydown
2715
+ );
2716
+ const label = this.telInput.closest("label");
2717
+ if (label) {
2718
+ label.removeEventListener("click", this._handleLabelClick);
2719
+ }
2720
+ }
2721
+ const { form } = this.telInput;
2722
+ if (this._handleHiddenInputSubmit && form) {
2723
+ form.removeEventListener("submit", this._handleHiddenInputSubmit);
2724
+ }
2725
+ this.telInput.removeEventListener("input", this._handleInputEvent);
2726
+ if (this._handleKeydownEvent) {
2727
+ this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
2728
+ }
2729
+ this.telInput.removeAttribute("data-intl-tel-input-id");
2730
+ const wrapper = this.telInput.parentNode;
2731
+ wrapper?.parentNode?.insertBefore(this.telInput, wrapper);
2732
+ wrapper?.parentNode?.removeChild(wrapper);
2733
+ delete window.intlTelInputGlobals.instances[this.id];
2734
+ }
2735
+ // get the extension from the current number
2736
+ getExtension() {
2737
+ if (window.intlTelInputUtils) {
2738
+ return window.intlTelInputUtils.getExtension(
2739
+ this._getFullNumber(),
2740
+ this.selectedCountryData.iso2
2741
+ );
2742
+ }
2743
+ return "";
2744
+ }
2745
+ // format the number to the given format
2746
+ getNumber(format) {
2747
+ if (window.intlTelInputUtils) {
2748
+ const { iso2 } = this.selectedCountryData;
2749
+ return window.intlTelInputUtils.formatNumber(
2750
+ this._getFullNumber(),
2751
+ iso2,
2752
+ format
2753
+ );
2754
+ }
2755
+ return "";
2756
+ }
2757
+ // get the type of the entered number e.g. landline/mobile
2758
+ getNumberType() {
2759
+ if (window.intlTelInputUtils) {
2760
+ return window.intlTelInputUtils.getNumberType(
2761
+ this._getFullNumber(),
2762
+ this.selectedCountryData.iso2
2763
+ );
2764
+ }
2765
+ return -99;
2766
+ }
2767
+ // get the country data for the currently selected country
2768
+ getSelectedCountryData() {
2769
+ return this.selectedCountryData;
2770
+ }
2771
+ // get the validation error
2772
+ getValidationError() {
2773
+ if (window.intlTelInputUtils) {
2774
+ const { iso2 } = this.selectedCountryData;
2775
+ return window.intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
2776
+ }
2777
+ return -99;
2778
+ }
2779
+ // validate the input val - assumes the global function isPossibleNumber (from utilsScript)
2780
+ isValidNumber(mobileOnly = true) {
2781
+ const val = this._getFullNumber();
2782
+ if (/\p{L}/u.test(val)) {
2783
+ return false;
2784
+ }
2785
+ return window.intlTelInputUtils ? window.intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
2786
+ }
2787
+ // validate the input val (precise) - assumes the global function isValidNumber (from utilsScript)
2788
+ isValidNumberPrecise() {
2789
+ const val = this._getFullNumber();
2790
+ if (/\p{L}/u.test(val)) {
2791
+ return false;
2792
+ }
2793
+ return window.intlTelInputUtils ? window.intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
2794
+ }
2795
+ // update the selected country, and update the input val accordingly
2796
+ setCountry(iso2) {
2797
+ const iso2Lower = iso2.toLowerCase();
2798
+ if (this.selectedCountryData.iso2 !== iso2Lower) {
2799
+ this._setCountry(iso2Lower);
2800
+ this._updateDialCode(this.selectedCountryData.dialCode);
2801
+ this._triggerCountryChange();
2802
+ }
2803
+ }
2804
+ // set the input value and update the country
2805
+ setNumber(number) {
2806
+ const countryChanged = this._updateCountryFromNumber(number);
2807
+ this._updateValFromNumber(number);
2808
+ if (countryChanged) {
2809
+ this._triggerCountryChange();
2810
+ }
2811
+ }
2812
+ // set the placeholder number typ
2813
+ setPlaceholderNumberType(type) {
2814
+ this.options.placeholderNumberType = type;
2815
+ this._updatePlaceholder();
2816
+ }
2817
+ };
2818
+ var injectScript = (path, handleSuccess, handleFailure) => {
2819
+ const script = document.createElement("script");
2820
+ script.onload = () => {
2821
+ forEachInstance("handleUtils");
2822
+ if (handleSuccess) {
2823
+ handleSuccess();
2824
+ }
2825
+ };
2826
+ script.onerror = () => {
2827
+ forEachInstance("rejectUtilsScriptPromise");
2828
+ if (handleFailure) {
2829
+ handleFailure();
2830
+ }
2831
+ };
2832
+ script.className = "iti-load-utils";
2833
+ script.async = true;
2834
+ script.src = path;
2835
+ document.body.appendChild(script);
2836
+ };
2837
+ var loadUtils = (path) => {
2838
+ if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) {
2839
+ window.intlTelInputGlobals.startedLoadingUtilsScript = true;
2840
+ return new Promise(
2841
+ (resolve, reject) => injectScript(path, resolve, reject)
2842
+ );
2843
+ }
2844
+ return null;
2845
+ };
2846
+ if (typeof window === "object") {
2847
+ const intlTelInputGlobals = {
2848
+ defaults,
2849
+ // using a global like this allows us to mock it in the tests
2850
+ documentReady: () => document.readyState === "complete",
2851
+ // get the country data object
2852
+ getCountryData: () => data_default,
2853
+ // a getter for the plugin instance
2854
+ getInstance: (input) => {
2855
+ const id2 = input.getAttribute("data-intl-tel-input-id");
2856
+ return id2 ? intlTelInputGlobals.instances[id2] : null;
2857
+ },
2858
+ // a map from instance ID to instance object
2859
+ instances: {},
2860
+ loadUtils,
2861
+ version: "21.0.0"
2862
+ };
2863
+ window.intlTelInputGlobals = intlTelInputGlobals;
2864
+ }
2865
+ var intlTelInput = (input, options) => {
2866
+ const iti = new Iti(input, options);
2867
+ iti._init();
2868
+ input.setAttribute("data-intl-tel-input-id", iti.id.toString());
2869
+ window.intlTelInputGlobals.instances[iti.id] = iti;
2870
+ return iti;
2871
+ };
2872
+ var intlTelInput_default = intlTelInput;
2873
+ return __toCommonJS(intlTelInput_exports);
2874
+ })();
2875
+
2876
+ // UMD
2877
+ return factoryOutput.default;
2878
+ }));