intl-tel-input 20.2.0 → 20.3.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.
- package/README.md +6 -6
- package/build/js/data.js +1 -2
- package/build/js/data.min.js +2 -3
- package/build/js/intlTelInput-jquery.js +1434 -1578
- package/build/js/intlTelInput-jquery.min.js +2 -4
- package/build/js/intlTelInput.js +1434 -1578
- package/build/js/intlTelInput.min.js +2 -4
- package/package.json +13 -16
- package/react/build/IntlTelInput.cjs.js +1 -2
- package/react/build/IntlTelInput.cjs.js.map +3 -3
- package/react/build/IntlTelInput.esm.js +1 -2
- package/react/build/IntlTelInput.esm.js.map +3 -3
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* International Telephone Input v20.
|
|
2
|
+
* International Telephone Input v20.3.0
|
|
3
3
|
* https://github.com/jackocnr/intl-tel-input.git
|
|
4
4
|
* Licensed under the MIT license
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
6
|
// wrap in UMD
|
|
8
7
|
(function(factory) {
|
|
9
8
|
if (typeof module === "object" && module.exports) {
|
|
@@ -42,138 +41,21 @@
|
|
|
42
41
|
nodeById: {}
|
|
43
42
|
};
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (Object.getOwnPropertySymbols) {
|
|
49
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
50
|
-
enumerableOnly && (symbols = symbols.filter(function(sym) {
|
|
51
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
52
|
-
})), keys.push.apply(keys, symbols);
|
|
53
|
-
}
|
|
54
|
-
return keys;
|
|
55
|
-
}
|
|
56
|
-
function _objectSpread(target) {
|
|
57
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
58
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
|
59
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function(key) {
|
|
60
|
-
_defineProperty(target, key, source[key]);
|
|
61
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function(key) {
|
|
62
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
return target;
|
|
66
|
-
}
|
|
67
|
-
function _defineProperty(obj, key, value) {
|
|
68
|
-
key = _toPropertyKey(key);
|
|
69
|
-
if (key in obj) {
|
|
70
|
-
Object.defineProperty(obj, key, {
|
|
71
|
-
value: value,
|
|
72
|
-
enumerable: true,
|
|
73
|
-
configurable: true,
|
|
74
|
-
writable: true
|
|
75
|
-
});
|
|
76
|
-
} else {
|
|
77
|
-
obj[key] = value;
|
|
78
|
-
}
|
|
79
|
-
return obj;
|
|
80
|
-
}
|
|
81
|
-
function _slicedToArray(arr, i) {
|
|
82
|
-
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
83
|
-
}
|
|
84
|
-
function _nonIterableRest() {
|
|
85
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
86
|
-
}
|
|
87
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
88
|
-
if (!o) return;
|
|
89
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
90
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
91
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
92
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
93
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
94
|
-
}
|
|
95
|
-
function _arrayLikeToArray(arr, len) {
|
|
96
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
97
|
-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
98
|
-
return arr2;
|
|
99
|
-
}
|
|
100
|
-
function _iterableToArrayLimit(arr, i) {
|
|
101
|
-
var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
|
|
102
|
-
if (null != _i) {
|
|
103
|
-
var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1;
|
|
104
|
-
try {
|
|
105
|
-
if (_x = (_i = _i.call(arr)).next, 0 === i) {
|
|
106
|
-
if (Object(_i) !== _i) return;
|
|
107
|
-
_n = !1;
|
|
108
|
-
} else for (;!(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) ;
|
|
109
|
-
} catch (err) {
|
|
110
|
-
_d = !0, _e = err;
|
|
111
|
-
} finally {
|
|
112
|
-
try {
|
|
113
|
-
if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return;
|
|
114
|
-
} finally {
|
|
115
|
-
if (_d) throw _e;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return _arr;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
function _arrayWithHoles(arr) {
|
|
122
|
-
if (Array.isArray(arr)) return arr;
|
|
123
|
-
}
|
|
124
|
-
function _classCallCheck(instance, Constructor) {
|
|
125
|
-
if (!(instance instanceof Constructor)) {
|
|
126
|
-
throw new TypeError("Cannot call a class as a function");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function _defineProperties(target, props) {
|
|
130
|
-
for (var i = 0; i < props.length; i++) {
|
|
131
|
-
var descriptor = props[i];
|
|
132
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
133
|
-
descriptor.configurable = true;
|
|
134
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
135
|
-
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
139
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
140
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
141
|
-
Object.defineProperty(Constructor, "prototype", {
|
|
142
|
-
writable: false
|
|
143
|
-
});
|
|
144
|
-
return Constructor;
|
|
145
|
-
}
|
|
146
|
-
function _toPropertyKey(arg) {
|
|
147
|
-
var key = _toPrimitive(arg, "string");
|
|
148
|
-
return typeof key === "symbol" ? key : String(key);
|
|
149
|
-
}
|
|
150
|
-
function _toPrimitive(input, hint) {
|
|
151
|
-
if (typeof input !== "object" || input === null) return input;
|
|
152
|
-
var prim = input[Symbol.toPrimitive];
|
|
153
|
-
if (prim !== undefined) {
|
|
154
|
-
var res = prim.call(input, hint || "default");
|
|
155
|
-
if (typeof res !== "object") return res;
|
|
156
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
157
|
-
}
|
|
158
|
-
return (hint === "string" ? String : Number)(input);
|
|
159
|
-
}
|
|
160
|
-
var intlTelInputGlobals = {
|
|
161
|
-
getInstance: function getInstance(input) {
|
|
162
|
-
var id = input.getAttribute("data-intl-tel-input-id");
|
|
44
|
+
const intlTelInputGlobals = {
|
|
45
|
+
getInstance: input => {
|
|
46
|
+
const id = input.getAttribute("data-intl-tel-input-id");
|
|
163
47
|
return window.intlTelInputGlobals.instances[id];
|
|
164
48
|
},
|
|
165
49
|
instances: {},
|
|
166
50
|
// using a global like this allows us to mock it in the tests
|
|
167
|
-
documentReady:
|
|
168
|
-
return document.readyState === "complete";
|
|
169
|
-
}
|
|
51
|
+
documentReady: () => document.readyState === "complete"
|
|
170
52
|
};
|
|
171
53
|
if (typeof window === "object") {
|
|
172
54
|
window.intlTelInputGlobals = intlTelInputGlobals;
|
|
173
55
|
}
|
|
174
56
|
// these vars persist through all instances of the plugin
|
|
175
|
-
|
|
176
|
-
|
|
57
|
+
let id = 0;
|
|
58
|
+
const defaults = {
|
|
177
59
|
// whether or not to allow the dropdown
|
|
178
60
|
allowDropdown: true,
|
|
179
61
|
// add a placeholder in the input with an example number for the selected country
|
|
@@ -225,20 +107,18 @@
|
|
|
225
107
|
utilsScript: ""
|
|
226
108
|
};
|
|
227
109
|
// https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
|
|
228
|
-
|
|
110
|
+
const regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
|
|
229
111
|
// run a method on each instance of the plugin
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
112
|
+
const forEachInstance = method => {
|
|
113
|
+
const {
|
|
114
|
+
instances
|
|
115
|
+
} = window.intlTelInputGlobals;
|
|
116
|
+
Object.values(instances).forEach(instance => instance[method]());
|
|
235
117
|
};
|
|
236
118
|
// this is our plugin class that we will create an instance of
|
|
237
119
|
// eslint-disable-next-line no-unused-vars
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
241
|
-
_classCallCheck(this, Iti);
|
|
120
|
+
class Iti {
|
|
121
|
+
constructor(input, customOptions = {}) {
|
|
242
122
|
this.id = id++;
|
|
243
123
|
this.telInput = input;
|
|
244
124
|
this.activeItem = null;
|
|
@@ -247,1558 +127,1536 @@
|
|
|
247
127
|
this.options = Object.assign({}, defaults, customOptions);
|
|
248
128
|
this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
|
|
249
129
|
}
|
|
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
|
-
}
|
|
130
|
+
_init() {
|
|
131
|
+
// if showing fullscreen popup, do not fix the width
|
|
132
|
+
if (this.options.useFullscreenPopup) {
|
|
133
|
+
this.options.fixDropdownWidth = false;
|
|
134
|
+
}
|
|
135
|
+
// when search enabled, we must fix the width else it would change with different results
|
|
136
|
+
if (this.options.countrySearch && !this.options.useFullscreenPopup) {
|
|
137
|
+
this.options.fixDropdownWidth = true;
|
|
138
|
+
}
|
|
139
|
+
// force showFlags=true if there's a dropdown and we're not displaying the dial code,
|
|
140
|
+
// as otherwise you just have a down arrow on it's own which doesn't make sense
|
|
141
|
+
const forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
|
|
142
|
+
if (!this.options.showFlags && forceShowFlags) {
|
|
143
|
+
this.options.showFlags = true;
|
|
144
|
+
}
|
|
145
|
+
// on mobile, we want a full screen dropdown, so we must append it to the body
|
|
146
|
+
if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
|
|
147
|
+
this.options.dropdownContainer = document.body;
|
|
148
|
+
}
|
|
149
|
+
// check if input has one parent with RTL
|
|
150
|
+
this.isRTL = !!this.telInput.closest("[dir=rtl]");
|
|
151
|
+
// these promises get resolved when their individual requests complete
|
|
152
|
+
// this way the dev can do something like iti.promise.then(...) to know when all requests are
|
|
153
|
+
// complete
|
|
154
|
+
const autoCountryPromise = new Promise((resolve, reject) => {
|
|
155
|
+
this.resolveAutoCountryPromise = resolve;
|
|
156
|
+
this.rejectAutoCountryPromise = reject;
|
|
157
|
+
});
|
|
158
|
+
const utilsScriptPromise = new Promise((resolve, reject) => {
|
|
159
|
+
this.resolveUtilsScriptPromise = resolve;
|
|
160
|
+
this.rejectUtilsScriptPromise = reject;
|
|
161
|
+
});
|
|
162
|
+
this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
|
|
163
|
+
// in various situations there could be no country selected initially, but we need to be able
|
|
164
|
+
// to assume this variable exists
|
|
165
|
+
this.selectedCountryData = {};
|
|
166
|
+
// process all the data: onlyCountries, excludeCountries, preferredCountries etc
|
|
167
|
+
this._processCountryData();
|
|
168
|
+
// generate the markup
|
|
169
|
+
this._generateMarkup();
|
|
170
|
+
// set the initial state of the input value and the selected flag
|
|
171
|
+
this._setInitialState();
|
|
172
|
+
// start all of the event listeners: input keydown, selectedFlag click
|
|
173
|
+
this._initListeners();
|
|
174
|
+
// utils script, and auto country
|
|
175
|
+
this._initRequests();
|
|
176
|
+
}
|
|
177
|
+
/********************
|
|
178
|
+
* PRIVATE METHODS
|
|
179
|
+
********************/
|
|
180
|
+
// prepare all of the country data, including onlyCountries, excludeCountries and
|
|
181
|
+
// preferredCountries options
|
|
182
|
+
_processCountryData() {
|
|
183
|
+
// process onlyCountries or excludeCountries array if present
|
|
184
|
+
this._processAllCountries();
|
|
185
|
+
// generate this.dialCodes and this.dialCodeToIso2Map
|
|
186
|
+
this._processDialCodes();
|
|
187
|
+
// process the preferredCountries
|
|
188
|
+
this._processPreferredCountries();
|
|
189
|
+
// translate country names according to i18n option
|
|
190
|
+
this._translateCountryNames();
|
|
191
|
+
// sort countries by name
|
|
192
|
+
if (this.options.onlyCountries.length || this.options.i18n) {
|
|
193
|
+
this.countries.sort(this._countryNameSort);
|
|
315
194
|
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
|
|
323
|
-
this.dialCodeToIso2Map[dialCode] = [];
|
|
324
|
-
}
|
|
325
|
-
// bail if we already have this country for this dialCode
|
|
326
|
-
for (var i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
|
|
327
|
-
if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
// check for undefined as 0 is falsy
|
|
332
|
-
var index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
|
|
333
|
-
this.dialCodeToIso2Map[dialCode][index] = iso2;
|
|
334
|
-
}
|
|
335
|
-
}, {
|
|
336
|
-
key: "_processAllCountries",
|
|
337
|
-
value: function _processAllCountries() {
|
|
338
|
-
if (this.options.onlyCountries.length) {
|
|
339
|
-
var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) {
|
|
340
|
-
return country.toLowerCase();
|
|
341
|
-
});
|
|
342
|
-
this.countries = allCountries.filter(function(country) {
|
|
343
|
-
return lowerCaseOnlyCountries.indexOf(country.iso2) > -1;
|
|
344
|
-
});
|
|
345
|
-
} else if (this.options.excludeCountries.length) {
|
|
346
|
-
var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) {
|
|
347
|
-
return country.toLowerCase();
|
|
348
|
-
});
|
|
349
|
-
this.countries = allCountries.filter(function(country) {
|
|
350
|
-
return lowerCaseExcludeCountries.indexOf(country.iso2) === -1;
|
|
351
|
-
});
|
|
352
|
-
} else {
|
|
353
|
-
this.countries = allCountries;
|
|
354
|
-
}
|
|
195
|
+
}
|
|
196
|
+
// add a dial code to this.dialCodeToIso2Map
|
|
197
|
+
_addToDialCodeMap(iso2, dialCode, priority) {
|
|
198
|
+
if (dialCode.length > this.dialCodeMaxLen) {
|
|
199
|
+
this.dialCodeMaxLen = dialCode.length;
|
|
355
200
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
value: function _translateCountryNames() {
|
|
359
|
-
for (var i = 0; i < this.countries.length; i++) {
|
|
360
|
-
var iso2 = this.countries[i].iso2.toLowerCase();
|
|
361
|
-
if (this.options.i18n.hasOwnProperty(iso2)) {
|
|
362
|
-
this.countries[i].name = this.options.i18n[iso2];
|
|
363
|
-
}
|
|
364
|
-
}
|
|
201
|
+
if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
|
|
202
|
+
this.dialCodeToIso2Map[dialCode] = [];
|
|
365
203
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
return -1;
|
|
371
|
-
}
|
|
372
|
-
if (a.name > b.name) {
|
|
373
|
-
return 1;
|
|
204
|
+
// bail if we already have this country for this dialCode
|
|
205
|
+
for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
|
|
206
|
+
if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
|
|
207
|
+
return;
|
|
374
208
|
}
|
|
375
|
-
return 0;
|
|
376
209
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
this.dialCodes[c.dialCode] = true;
|
|
400
|
-
}
|
|
401
|
-
this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
|
|
210
|
+
// check for undefined as 0 is falsy
|
|
211
|
+
const index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
|
|
212
|
+
this.dialCodeToIso2Map[dialCode][index] = iso2;
|
|
213
|
+
}
|
|
214
|
+
// process onlyCountries or excludeCountries array if present
|
|
215
|
+
_processAllCountries() {
|
|
216
|
+
if (this.options.onlyCountries.length) {
|
|
217
|
+
const lowerCaseOnlyCountries = this.options.onlyCountries.map(country => country.toLowerCase());
|
|
218
|
+
this.countries = allCountries.filter(country => lowerCaseOnlyCountries.indexOf(country.iso2) > -1);
|
|
219
|
+
} else if (this.options.excludeCountries.length) {
|
|
220
|
+
const lowerCaseExcludeCountries = this.options.excludeCountries.map(country => country.toLowerCase());
|
|
221
|
+
this.countries = allCountries.filter(country => lowerCaseExcludeCountries.indexOf(country.iso2) === -1);
|
|
222
|
+
} else {
|
|
223
|
+
this.countries = allCountries;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// Translate Countries by object literal provided on config
|
|
227
|
+
_translateCountryNames() {
|
|
228
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
229
|
+
const iso2 = this.countries[i].iso2.toLowerCase();
|
|
230
|
+
if (this.options.i18n.hasOwnProperty(iso2)) {
|
|
231
|
+
this.countries[i].name = this.options.i18n[iso2];
|
|
402
232
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// sort by country name
|
|
236
|
+
_countryNameSort(a, b) {
|
|
237
|
+
if (a.name < b.name) {
|
|
238
|
+
return -1;
|
|
239
|
+
}
|
|
240
|
+
if (a.name > b.name) {
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
return 0;
|
|
244
|
+
}
|
|
245
|
+
// generate this.dialCodes and this.dialCodeToIso2Map
|
|
246
|
+
_processDialCodes() {
|
|
247
|
+
// here we store just dial codes, where the key is the dial code, and the value is true
|
|
248
|
+
// e.g. { 1: true, 7: true, 20: true, ... }
|
|
249
|
+
this.dialCodes = {};
|
|
250
|
+
this.dialCodeMaxLen = 0;
|
|
251
|
+
// here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
|
|
252
|
+
/* e.g.
|
|
253
|
+
* {
|
|
254
|
+
* 1: [ 'us', 'ca', ... ], # all NANP countries
|
|
255
|
+
* 12: [ 'us', 'ca', ... ], # subset of NANP countries
|
|
256
|
+
* 120: [ 'us', 'ca' ], # just US and Canada
|
|
257
|
+
* 1204: [ 'ca' ], # only Canada
|
|
258
|
+
* ...
|
|
259
|
+
* }
|
|
260
|
+
*/
|
|
261
|
+
this.dialCodeToIso2Map = {};
|
|
262
|
+
// first: add dial codes
|
|
263
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
264
|
+
const c = this.countries[i];
|
|
265
|
+
if (!this.dialCodes[c.dialCode]) {
|
|
266
|
+
this.dialCodes[c.dialCode] = true;
|
|
267
|
+
}
|
|
268
|
+
this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
|
|
269
|
+
}
|
|
270
|
+
// next: add area codes
|
|
271
|
+
// this is a second loop over countries, to make sure we have all of the "root" countries
|
|
272
|
+
// already in the map, so that we can access them, as each time we add an area code substring
|
|
273
|
+
// to the map, we also need to include the "root" country's code, as that also matches
|
|
274
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
275
|
+
const c = this.countries[i];
|
|
276
|
+
// area codes
|
|
277
|
+
if (c.areaCodes) {
|
|
278
|
+
const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
|
|
279
|
+
// for each area code
|
|
280
|
+
for (let j = 0; j < c.areaCodes.length; j++) {
|
|
281
|
+
const areaCode = c.areaCodes[j];
|
|
282
|
+
// for each digit in the area code to add all partial matches as well
|
|
283
|
+
for (let k = 1; k < areaCode.length; k++) {
|
|
284
|
+
const partialDialCode = c.dialCode + areaCode.substr(0, k);
|
|
285
|
+
// start with the root country, as that also matches this dial code
|
|
286
|
+
this._addToDialCodeMap(rootIso2Code, partialDialCode);
|
|
287
|
+
this._addToDialCodeMap(c.iso2, partialDialCode);
|
|
424
288
|
}
|
|
289
|
+
// add the full area code
|
|
290
|
+
this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
|
|
425
291
|
}
|
|
426
292
|
}
|
|
427
293
|
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
294
|
+
}
|
|
295
|
+
// process preferred countries - iterate through the preferences, fetching the country data for
|
|
296
|
+
// each one
|
|
297
|
+
_processPreferredCountries() {
|
|
298
|
+
this.preferredCountries = [];
|
|
299
|
+
for (let i = 0; i < this.options.preferredCountries.length; i++) {
|
|
300
|
+
const iso2 = this.options.preferredCountries[i].toLowerCase();
|
|
301
|
+
const countryData = this._getCountryData(iso2, true);
|
|
302
|
+
if (countryData) {
|
|
303
|
+
this.preferredCountries.push(countryData);
|
|
438
304
|
}
|
|
439
305
|
}
|
|
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
|
-
|
|
306
|
+
}
|
|
307
|
+
// create a DOM element
|
|
308
|
+
_createEl(name, attrs, container) {
|
|
309
|
+
const el = document.createElement(name);
|
|
310
|
+
if (attrs) {
|
|
311
|
+
Object.entries(attrs).forEach(([ key, value ]) => el.setAttribute(key, value));
|
|
312
|
+
}
|
|
313
|
+
if (container) {
|
|
314
|
+
container.appendChild(el);
|
|
315
|
+
}
|
|
316
|
+
return el;
|
|
317
|
+
}
|
|
318
|
+
// generate all of the markup for the plugin: the selected flag overlay, and the dropdown
|
|
319
|
+
_generateMarkup() {
|
|
320
|
+
this.telInput.classList.add("iti__tel-input");
|
|
321
|
+
// if autocomplete does not exist on the element and its form, then
|
|
322
|
+
// prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
|
|
323
|
+
// easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
|
|
324
|
+
// autocompleted number, which on submit could mean wrong number is saved
|
|
325
|
+
if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
|
|
326
|
+
this.telInput.setAttribute("autocomplete", "off");
|
|
327
|
+
}
|
|
328
|
+
const {
|
|
329
|
+
allowDropdown,
|
|
330
|
+
showSelectedDialCode,
|
|
331
|
+
showFlags,
|
|
332
|
+
containerClass,
|
|
333
|
+
hiddenInput,
|
|
334
|
+
dropdownContainer,
|
|
335
|
+
fixDropdownWidth,
|
|
336
|
+
useFullscreenPopup,
|
|
337
|
+
countrySearch,
|
|
338
|
+
i18n
|
|
339
|
+
} = this.options;
|
|
340
|
+
// containers (mostly for positioning)
|
|
341
|
+
let parentClass = "iti";
|
|
342
|
+
if (allowDropdown) {
|
|
343
|
+
parentClass += " iti--allow-dropdown";
|
|
344
|
+
}
|
|
345
|
+
if (showSelectedDialCode) {
|
|
346
|
+
parentClass += " iti--show-selected-dial-code";
|
|
347
|
+
}
|
|
348
|
+
if (showFlags) {
|
|
349
|
+
parentClass += " iti--show-flags";
|
|
350
|
+
}
|
|
351
|
+
if (containerClass) {
|
|
352
|
+
parentClass += ` ${containerClass}`;
|
|
353
|
+
}
|
|
354
|
+
if (!useFullscreenPopup) {
|
|
355
|
+
parentClass += " iti--inline-dropdown";
|
|
356
|
+
}
|
|
357
|
+
const wrapper = this._createEl("div", {
|
|
358
|
+
class: parentClass
|
|
359
|
+
});
|
|
360
|
+
this.telInput.parentNode.insertBefore(wrapper, this.telInput);
|
|
361
|
+
// if we're showing flags or dial codes, we need the flags container etc
|
|
362
|
+
if (showFlags || showSelectedDialCode) {
|
|
363
|
+
this.flagsContainer = this._createEl("div", {
|
|
364
|
+
class: "iti__flag-container"
|
|
365
|
+
}, wrapper);
|
|
366
|
+
// selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
|
|
367
|
+
// when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
|
|
368
|
+
// https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
|
|
369
|
+
this.selectedFlag = this._createEl("div", {
|
|
370
|
+
class: "iti__selected-flag",
|
|
371
|
+
...allowDropdown && {
|
|
499
372
|
role: "button",
|
|
500
373
|
"aria-expanded": "false",
|
|
501
374
|
"aria-label": this.options.i18n.selectedCountryAriaLabel || "Selected country",
|
|
502
375
|
"aria-haspopup": countrySearch ? "true" : "listbox",
|
|
503
|
-
"aria-controls": countrySearch ?
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
376
|
+
"aria-controls": countrySearch ? `iti-${this.id}__dropdown-content` : `iti-${this.id}__country-listbox`,
|
|
377
|
+
...countrySearch || {
|
|
378
|
+
role: "combobox"
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}, this.flagsContainer);
|
|
382
|
+
// we now include the selected flag element even when showFlags is disabled,
|
|
383
|
+
// as need to show globe icon for showSelectedDialCode empty state
|
|
384
|
+
this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
|
|
385
|
+
this.selectedFlagA11yText = this._createEl("span", {
|
|
386
|
+
class: "iti__a11y-text"
|
|
387
|
+
}, this.selectedFlagInner);
|
|
388
|
+
}
|
|
389
|
+
wrapper.appendChild(this.telInput);
|
|
390
|
+
if (this.selectedFlag && this.telInput.disabled) {
|
|
391
|
+
this.selectedFlag.setAttribute("aria-disabled", "true");
|
|
392
|
+
}
|
|
393
|
+
if (showSelectedDialCode) {
|
|
394
|
+
this.selectedDialCode = this._createEl("div", {
|
|
395
|
+
class: "iti__selected-dial-code"
|
|
396
|
+
}, this.selectedFlag);
|
|
397
|
+
}
|
|
398
|
+
if (allowDropdown) {
|
|
399
|
+
if (!this.telInput.disabled) {
|
|
400
|
+
// make element focusable and tab navigable
|
|
401
|
+
this.selectedFlag.setAttribute("tabindex", "0");
|
|
402
|
+
}
|
|
403
|
+
this.dropdownArrow = this._createEl("div", {
|
|
404
|
+
class: "iti__arrow",
|
|
405
|
+
"aria-hidden": "true"
|
|
406
|
+
}, this.selectedFlag);
|
|
407
|
+
const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
|
|
408
|
+
this.dropdownContent = this._createEl("div", {
|
|
409
|
+
id: `iti-${this.id}__dropdown-content`,
|
|
410
|
+
class: `iti__dropdown-content iti__hide ${extraClasses}`
|
|
411
|
+
});
|
|
412
|
+
if (countrySearch) {
|
|
413
|
+
this.searchInput = this._createEl("input", {
|
|
414
|
+
type: "text",
|
|
415
|
+
class: "iti__search-input",
|
|
416
|
+
placeholder: i18n.searchPlaceholder || "Search",
|
|
417
|
+
role: "combobox",
|
|
418
|
+
"aria-expanded": "true",
|
|
419
|
+
"aria-label": i18n.searchPlaceholder || "Search",
|
|
420
|
+
"aria-controls": `iti-${this.id}__country-listbox`,
|
|
421
|
+
"aria-autocomplete": "list",
|
|
422
|
+
autocomplete: "off"
|
|
423
|
+
}, this.dropdownContent);
|
|
424
|
+
this.searchResultsA11yText = this._createEl("span", {
|
|
425
|
+
class: "iti__a11y-text"
|
|
426
|
+
}, this.dropdownContent);
|
|
513
427
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
428
|
+
// country list: preferred countries, then divider, then all countries
|
|
429
|
+
this.countryList = this._createEl("ul", {
|
|
430
|
+
class: "iti__country-list",
|
|
431
|
+
id: `iti-${this.id}__country-listbox`,
|
|
432
|
+
role: "listbox",
|
|
433
|
+
"aria-label": i18n.countryListAriaLabel || "List of countries"
|
|
434
|
+
}, this.dropdownContent);
|
|
435
|
+
if (this.preferredCountries.length && !countrySearch) {
|
|
436
|
+
this._appendListItems(this.preferredCountries, "iti__preferred", true);
|
|
437
|
+
this._createEl("li", {
|
|
438
|
+
class: "iti__divider",
|
|
439
|
+
"aria-hidden": "true"
|
|
440
|
+
}, this.countryList);
|
|
517
441
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}, this.selectedFlag);
|
|
442
|
+
this._appendListItems(this.countries, "iti__standard");
|
|
443
|
+
if (countrySearch) {
|
|
444
|
+
this._updateSearchResultsText();
|
|
522
445
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
this.dropdownArrow = this._createEl("div", {
|
|
529
|
-
"class": "iti__arrow",
|
|
530
|
-
"aria-hidden": "true"
|
|
531
|
-
}, this.selectedFlag);
|
|
532
|
-
var extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
|
|
533
|
-
this.dropdownContent = this._createEl("div", {
|
|
534
|
-
id: "iti-".concat(this.id, "__dropdown-content"),
|
|
535
|
-
"class": "iti__dropdown-content iti__hide ".concat(extraClasses)
|
|
536
|
-
});
|
|
537
|
-
if (countrySearch) {
|
|
538
|
-
this.searchInput = this._createEl("input", {
|
|
539
|
-
type: "text",
|
|
540
|
-
"class": "iti__search-input",
|
|
541
|
-
placeholder: i18n.searchPlaceholder || "Search",
|
|
542
|
-
role: "combobox",
|
|
543
|
-
"aria-expanded": "true",
|
|
544
|
-
"aria-label": i18n.searchPlaceholder || "Search",
|
|
545
|
-
"aria-controls": "iti-".concat(this.id, "__country-listbox"),
|
|
546
|
-
"aria-autocomplete": "list",
|
|
547
|
-
autocomplete: "off"
|
|
548
|
-
}, this.dropdownContent);
|
|
549
|
-
this.searchResultsA11yText = this._createEl("span", {
|
|
550
|
-
"class": "iti__a11y-text"
|
|
551
|
-
}, this.dropdownContent);
|
|
552
|
-
}
|
|
553
|
-
// country list: preferred countries, then divider, then all countries
|
|
554
|
-
this.countryList = this._createEl("ul", {
|
|
555
|
-
"class": "iti__country-list",
|
|
556
|
-
id: "iti-".concat(this.id, "__country-listbox"),
|
|
557
|
-
role: "listbox",
|
|
558
|
-
"aria-label": i18n.countryListAriaLabel || "List of countries"
|
|
559
|
-
}, this.dropdownContent);
|
|
560
|
-
if (this.preferredCountries.length && !countrySearch) {
|
|
561
|
-
this._appendListItems(this.preferredCountries, "iti__preferred", true);
|
|
562
|
-
this._createEl("li", {
|
|
563
|
-
"class": "iti__divider",
|
|
564
|
-
"aria-hidden": "true"
|
|
565
|
-
}, this.countryList);
|
|
566
|
-
}
|
|
567
|
-
this._appendListItems(this.countries, "iti__standard");
|
|
568
|
-
if (countrySearch) {
|
|
569
|
-
this._updateSearchResultsText();
|
|
570
|
-
}
|
|
571
|
-
// create dropdownContainer markup
|
|
572
|
-
if (dropdownContainer) {
|
|
573
|
-
var dropdownClasses = "iti iti--container";
|
|
574
|
-
if (useFullscreenPopup) {
|
|
575
|
-
dropdownClasses += " iti--fullscreen-popup";
|
|
576
|
-
} else {
|
|
577
|
-
dropdownClasses += " iti--inline-dropdown";
|
|
578
|
-
}
|
|
579
|
-
if (countrySearch) {
|
|
580
|
-
dropdownClasses += " iti--country-search";
|
|
581
|
-
}
|
|
582
|
-
this.dropdown = this._createEl("div", {
|
|
583
|
-
"class": dropdownClasses
|
|
584
|
-
});
|
|
585
|
-
this.dropdown.appendChild(this.dropdownContent);
|
|
446
|
+
// create dropdownContainer markup
|
|
447
|
+
if (dropdownContainer) {
|
|
448
|
+
let dropdownClasses = "iti iti--container";
|
|
449
|
+
if (useFullscreenPopup) {
|
|
450
|
+
dropdownClasses += " iti--fullscreen-popup";
|
|
586
451
|
} else {
|
|
587
|
-
|
|
452
|
+
dropdownClasses += " iti--inline-dropdown";
|
|
588
453
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
var telInputName = this.telInput.getAttribute("name");
|
|
592
|
-
var names = hiddenInput(telInputName);
|
|
593
|
-
if (names.phone) {
|
|
594
|
-
// Create hidden input for the full international number
|
|
595
|
-
this.hiddenInput = this._createEl("input", {
|
|
596
|
-
type: "hidden",
|
|
597
|
-
name: names.phone
|
|
598
|
-
});
|
|
599
|
-
wrapper.appendChild(this.hiddenInput);
|
|
600
|
-
}
|
|
601
|
-
if (names.country) {
|
|
602
|
-
// Create hidden input for the selected country iso2 code
|
|
603
|
-
this.hiddenInputCountry = this._createEl("input", {
|
|
604
|
-
type: "hidden",
|
|
605
|
-
name: names.country
|
|
606
|
-
});
|
|
607
|
-
wrapper.appendChild(this.hiddenInputCountry);
|
|
454
|
+
if (countrySearch) {
|
|
455
|
+
dropdownClasses += " iti--country-search";
|
|
608
456
|
}
|
|
457
|
+
this.dropdown = this._createEl("div", {
|
|
458
|
+
class: dropdownClasses
|
|
459
|
+
});
|
|
460
|
+
this.dropdown.appendChild(this.dropdownContent);
|
|
461
|
+
} else {
|
|
462
|
+
this.flagsContainer.appendChild(this.dropdownContent);
|
|
609
463
|
}
|
|
610
464
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
role: "option",
|
|
622
|
-
"data-dial-code": c.dialCode,
|
|
623
|
-
"data-country-code": c.iso2,
|
|
624
|
-
"aria-selected": "false"
|
|
625
|
-
}, this.countryList);
|
|
626
|
-
// store this for later use e.g. country search filtering
|
|
627
|
-
c.nodeById[this.id] = listItem;
|
|
628
|
-
var content = "";
|
|
629
|
-
// add the flag
|
|
630
|
-
if (this.options.showFlags) {
|
|
631
|
-
content += "<div class='iti__flag-box'><div class='iti__flag iti__".concat(c.iso2, "'></div></div>");
|
|
632
|
-
}
|
|
633
|
-
// and the country name and dial code
|
|
634
|
-
content += "<span class='iti__country-name'>".concat(c.name, "</span>");
|
|
635
|
-
content += "<span class='iti__dial-code'>+".concat(c.dialCode, "</span>");
|
|
636
|
-
listItem.insertAdjacentHTML("beforeend", content);
|
|
465
|
+
if (hiddenInput) {
|
|
466
|
+
const telInputName = this.telInput.getAttribute("name");
|
|
467
|
+
const names = hiddenInput(telInputName);
|
|
468
|
+
if (names.phone) {
|
|
469
|
+
// Create hidden input for the full international number
|
|
470
|
+
this.hiddenInput = this._createEl("input", {
|
|
471
|
+
type: "hidden",
|
|
472
|
+
name: names.phone
|
|
473
|
+
});
|
|
474
|
+
wrapper.appendChild(this.hiddenInput);
|
|
637
475
|
}
|
|
476
|
+
if (names.country) {
|
|
477
|
+
// Create hidden input for the selected country iso2 code
|
|
478
|
+
this.hiddenInputCountry = this._createEl("input", {
|
|
479
|
+
type: "hidden",
|
|
480
|
+
name: names.country
|
|
481
|
+
});
|
|
482
|
+
wrapper.appendChild(this.hiddenInputCountry);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
// for each of the passed countries: add a country <li> to the countryList <ul> container
|
|
487
|
+
_appendListItems(countries, className, preferred) {
|
|
488
|
+
for (let i = 0; i < countries.length; i++) {
|
|
489
|
+
const c = countries[i];
|
|
490
|
+
const idSuffix = preferred ? "-preferred" : "";
|
|
491
|
+
const listItem = this._createEl("li", {
|
|
492
|
+
id: `iti-${this.id}__item-${c.iso2}${idSuffix}`,
|
|
493
|
+
class: `iti__country ${className}`,
|
|
494
|
+
tabindex: "-1",
|
|
495
|
+
role: "option",
|
|
496
|
+
"data-dial-code": c.dialCode,
|
|
497
|
+
"data-country-code": c.iso2,
|
|
498
|
+
"aria-selected": "false"
|
|
499
|
+
}, this.countryList);
|
|
500
|
+
// store this for later use e.g. country search filtering
|
|
501
|
+
c.nodeById[this.id] = listItem;
|
|
502
|
+
let content = "";
|
|
503
|
+
// add the flag
|
|
504
|
+
if (this.options.showFlags) {
|
|
505
|
+
content += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
|
|
506
|
+
}
|
|
507
|
+
// and the country name and dial code
|
|
508
|
+
content += `<span class='iti__country-name'>${c.name}</span>`;
|
|
509
|
+
content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
|
|
510
|
+
listItem.insertAdjacentHTML("beforeend", content);
|
|
638
511
|
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
512
|
+
}
|
|
513
|
+
// set the initial state of the input value and the selected flag by:
|
|
514
|
+
// 1. extracting a dial code from the given number
|
|
515
|
+
// 2. using explicit initialCountry
|
|
516
|
+
// 3. picking the first preferred country
|
|
517
|
+
// 4. picking the first country
|
|
518
|
+
_setInitialState(overrideAutoCountry = false) {
|
|
519
|
+
// fix firefox bug: when first load page (with input with value set to number with intl dial
|
|
520
|
+
// code) and initialising plugin removes the dial code from the input, then refresh page,
|
|
521
|
+
// and we try to init plugin again but this time on number without dial code so get grey flag
|
|
522
|
+
const attributeValue = this.telInput.getAttribute("value");
|
|
523
|
+
const inputValue = this.telInput.value;
|
|
524
|
+
const useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
|
|
525
|
+
const val = useAttribute ? attributeValue : inputValue;
|
|
526
|
+
const dialCode = this._getDialCode(val);
|
|
527
|
+
const isRegionlessNanp = this._isRegionlessNanp(val);
|
|
528
|
+
const {
|
|
529
|
+
initialCountry
|
|
530
|
+
} = this.options;
|
|
531
|
+
// if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
|
|
532
|
+
// flag, else fall back to the default country
|
|
533
|
+
if (dialCode && !isRegionlessNanp) {
|
|
534
|
+
this._updateFlagFromNumber(val);
|
|
535
|
+
} else if (initialCountry !== "auto" || overrideAutoCountry) {
|
|
536
|
+
const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
|
|
537
|
+
const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
|
|
538
|
+
// see if we should select a flag
|
|
539
|
+
if (isValidInitialCountry) {
|
|
540
|
+
this._setCountry(lowerInitialCountry);
|
|
541
|
+
} else {
|
|
542
|
+
if (dialCode && isRegionlessNanp) {
|
|
543
|
+
// has intl dial code, is regionless nanp, and no initialCountry, so default to US
|
|
544
|
+
this._setCountry("us");
|
|
663
545
|
} else {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
this._setCountry("us");
|
|
667
|
-
} else {
|
|
668
|
-
// display the empty state (globe icon)
|
|
669
|
-
this._setCountry();
|
|
670
|
-
}
|
|
546
|
+
// display the empty state (globe icon)
|
|
547
|
+
this._setCountry();
|
|
671
548
|
}
|
|
672
549
|
}
|
|
673
|
-
// NOTE: if initialCountry is set to auto, that will be handled separately
|
|
674
|
-
// format - note this wont be run after _updateDialCode as that's only called if no val
|
|
675
|
-
if (val) {
|
|
676
|
-
this._updateValFromNumber(val);
|
|
677
|
-
}
|
|
678
550
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
this.
|
|
683
|
-
if (this.options.allowDropdown) {
|
|
684
|
-
this._initDropdownListeners();
|
|
685
|
-
}
|
|
686
|
-
if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
|
|
687
|
-
this._initHiddenInputListener();
|
|
688
|
-
}
|
|
551
|
+
// NOTE: if initialCountry is set to auto, that will be handled separately
|
|
552
|
+
// format - note this wont be run after _updateDialCode as that's only called if no val
|
|
553
|
+
if (val) {
|
|
554
|
+
this._updateValFromNumber(val);
|
|
689
555
|
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
value: function _initDropdownListeners() {
|
|
707
|
-
var _this3 = this;
|
|
708
|
-
// hack for input nested inside label (which is valid markup): clicking the selected-flag to
|
|
709
|
-
// open the dropdown would then automatically trigger a 2nd click on the input which would
|
|
710
|
-
// close it again
|
|
711
|
-
this._handleLabelClick = function(e) {
|
|
712
|
-
// if the dropdown is closed, then focus the input, else ignore the click
|
|
713
|
-
if (_this3.dropdownContent.classList.contains("iti__hide")) {
|
|
714
|
-
_this3.telInput.focus();
|
|
715
|
-
} else {
|
|
716
|
-
e.preventDefault();
|
|
717
|
-
}
|
|
718
|
-
};
|
|
719
|
-
var label = this.telInput.closest("label");
|
|
720
|
-
if (label) {
|
|
721
|
-
label.addEventListener("click", this._handleLabelClick);
|
|
556
|
+
}
|
|
557
|
+
// initialise the main event listeners: input keyup, and click selected flag
|
|
558
|
+
_initListeners() {
|
|
559
|
+
this._initTelInputListeners();
|
|
560
|
+
if (this.options.allowDropdown) {
|
|
561
|
+
this._initDropdownListeners();
|
|
562
|
+
}
|
|
563
|
+
if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
|
|
564
|
+
this._initHiddenInputListener();
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
// update hidden input on form submit
|
|
568
|
+
_initHiddenInputListener() {
|
|
569
|
+
this._handleHiddenInputSubmit = () => {
|
|
570
|
+
if (this.hiddenInput) {
|
|
571
|
+
this.hiddenInput.value = this.getNumber();
|
|
722
572
|
}
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
// only intercept this event if we're opening the dropdown
|
|
726
|
-
// else let it bubble up to the top ("click-off-to-close" listener)
|
|
727
|
-
// we cannot just stopPropagation as it may be needed to close another instance
|
|
728
|
-
if (_this3.dropdownContent.classList.contains("iti__hide") && !_this3.telInput.disabled && !_this3.telInput.readOnly) {
|
|
729
|
-
_this3._openDropdown();
|
|
730
|
-
}
|
|
731
|
-
};
|
|
732
|
-
this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
|
|
733
|
-
// open dropdown if selected flag is focused and they press up/down/space/enter
|
|
734
|
-
this._handleFlagsContainerKeydown = function(e) {
|
|
735
|
-
var isDropdownHidden = _this3.dropdownContent.classList.contains("iti__hide");
|
|
736
|
-
if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
|
|
737
|
-
// prevent form from being submitted if "ENTER" was pressed
|
|
738
|
-
e.preventDefault();
|
|
739
|
-
// prevent event from being handled again by document
|
|
740
|
-
e.stopPropagation();
|
|
741
|
-
_this3._openDropdown();
|
|
742
|
-
}
|
|
743
|
-
// allow navigation from dropdown to input on TAB
|
|
744
|
-
if (e.key === "Tab") {
|
|
745
|
-
_this3._closeDropdown();
|
|
746
|
-
}
|
|
747
|
-
};
|
|
748
|
-
this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
|
|
749
|
-
}
|
|
750
|
-
}, {
|
|
751
|
-
key: "_initRequests",
|
|
752
|
-
value: function _initRequests() {
|
|
753
|
-
var _this4 = this;
|
|
754
|
-
// if the user has specified the path to the utils script, fetch it on window.load, else resolve
|
|
755
|
-
if (this.options.utilsScript && !window.intlTelInputUtils) {
|
|
756
|
-
// if the plugin is being initialised after the window.load event has already been fired
|
|
757
|
-
if (window.intlTelInputGlobals.documentReady()) {
|
|
758
|
-
window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
|
|
759
|
-
} else {
|
|
760
|
-
// wait until the load event so we don't block any other requests e.g. the flags image
|
|
761
|
-
window.addEventListener("load", function() {
|
|
762
|
-
window.intlTelInputGlobals.loadUtils(_this4.options.utilsScript);
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
|
-
} else {
|
|
766
|
-
this.resolveUtilsScriptPromise();
|
|
573
|
+
if (this.hiddenInputCountry) {
|
|
574
|
+
this.hiddenInputCountry.value = this.getSelectedCountryData().iso2;
|
|
767
575
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
576
|
+
};
|
|
577
|
+
this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
|
|
578
|
+
}
|
|
579
|
+
// initialise the dropdown listeners
|
|
580
|
+
_initDropdownListeners() {
|
|
581
|
+
// hack for input nested inside label (which is valid markup): clicking the selected-flag to
|
|
582
|
+
// open the dropdown would then automatically trigger a 2nd click on the input which would
|
|
583
|
+
// close it again
|
|
584
|
+
this._handleLabelClick = e => {
|
|
585
|
+
// if the dropdown is closed, then focus the input, else ignore the click
|
|
586
|
+
if (this.dropdownContent.classList.contains("iti__hide")) {
|
|
587
|
+
this.telInput.focus();
|
|
771
588
|
} else {
|
|
772
|
-
|
|
589
|
+
e.preventDefault();
|
|
773
590
|
}
|
|
591
|
+
};
|
|
592
|
+
const label = this.telInput.closest("label");
|
|
593
|
+
if (label) {
|
|
594
|
+
label.addEventListener("click", this._handleLabelClick);
|
|
774
595
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
//
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
// away (e.g. if they have already done the geo ip lookup somewhere else). Using
|
|
799
|
-
// setTimeout means that the current thread of execution will finish before executing
|
|
800
|
-
// this, which allows the plugin to finish initialising.
|
|
801
|
-
setTimeout(function() {
|
|
802
|
-
return forEachInstance("handleAutoCountry");
|
|
803
|
-
});
|
|
804
|
-
} else {
|
|
805
|
-
_this5._setInitialState(true);
|
|
806
|
-
forEachInstance("rejectAutoCountryPromise");
|
|
807
|
-
}
|
|
808
|
-
}, function() {
|
|
809
|
-
return forEachInstance("rejectAutoCountryPromise");
|
|
810
|
-
});
|
|
811
|
-
}
|
|
596
|
+
// toggle country dropdown on click
|
|
597
|
+
this._handleClickSelectedFlag = () => {
|
|
598
|
+
// only intercept this event if we're opening the dropdown
|
|
599
|
+
// else let it bubble up to the top ("click-off-to-close" listener)
|
|
600
|
+
// we cannot just stopPropagation as it may be needed to close another instance
|
|
601
|
+
if (this.dropdownContent.classList.contains("iti__hide") && !this.telInput.disabled && !this.telInput.readOnly) {
|
|
602
|
+
this._openDropdown();
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
|
|
606
|
+
// open dropdown if selected flag is focused and they press up/down/space/enter
|
|
607
|
+
this._handleFlagsContainerKeydown = e => {
|
|
608
|
+
const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
|
|
609
|
+
if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
|
|
610
|
+
// prevent form from being submitted if "ENTER" was pressed
|
|
611
|
+
e.preventDefault();
|
|
612
|
+
// prevent event from being handled again by document
|
|
613
|
+
e.stopPropagation();
|
|
614
|
+
this._openDropdown();
|
|
615
|
+
}
|
|
616
|
+
// allow navigation from dropdown to input on TAB
|
|
617
|
+
if (e.key === "Tab") {
|
|
618
|
+
this._closeDropdown();
|
|
812
619
|
}
|
|
620
|
+
};
|
|
621
|
+
this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
|
|
622
|
+
}
|
|
623
|
+
// init many requests: utils script / geo ip lookup
|
|
624
|
+
_initRequests() {
|
|
625
|
+
// if the user has specified the path to the utils script, fetch it on window.load, else resolve
|
|
626
|
+
if (this.options.utilsScript && !window.intlTelInputUtils) {
|
|
627
|
+
// if the plugin is being initialised after the window.load event has already been fired
|
|
628
|
+
if (window.intlTelInputGlobals.documentReady()) {
|
|
629
|
+
window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
|
|
630
|
+
} else {
|
|
631
|
+
// wait until the load event so we don't block any other requests e.g. the flags image
|
|
632
|
+
window.addEventListener("load", () => {
|
|
633
|
+
window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
} else {
|
|
637
|
+
this.resolveUtilsScriptPromise();
|
|
813
638
|
}
|
|
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
|
-
// whereas "keyup" fires even for shift key, arrow key presses etc
|
|
849
|
-
this.telInput.addEventListener("input", this._handleInputEvent);
|
|
850
|
-
if (strictMode) {
|
|
851
|
-
this._handleKeydownEvent = function(e) {
|
|
852
|
-
// only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
|
|
853
|
-
if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
|
|
854
|
-
var isInitialPlus = _this6.telInput.selectionStart === 0 && e.key === "+";
|
|
855
|
-
var isNumeric = /^[0-9]$/.test(e.key);
|
|
856
|
-
var isAllowedChar = isInitialPlus || isNumeric;
|
|
857
|
-
var fullNumber = _this6._getFullNumber();
|
|
858
|
-
var coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, _this6.selectedCountryData.iso2);
|
|
859
|
-
var hasReachedMaxLength = _this6.maxCoreNumberLength && coreNumber.length >= _this6.maxCoreNumberLength;
|
|
860
|
-
if (!isAllowedChar || hasReachedMaxLength) {
|
|
861
|
-
e.preventDefault();
|
|
862
|
-
}
|
|
639
|
+
// dont bother with IP lookup if we already have a selected country
|
|
640
|
+
if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
|
|
641
|
+
this._loadAutoCountry();
|
|
642
|
+
} else {
|
|
643
|
+
this.resolveAutoCountryPromise();
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
// perform the geo ip lookup
|
|
647
|
+
_loadAutoCountry() {
|
|
648
|
+
// 3 options:
|
|
649
|
+
// 1) already loaded (we're done)
|
|
650
|
+
// 2) not already started loading (start)
|
|
651
|
+
// 3) already started loading (do nothing - just wait for loading callback to fire)
|
|
652
|
+
if (window.intlTelInputGlobals.autoCountry) {
|
|
653
|
+
this.handleAutoCountry();
|
|
654
|
+
} else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
|
|
655
|
+
// don't do this twice!
|
|
656
|
+
window.intlTelInputGlobals.startedLoadingAutoCountry = true;
|
|
657
|
+
if (typeof this.options.geoIpLookup === "function") {
|
|
658
|
+
this.options.geoIpLookup((iso2 = "") => {
|
|
659
|
+
const iso2Lower = iso2.toLowerCase();
|
|
660
|
+
const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
|
|
661
|
+
if (isValidIso2) {
|
|
662
|
+
window.intlTelInputGlobals.autoCountry = iso2Lower;
|
|
663
|
+
// tell all instances the auto country is ready
|
|
664
|
+
// TODO: this should just be the current instances
|
|
665
|
+
// UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
|
|
666
|
+
// away (e.g. if they have already done the geo ip lookup somewhere else). Using
|
|
667
|
+
// setTimeout means that the current thread of execution will finish before executing
|
|
668
|
+
// this, which allows the plugin to finish initialising.
|
|
669
|
+
setTimeout(() => forEachInstance("handleAutoCountry"));
|
|
670
|
+
} else {
|
|
671
|
+
this._setInitialState(true);
|
|
672
|
+
forEachInstance("rejectAutoCountryPromise");
|
|
863
673
|
}
|
|
864
|
-
};
|
|
865
|
-
this.telInput.addEventListener("keydown", this._handleKeydownEvent);
|
|
674
|
+
}, () => forEachInstance("rejectAutoCountryPromise"));
|
|
866
675
|
}
|
|
867
676
|
}
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
}
|
|
881
|
-
// normal case: stop when you hit the right number of relevant chars
|
|
882
|
-
// (cursor will be just after the final relevant char)
|
|
883
|
-
if (count === relevantChars && !isDeleteForwards) {
|
|
884
|
-
return i + 1;
|
|
885
|
-
}
|
|
886
|
-
// spacial case: delete forwards (fn + delete on a mac):
|
|
887
|
-
// wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
|
|
888
|
-
if (isDeleteForwards && count === relevantChars + 1) {
|
|
889
|
-
return i;
|
|
890
|
-
}
|
|
677
|
+
}
|
|
678
|
+
// initialize the tel input listeners
|
|
679
|
+
_initTelInputListeners() {
|
|
680
|
+
const {
|
|
681
|
+
strictMode,
|
|
682
|
+
formatAsYouType
|
|
683
|
+
} = this.options;
|
|
684
|
+
let userOverrideFormatting = false;
|
|
685
|
+
// update flag on input event
|
|
686
|
+
this._handleInputEvent = e => {
|
|
687
|
+
if (this._updateFlagFromNumber(this.telInput.value)) {
|
|
688
|
+
this._triggerCountryChange();
|
|
891
689
|
}
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
if (
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
690
|
+
// if user types their own formatting char (not a plus or a numeric), or they paste something, then set the override
|
|
691
|
+
const isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
|
|
692
|
+
const isPaste = e && e.inputType === "insertFromPaste" && this.telInput.value;
|
|
693
|
+
if (isFormattingChar || isPaste && !strictMode) {
|
|
694
|
+
userOverrideFormatting = true;
|
|
695
|
+
}
|
|
696
|
+
// if user removes all formatting chars, then reset the override
|
|
697
|
+
else if (!/[^+0-9]/.test(this.telInput.value)) {
|
|
698
|
+
userOverrideFormatting = false;
|
|
699
|
+
}
|
|
700
|
+
// handle FAYT, unless userOverrideFormatting
|
|
701
|
+
if (formatAsYouType && !userOverrideFormatting) {
|
|
702
|
+
// maintain caret position after reformatting
|
|
703
|
+
const currentCaretPos = this.telInput.selectionStart;
|
|
704
|
+
const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
|
|
705
|
+
const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
|
|
706
|
+
const isDeleteForwards = e && e.inputType === "deleteContentForward";
|
|
707
|
+
const formattedValue = this._formatNumberAsYouType();
|
|
708
|
+
const newCaretPos = this._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
|
|
709
|
+
this.telInput.value = formattedValue;
|
|
710
|
+
this.telInput.setSelectionRange(newCaretPos, newCaretPos);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
// this handles individual key presses as well as cut/paste events
|
|
714
|
+
// the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
|
|
715
|
+
// whereas "keyup" fires even for shift key, arrow key presses etc
|
|
716
|
+
this.telInput.addEventListener("input", this._handleInputEvent);
|
|
717
|
+
if (strictMode) {
|
|
718
|
+
this._handleKeydownEvent = e => {
|
|
719
|
+
// only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
|
|
720
|
+
if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
|
|
721
|
+
const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
|
|
722
|
+
const isNumeric = /^[0-9]$/.test(e.key);
|
|
723
|
+
const isAllowedChar = isInitialPlus || isNumeric;
|
|
724
|
+
const fullNumber = this._getFullNumber();
|
|
725
|
+
const coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
|
|
726
|
+
const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
|
|
727
|
+
if (!isAllowedChar || hasReachedMaxLength) {
|
|
728
|
+
e.preventDefault();
|
|
729
|
+
}
|
|
908
730
|
}
|
|
909
|
-
}
|
|
731
|
+
};
|
|
732
|
+
this.telInput.addEventListener("keydown", this._handleKeydownEvent);
|
|
910
733
|
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
this.telInput.dispatchEvent(e);
|
|
924
|
-
}
|
|
925
|
-
}, {
|
|
926
|
-
key: "_openDropdown",
|
|
927
|
-
value: function _openDropdown() {
|
|
928
|
-
var _this$options3 = this.options, fixDropdownWidth = _this$options3.fixDropdownWidth, countrySearch = _this$options3.countrySearch;
|
|
929
|
-
if (fixDropdownWidth) {
|
|
930
|
-
this.dropdownContent.style.width = "".concat(this.telInput.offsetWidth, "px");
|
|
734
|
+
}
|
|
735
|
+
// iterate through the formattedValue until hit the right number of relevant chars
|
|
736
|
+
_translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
|
|
737
|
+
// if the first char is a formatting char, and they backspace delete it:
|
|
738
|
+
// cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
|
|
739
|
+
if (prevCaretPos === 0 && !isDeleteForwards) {
|
|
740
|
+
return 0;
|
|
741
|
+
}
|
|
742
|
+
let count = 0;
|
|
743
|
+
for (let i = 0; i < formattedValue.length; i++) {
|
|
744
|
+
if (/[+0-9]/.test(formattedValue[i])) {
|
|
745
|
+
count++;
|
|
931
746
|
}
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
// else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
|
|
937
|
-
if (this.activeItem && !countrySearch) {
|
|
938
|
-
// update highlighting and scroll to active list item
|
|
939
|
-
this._highlightListItem(this.activeItem, false);
|
|
940
|
-
this._scrollTo(this.activeItem, true);
|
|
941
|
-
} else {
|
|
942
|
-
// start by highlighting the first item in the list
|
|
943
|
-
var firstElementChild = this.countryList.firstElementChild;
|
|
944
|
-
if (firstElementChild) {
|
|
945
|
-
this._highlightListItem(firstElementChild, false);
|
|
946
|
-
this.countryList.scrollTop = 0;
|
|
947
|
-
}
|
|
948
|
-
if (countrySearch) {
|
|
949
|
-
this.searchInput.focus();
|
|
950
|
-
}
|
|
747
|
+
// normal case: stop when you hit the right number of relevant chars
|
|
748
|
+
// (cursor will be just after the final relevant char)
|
|
749
|
+
if (count === relevantChars && !isDeleteForwards) {
|
|
750
|
+
return i + 1;
|
|
951
751
|
}
|
|
952
|
-
//
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
this._trigger("open:countrydropdown");
|
|
957
|
-
}
|
|
958
|
-
}, {
|
|
959
|
-
key: "_toggleClass",
|
|
960
|
-
value: function _toggleClass(el, className, shouldHaveClass) {
|
|
961
|
-
if (shouldHaveClass && !el.classList.contains(className)) {
|
|
962
|
-
el.classList.add(className);
|
|
963
|
-
} else if (!shouldHaveClass && el.classList.contains(className)) {
|
|
964
|
-
el.classList.remove(className);
|
|
752
|
+
// spacial case: delete forwards (fn + delete on a mac):
|
|
753
|
+
// wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
|
|
754
|
+
if (isDeleteForwards && count === relevantChars + 1) {
|
|
755
|
+
return i;
|
|
965
756
|
}
|
|
966
757
|
}
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
758
|
+
return formattedValue.length;
|
|
759
|
+
}
|
|
760
|
+
// adhere to the input's maxlength attr
|
|
761
|
+
_cap(number) {
|
|
762
|
+
const max = this.telInput.getAttribute("maxlength");
|
|
763
|
+
return max && number.length > max ? number.substr(0, max) : number;
|
|
764
|
+
}
|
|
765
|
+
// clear the input if it just contains a dial code
|
|
766
|
+
_removeEmptyDialCode() {
|
|
767
|
+
if (this.telInput.value.charAt(0) === "+") {
|
|
768
|
+
const numeric = this._getNumeric(this.telInput.value);
|
|
769
|
+
// if just a plus, or if just a dial code
|
|
770
|
+
if (!numeric || this.selectedCountryData.dialCode === numeric) {
|
|
771
|
+
this.telInput.value = "";
|
|
973
772
|
}
|
|
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
|
-
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
// extract the numeric digits from the given string
|
|
776
|
+
_getNumeric(s) {
|
|
777
|
+
return s.replace(/\D/g, "");
|
|
778
|
+
}
|
|
779
|
+
// trigger a custom event on the input
|
|
780
|
+
_trigger(name) {
|
|
781
|
+
const e = new Event(name, {
|
|
782
|
+
bubbles: true,
|
|
783
|
+
cancelable: true
|
|
784
|
+
});
|
|
785
|
+
this.telInput.dispatchEvent(e);
|
|
786
|
+
}
|
|
787
|
+
// open the dropdown
|
|
788
|
+
_openDropdown() {
|
|
789
|
+
const {
|
|
790
|
+
fixDropdownWidth,
|
|
791
|
+
countrySearch
|
|
792
|
+
} = this.options;
|
|
793
|
+
if (fixDropdownWidth) {
|
|
794
|
+
this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
|
|
795
|
+
}
|
|
796
|
+
this.dropdownContent.classList.remove("iti__hide");
|
|
797
|
+
this.selectedFlag.setAttribute("aria-expanded", "true");
|
|
798
|
+
this._setDropdownPosition();
|
|
799
|
+
// if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
|
|
800
|
+
// else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
|
|
801
|
+
if (this.activeItem && !countrySearch) {
|
|
802
|
+
// update highlighting and scroll to active list item
|
|
803
|
+
this._highlightListItem(this.activeItem, false);
|
|
804
|
+
this._scrollTo(this.activeItem, true);
|
|
805
|
+
} else {
|
|
806
|
+
// start by highlighting the first item in the list
|
|
807
|
+
const {
|
|
808
|
+
firstElementChild
|
|
809
|
+
} = this.countryList;
|
|
810
|
+
if (firstElementChild) {
|
|
811
|
+
this._highlightListItem(firstElementChild, false);
|
|
812
|
+
this.countryList.scrollTop = 0;
|
|
813
|
+
}
|
|
814
|
+
if (countrySearch) {
|
|
815
|
+
this.searchInput.focus();
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
// bind all the dropdown-related listeners: mouseover, click, click-off, keydown
|
|
819
|
+
this._bindDropdownListeners();
|
|
820
|
+
// update the arrow
|
|
821
|
+
this.dropdownArrow.classList.add("iti__arrow--up");
|
|
822
|
+
this._trigger("open:countrydropdown");
|
|
823
|
+
}
|
|
824
|
+
// make sure the el has the className or not, depending on the value of shouldHaveClass
|
|
825
|
+
_toggleClass(el, className, shouldHaveClass) {
|
|
826
|
+
if (shouldHaveClass && !el.classList.contains(className)) {
|
|
827
|
+
el.classList.add(className);
|
|
828
|
+
} else if (!shouldHaveClass && el.classList.contains(className)) {
|
|
829
|
+
el.classList.remove(className);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
// decide if should position dropdown above or below input (depends on position within viewport, and scroll)
|
|
833
|
+
_setDropdownPosition() {
|
|
834
|
+
if (this.options.dropdownContainer) {
|
|
835
|
+
this.options.dropdownContainer.appendChild(this.dropdown);
|
|
836
|
+
}
|
|
837
|
+
if (!this.options.useFullscreenPopup) {
|
|
838
|
+
const pos = this.telInput.getBoundingClientRect();
|
|
839
|
+
// windowTop from https://stackoverflow.com/a/14384091/217866
|
|
840
|
+
const windowTop = document.documentElement.scrollTop;
|
|
841
|
+
const inputTop = pos.top + windowTop;
|
|
842
|
+
const dropdownHeight = this.dropdownContent.offsetHeight;
|
|
843
|
+
// dropdownFitsBelow = (dropdownBottom < windowBottom)
|
|
844
|
+
const dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
|
|
845
|
+
const dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
|
|
846
|
+
// dont allow positioning above when country search enabled as the search box jumps around as you filter countries
|
|
847
|
+
const positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
|
|
848
|
+
// by default, the dropdown will be below the input. If we want to position it above the
|
|
849
|
+
// input, we add the dropup class.
|
|
850
|
+
this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
|
|
851
|
+
// if dropdownContainer is enabled, calculate postion
|
|
852
|
+
if (this.options.dropdownContainer) {
|
|
853
|
+
// if we want to position the dropdown below the input, we need to add the input height to the top value
|
|
854
|
+
const extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
|
|
855
|
+
// calculate placement
|
|
856
|
+
this.dropdown.style.top = `${inputTop + extraTop}px`;
|
|
857
|
+
this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
|
|
858
|
+
// close menu on window scroll
|
|
859
|
+
this._handleWindowScroll = () => this._closeDropdown();
|
|
860
|
+
window.addEventListener("scroll", this._handleWindowScroll);
|
|
1001
861
|
}
|
|
1002
862
|
}
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
this
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
863
|
+
}
|
|
864
|
+
// we only bind dropdown listeners when the dropdown is open
|
|
865
|
+
_bindDropdownListeners() {
|
|
866
|
+
// when mouse over a list item, just highlight that one
|
|
867
|
+
// we add the class "highlight", so if they hit "enter" we know which one to select
|
|
868
|
+
this._handleMouseoverCountryList = e => {
|
|
869
|
+
// handle event delegation, as we're listening for this event on the countryList
|
|
870
|
+
const listItem = e.target.closest(".iti__country");
|
|
871
|
+
if (listItem) {
|
|
872
|
+
this._highlightListItem(listItem, false);
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
|
|
876
|
+
// listen for country selection
|
|
877
|
+
this._handleClickCountryList = e => {
|
|
878
|
+
const listItem = e.target.closest(".iti__country");
|
|
879
|
+
if (listItem) {
|
|
880
|
+
this._selectListItem(listItem);
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
this.countryList.addEventListener("click", this._handleClickCountryList);
|
|
884
|
+
// click off to close
|
|
885
|
+
// (except when this initial opening click is bubbling up)
|
|
886
|
+
// we cannot just stopPropagation as it may be needed to close another instance
|
|
887
|
+
let isOpening = true;
|
|
888
|
+
this._handleClickOffToClose = () => {
|
|
889
|
+
if (!isOpening) {
|
|
890
|
+
this._closeDropdown();
|
|
891
|
+
}
|
|
892
|
+
isOpening = false;
|
|
893
|
+
};
|
|
894
|
+
document.documentElement.addEventListener("click", this._handleClickOffToClose);
|
|
895
|
+
// listen for up/down scrolling, enter to select, or escape to close
|
|
896
|
+
// use keydown as keypress doesn't fire for non-char keys and we want to catch if they
|
|
897
|
+
// just hit down and hold it to scroll down (no keyup event).
|
|
898
|
+
// listen on the document because that's where key events are triggered if no input has focus
|
|
899
|
+
let query = "";
|
|
900
|
+
let queryTimer = null;
|
|
901
|
+
this._handleKeydownOnDropdown = e => {
|
|
902
|
+
// prevent down key from scrolling the whole page,
|
|
903
|
+
// and enter key from submitting a form etc
|
|
904
|
+
if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
|
|
905
|
+
e.preventDefault();
|
|
906
|
+
e.stopPropagation();
|
|
907
|
+
// up and down to navigate
|
|
908
|
+
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
909
|
+
this._handleUpDownKey(e.key);
|
|
1014
910
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
this._handleClickCountryList = function(e) {
|
|
1019
|
-
var listItem = e.target.closest(".iti__country");
|
|
1020
|
-
if (listItem) {
|
|
1021
|
-
_this8._selectListItem(listItem);
|
|
911
|
+
// enter to select
|
|
912
|
+
else if (e.key === "Enter") {
|
|
913
|
+
this._handleEnterKey();
|
|
1022
914
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
// (except when this initial opening click is bubbling up)
|
|
1027
|
-
// we cannot just stopPropagation as it may be needed to close another instance
|
|
1028
|
-
var isOpening = true;
|
|
1029
|
-
this._handleClickOffToClose = function() {
|
|
1030
|
-
if (!isOpening) {
|
|
1031
|
-
_this8._closeDropdown();
|
|
915
|
+
// esc to close
|
|
916
|
+
else if (e.key === "Escape") {
|
|
917
|
+
this._closeDropdown();
|
|
1032
918
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
var queryTimer = null;
|
|
1042
|
-
this._handleKeydownOnDropdown = function(e) {
|
|
1043
|
-
// prevent down key from scrolling the whole page,
|
|
1044
|
-
// and enter key from submitting a form etc
|
|
1045
|
-
if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
|
|
1046
|
-
e.preventDefault();
|
|
1047
|
-
e.stopPropagation();
|
|
1048
|
-
// up and down to navigate
|
|
1049
|
-
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
1050
|
-
_this8._handleUpDownKey(e.key);
|
|
1051
|
-
} else if (e.key === "Enter") {
|
|
1052
|
-
_this8._handleEnterKey();
|
|
1053
|
-
} else if (e.key === "Escape") {
|
|
1054
|
-
_this8._closeDropdown();
|
|
1055
|
-
}
|
|
919
|
+
}
|
|
920
|
+
// alpha chars to perform search
|
|
921
|
+
// regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
|
|
922
|
+
if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
|
|
923
|
+
e.stopPropagation();
|
|
924
|
+
// jump to countries that start with the query string
|
|
925
|
+
if (queryTimer) {
|
|
926
|
+
clearTimeout(queryTimer);
|
|
1056
927
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
if
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
928
|
+
query += e.key.toLowerCase();
|
|
929
|
+
this._searchForCountry(query);
|
|
930
|
+
// if the timer hits 1 second, reset the query
|
|
931
|
+
queryTimer = setTimeout(() => {
|
|
932
|
+
query = "";
|
|
933
|
+
}, 1e3);
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
document.addEventListener("keydown", this._handleKeydownOnDropdown);
|
|
937
|
+
if (this.options.countrySearch) {
|
|
938
|
+
const doFilter = () => {
|
|
939
|
+
const inputQuery = this.searchInput.value.trim();
|
|
940
|
+
if (inputQuery) {
|
|
941
|
+
this._filterCountries(inputQuery);
|
|
942
|
+
} else {
|
|
943
|
+
this._filterCountries("", true);
|
|
1071
944
|
}
|
|
1072
945
|
};
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
_this8._filterCountries(inputQuery);
|
|
1079
|
-
} else {
|
|
1080
|
-
_this8._filterCountries("", true);
|
|
1081
|
-
}
|
|
1082
|
-
};
|
|
1083
|
-
var keyupTimer = null;
|
|
1084
|
-
this._handleSearchChange = function() {
|
|
1085
|
-
// filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
|
|
1086
|
-
if (keyupTimer) {
|
|
1087
|
-
clearTimeout(keyupTimer);
|
|
1088
|
-
}
|
|
1089
|
-
keyupTimer = setTimeout(function() {
|
|
1090
|
-
doFilter();
|
|
1091
|
-
keyupTimer = null;
|
|
1092
|
-
}, 100);
|
|
1093
|
-
};
|
|
1094
|
-
this.searchInput.addEventListener("input", this._handleSearchChange);
|
|
1095
|
-
// stop propagation on search input click, so doesn't trigger click-off-to-close listener
|
|
1096
|
-
this.searchInput.addEventListener("click", function(e) {
|
|
1097
|
-
return e.stopPropagation();
|
|
1098
|
-
});
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
}, {
|
|
1102
|
-
key: "_normaliseString",
|
|
1103
|
-
value: function _normaliseString() {
|
|
1104
|
-
var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
|
|
1105
|
-
return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
|
|
1106
|
-
}
|
|
1107
|
-
}, {
|
|
1108
|
-
key: "_filterCountries",
|
|
1109
|
-
value: function _filterCountries(query) {
|
|
1110
|
-
var isReset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
1111
|
-
var isFirst = true;
|
|
1112
|
-
this.countryList.innerHTML = "";
|
|
1113
|
-
var normalisedQuery = this._normaliseString(query);
|
|
1114
|
-
for (var i = 0; i < this.countries.length; i++) {
|
|
1115
|
-
var c = this.countries[i];
|
|
1116
|
-
var normalisedCountryName = this._normaliseString(c.name);
|
|
1117
|
-
var fullDialCode = "+".concat(c.dialCode);
|
|
1118
|
-
if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
|
|
1119
|
-
this.countryList.appendChild(c.nodeById[this.id]);
|
|
1120
|
-
// highlight the first item
|
|
1121
|
-
if (isFirst) {
|
|
1122
|
-
this._highlightListItem(c.nodeById[this.id], false);
|
|
1123
|
-
isFirst = false;
|
|
1124
|
-
}
|
|
946
|
+
let keyupTimer = null;
|
|
947
|
+
this._handleSearchChange = () => {
|
|
948
|
+
// filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
|
|
949
|
+
if (keyupTimer) {
|
|
950
|
+
clearTimeout(keyupTimer);
|
|
1125
951
|
}
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
952
|
+
keyupTimer = setTimeout(() => {
|
|
953
|
+
doFilter();
|
|
954
|
+
keyupTimer = null;
|
|
955
|
+
}, 100);
|
|
956
|
+
};
|
|
957
|
+
this.searchInput.addEventListener("input", this._handleSearchChange);
|
|
958
|
+
// stop propagation on search input click, so doesn't trigger click-off-to-close listener
|
|
959
|
+
this.searchInput.addEventListener("click", e => e.stopPropagation());
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
// turns "Réunion" into "Reunion"
|
|
963
|
+
// from https://stackoverflow.com/a/37511463
|
|
964
|
+
_normaliseString(s = "") {
|
|
965
|
+
return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
|
|
966
|
+
}
|
|
967
|
+
_filterCountries(query, isReset = false) {
|
|
968
|
+
let isFirst = true;
|
|
969
|
+
this.countryList.innerHTML = "";
|
|
970
|
+
const normalisedQuery = this._normaliseString(query);
|
|
971
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
972
|
+
const c = this.countries[i];
|
|
973
|
+
const normalisedCountryName = this._normaliseString(c.name);
|
|
974
|
+
const fullDialCode = `+${c.dialCode}`;
|
|
975
|
+
if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
|
|
976
|
+
this.countryList.appendChild(c.nodeById[this.id]);
|
|
977
|
+
// highlight the first item
|
|
978
|
+
if (isFirst) {
|
|
979
|
+
this._highlightListItem(c.nodeById[this.id], false);
|
|
980
|
+
isFirst = false;
|
|
1155
981
|
}
|
|
1156
|
-
} else if (this.countryList.childElementCount > 1) {
|
|
1157
|
-
// otherwise, we must be at the end, so loop round again
|
|
1158
|
-
next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
|
|
1159
|
-
}
|
|
1160
|
-
if (next) {
|
|
1161
|
-
// make sure the next item is visible
|
|
1162
|
-
// (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
|
|
1163
|
-
this._scrollTo(next, false);
|
|
1164
|
-
// if country search enabled, dont lose focus from the search input on up/down
|
|
1165
|
-
var doFocus = !this.options.countrySearch;
|
|
1166
|
-
this._highlightListItem(next, doFocus);
|
|
1167
982
|
}
|
|
1168
983
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
984
|
+
// scroll to top (useful if user had previously scrolled down)
|
|
985
|
+
this.countryList.scrollTop = 0;
|
|
986
|
+
this._updateSearchResultsText();
|
|
987
|
+
}
|
|
988
|
+
// update search results text (for a11y)
|
|
989
|
+
_updateSearchResultsText() {
|
|
990
|
+
const {
|
|
991
|
+
i18n
|
|
992
|
+
} = this.options;
|
|
993
|
+
const count = this.countryList.childElementCount;
|
|
994
|
+
let searchText;
|
|
995
|
+
if (count === 0) {
|
|
996
|
+
searchText = i18n.zeroSearchResults || "No results found";
|
|
997
|
+
} else if (count === 1) {
|
|
998
|
+
searchText = i18n.oneSearchResult || "1 result found";
|
|
999
|
+
} else {
|
|
1000
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
1001
|
+
searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : `${count} results found`;
|
|
1002
|
+
}
|
|
1003
|
+
this.searchResultsA11yText.textContent = searchText;
|
|
1004
|
+
}
|
|
1005
|
+
// highlight the next/prev item in the list (and ensure it is visible)
|
|
1006
|
+
_handleUpDownKey(key) {
|
|
1007
|
+
let next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
|
|
1008
|
+
if (next) {
|
|
1009
|
+
// skip the divider
|
|
1010
|
+
if (next.classList.contains("iti__divider")) {
|
|
1011
|
+
next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
|
|
1012
|
+
}
|
|
1013
|
+
} else if (this.countryList.childElementCount > 1) {
|
|
1014
|
+
// otherwise, we must be at the end, so loop round again
|
|
1015
|
+
next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
|
|
1016
|
+
}
|
|
1017
|
+
if (next) {
|
|
1018
|
+
// make sure the next item is visible
|
|
1019
|
+
// (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
|
|
1020
|
+
this._scrollTo(next, false);
|
|
1021
|
+
// if country search enabled, dont lose focus from the search input on up/down
|
|
1022
|
+
const doFocus = !this.options.countrySearch;
|
|
1023
|
+
this._highlightListItem(next, doFocus);
|
|
1175
1024
|
}
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
var listItem = this.countries[i].nodeById[this.id];
|
|
1182
|
-
// update highlighting and scroll
|
|
1183
|
-
this._highlightListItem(listItem, false);
|
|
1184
|
-
this._scrollTo(listItem, true);
|
|
1185
|
-
break;
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1025
|
+
}
|
|
1026
|
+
// select the currently highlighted item
|
|
1027
|
+
_handleEnterKey() {
|
|
1028
|
+
if (this.highlightedItem) {
|
|
1029
|
+
this._selectListItem(this.highlightedItem);
|
|
1188
1030
|
}
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
var useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
|
|
1200
|
-
var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL;
|
|
1201
|
-
var format = useNational ? NATIONAL : INTERNATIONAL;
|
|
1202
|
-
number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
|
|
1203
|
-
}
|
|
1204
|
-
number = this._beforeSetNumber(number);
|
|
1205
|
-
this.telInput.value = number;
|
|
1206
|
-
}
|
|
1207
|
-
}, {
|
|
1208
|
-
key: "_updateFlagFromNumber",
|
|
1209
|
-
value: function _updateFlagFromNumber(fullNumber) {
|
|
1210
|
-
var plusIndex = fullNumber.indexOf("+");
|
|
1211
|
-
// if it contains a plus, discard any chars before it e.g. accidental space char.
|
|
1212
|
-
// this keeps the selected country auto-updating correctly, which we want as
|
|
1213
|
-
// libphonenumber's validation/getNumber methods will ignore these chars anyway
|
|
1214
|
-
var number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
|
|
1215
|
-
// if we already have US/Canada selected, make sure the number starts
|
|
1216
|
-
// with a +1 so _getDialCode will be able to extract the area code
|
|
1217
|
-
// update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
|
|
1218
|
-
// from the number), that means we're initialising the plugin with a number that already has a
|
|
1219
|
-
// dial code, so fine to ignore this bit
|
|
1220
|
-
var selectedDialCode = this.selectedCountryData.dialCode;
|
|
1221
|
-
var isNanp = selectedDialCode === "1";
|
|
1222
|
-
if (number && isNanp && number.charAt(0) !== "+") {
|
|
1223
|
-
if (number.charAt(0) !== "1") {
|
|
1224
|
-
number = "1".concat(number);
|
|
1225
|
-
}
|
|
1226
|
-
number = "+".concat(number);
|
|
1227
|
-
}
|
|
1228
|
-
// if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
|
|
1229
|
-
if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
|
|
1230
|
-
number = "+".concat(selectedDialCode).concat(number);
|
|
1031
|
+
}
|
|
1032
|
+
// find the first list item whose name starts with the query string
|
|
1033
|
+
_searchForCountry(query) {
|
|
1034
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
1035
|
+
if (this._startsWith(this.countries[i].name, query)) {
|
|
1036
|
+
const listItem = this.countries[i].nodeById[this.id];
|
|
1037
|
+
// update highlighting and scroll
|
|
1038
|
+
this._highlightListItem(listItem, false);
|
|
1039
|
+
this._scrollTo(listItem, true);
|
|
1040
|
+
break;
|
|
1231
1041
|
}
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
// check if string a starts with string b
|
|
1045
|
+
_startsWith(a, b) {
|
|
1046
|
+
return a.substr(0, b.length).toLowerCase() === b;
|
|
1047
|
+
}
|
|
1048
|
+
// update the input's value to the given val (format first if possible)
|
|
1049
|
+
// NOTE: this is called from _setInitialState, handleUtils and setNumber
|
|
1050
|
+
_updateValFromNumber(fullNumber) {
|
|
1051
|
+
let number = fullNumber;
|
|
1052
|
+
if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
|
|
1053
|
+
const useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
|
|
1054
|
+
const {
|
|
1055
|
+
NATIONAL,
|
|
1056
|
+
INTERNATIONAL
|
|
1057
|
+
} = intlTelInputUtils.numberFormat;
|
|
1058
|
+
const format = useNational ? NATIONAL : INTERNATIONAL;
|
|
1059
|
+
number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
|
|
1060
|
+
}
|
|
1061
|
+
number = this._beforeSetNumber(number);
|
|
1062
|
+
this.telInput.value = number;
|
|
1063
|
+
}
|
|
1064
|
+
// check if need to select a new flag based on the given number
|
|
1065
|
+
// Note: called from _setInitialState, keyup handler, setNumber
|
|
1066
|
+
_updateFlagFromNumber(fullNumber) {
|
|
1067
|
+
const plusIndex = fullNumber.indexOf("+");
|
|
1068
|
+
// if it contains a plus, discard any chars before it e.g. accidental space char.
|
|
1069
|
+
// this keeps the selected country auto-updating correctly, which we want as
|
|
1070
|
+
// libphonenumber's validation/getNumber methods will ignore these chars anyway
|
|
1071
|
+
let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
|
|
1072
|
+
// if we already have US/Canada selected, make sure the number starts
|
|
1073
|
+
// with a +1 so _getDialCode will be able to extract the area code
|
|
1074
|
+
// update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
|
|
1075
|
+
// from the number), that means we're initialising the plugin with a number that already has a
|
|
1076
|
+
// dial code, so fine to ignore this bit
|
|
1077
|
+
const selectedDialCode = this.selectedCountryData.dialCode;
|
|
1078
|
+
const isNanp = selectedDialCode === "1";
|
|
1079
|
+
if (number && isNanp && number.charAt(0) !== "+") {
|
|
1080
|
+
if (number.charAt(0) !== "1") {
|
|
1081
|
+
number = `1${number}`;
|
|
1082
|
+
}
|
|
1083
|
+
number = `+${number}`;
|
|
1084
|
+
}
|
|
1085
|
+
// if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
|
|
1086
|
+
if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
|
|
1087
|
+
number = `+${selectedDialCode}${number}`;
|
|
1088
|
+
}
|
|
1089
|
+
// try and extract valid dial code from input
|
|
1090
|
+
const dialCode = this._getDialCode(number, true);
|
|
1091
|
+
const numeric = this._getNumeric(number);
|
|
1092
|
+
let iso2 = null;
|
|
1093
|
+
if (dialCode) {
|
|
1094
|
+
const iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
|
|
1095
|
+
// check if the right country is already selected. this should be false if the number is
|
|
1096
|
+
// longer than the matched dial code because in this case we need to make sure that if
|
|
1097
|
+
// there are multiple country matches, that the first one is selected (note: we could
|
|
1098
|
+
// just check that here, but it requires the same loop that we already have later)
|
|
1099
|
+
const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
|
|
1100
|
+
const isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
|
|
1101
|
+
// only update the flag if:
|
|
1102
|
+
// A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
|
|
1103
|
+
// AND
|
|
1104
|
+
// B) the right country is not already selected
|
|
1105
|
+
if (!isRegionlessNanpNumber && !alreadySelected) {
|
|
1106
|
+
// if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
|
|
1107
|
+
// non-empty index
|
|
1108
|
+
for (let j = 0; j < iso2Codes.length; j++) {
|
|
1109
|
+
if (iso2Codes[j]) {
|
|
1110
|
+
iso2 = iso2Codes[j];
|
|
1111
|
+
break;
|
|
1256
1112
|
}
|
|
1257
1113
|
}
|
|
1258
|
-
} else if (number.charAt(0) === "+" && numeric.length) {
|
|
1259
|
-
// invalid dial code, so empty
|
|
1260
|
-
// Note: use getNumeric here because the number has not been formatted yet, so could contain
|
|
1261
|
-
// bad chars
|
|
1262
|
-
iso2 = "";
|
|
1263
|
-
} else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
|
|
1264
|
-
// if no selected flag, and user either clears the input, or just types a plus, then show default
|
|
1265
|
-
iso2 = this.defaultCountry;
|
|
1266
1114
|
}
|
|
1267
|
-
|
|
1268
|
-
|
|
1115
|
+
} else if (number.charAt(0) === "+" && numeric.length) {
|
|
1116
|
+
// invalid dial code, so empty
|
|
1117
|
+
// Note: use getNumeric here because the number has not been formatted yet, so could contain
|
|
1118
|
+
// bad chars
|
|
1119
|
+
iso2 = "";
|
|
1120
|
+
} else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
|
|
1121
|
+
// if no selected flag, and user either clears the input, or just types a plus, then show default
|
|
1122
|
+
iso2 = this.defaultCountry;
|
|
1123
|
+
}
|
|
1124
|
+
if (iso2 !== null) {
|
|
1125
|
+
return this._setCountry(iso2);
|
|
1126
|
+
}
|
|
1127
|
+
return false;
|
|
1128
|
+
}
|
|
1129
|
+
// check if the given number is a regionless NANP number (expects the number to contain an
|
|
1130
|
+
// international dial code)
|
|
1131
|
+
_isRegionlessNanp(number) {
|
|
1132
|
+
const numeric = this._getNumeric(number);
|
|
1133
|
+
if (numeric.charAt(0) === "1") {
|
|
1134
|
+
const areaCode = numeric.substr(1, 3);
|
|
1135
|
+
return regionlessNanpNumbers.indexOf(areaCode) !== -1;
|
|
1136
|
+
}
|
|
1137
|
+
return false;
|
|
1138
|
+
}
|
|
1139
|
+
// remove highlighting from other list items and highlight the given item
|
|
1140
|
+
_highlightListItem(listItem, shouldFocus) {
|
|
1141
|
+
const prevItem = this.highlightedItem;
|
|
1142
|
+
if (prevItem) {
|
|
1143
|
+
prevItem.classList.remove("iti__highlight");
|
|
1144
|
+
prevItem.setAttribute("aria-selected", "false");
|
|
1145
|
+
}
|
|
1146
|
+
this.highlightedItem = listItem;
|
|
1147
|
+
this.highlightedItem.classList.add("iti__highlight");
|
|
1148
|
+
this.highlightedItem.setAttribute("aria-selected", "true");
|
|
1149
|
+
this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
|
|
1150
|
+
if (this.options.countrySearch) {
|
|
1151
|
+
this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
|
|
1152
|
+
}
|
|
1153
|
+
if (shouldFocus) {
|
|
1154
|
+
this.highlightedItem.focus();
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
// find the country data for the given iso2 code
|
|
1158
|
+
// the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
|
|
1159
|
+
_getCountryData(iso2, allowFail) {
|
|
1160
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
1161
|
+
if (this.countries[i].iso2 === iso2) {
|
|
1162
|
+
return this.countries[i];
|
|
1269
1163
|
}
|
|
1270
|
-
return false;
|
|
1271
1164
|
}
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1165
|
+
if (allowFail) {
|
|
1166
|
+
return null;
|
|
1167
|
+
}
|
|
1168
|
+
throw new Error(`No country data for '${iso2}'`);
|
|
1169
|
+
}
|
|
1170
|
+
// update the selected flag, dial code (if showSelectedDialCode), placeholder, title, and active list item
|
|
1171
|
+
// Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry
|
|
1172
|
+
_setCountry(iso2) {
|
|
1173
|
+
const {
|
|
1174
|
+
allowDropdown,
|
|
1175
|
+
showSelectedDialCode,
|
|
1176
|
+
showFlags,
|
|
1177
|
+
countrySearch,
|
|
1178
|
+
i18n
|
|
1179
|
+
} = this.options;
|
|
1180
|
+
const prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
|
|
1181
|
+
// do this first as it will throw an error and stop if iso2 is invalid
|
|
1182
|
+
this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
|
|
1183
|
+
// update the defaultCountry - we only need the iso2 from now on, so just store that
|
|
1184
|
+
if (this.selectedCountryData.iso2) {
|
|
1185
|
+
this.defaultCountry = this.selectedCountryData.iso2;
|
|
1186
|
+
}
|
|
1187
|
+
// update the flag class and the a11y text
|
|
1188
|
+
if (this.selectedFlagInner) {
|
|
1189
|
+
let flagClass = "";
|
|
1190
|
+
let a11yText = "";
|
|
1191
|
+
if (iso2) {
|
|
1192
|
+
if (showFlags) {
|
|
1193
|
+
flagClass = `iti__flag iti__${iso2}`;
|
|
1194
|
+
a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
|
|
1195
|
+
}
|
|
1196
|
+
// if showFlags disabled and showSelectedDialCode is enabled, we dont show a flag or have any a11y text, as the displayed dial code is enough
|
|
1197
|
+
} else {
|
|
1198
|
+
flagClass = "iti__flag iti__globe";
|
|
1199
|
+
a11yText = i18n.noCountrySelected || "No country selected";
|
|
1200
|
+
}
|
|
1201
|
+
this.selectedFlagInner.className = flagClass;
|
|
1202
|
+
this.selectedFlagA11yText.textContent = a11yText;
|
|
1203
|
+
}
|
|
1204
|
+
this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
|
|
1205
|
+
// update the selected dial code
|
|
1206
|
+
if (showSelectedDialCode) {
|
|
1207
|
+
const dialCode = this.selectedCountryData.dialCode ? `+${this.selectedCountryData.dialCode}` : "";
|
|
1208
|
+
this.selectedDialCode.innerHTML = dialCode;
|
|
1209
|
+
// offsetWidth is zero if input is in a hidden container during initialisation
|
|
1210
|
+
const selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
|
|
1211
|
+
// add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
|
|
1212
|
+
if (this.isRTL) {
|
|
1213
|
+
this.telInput.style.paddingRight = `${selectedFlagWidth + 6}px`;
|
|
1214
|
+
} else {
|
|
1215
|
+
this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
|
|
1279
1216
|
}
|
|
1280
|
-
return false;
|
|
1281
1217
|
}
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1218
|
+
// and the input's placeholder
|
|
1219
|
+
this._updatePlaceholder();
|
|
1220
|
+
// update the maximum valid number length
|
|
1221
|
+
this._updateMaxLength();
|
|
1222
|
+
// update the active list item (only if country search disabled, as country search doesn't store the active item)
|
|
1223
|
+
if (allowDropdown && !countrySearch) {
|
|
1224
|
+
const prevItem = this.activeItem;
|
|
1286
1225
|
if (prevItem) {
|
|
1287
|
-
prevItem.classList.remove("
|
|
1226
|
+
prevItem.classList.remove("iti__active");
|
|
1288
1227
|
prevItem.setAttribute("aria-selected", "false");
|
|
1289
1228
|
}
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
this.
|
|
1296
|
-
}
|
|
1297
|
-
if (shouldFocus) {
|
|
1298
|
-
this.highlightedItem.focus();
|
|
1229
|
+
if (iso2) {
|
|
1230
|
+
// check if there is a preferred item first, else fall back to standard
|
|
1231
|
+
const nextItem = this.countryList.querySelector(`#iti-${this.id}__item-${iso2}-preferred`) || this.countryList.querySelector(`#iti-${this.id}__item-${iso2}`);
|
|
1232
|
+
nextItem.setAttribute("aria-selected", "true");
|
|
1233
|
+
nextItem.classList.add("iti__active");
|
|
1234
|
+
this.activeItem = nextItem;
|
|
1299
1235
|
}
|
|
1300
1236
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
if (allowFail) {
|
|
1310
|
-
return null;
|
|
1311
|
-
}
|
|
1312
|
-
throw new Error("No country data for '".concat(iso2, "'"));
|
|
1313
|
-
}
|
|
1314
|
-
}, {
|
|
1315
|
-
key: "_setCountry",
|
|
1316
|
-
value: function _setCountry(iso2) {
|
|
1317
|
-
var _this$options4 = this.options, allowDropdown = _this$options4.allowDropdown, showSelectedDialCode = _this$options4.showSelectedDialCode, showFlags = _this$options4.showFlags, countrySearch = _this$options4.countrySearch, i18n = _this$options4.i18n;
|
|
1318
|
-
var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
|
|
1319
|
-
// do this first as it will throw an error and stop if iso2 is invalid
|
|
1320
|
-
this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
|
|
1321
|
-
// update the defaultCountry - we only need the iso2 from now on, so just store that
|
|
1237
|
+
// return if the flag has changed or not
|
|
1238
|
+
return prevCountry.iso2 !== iso2;
|
|
1239
|
+
}
|
|
1240
|
+
// update the maximum valid number length for the currently selected country
|
|
1241
|
+
_updateMaxLength() {
|
|
1242
|
+
if (this.options.strictMode && window.intlTelInputUtils) {
|
|
1322
1243
|
if (this.selectedCountryData.iso2) {
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
if (showFlags) {
|
|
1331
|
-
flagClass = "iti__flag iti__".concat(iso2);
|
|
1332
|
-
a11yText = "".concat(this.selectedCountryData.name, " +").concat(this.selectedCountryData.dialCode);
|
|
1333
|
-
}
|
|
1334
|
-
} else {
|
|
1335
|
-
flagClass = "iti__flag iti__globe";
|
|
1336
|
-
a11yText = i18n.noCountrySelected || "No country selected";
|
|
1337
|
-
}
|
|
1338
|
-
this.selectedFlagInner.className = flagClass;
|
|
1339
|
-
this.selectedFlagA11yText.textContent = a11yText;
|
|
1340
|
-
}
|
|
1341
|
-
this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
|
|
1342
|
-
// update the selected dial code
|
|
1343
|
-
if (showSelectedDialCode) {
|
|
1344
|
-
var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : "";
|
|
1345
|
-
this.selectedDialCode.innerHTML = dialCode;
|
|
1346
|
-
// offsetWidth is zero if input is in a hidden container during initialisation
|
|
1347
|
-
var selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
|
|
1348
|
-
// add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
|
|
1349
|
-
if (this.isRTL) {
|
|
1350
|
-
this.telInput.style.paddingRight = "".concat(selectedFlagWidth + 6, "px");
|
|
1351
|
-
} else {
|
|
1352
|
-
this.telInput.style.paddingLeft = "".concat(selectedFlagWidth + 6, "px");
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
// and the input's placeholder
|
|
1356
|
-
this._updatePlaceholder();
|
|
1357
|
-
// update the maximum valid number length
|
|
1358
|
-
this._updateMaxLength();
|
|
1359
|
-
// update the active list item (only if country search disabled, as country search doesn't store the active item)
|
|
1360
|
-
if (allowDropdown && !countrySearch) {
|
|
1361
|
-
var prevItem = this.activeItem;
|
|
1362
|
-
if (prevItem) {
|
|
1363
|
-
prevItem.classList.remove("iti__active");
|
|
1364
|
-
prevItem.setAttribute("aria-selected", "false");
|
|
1365
|
-
}
|
|
1366
|
-
if (iso2) {
|
|
1367
|
-
// check if there is a preferred item first, else fall back to standard
|
|
1368
|
-
var nextItem = this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2, "-preferred")) || this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2));
|
|
1369
|
-
nextItem.setAttribute("aria-selected", "true");
|
|
1370
|
-
nextItem.classList.add("iti__active");
|
|
1371
|
-
this.activeItem = nextItem;
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
// return if the flag has changed or not
|
|
1375
|
-
return prevCountry.iso2 !== iso2;
|
|
1376
|
-
}
|
|
1377
|
-
}, {
|
|
1378
|
-
key: "_updateMaxLength",
|
|
1379
|
-
value: function _updateMaxLength() {
|
|
1380
|
-
if (this.options.strictMode && window.intlTelInputUtils) {
|
|
1381
|
-
if (this.selectedCountryData.iso2) {
|
|
1382
|
-
var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
|
|
1383
|
-
var exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
|
|
1384
|
-
// see if adding more digits is still valid to get the true maximum valid length
|
|
1385
|
-
var validNumber = exampleNumber;
|
|
1386
|
-
while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
|
|
1387
|
-
validNumber = exampleNumber;
|
|
1388
|
-
exampleNumber += "0";
|
|
1389
|
-
}
|
|
1390
|
-
var coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
|
|
1391
|
-
this.maxCoreNumberLength = coreNumber.length;
|
|
1392
|
-
} else {
|
|
1393
|
-
this.maxCoreNumberLength = null;
|
|
1244
|
+
const numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
|
|
1245
|
+
let exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
|
|
1246
|
+
// see if adding more digits is still valid to get the true maximum valid length
|
|
1247
|
+
let validNumber = exampleNumber;
|
|
1248
|
+
while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
|
|
1249
|
+
validNumber = exampleNumber;
|
|
1250
|
+
exampleNumber += "0";
|
|
1394
1251
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
}, {
|
|
1398
|
-
key: "_setSelectedCountryFlagTitleAttribute",
|
|
1399
|
-
value: function _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
|
|
1400
|
-
if (!this.selectedFlag) {
|
|
1401
|
-
return;
|
|
1402
|
-
}
|
|
1403
|
-
var title;
|
|
1404
|
-
if (iso2 && !showSelectedDialCode) {
|
|
1405
|
-
title = "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode);
|
|
1406
|
-
} else if (iso2) {
|
|
1407
|
-
// For screen reader output, we don't want to include the dial code in the reader output twice
|
|
1408
|
-
// so just use the selected country name here:
|
|
1409
|
-
title = this.selectedCountryData.name;
|
|
1252
|
+
const coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
|
|
1253
|
+
this.maxCoreNumberLength = coreNumber.length;
|
|
1410
1254
|
} else {
|
|
1411
|
-
|
|
1412
|
-
}
|
|
1413
|
-
this.selectedFlag.setAttribute("title", title);
|
|
1414
|
-
}
|
|
1415
|
-
}, {
|
|
1416
|
-
key: "_getHiddenSelectedFlagWidth",
|
|
1417
|
-
value: function _getHiddenSelectedFlagWidth() {
|
|
1418
|
-
// to get the right styling to apply, all we need is a shallow clone of the container,
|
|
1419
|
-
// and then to inject a deep clone of the selectedFlag element
|
|
1420
|
-
var containerClone = this.telInput.parentNode.cloneNode();
|
|
1421
|
-
containerClone.style.visibility = "hidden";
|
|
1422
|
-
document.body.appendChild(containerClone);
|
|
1423
|
-
var flagsContainerClone = this.flagsContainer.cloneNode();
|
|
1424
|
-
containerClone.appendChild(flagsContainerClone);
|
|
1425
|
-
var selectedFlagClone = this.selectedFlag.cloneNode(true);
|
|
1426
|
-
flagsContainerClone.appendChild(selectedFlagClone);
|
|
1427
|
-
var width = selectedFlagClone.offsetWidth;
|
|
1428
|
-
containerClone.parentNode.removeChild(containerClone);
|
|
1429
|
-
return width;
|
|
1430
|
-
}
|
|
1431
|
-
}, {
|
|
1432
|
-
key: "_updatePlaceholder",
|
|
1433
|
-
value: function _updatePlaceholder() {
|
|
1434
|
-
var _this$options5 = this.options, autoPlaceholder = _this$options5.autoPlaceholder, placeholderNumberType = _this$options5.placeholderNumberType, nationalMode = _this$options5.nationalMode, customPlaceholder = _this$options5.customPlaceholder;
|
|
1435
|
-
var shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
|
|
1436
|
-
if (window.intlTelInputUtils && shouldSetPlaceholder) {
|
|
1437
|
-
var numberType = intlTelInputUtils.numberType[placeholderNumberType];
|
|
1438
|
-
// note: must set placeholder to empty string if no country selected (globe icon showing)
|
|
1439
|
-
var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
|
|
1440
|
-
placeholder = this._beforeSetNumber(placeholder);
|
|
1441
|
-
if (typeof customPlaceholder === "function") {
|
|
1442
|
-
placeholder = customPlaceholder(placeholder, this.selectedCountryData);
|
|
1443
|
-
}
|
|
1444
|
-
this.telInput.setAttribute("placeholder", placeholder);
|
|
1255
|
+
this.maxCoreNumberLength = null;
|
|
1445
1256
|
}
|
|
1446
1257
|
}
|
|
1447
|
-
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1258
|
+
}
|
|
1259
|
+
_setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
|
|
1260
|
+
if (!this.selectedFlag) {
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
let title;
|
|
1264
|
+
if (iso2 && !showSelectedDialCode) {
|
|
1265
|
+
title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
|
|
1266
|
+
} else if (iso2) {
|
|
1267
|
+
// For screen reader output, we don't want to include the dial code in the reader output twice
|
|
1268
|
+
// so just use the selected country name here:
|
|
1269
|
+
title = this.selectedCountryData.name;
|
|
1270
|
+
} else {
|
|
1271
|
+
title = "Unknown";
|
|
1272
|
+
}
|
|
1273
|
+
this.selectedFlag.setAttribute("title", title);
|
|
1274
|
+
}
|
|
1275
|
+
// when the input is in a hidden container during initialisation, we must inject some markup
|
|
1276
|
+
// into the end of the DOM to calculate the correct offsetWidth
|
|
1277
|
+
// NOTE: this is only used when showSelectedDialCode is enabled, so flagsContainer and selectedFlag
|
|
1278
|
+
// will definitely exist
|
|
1279
|
+
_getHiddenSelectedFlagWidth() {
|
|
1280
|
+
// to get the right styling to apply, all we need is a shallow clone of the container,
|
|
1281
|
+
// and then to inject a deep clone of the selectedFlag element
|
|
1282
|
+
const containerClone = this.telInput.parentNode.cloneNode();
|
|
1283
|
+
containerClone.style.visibility = "hidden";
|
|
1284
|
+
document.body.appendChild(containerClone);
|
|
1285
|
+
const flagsContainerClone = this.flagsContainer.cloneNode();
|
|
1286
|
+
containerClone.appendChild(flagsContainerClone);
|
|
1287
|
+
const selectedFlagClone = this.selectedFlag.cloneNode(true);
|
|
1288
|
+
flagsContainerClone.appendChild(selectedFlagClone);
|
|
1289
|
+
const width = selectedFlagClone.offsetWidth;
|
|
1290
|
+
containerClone.parentNode.removeChild(containerClone);
|
|
1291
|
+
return width;
|
|
1292
|
+
}
|
|
1293
|
+
// update the input placeholder to an example number from the currently selected country
|
|
1294
|
+
_updatePlaceholder() {
|
|
1295
|
+
const {
|
|
1296
|
+
autoPlaceholder,
|
|
1297
|
+
placeholderNumberType,
|
|
1298
|
+
nationalMode,
|
|
1299
|
+
customPlaceholder
|
|
1300
|
+
} = this.options;
|
|
1301
|
+
const shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
|
|
1302
|
+
if (window.intlTelInputUtils && shouldSetPlaceholder) {
|
|
1303
|
+
const numberType = intlTelInputUtils.numberType[placeholderNumberType];
|
|
1304
|
+
// note: must set placeholder to empty string if no country selected (globe icon showing)
|
|
1305
|
+
let placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
|
|
1306
|
+
placeholder = this._beforeSetNumber(placeholder);
|
|
1307
|
+
if (typeof customPlaceholder === "function") {
|
|
1308
|
+
placeholder = customPlaceholder(placeholder, this.selectedCountryData);
|
|
1309
|
+
}
|
|
1310
|
+
this.telInput.setAttribute("placeholder", placeholder);
|
|
1459
1311
|
}
|
|
1460
|
-
}
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1312
|
+
}
|
|
1313
|
+
// called when the user selects a list item from the dropdown
|
|
1314
|
+
_selectListItem(listItem) {
|
|
1315
|
+
// update selected flag and active list item
|
|
1316
|
+
const flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
|
|
1317
|
+
this._closeDropdown();
|
|
1318
|
+
this._updateDialCode(listItem.getAttribute("data-dial-code"));
|
|
1319
|
+
// focus the input
|
|
1320
|
+
this.telInput.focus();
|
|
1321
|
+
if (flagChanged) {
|
|
1322
|
+
this._triggerCountryChange();
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
// close the dropdown and unbind any listeners
|
|
1326
|
+
_closeDropdown() {
|
|
1327
|
+
this.dropdownContent.classList.add("iti__hide");
|
|
1328
|
+
this.selectedFlag.setAttribute("aria-expanded", "false");
|
|
1329
|
+
this.selectedFlag.removeAttribute("aria-activedescendant");
|
|
1330
|
+
if (this.highlightedItem) {
|
|
1331
|
+
this.highlightedItem.setAttribute("aria-selected", "false");
|
|
1332
|
+
}
|
|
1333
|
+
if (this.options.countrySearch) {
|
|
1334
|
+
this.searchInput.removeAttribute("aria-activedescendant");
|
|
1335
|
+
}
|
|
1336
|
+
// update the arrow
|
|
1337
|
+
this.dropdownArrow.classList.remove("iti__arrow--up");
|
|
1338
|
+
// unbind key events
|
|
1339
|
+
document.removeEventListener("keydown", this._handleKeydownOnDropdown);
|
|
1340
|
+
if (this.options.countrySearch) {
|
|
1341
|
+
this.searchInput.removeEventListener("input", this._handleSearchChange);
|
|
1342
|
+
}
|
|
1343
|
+
document.documentElement.removeEventListener("click", this._handleClickOffToClose);
|
|
1344
|
+
this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
|
|
1345
|
+
this.countryList.removeEventListener("click", this._handleClickCountryList);
|
|
1346
|
+
// remove menu from container
|
|
1347
|
+
if (this.options.dropdownContainer) {
|
|
1348
|
+
if (!this.options.useFullscreenPopup) {
|
|
1349
|
+
window.removeEventListener("scroll", this._handleWindowScroll);
|
|
1478
1350
|
}
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
this.countryList.removeEventListener("click", this._handleClickCountryList);
|
|
1482
|
-
// remove menu from container
|
|
1483
|
-
if (this.options.dropdownContainer) {
|
|
1484
|
-
if (!this.options.useFullscreenPopup) {
|
|
1485
|
-
window.removeEventListener("scroll", this._handleWindowScroll);
|
|
1486
|
-
}
|
|
1487
|
-
if (this.dropdown.parentNode) {
|
|
1488
|
-
this.dropdown.parentNode.removeChild(this.dropdown);
|
|
1489
|
-
}
|
|
1351
|
+
if (this.dropdown.parentNode) {
|
|
1352
|
+
this.dropdown.parentNode.removeChild(this.dropdown);
|
|
1490
1353
|
}
|
|
1491
|
-
this._trigger("close:countrydropdown");
|
|
1492
1354
|
}
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1355
|
+
this._trigger("close:countrydropdown");
|
|
1356
|
+
}
|
|
1357
|
+
// check if an element is visible within it's container, else scroll until it is
|
|
1358
|
+
_scrollTo(element, middle) {
|
|
1359
|
+
const container = this.countryList;
|
|
1360
|
+
// windowTop from https://stackoverflow.com/a/14384091/217866
|
|
1361
|
+
const windowTop = document.documentElement.scrollTop;
|
|
1362
|
+
const containerHeight = container.offsetHeight;
|
|
1363
|
+
const containerTop = container.getBoundingClientRect().top + windowTop;
|
|
1364
|
+
const containerBottom = containerTop + containerHeight;
|
|
1365
|
+
const elementHeight = element.offsetHeight;
|
|
1366
|
+
const elementTop = element.getBoundingClientRect().top + windowTop;
|
|
1367
|
+
const elementBottom = elementTop + elementHeight;
|
|
1368
|
+
let newScrollTop = elementTop - containerTop + container.scrollTop;
|
|
1369
|
+
const middleOffset = containerHeight / 2 - elementHeight / 2;
|
|
1370
|
+
if (elementTop < containerTop) {
|
|
1371
|
+
// scroll up
|
|
1372
|
+
if (middle) {
|
|
1373
|
+
newScrollTop -= middleOffset;
|
|
1374
|
+
}
|
|
1375
|
+
container.scrollTop = newScrollTop;
|
|
1376
|
+
} else if (elementBottom > containerBottom) {
|
|
1377
|
+
// scroll down
|
|
1378
|
+
if (middle) {
|
|
1379
|
+
newScrollTop += middleOffset;
|
|
1380
|
+
}
|
|
1381
|
+
const heightDifference = containerHeight - elementHeight;
|
|
1382
|
+
container.scrollTop = newScrollTop - heightDifference;
|
|
1521
1383
|
}
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
this.telInput.value = newNumber;
|
|
1384
|
+
}
|
|
1385
|
+
// replace any existing dial code with the new one
|
|
1386
|
+
// Note: called from _selectListItem and setCountry
|
|
1387
|
+
_updateDialCode(newDialCodeBare) {
|
|
1388
|
+
const inputVal = this.telInput.value;
|
|
1389
|
+
// save having to pass this every time
|
|
1390
|
+
const newDialCode = `+${newDialCodeBare}`;
|
|
1391
|
+
let newNumber;
|
|
1392
|
+
if (inputVal.charAt(0) === "+") {
|
|
1393
|
+
// there's a plus so we're dealing with a replacement
|
|
1394
|
+
const prevDialCode = this._getDialCode(inputVal);
|
|
1395
|
+
if (prevDialCode) {
|
|
1396
|
+
// current number contains a valid dial code, so replace it
|
|
1397
|
+
newNumber = inputVal.replace(prevDialCode, newDialCode);
|
|
1398
|
+
} else {
|
|
1399
|
+
// current number contains an invalid dial code, so ditch it
|
|
1400
|
+
// (no way to determine where the invalid dial code ends and the rest of the number begins)
|
|
1401
|
+
newNumber = newDialCode;
|
|
1541
1402
|
}
|
|
1403
|
+
this.telInput.value = newNumber;
|
|
1542
1404
|
}
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
} else {
|
|
1563
|
-
if (this.dialCodes[numericChars]) {
|
|
1564
|
-
dialCode = number.substr(0, i + 1);
|
|
1565
|
-
// if we're just looking for a dial code, we can break as soon as we find one
|
|
1566
|
-
break;
|
|
1567
|
-
}
|
|
1405
|
+
}
|
|
1406
|
+
// try and extract a valid international dial code from a full telephone number
|
|
1407
|
+
// Note: returns the raw string inc plus character and any whitespace/dots etc
|
|
1408
|
+
_getDialCode(number, includeAreaCode) {
|
|
1409
|
+
let dialCode = "";
|
|
1410
|
+
// only interested in international numbers (starting with a plus)
|
|
1411
|
+
if (number.charAt(0) === "+") {
|
|
1412
|
+
let numericChars = "";
|
|
1413
|
+
// iterate over chars
|
|
1414
|
+
for (let i = 0; i < number.length; i++) {
|
|
1415
|
+
const c = number.charAt(i);
|
|
1416
|
+
// if char is number (https://stackoverflow.com/a/8935649/217866)
|
|
1417
|
+
if (!isNaN(parseInt(c, 10))) {
|
|
1418
|
+
numericChars += c;
|
|
1419
|
+
// if current numericChars make a valid dial code
|
|
1420
|
+
if (includeAreaCode) {
|
|
1421
|
+
if (this.dialCodeToIso2Map[numericChars]) {
|
|
1422
|
+
// store the actual raw string (useful for matching later)
|
|
1423
|
+
dialCode = number.substr(0, i + 1);
|
|
1568
1424
|
}
|
|
1569
|
-
|
|
1570
|
-
if (
|
|
1425
|
+
} else {
|
|
1426
|
+
if (this.dialCodes[numericChars]) {
|
|
1427
|
+
dialCode = number.substr(0, i + 1);
|
|
1428
|
+
// if we're just looking for a dial code, we can break as soon as we find one
|
|
1571
1429
|
break;
|
|
1572
1430
|
}
|
|
1573
1431
|
}
|
|
1432
|
+
// stop searching as soon as we can - in this case when we hit max len
|
|
1433
|
+
if (numericChars.length === this.dialCodeMaxLen) {
|
|
1434
|
+
break;
|
|
1435
|
+
}
|
|
1574
1436
|
}
|
|
1575
1437
|
}
|
|
1576
|
-
return dialCode;
|
|
1577
|
-
}
|
|
1578
|
-
}, {
|
|
1579
|
-
key: "_getFullNumber",
|
|
1580
|
-
value: function _getFullNumber() {
|
|
1581
|
-
var val = this.telInput.value.trim();
|
|
1582
|
-
var dialCode = this.selectedCountryData.dialCode;
|
|
1583
|
-
var prefix;
|
|
1584
|
-
var numericVal = this._getNumeric(val);
|
|
1585
|
-
if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
|
|
1586
|
-
// when using showSelectedDialCode, it is visible so is effectively part of the typed number
|
|
1587
|
-
prefix = "+".concat(dialCode);
|
|
1588
|
-
} else {
|
|
1589
|
-
prefix = "";
|
|
1590
|
-
}
|
|
1591
|
-
return prefix + val;
|
|
1592
|
-
}
|
|
1593
|
-
}, {
|
|
1594
|
-
key: "_beforeSetNumber",
|
|
1595
|
-
value: function _beforeSetNumber(fullNumber) {
|
|
1596
|
-
var number = fullNumber;
|
|
1597
|
-
if (this.options.showSelectedDialCode) {
|
|
1598
|
-
var dialCode = this._getDialCode(number);
|
|
1599
|
-
// if there is a valid dial code
|
|
1600
|
-
if (dialCode) {
|
|
1601
|
-
// in case _getDialCode returned an area code as well
|
|
1602
|
-
dialCode = "+".concat(this.selectedCountryData.dialCode);
|
|
1603
|
-
// a lot of numbers will have a space separating the dial code and the main number, and
|
|
1604
|
-
// some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
|
|
1605
|
-
// rid of it
|
|
1606
|
-
// NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
|
|
1607
|
-
var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
|
|
1608
|
-
number = number.substr(start);
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
return this._cap(number);
|
|
1612
|
-
}
|
|
1613
|
-
}, {
|
|
1614
|
-
key: "_triggerCountryChange",
|
|
1615
|
-
value: function _triggerCountryChange() {
|
|
1616
|
-
this._trigger("countrychange");
|
|
1617
|
-
}
|
|
1618
|
-
}, {
|
|
1619
|
-
key: "_formatNumberAsYouType",
|
|
1620
|
-
value: function _formatNumberAsYouType() {
|
|
1621
|
-
var val = this._getFullNumber();
|
|
1622
|
-
var result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
|
|
1623
|
-
// if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
|
|
1624
|
-
var dialCode = this.selectedCountryData.dialCode;
|
|
1625
|
-
if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes("+".concat(dialCode))) {
|
|
1626
|
-
var afterDialCode = result.split("+".concat(dialCode))[1] || "";
|
|
1627
|
-
return afterDialCode.trim();
|
|
1628
|
-
}
|
|
1629
|
-
return result;
|
|
1630
|
-
}
|
|
1631
|
-
}, {
|
|
1632
|
-
key: "handleAutoCountry",
|
|
1633
|
-
value: function handleAutoCountry() {
|
|
1634
|
-
if (this.options.initialCountry === "auto") {
|
|
1635
|
-
// we must set this even if there is an initial val in the input: in case the initial val is
|
|
1636
|
-
// invalid and they delete it - they should see their auto country
|
|
1637
|
-
this.defaultCountry = window.intlTelInputGlobals.autoCountry;
|
|
1638
|
-
// if there's no initial value in the input, then update the flag
|
|
1639
|
-
if (!this.telInput.value) {
|
|
1640
|
-
this.setCountry(this.defaultCountry);
|
|
1641
|
-
}
|
|
1642
|
-
this.resolveAutoCountryPromise();
|
|
1643
|
-
}
|
|
1644
1438
|
}
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1439
|
+
return dialCode;
|
|
1440
|
+
}
|
|
1441
|
+
// get the input val, adding the dial code if showSelectedDialCode is enabled
|
|
1442
|
+
_getFullNumber() {
|
|
1443
|
+
const val = this.telInput.value.trim();
|
|
1444
|
+
const {
|
|
1445
|
+
dialCode
|
|
1446
|
+
} = this.selectedCountryData;
|
|
1447
|
+
let prefix;
|
|
1448
|
+
const numericVal = this._getNumeric(val);
|
|
1449
|
+
if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
|
|
1450
|
+
// when using showSelectedDialCode, it is visible so is effectively part of the typed number
|
|
1451
|
+
prefix = `+${dialCode}`;
|
|
1452
|
+
} else {
|
|
1453
|
+
prefix = "";
|
|
1454
|
+
}
|
|
1455
|
+
return prefix + val;
|
|
1456
|
+
}
|
|
1457
|
+
// remove the dial code if showSelectedDialCode is enabled
|
|
1458
|
+
// also cap the length if the input has a maxlength attribute
|
|
1459
|
+
_beforeSetNumber(fullNumber) {
|
|
1460
|
+
let number = fullNumber;
|
|
1461
|
+
if (this.options.showSelectedDialCode) {
|
|
1462
|
+
let dialCode = this._getDialCode(number);
|
|
1463
|
+
// if there is a valid dial code
|
|
1464
|
+
if (dialCode) {
|
|
1465
|
+
// in case _getDialCode returned an area code as well
|
|
1466
|
+
dialCode = `+${this.selectedCountryData.dialCode}`;
|
|
1467
|
+
// a lot of numbers will have a space separating the dial code and the main number, and
|
|
1468
|
+
// some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
|
|
1469
|
+
// rid of it
|
|
1470
|
+
// NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
|
|
1471
|
+
const start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
|
|
1472
|
+
number = number.substr(start);
|
|
1658
1473
|
}
|
|
1659
|
-
this.resolveUtilsScriptPromise();
|
|
1660
1474
|
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
//
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
value: function getExtension() {
|
|
1696
|
-
if (window.intlTelInputUtils) {
|
|
1697
|
-
return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
|
|
1698
|
-
}
|
|
1699
|
-
return "";
|
|
1700
|
-
}
|
|
1701
|
-
}, {
|
|
1702
|
-
key: "getNumber",
|
|
1703
|
-
value: function getNumber(format) {
|
|
1704
|
-
if (window.intlTelInputUtils) {
|
|
1705
|
-
var iso2 = this.selectedCountryData.iso2;
|
|
1706
|
-
return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
|
|
1707
|
-
}
|
|
1708
|
-
return "";
|
|
1475
|
+
return this._cap(number);
|
|
1476
|
+
}
|
|
1477
|
+
// trigger the 'countrychange' event
|
|
1478
|
+
_triggerCountryChange() {
|
|
1479
|
+
this._trigger("countrychange");
|
|
1480
|
+
}
|
|
1481
|
+
// format the number as the user types
|
|
1482
|
+
_formatNumberAsYouType() {
|
|
1483
|
+
const val = this._getFullNumber();
|
|
1484
|
+
const result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
|
|
1485
|
+
// if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
|
|
1486
|
+
const {
|
|
1487
|
+
dialCode
|
|
1488
|
+
} = this.selectedCountryData;
|
|
1489
|
+
if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes(`+${dialCode}`)) {
|
|
1490
|
+
const afterDialCode = result.split(`+${dialCode}`)[1] || "";
|
|
1491
|
+
return afterDialCode.trim();
|
|
1492
|
+
}
|
|
1493
|
+
return result;
|
|
1494
|
+
}
|
|
1495
|
+
/**************************
|
|
1496
|
+
* SECRET PUBLIC METHODS
|
|
1497
|
+
**************************/
|
|
1498
|
+
// this is called when the geoip call returns
|
|
1499
|
+
handleAutoCountry() {
|
|
1500
|
+
if (this.options.initialCountry === "auto") {
|
|
1501
|
+
// we must set this even if there is an initial val in the input: in case the initial val is
|
|
1502
|
+
// invalid and they delete it - they should see their auto country
|
|
1503
|
+
this.defaultCountry = window.intlTelInputGlobals.autoCountry;
|
|
1504
|
+
// if there's no initial value in the input, then update the flag
|
|
1505
|
+
if (!this.telInput.value) {
|
|
1506
|
+
this.setCountry(this.defaultCountry);
|
|
1507
|
+
}
|
|
1508
|
+
this.resolveAutoCountryPromise();
|
|
1709
1509
|
}
|
|
1710
|
-
}
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
}, {
|
|
1719
|
-
key: "getSelectedCountryData",
|
|
1720
|
-
value: function getSelectedCountryData() {
|
|
1721
|
-
return this.selectedCountryData;
|
|
1722
|
-
}
|
|
1723
|
-
}, {
|
|
1724
|
-
key: "getValidationError",
|
|
1725
|
-
value: function getValidationError() {
|
|
1726
|
-
if (window.intlTelInputUtils) {
|
|
1727
|
-
var iso2 = this.selectedCountryData.iso2;
|
|
1728
|
-
return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
|
|
1729
|
-
}
|
|
1730
|
-
return -99;
|
|
1731
|
-
}
|
|
1732
|
-
}, {
|
|
1733
|
-
key: "isValidNumber",
|
|
1734
|
-
value: function isValidNumber() {
|
|
1735
|
-
var mobileOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
1736
|
-
var val = this._getFullNumber();
|
|
1737
|
-
// return false for any alpha chars
|
|
1738
|
-
if (/(?:[A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16F1-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CA\uA7D0\uA7D1\uA7D3\uA7D5-\uA7D9\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF40\uDF42-\uDF49\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDD00-\uDD23\uDE80-\uDEA9\uDEB0\uDEB1\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\uDF81\uDFB0-\uDFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDD00-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF])/.test(val)) {
|
|
1739
|
-
return false;
|
|
1740
|
-
}
|
|
1741
|
-
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
|
|
1742
|
-
}
|
|
1743
|
-
}, {
|
|
1744
|
-
key: "isValidNumberPrecise",
|
|
1745
|
-
value: function isValidNumberPrecise() {
|
|
1746
|
-
var val = this._getFullNumber();
|
|
1747
|
-
// return false for any alpha chars
|
|
1748
|
-
if (/(?:[A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16F1-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CA\uA7D0\uA7D1\uA7D3\uA7D5-\uA7D9\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF40\uDF42-\uDF49\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDD00-\uDD23\uDE80-\uDEA9\uDEB0\uDEB1\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\uDF81\uDFB0-\uDFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDD00-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF])/.test(val)) {
|
|
1749
|
-
return false;
|
|
1510
|
+
}
|
|
1511
|
+
// this is called when the utils request completes
|
|
1512
|
+
handleUtils() {
|
|
1513
|
+
// if the request was successful
|
|
1514
|
+
if (window.intlTelInputUtils) {
|
|
1515
|
+
// if there's an initial value in the input, then format it
|
|
1516
|
+
if (this.telInput.value) {
|
|
1517
|
+
this._updateValFromNumber(this.telInput.value);
|
|
1750
1518
|
}
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
key: "setCountry",
|
|
1755
|
-
value: function setCountry(iso2) {
|
|
1756
|
-
var iso2Lower = iso2.toLowerCase();
|
|
1757
|
-
// check if already selected
|
|
1758
|
-
if (this.selectedCountryData.iso2 !== iso2Lower) {
|
|
1759
|
-
this._setCountry(iso2Lower);
|
|
1760
|
-
this._updateDialCode(this.selectedCountryData.dialCode);
|
|
1761
|
-
this._triggerCountryChange();
|
|
1519
|
+
if (this.selectedCountryData.iso2) {
|
|
1520
|
+
this._updatePlaceholder();
|
|
1521
|
+
this._updateMaxLength();
|
|
1762
1522
|
}
|
|
1763
1523
|
}
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1524
|
+
this.resolveUtilsScriptPromise();
|
|
1525
|
+
}
|
|
1526
|
+
/********************
|
|
1527
|
+
* PUBLIC METHODS
|
|
1528
|
+
********************/
|
|
1529
|
+
// remove plugin
|
|
1530
|
+
destroy() {
|
|
1531
|
+
if (this.options.allowDropdown) {
|
|
1532
|
+
// make sure the dropdown is closed (and unbind listeners)
|
|
1533
|
+
this._closeDropdown();
|
|
1534
|
+
this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
|
|
1535
|
+
this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
|
|
1536
|
+
// label click hack
|
|
1537
|
+
const label = this.telInput.closest("label");
|
|
1538
|
+
if (label) {
|
|
1539
|
+
label.removeEventListener("click", this._handleLabelClick);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
// unbind hiddenInput listeners
|
|
1543
|
+
const {
|
|
1544
|
+
form
|
|
1545
|
+
} = this.telInput;
|
|
1546
|
+
if (this._handleHiddenInputSubmit && form) {
|
|
1547
|
+
form.removeEventListener("submit", this._handleHiddenInputSubmit);
|
|
1548
|
+
}
|
|
1549
|
+
// unbind key events, and cut/paste events
|
|
1550
|
+
this.telInput.removeEventListener("input", this._handleInputEvent);
|
|
1551
|
+
if (this._handleKeydownEvent) {
|
|
1552
|
+
this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
|
|
1553
|
+
}
|
|
1554
|
+
// remove attribute of id instance: data-intl-tel-input-id
|
|
1555
|
+
this.telInput.removeAttribute("data-intl-tel-input-id");
|
|
1556
|
+
// remove markup (but leave the original input)
|
|
1557
|
+
const wrapper = this.telInput.parentNode;
|
|
1558
|
+
wrapper.parentNode.insertBefore(this.telInput, wrapper);
|
|
1559
|
+
wrapper.parentNode.removeChild(wrapper);
|
|
1560
|
+
delete window.intlTelInputGlobals.instances[this.id];
|
|
1561
|
+
}
|
|
1562
|
+
// get the extension from the current number
|
|
1563
|
+
getExtension() {
|
|
1564
|
+
if (window.intlTelInputUtils) {
|
|
1565
|
+
return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
|
|
1566
|
+
}
|
|
1567
|
+
return "";
|
|
1568
|
+
}
|
|
1569
|
+
// format the number to the given format
|
|
1570
|
+
getNumber(format) {
|
|
1571
|
+
if (window.intlTelInputUtils) {
|
|
1572
|
+
const {
|
|
1573
|
+
iso2
|
|
1574
|
+
} = this.selectedCountryData;
|
|
1575
|
+
return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
|
|
1576
|
+
}
|
|
1577
|
+
return "";
|
|
1578
|
+
}
|
|
1579
|
+
// get the type of the entered number e.g. landline/mobile
|
|
1580
|
+
getNumberType() {
|
|
1581
|
+
if (window.intlTelInputUtils) {
|
|
1582
|
+
return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
|
|
1583
|
+
}
|
|
1584
|
+
return -99;
|
|
1585
|
+
}
|
|
1586
|
+
// get the country data for the currently selected flag
|
|
1587
|
+
getSelectedCountryData() {
|
|
1588
|
+
return this.selectedCountryData;
|
|
1589
|
+
}
|
|
1590
|
+
// get the validation error
|
|
1591
|
+
getValidationError() {
|
|
1592
|
+
if (window.intlTelInputUtils) {
|
|
1593
|
+
const {
|
|
1594
|
+
iso2
|
|
1595
|
+
} = this.selectedCountryData;
|
|
1596
|
+
return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
|
|
1597
|
+
}
|
|
1598
|
+
return -99;
|
|
1599
|
+
}
|
|
1600
|
+
// validate the input val - assumes the global function isPossibleNumber (from utilsScript)
|
|
1601
|
+
isValidNumber(mobileOnly = true) {
|
|
1602
|
+
const val = this._getFullNumber();
|
|
1603
|
+
// return false for any alpha chars
|
|
1604
|
+
if (/\p{L}/u.test(val)) {
|
|
1605
|
+
return false;
|
|
1774
1606
|
}
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1607
|
+
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
|
|
1608
|
+
}
|
|
1609
|
+
// validate the input val (precise) - assumes the global function isValidNumber (from utilsScript)
|
|
1610
|
+
isValidNumberPrecise() {
|
|
1611
|
+
const val = this._getFullNumber();
|
|
1612
|
+
// return false for any alpha chars
|
|
1613
|
+
if (/\p{L}/u.test(val)) {
|
|
1614
|
+
return false;
|
|
1615
|
+
}
|
|
1616
|
+
return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
|
|
1617
|
+
}
|
|
1618
|
+
// update the selected flag, and update the input val accordingly
|
|
1619
|
+
setCountry(iso2) {
|
|
1620
|
+
const iso2Lower = iso2.toLowerCase();
|
|
1621
|
+
// check if already selected
|
|
1622
|
+
if (this.selectedCountryData.iso2 !== iso2Lower) {
|
|
1623
|
+
this._setCountry(iso2Lower);
|
|
1624
|
+
this._updateDialCode(this.selectedCountryData.dialCode);
|
|
1625
|
+
this._triggerCountryChange();
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
// set the input value and update the flag
|
|
1629
|
+
setNumber(number) {
|
|
1630
|
+
// we must update the flag first, which updates this.selectedCountryData, which is used for
|
|
1631
|
+
// formatting the number before displaying it
|
|
1632
|
+
const flagChanged = this._updateFlagFromNumber(number);
|
|
1633
|
+
this._updateValFromNumber(number);
|
|
1634
|
+
if (flagChanged) {
|
|
1635
|
+
this._triggerCountryChange();
|
|
1780
1636
|
}
|
|
1781
|
-
}
|
|
1782
|
-
|
|
1783
|
-
|
|
1637
|
+
}
|
|
1638
|
+
// set the placeholder number typ
|
|
1639
|
+
setPlaceholderNumberType(type) {
|
|
1640
|
+
this.options.placeholderNumberType = type;
|
|
1641
|
+
this._updatePlaceholder();
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1784
1644
|
/********************
|
|
1785
1645
|
* STATIC METHODS
|
|
1786
1646
|
********************/
|
|
1787
1647
|
// get the country data object
|
|
1788
|
-
intlTelInputGlobals.getCountryData =
|
|
1789
|
-
return allCountries;
|
|
1790
|
-
};
|
|
1648
|
+
intlTelInputGlobals.getCountryData = () => allCountries;
|
|
1791
1649
|
// inject a <script> element to load utils.js
|
|
1792
|
-
|
|
1650
|
+
const injectScript = (path, handleSuccess, handleFailure) => {
|
|
1793
1651
|
// inject a new script element into the page
|
|
1794
|
-
|
|
1795
|
-
script.onload =
|
|
1652
|
+
const script = document.createElement("script");
|
|
1653
|
+
script.onload = () => {
|
|
1796
1654
|
forEachInstance("handleUtils");
|
|
1797
1655
|
if (handleSuccess) {
|
|
1798
1656
|
handleSuccess();
|
|
1799
1657
|
}
|
|
1800
1658
|
};
|
|
1801
|
-
script.onerror =
|
|
1659
|
+
script.onerror = () => {
|
|
1802
1660
|
forEachInstance("rejectUtilsScriptPromise");
|
|
1803
1661
|
if (handleFailure) {
|
|
1804
1662
|
handleFailure();
|
|
@@ -1810,7 +1668,7 @@
|
|
|
1810
1668
|
document.body.appendChild(script);
|
|
1811
1669
|
};
|
|
1812
1670
|
// load the utils script
|
|
1813
|
-
intlTelInputGlobals.loadUtils =
|
|
1671
|
+
intlTelInputGlobals.loadUtils = path => {
|
|
1814
1672
|
// 2 options:
|
|
1815
1673
|
// 1) not already started loading (start)
|
|
1816
1674
|
// 2) already started loading (do nothing - just wait for the onload callback to fire, which will
|
|
@@ -1818,16 +1676,14 @@
|
|
|
1818
1676
|
if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) {
|
|
1819
1677
|
// only do this once
|
|
1820
1678
|
window.intlTelInputGlobals.startedLoadingUtilsScript = true;
|
|
1821
|
-
return new Promise(
|
|
1822
|
-
return injectScript(path, resolve, reject);
|
|
1823
|
-
});
|
|
1679
|
+
return new Promise((resolve, reject) => injectScript(path, resolve, reject));
|
|
1824
1680
|
}
|
|
1825
1681
|
return null;
|
|
1826
1682
|
};
|
|
1827
1683
|
// default options
|
|
1828
1684
|
intlTelInputGlobals.defaults = defaults;
|
|
1829
1685
|
// version
|
|
1830
|
-
intlTelInputGlobals.version = "20.
|
|
1686
|
+
intlTelInputGlobals.version = "20.3.0";
|
|
1831
1687
|
var pluginName = "intlTelInput";
|
|
1832
1688
|
// A really lightweight plugin wrapper around the constructor,
|
|
1833
1689
|
// preventing against multiple instantiations
|