-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
intlTelInput.js
1430 lines (1198 loc) · 53.3 KB
/
intlTelInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
window.intlTelInputGlobals = {
getInstance: (input) => {
const id = input.getAttribute('data-intl-tel-input-id');
return window.intlTelInputGlobals.instances[id];
},
instances: {},
};
// these vars persist through all instances of the plugin
let id = 0;
const defaults = {
// whether or not to allow the dropdown
allowDropdown: true,
// if there is just a dial code in the input: remove it on blur
autoHideDialCode: true,
// add a placeholder in the input with an example number for the selected country
autoPlaceholder: 'polite',
// modify the parentClass
customContainer: '',
// modify the auto placeholder
customPlaceholder: null,
// append menu to specified element
dropdownContainer: null,
// don't display these countries
excludeCountries: [],
// format the input value during initialisation and on setNumber
formatOnDisplay: true,
// geoIp lookup function
geoIpLookup: null,
// inject a hidden input with this name, and on submit, populate it with the result of getNumber
hiddenInput: '',
// initial country
initialCountry: '',
// localized country names e.g. { 'de': 'Deutschland' }
localizedCountries: null,
// don't insert international dial codes
nationalMode: true,
// display only these countries
onlyCountries: [],
// number type to use for placeholders
placeholderNumberType: 'MOBILE',
// the countries at the top of the list. defaults to united states and united kingdom
preferredCountries: ['us', 'gb'],
// display the country dial code next to the selected flag so it's not part of the typed number
separateDialCode: false,
// specify the path to the libphonenumber script to enable validation/formatting
utilsScript: '',
};
// https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
const regionlessNanpNumbers = ['800', '822', '833', '844', '855', '866', '877', '880', '881', '882', '883', '884', '885', '886', '887', '888', '889'];
// keep track of if the window.load event has fired as impossible to check after the fact
window.addEventListener('load', () => {
// UPDATE: use a public static field so we can fudge it in the tests
window.intlTelInputGlobals.windowLoaded = true;
});
// utility function to iterate over an object. can't use Object.entries or native forEach because
// of IE11
const forEachProp = (obj, callback) => {
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
callback(keys[i], obj[keys[i]]);
}
};
// run a method on each instance of the plugin
const forEachInstance = (method) => {
forEachProp(window.intlTelInputGlobals.instances, (key) => {
window.intlTelInputGlobals.instances[key][method]();
});
};
// this is our plugin class that we will create an instance of
// eslint-disable-next-line no-unused-vars
class Iti {
constructor(input, options) {
this.id = id++;
this.telInput = input;
this.activeItem = null;
this.highlightedItem = null;
// process specified options / defaults
// alternative to Object.assign, which isn't supported by IE11
const customOptions = options || {};
this.options = {};
forEachProp(defaults, (key, value) => {
this.options[key] = (customOptions.hasOwnProperty(key)) ? customOptions[key] : value;
});
this.hadInitialPlaceholder = Boolean(input.getAttribute('placeholder'));
}
_init() {
// if in nationalMode, disable options relating to dial codes
if (this.options.nationalMode) this.options.autoHideDialCode = false;
// if separateDialCode then doesn't make sense to A) insert dial code into input
// (autoHideDialCode), and B) display national numbers (because we're displaying the country
// dial code next to them)
if (this.options.separateDialCode) {
this.options.autoHideDialCode = this.options.nationalMode = false;
}
// we cannot just test screen size as some smartphones/website meta tags will report desktop
// resolutions
// Note: for some reason jasmine breaks if you put this in the main Plugin function with the
// rest of these declarations
// Note: to target Android Mobiles (and not Tablets), we must find 'Android' and 'Mobile'
this.isMobile = /Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (this.isMobile) {
// trigger the mobile dropdown css
document.body.classList.add('iti-mobile');
// on mobile, we want a full screen dropdown, so we must append it to the body
if (!this.options.dropdownContainer) this.options.dropdownContainer = document.body;
}
// these promises get resolved when their individual requests complete
// this way the dev can do something like iti.promise.then(...) to know when all requests are
// complete
if (typeof Promise !== 'undefined') {
const autoCountryPromise = new Promise((resolve, reject) => {
this.resolveAutoCountryPromise = resolve;
this.rejectAutoCountryPromise = reject;
});
const utilsScriptPromise = new Promise((resolve, reject) => {
this.resolveUtilsScriptPromise = resolve;
this.rejectUtilsScriptPromise = reject;
});
this.promise = Promise.all([autoCountryPromise, utilsScriptPromise]);
} else {
// prevent errors when Promise doesn't exist
this.resolveAutoCountryPromise = this.rejectAutoCountryPromise = () => {};
this.resolveUtilsScriptPromise = this.rejectUtilsScriptPromise = () => {};
}
// in various situations there could be no country selected initially, but we need to be able
// to assume this variable exists
this.selectedCountryData = {};
// process all the data: onlyCountries, excludeCountries, preferredCountries etc
this._processCountryData();
// generate the markup
this._generateMarkup();
// set the initial state of the input value and the selected flag
this._setInitialState();
// start all of the event listeners: autoHideDialCode, input keydown, selectedFlag click
this._initListeners();
// utils script, and auto country
this._initRequests();
}
/********************
* PRIVATE METHODS
********************/
// prepare all of the country data, including onlyCountries, excludeCountries and
// preferredCountries options
_processCountryData() {
// process onlyCountries or excludeCountries array if present
this._processAllCountries();
// process the countryCodes map
this._processCountryCodes();
// process the preferredCountries
this._processPreferredCountries();
// translate countries according to localizedCountries option
if (this.options.localizedCountries) this._translateCountriesByLocale();
// sort countries by name
if (this.options.onlyCountries.length || this.options.localizedCountries) {
this.countries.sort(this._countryNameSort);
}
}
// add a country code to this.countryCodes
_addCountryCode(iso2, dialCode, priority) {
if (dialCode.length > this.dialCodeMaxLen) {
this.dialCodeMaxLen = dialCode.length;
}
if (!this.countryCodes.hasOwnProperty(dialCode)) {
this.countryCodes[dialCode] = [];
}
// bail if we already have this country for this dialCode
for (let i = 0; i < this.countryCodes[dialCode].length; i++) {
if (this.countryCodes[dialCode][i] === iso2) return;
}
// check for undefined as 0 is falsy
const index = (priority !== undefined) ? priority : this.countryCodes[dialCode].length;
this.countryCodes[dialCode][index] = iso2;
}
// process onlyCountries or excludeCountries array if present
_processAllCountries() {
if (this.options.onlyCountries.length) {
const lowerCaseOnlyCountries = this.options.onlyCountries.map(
country => country.toLowerCase()
);
this.countries = allCountries.filter(
country => lowerCaseOnlyCountries.indexOf(country.iso2) > -1
);
} else if (this.options.excludeCountries.length) {
const lowerCaseExcludeCountries = this.options.excludeCountries.map(
country => country.toLowerCase()
);
this.countries = allCountries.filter(
country => lowerCaseExcludeCountries.indexOf(country.iso2) === -1
);
} else {
this.countries = allCountries;
}
}
// Translate Countries by object literal provided on config
_translateCountriesByLocale() {
for (let i = 0; i < this.countries.length; i++) {
const iso = this.countries[i].iso2.toLowerCase();
if (this.options.localizedCountries.hasOwnProperty(iso)) {
this.countries[i].name = this.options.localizedCountries[iso];
}
}
}
// sort by country name
_countryNameSort(a, b) {
return a.name.localeCompare(b.name);
}
// process the countryCodes map
_processCountryCodes() {
this.dialCodeMaxLen = 0;
this.countryCodes = {};
// first: add dial codes
for (let i = 0; i < this.countries.length; i++) {
const c = this.countries[i];
this._addCountryCode(c.iso2, c.dialCode, c.priority);
}
// next: add area codes
// this is a second loop over countries, to make sure we have all of the "root" countries
// already in the map, so that we can access them, as each time we add an area code substring
// to the map, we also need to include the "root" country's code, as that also matches
for (let i = 0; i < this.countries.length; i++) {
const c = this.countries[i];
// area codes
if (c.areaCodes) {
const rootCountryCode = this.countryCodes[c.dialCode][0];
// for each area code
for (let j = 0; j < c.areaCodes.length; j++) {
const areaCode = c.areaCodes[j];
// for each digit in the area code to add all partial matches as well
for (let k = 1; k < areaCode.length; k++) {
const partialDialCode = c.dialCode + areaCode.substr(0, k);
// start with the root country, as that also matches this dial code
this._addCountryCode(rootCountryCode, partialDialCode);
this._addCountryCode(c.iso2, partialDialCode);
}
// add the full area code
this._addCountryCode(c.iso2, c.dialCode + areaCode);
}
}
}
}
// process preferred countries - iterate through the preferences, fetching the country data for
// each one
_processPreferredCountries() {
this.preferredCountries = [];
for (let i = 0; i < this.options.preferredCountries.length; i++) {
const countryCode = this.options.preferredCountries[i].toLowerCase();
const countryData = this._getCountryData(countryCode, false, true);
if (countryData) this.preferredCountries.push(countryData);
}
}
// create a DOM element
_createEl(name, attrs, container) {
const el = document.createElement(name);
if (attrs) forEachProp(attrs, (key, value) => el.setAttribute(key, value));
if (container) container.appendChild(el);
return el;
}
// generate all of the markup for the plugin: the selected flag overlay, and the dropdown
_generateMarkup() {
// prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
// easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
// autocompleted number, which on submit could mean wrong number is saved (esp in nationalMode)
this.telInput.setAttribute('autocomplete', 'off');
// containers (mostly for positioning)
let parentClass = 'iti';
if (this.options.allowDropdown) parentClass += ' iti--allow-dropdown';
if (this.options.separateDialCode) parentClass += ' iti--separate-dial-code';
if (this.options.customContainer) {
parentClass += ' ';
parentClass += this.options.customContainer;
}
const wrapper = this._createEl('div', { class: parentClass });
this.telInput.parentNode.insertBefore(wrapper, this.telInput);
this.flagsContainer = this._createEl('div', { class: 'iti__flag-container' }, wrapper);
wrapper.appendChild(this.telInput);
// selected flag (displayed to left of input)
this.selectedFlag = this._createEl('div', {
class: 'iti__selected-flag',
role: 'combobox',
'aria-owns': 'country-listbox',
}, this.flagsContainer);
this.selectedFlagInner = this._createEl('div', { class: 'iti__flag' }, this.selectedFlag);
if (this.options.separateDialCode) {
this.selectedDialCode = this._createEl('div', { class: 'iti__selected-dial-code' }, this.selectedFlag);
}
if (this.options.allowDropdown) {
// make element focusable and tab navigable
this.selectedFlag.setAttribute('tabindex', '0');
this.dropdownArrow = this._createEl('div', { class: 'iti__arrow' }, this.selectedFlag);
// country dropdown: preferred countries, then divider, then all countries
this.countryList = this._createEl('ul', {
class: 'iti__country-list iti__hide',
id: 'country-listbox',
'aria-expanded': 'false',
role: 'listbox',
});
if (this.preferredCountries.length) {
this._appendListItems(this.preferredCountries, 'iti__preferred');
this._createEl('li', {
class: 'iti__divider',
role: 'separator',
'aria-disabled': 'true',
}, this.countryList);
}
this._appendListItems(this.countries, 'iti__standard');
// create dropdownContainer markup
if (this.options.dropdownContainer) {
this.dropdown = this._createEl('div', { class: 'iti iti--container' });
this.dropdown.appendChild(this.countryList);
} else {
this.flagsContainer.appendChild(this.countryList);
}
}
if (this.options.hiddenInput) {
let hiddenInputName = this.options.hiddenInput;
const name = this.telInput.getAttribute('name');
if (name) {
const i = name.lastIndexOf('[');
// if input name contains square brackets, then give the hidden input the same name,
// replacing the contents of the last set of brackets with the given hiddenInput name
if (i !== -1) hiddenInputName = `${name.substr(0, i)}[${hiddenInputName}]`;
}
this.hiddenInput = this._createEl('input', {
type: 'hidden',
name: hiddenInputName,
});
wrapper.appendChild(this.hiddenInput);
}
}
// add a country <li> to the countryList <ul> container
_appendListItems(countries, className) {
// we create so many DOM elements, it is faster to build a temp string
// and then add everything to the DOM in one go at the end
let tmp = '';
// for each country
for (let i = 0; i < countries.length; i++) {
const c = countries[i];
// open the list item
tmp += `<li class='iti__country ${className}' tabIndex='-1' id='iti-item-${c.iso2}' role='option' data-dial-code='${c.dialCode}' data-country-code='${c.iso2}'>`;
// add the flag
tmp += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
// and the country name and dial code
tmp += `<span class='iti__country-name'>${c.name}</span>`;
tmp += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
// close the list item
tmp += '</li>';
}
this.countryList.insertAdjacentHTML('beforeend', tmp);
}
// set the initial state of the input value and the selected flag by:
// 1. extracting a dial code from the given number
// 2. using explicit initialCountry
// 3. picking the first preferred country
// 4. picking the first country
_setInitialState() {
const val = this.telInput.value;
const dialCode = this._getDialCode(val);
const isRegionlessNanp = this._isRegionlessNanp(val);
const {
initialCountry,
nationalMode,
autoHideDialCode,
separateDialCode,
} = this.options;
// if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
// flag, else fall back to the default country
if (dialCode && !isRegionlessNanp) {
this._updateFlagFromNumber(val);
} else if (initialCountry !== 'auto') {
// see if we should select a flag
if (initialCountry) {
this._setFlag(initialCountry.toLowerCase());
} else {
if (dialCode && isRegionlessNanp) {
// has intl dial code, is regionless nanp, and no initialCountry, so default to US
this._setFlag('us');
} else {
// no dial code and no initialCountry, so default to first in list
this.defaultCountry = (this.preferredCountries.length) ? this.preferredCountries[0].iso2
: this.countries[0].iso2;
if (!val) {
this._setFlag(this.defaultCountry);
}
}
}
// if empty and no nationalMode and no autoHideDialCode then insert the default dial code
if (!val && !nationalMode && !autoHideDialCode && !separateDialCode) {
this.telInput.value = `+${this.selectedCountryData.dialCode}`;
}
}
// NOTE: if initialCountry is set to auto, that will be handled separately
// format - note this wont be run after _updateDialCode as that's only called if no val
if (val) this._updateValFromNumber(val);
}
// initialise the main event listeners: input keyup, and click selected flag
_initListeners() {
this._initKeyListeners();
if (this.options.autoHideDialCode) this._initBlurListeners();
if (this.options.allowDropdown) this._initDropdownListeners();
if (this.hiddenInput) this._initHiddenInputListener();
}
// update hidden input on form submit
_initHiddenInputListener() {
this._handleHiddenInputSubmit = () => {
this.hiddenInput.value = this.getNumber();
};
if (this.telInput.form) this.telInput.form.addEventListener('submit', this._handleHiddenInputSubmit);
}
// iterate through parent nodes to find the closest label ancestor, if it exists
_getClosestLabel() {
let el = this.telInput;
while (el && el.tagName !== 'LABEL') el = el.parentNode;
return el;
}
// initialise the dropdown listeners
_initDropdownListeners() {
// hack for input nested inside label (which is valid markup): clicking the selected-flag to
// open the dropdown would then automatically trigger a 2nd click on the input which would
// close it again
this._handleLabelClick = (e) => {
// if the dropdown is closed, then focus the input, else ignore the click
if (this.countryList.classList.contains('iti__hide')) this.telInput.focus();
else e.preventDefault();
};
const label = this._getClosestLabel();
if (label) label.addEventListener('click', this._handleLabelClick);
// toggle country dropdown on click
this._handleClickSelectedFlag = () => {
// only intercept this event if we're opening the dropdown
// else let it bubble up to the top ("click-off-to-close" listener)
// we cannot just stopPropagation as it may be needed to close another instance
if (this.countryList.classList.contains('iti__hide') && !this.telInput.disabled && !this.telInput.readOnly) {
this._showDropdown();
}
};
this.selectedFlag.addEventListener('click', this._handleClickSelectedFlag);
// open dropdown list if currently focused
this._handleFlagsContainerKeydown = (e) => {
const isDropdownHidden = this.countryList.classList.contains('iti__hide');
if (isDropdownHidden && ['ArrowUp', 'Up', 'ArrowDown', 'Down', ' ', 'Enter'].indexOf(e.key) !== -1) {
// prevent form from being submitted if "ENTER" was pressed
e.preventDefault();
// prevent event from being handled again by document
e.stopPropagation();
this._showDropdown();
}
// allow navigation from dropdown to input on TAB
if (e.key === 'Tab') this._closeDropdown();
};
this.flagsContainer.addEventListener('keydown', this._handleFlagsContainerKeydown);
}
// init many requests: utils script / geo ip lookup
_initRequests() {
// if the user has specified the path to the utils script, fetch it on window.load, else resolve
if (this.options.utilsScript && !window.intlTelInputUtils) {
// if the plugin is being initialised after the window.load event has already been fired
if (window.intlTelInputGlobals.windowLoaded) {
window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
} else {
// wait until the load event so we don't block any other requests e.g. the flags image
window.addEventListener('load', () => {
window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
});
}
} else this.resolveUtilsScriptPromise();
if (this.options.initialCountry === 'auto') this._loadAutoCountry();
else this.resolveAutoCountryPromise();
}
// perform the geo ip lookup
_loadAutoCountry() {
// 3 options:
// 1) already loaded (we're done)
// 2) not already started loading (start)
// 3) already started loading (do nothing - just wait for loading callback to fire)
if (window.intlTelInputGlobals.autoCountry) {
this.handleAutoCountry();
} else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
// don't do this twice!
window.intlTelInputGlobals.startedLoadingAutoCountry = true;
if (typeof this.options.geoIpLookup === 'function') {
this.options.geoIpLookup((countryCode) => {
window.intlTelInputGlobals.autoCountry = countryCode.toLowerCase();
// tell all instances the auto country is ready
// TODO: this should just be the current instances
// UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
// away (e.g. if they have already done the geo ip lookup somewhere else). Using
// setTimeout means that the current thread of execution will finish before executing
// this, which allows the plugin to finish initialising.
setTimeout(() => forEachInstance('handleAutoCountry'));
}, () => forEachInstance('rejectAutoCountryPromise'));
}
}
}
// initialize any key listeners
_initKeyListeners() {
// update flag on keyup
this._handleKeyupEvent = () => {
if (this._updateFlagFromNumber(this.telInput.value)) {
this._triggerCountryChange();
}
};
this.telInput.addEventListener('keyup', this._handleKeyupEvent);
// update flag on cut/paste events (now supported in all major browsers)
this._handleClipboardEvent = () => {
// hack because "paste" event is fired before input is updated
setTimeout(this._handleKeyupEvent);
};
this.telInput.addEventListener('cut', this._handleClipboardEvent);
this.telInput.addEventListener('paste', this._handleClipboardEvent);
}
// adhere to the input's maxlength attr
_cap(number) {
const max = this.telInput.getAttribute('maxlength');
return (max && number.length > max) ? number.substr(0, max) : number;
}
// listen for blur/submit (for autoHideDialCode feature)
_initBlurListeners() {
// on blur or form submit: if just a dial code then remove it
this._handleSubmitOrBlurEvent = () => {
this._removeEmptyDialCode();
};
if (this.telInput.form) this.telInput.form.addEventListener('submit', this._handleSubmitOrBlurEvent);
this.telInput.addEventListener('blur', this._handleSubmitOrBlurEvent);
// made the decision not to trigger blur() now, because would only do anything in the case
// where they manually set the initial value to just a dial code, in which case they probably
// want it to be displayed.
}
// clear the input if it just contains a dial code
_removeEmptyDialCode() {
if (this.telInput.value.charAt(0) === '+') {
const numeric = this._getNumeric(this.telInput.value);
// if just a plus, or if just a dial code
if (!numeric || this.selectedCountryData.dialCode === numeric) {
this.telInput.value = '';
}
}
}
// extract the numeric digits from the given string
_getNumeric(s) {
return s.replace(/\D/g, '');
}
// trigger a custom event on the input
_trigger(name) {
// have to use old school document.createEvent as IE11 doesn't support `new Event()` syntax
const e = document.createEvent('Event');
e.initEvent(name, true, true); // can bubble, and is cancellable
this.telInput.dispatchEvent(e);
}
// show the dropdown
_showDropdown() {
this.countryList.classList.remove('iti__hide');
this.countryList.setAttribute('aria-expanded', 'true');
this._setDropdownPosition();
// update highlighting and scroll to active list item
if (this.activeItem) {
this._highlightListItem(this.activeItem, false);
this._scrollTo(this.activeItem, true);
}
// bind all the dropdown-related listeners: mouseover, click, click-off, keydown
this._bindDropdownListeners();
// update the arrow
this.dropdownArrow.classList.add('iti__arrow--up');
this._trigger('open:countrydropdown');
}
// make sure the el has the className or not, depending on the value of shouldHaveClass
_toggleClass(el, className, shouldHaveClass) {
if (shouldHaveClass && !el.classList.contains(className)) el.classList.add(className);
else if (!shouldHaveClass && el.classList.contains(className)) el.classList.remove(className);
}
// decide where to position dropdown (depends on position within viewport, and scroll)
_setDropdownPosition() {
if (this.options.dropdownContainer) {
this.options.dropdownContainer.appendChild(this.dropdown);
}
if (!this.isMobile) {
const pos = this.telInput.getBoundingClientRect();
// windowTop from https://stackoverflow.com/a/14384091/217866
const windowTop = window.pageYOffset || document.documentElement.scrollTop;
const inputTop = pos.top + windowTop;
const dropdownHeight = this.countryList.offsetHeight;
// dropdownFitsBelow = (dropdownBottom < windowBottom)
const dropdownFitsBelow = (inputTop + this.telInput.offsetHeight + dropdownHeight
< (windowTop + window.innerHeight));
const dropdownFitsAbove = (inputTop - dropdownHeight > windowTop);
// by default, the dropdown will be below the input. If we want to position it above the
// input, we add the dropup class.
this._toggleClass(this.countryList, 'iti__country-list--dropup', (!dropdownFitsBelow && dropdownFitsAbove));
// if dropdownContainer is enabled, calculate postion
if (this.options.dropdownContainer) {
// by default the dropdown will be directly over the input because it's not in the flow.
// If we want to position it below, we need to add some extra top value.
const extraTop = (!dropdownFitsBelow && dropdownFitsAbove) ? 0 : this.telInput.offsetHeight;
// calculate placement
this.dropdown.style.top = `${inputTop + extraTop}px`;
this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
// close menu on window scroll
this._handleWindowScroll = () => this._closeDropdown();
window.addEventListener('scroll', this._handleWindowScroll);
}
}
}
// iterate through parent nodes to find the closest list item
_getClosestListItem(target) {
let el = target;
while (el && el !== this.countryList && !el.classList.contains('iti__country')) el = el.parentNode;
// if we reached the countryList element, then return null
return (el === this.countryList) ? null : el;
}
// we only bind dropdown listeners when the dropdown is open
_bindDropdownListeners() {
// when mouse over a list item, just highlight that one
// we add the class "highlight", so if they hit "enter" we know which one to select
this._handleMouseoverCountryList = (e) => {
// handle event delegation, as we're listening for this event on the countryList
const listItem = this._getClosestListItem(e.target);
if (listItem) this._highlightListItem(listItem, false);
};
this.countryList.addEventListener('mouseover', this._handleMouseoverCountryList);
// listen for country selection
this._handleClickCountryList = (e) => {
const listItem = this._getClosestListItem(e.target);
if (listItem) this._selectListItem(listItem);
};
this.countryList.addEventListener('click', this._handleClickCountryList);
// click off to close
// (except when this initial opening click is bubbling up)
// we cannot just stopPropagation as it may be needed to close another instance
let isOpening = true;
this._handleClickOffToClose = () => {
if (!isOpening) this._closeDropdown();
isOpening = false;
};
document.documentElement.addEventListener('click', this._handleClickOffToClose);
// listen for up/down scrolling, enter to select, or letters to jump to country name.
// use keydown as keypress doesn't fire for non-char keys and we want to catch if they
// just hit down and hold it to scroll down (no keyup event).
// listen on the document because that's where key events are triggered if no input has focus
let query = '';
let queryTimer = null;
this._handleKeydownOnDropdown = (e) => {
// prevent down key from scrolling the whole page,
// and enter key from submitting a form etc
e.preventDefault();
// up and down to navigate
if (e.key === 'ArrowUp' || e.key === 'Up' || e.key === 'ArrowDown' || e.key === 'Down') this._handleUpDownKey(e.key);
// enter to select
else if (e.key === 'Enter') this._handleEnterKey();
// esc to close
else if (e.key === 'Escape') this._closeDropdown();
// alpha chars to perform search
// regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
else if (/^[a-zA-ZÀ-ÿ ]$/.test(e.key)) {
// jump to countries that start with the query string
if (queryTimer) clearTimeout(queryTimer);
query += e.key.toLowerCase();
this._searchForCountry(query);
// if the timer hits 1 second, reset the query
queryTimer = setTimeout(() => {
query = '';
}, 1000);
}
};
document.addEventListener('keydown', this._handleKeydownOnDropdown);
}
// highlight the next/prev item in the list (and ensure it is visible)
_handleUpDownKey(key) {
let next = (key === 'ArrowUp' || key === 'Up') ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
if (next) {
// skip the divider
if (next.classList.contains('iti__divider')) {
next = (key === 'ArrowUp' || key === 'Up') ? next.previousElementSibling : next.nextElementSibling;
}
this._highlightListItem(next, true);
}
}
// select the currently highlighted item
_handleEnterKey() {
if (this.highlightedItem) this._selectListItem(this.highlightedItem);
}
// find the first list item whose name starts with the query string
_searchForCountry(query) {
for (let i = 0; i < this.countries.length; i++) {
if (this._startsWith(this.countries[i].name, query)) {
const listItem = this.countryList.querySelector(`#iti-item-${this.countries[i].iso2}`);
// update highlighting and scroll
this._highlightListItem(listItem, false);
this._scrollTo(listItem, true);
break;
}
}
}
// check if string a starts with string b
_startsWith(a, b) {
return (a.substr(0, b.length).toLowerCase() === b);
}
// update the input's value to the given val (format first if possible)
// NOTE: this is called from _setInitialState, handleUtils and setNumber
_updateValFromNumber(originalNumber) {
let number = originalNumber;
if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
const useNational = (!this.options.separateDialCode
&& (this.options.nationalMode || number.charAt(0) !== '+'));
const { NATIONAL, INTERNATIONAL } = intlTelInputUtils.numberFormat;
const format = useNational ? NATIONAL : INTERNATIONAL;
number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
}
number = this._beforeSetNumber(number);
this.telInput.value = number;
}
// check if need to select a new flag based on the given number
// Note: called from _setInitialState, keyup handler, setNumber
_updateFlagFromNumber(originalNumber) {
// if we're in nationalMode and we already have US/Canada selected, make sure the number starts
// with a +1 so _getDialCode will be able to extract the area code
// update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
// from the number), that means we're initialising the plugin with a number that already has a
// dial code, so fine to ignore this bit
let number = originalNumber;
const selectedDialCode = this.selectedCountryData.dialCode;
const isNanp = selectedDialCode === '1';
if (number && this.options.nationalMode && isNanp && number.charAt(0) !== '+') {
if (number.charAt(0) !== '1') number = `1${number}`;
number = `+${number}`;
}
// update flag if user types area code for another country
if (this.options.separateDialCode && selectedDialCode && number.charAt(0) !== '+') {
number = `+${selectedDialCode}${number}`;
}
// try and extract valid dial code from input
const dialCode = this._getDialCode(number);
const numeric = this._getNumeric(number);
let countryCode = null;
if (dialCode) {
const countryCodes = this.countryCodes[this._getNumeric(dialCode)];
// check if the right country is already selected. this should be false if the number is
// longer than the matched dial code because in this case we need to make sure that if
// there are multiple country matches, that the first one is selected (note: we could
// just check that here, but it requires the same loop that we already have later)
const alreadySelected = (countryCodes.indexOf(this.selectedCountryData.iso2) !== -1)
&& (numeric.length <= dialCode.length - 1);
const isRegionlessNanpNumber = (selectedDialCode === '1' && this._isRegionlessNanp(numeric));
// only update the flag if:
// A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
// AND
// B) the right country is not already selected
if (!isRegionlessNanpNumber && !alreadySelected) {
// if using onlyCountries option, countryCodes[0] may be empty, so we must find the first
// non-empty index
for (let j = 0; j < countryCodes.length; j++) {
if (countryCodes[j]) {
countryCode = countryCodes[j];
break;
}
}
}
} else if (number.charAt(0) === '+' && numeric.length) {
// invalid dial code, so empty
// Note: use getNumeric here because the number has not been formatted yet, so could contain
// bad chars
countryCode = '';
} else if (!number || number === '+') {
// empty, or just a plus, so default
countryCode = this.defaultCountry;
}
if (countryCode !== null) {
return this._setFlag(countryCode);
}
return false;
}
// check if the given number is a regionless NANP number (expects the number to contain an
// international dial code)
_isRegionlessNanp(number) {
const numeric = this._getNumeric(number);
if (numeric.charAt(0) === '1') {
const areaCode = numeric.substr(1, 3);
return (regionlessNanpNumbers.indexOf(areaCode) !== -1);
}
return false;
}
// remove highlighting from other list items and highlight the given item
_highlightListItem(listItem, shouldFocus) {
const prevItem = this.highlightedItem;
if (prevItem) prevItem.classList.remove('iti__highlight');
this.highlightedItem = listItem;
this.highlightedItem.classList.add('iti__highlight');
if (shouldFocus) this.highlightedItem.focus();
}
// find the country data for the given country code
// the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
_getCountryData(countryCode, ignoreOnlyCountriesOption, allowFail) {
const countryList = (ignoreOnlyCountriesOption) ? allCountries : this.countries;
for (let i = 0; i < countryList.length; i++) {
if (countryList[i].iso2 === countryCode) {
return countryList[i];
}
}
if (allowFail) {
return null;
}
throw new Error(`No country data for '${countryCode}'`);
}
// select the given flag, update the placeholder and the active list item
// Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry
_setFlag(countryCode) {
const prevCountry = (this.selectedCountryData.iso2) ? this.selectedCountryData : {};
// do this first as it will throw an error and stop if countryCode is invalid
this.selectedCountryData = (countryCode) ? this._getCountryData(countryCode, false, false) : {};
// update the defaultCountry - we only need the iso2 from now on, so just store that
if (this.selectedCountryData.iso2) {
this.defaultCountry = this.selectedCountryData.iso2;
}
this.selectedFlagInner.setAttribute('class', `iti__flag iti__${countryCode}`);
// update the selected country's title attribute
const title = (countryCode) ? `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}` : 'Unknown';
this.selectedFlag.setAttribute('title', title);
if (this.options.separateDialCode) {
const dialCode = (this.selectedCountryData.dialCode) ? `+${this.selectedCountryData.dialCode}` : '';
this.selectedDialCode.innerHTML = dialCode;
// offsetWidth is zero if input is in a hidden container during initialisation
const selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
// add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
}
// and the input's placeholder
this._updatePlaceholder();
// update the active list item
if (this.options.allowDropdown) {
const prevItem = this.activeItem;
if (prevItem) {
prevItem.classList.remove('iti__active');
prevItem.setAttribute('aria-selected', 'false');
}
if (countryCode) {
const nextItem = this.countryList.querySelector(`#iti-item-${countryCode}`);
nextItem.setAttribute('aria-selected', 'true');
nextItem.classList.add('iti__active');
this.activeItem = nextItem;
this.countryList.setAttribute('aria-activedescendant', nextItem.getAttribute('id'));
}
}
// return if the flag has changed or not
return (prevCountry.iso2 !== countryCode);
}
// when the input is in a hidden container during initialisation, we must inject some markup
// into the end of the DOM to calculate the correct offsetWidth
_getHiddenSelectedFlagWidth() {