intl_phone_picker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/intl_phone_picker.gemspec +29 -0
- data/lib/intl_phone_picker.rb +6 -0
- data/lib/intl_phone_picker/version.rb +3 -0
- data/vendor/assets/images/flags.png +0 -0
- data/vendor/assets/javascripts/intlTelInput.js +1007 -0
- data/vendor/assets/javascripts/intl_phone_picker.js +8 -0
- data/vendor/assets/javascripts/libphonenumber/utils.js +480 -0
- data/vendor/assets/stylesheets/intlTelInput.css +1 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 318e1c5b9ab34da80c63ac464f0238904d9631e9
|
4
|
+
data.tar.gz: abcfe47d4c66a86b5be532e28b187707b96b2420
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d76cfdd5d7f62c53b45d98850d068a3224dc7e7adc0e3f82aae9f3a00157063bc9091160f2fd1f4cdc98a7e832f4df3e1192a29b33705819cbc92028f5865a0
|
7
|
+
data.tar.gz: 43ded395b15f12a51f906f77de61f38c243d825baa0548e216671fb4a10174826ddd12b78b5fa705df47ded2aee138656ee48648858257ef3eda4b66657f2203
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Fabien Dobat
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
9
|
+
in the Software without restriction, including without limitation the rights
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
12
|
+
furnished to do so, subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
15
|
+
all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
## Sources
|
2
|
+
|
3
|
+
- Javascript based library : https://github.com/Bluefieldscom/intl-tel-input
|
4
|
+
- Ruby based gem : https://github.com/ispyropoulos/intl-tel-input-rails
|
5
|
+
- French language generator : https://github.com/harold-s/harold-s/tree/master/intl-tel-input-french-translation
|
6
|
+
|
7
|
+
# IntlPhonePicker
|
8
|
+
|
9
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/intl_phone_picker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
10
|
+
|
11
|
+
TODO: Delete this and the text above, and describe your gem
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'intl_phone_picker'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install intl_phone_picker
|
28
|
+
|
29
|
+
To use javascript and stylesheet libraries you need to add :
|
30
|
+
|
31
|
+
- In application default stylesheet file :
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
//= require intlTelInput
|
35
|
+
```
|
36
|
+
|
37
|
+
- In application default javascript file :
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
//= require intlTelInput
|
41
|
+
```
|
42
|
+
|
43
|
+
- If you want to enable it on all tel fields by default, add :
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
//= require intl_phone_picker
|
47
|
+
```
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
TODO: Write usage instructions here
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
56
|
+
|
57
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it ( https://github.com/[my-github-username]/intl_phone_picker/fork )
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "intl_phone_picker"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'intl_phone_picker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "intl_phone_picker"
|
8
|
+
spec.version = "0.0.1"
|
9
|
+
spec.authors = ["Fabien Dobat\n\n"]
|
10
|
+
spec.email = ["fabien.dobat@seniormedia.fr"]
|
11
|
+
|
12
|
+
if spec.respond_to?(:metadata)
|
13
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
14
|
+
end
|
15
|
+
|
16
|
+
spec.summary = %q{Write a short summary, because Rubygems requires one.}
|
17
|
+
spec.description = %q{Write a longer description or delete this line.}
|
18
|
+
# spec.homepage = "Put your gem's website or public repo URL here."
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files`.split("\n")
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_dependency "railties", "~> 4.2.1"
|
29
|
+
end
|
Binary file
|
@@ -0,0 +1,1007 @@
|
|
1
|
+
/*
|
2
|
+
International Telephone Input v4.0.0
|
3
|
+
https://github.com/Bluefieldscom/intl-tel-input.git
|
4
|
+
*/
|
5
|
+
// wrap in UMD - see https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
|
6
|
+
(function(factory) {
|
7
|
+
if (typeof define === "function" && define.amd) {
|
8
|
+
define([ "jquery" ], function($) {
|
9
|
+
factory($, window, document);
|
10
|
+
});
|
11
|
+
} else {
|
12
|
+
factory(jQuery, window, document);
|
13
|
+
}
|
14
|
+
})(function($, window, document, undefined) {
|
15
|
+
"use strict";
|
16
|
+
var pluginName = "intlTelInput", id = 1, // give each instance it's own id for namespaced event handling
|
17
|
+
defaults = {
|
18
|
+
// automatically format the number according to the selected country
|
19
|
+
autoFormat: true,
|
20
|
+
// if there is just a dial code in the input: remove it on blur, and re-add it on focus
|
21
|
+
autoHideDialCode: true,
|
22
|
+
// default country
|
23
|
+
defaultCountry: "",
|
24
|
+
// token for ipinfo - required for https or over 1000 daily page views support
|
25
|
+
ipinfoToken: "",
|
26
|
+
// don't insert international dial codes
|
27
|
+
nationalMode: true,
|
28
|
+
// number type to use for placeholders
|
29
|
+
numberType: "MOBILE",
|
30
|
+
// display only these countries
|
31
|
+
onlyCountries: [],
|
32
|
+
// the countries at the top of the list. defaults to united states and united kingdom
|
33
|
+
preferredCountries: [ "us", "gb" ],
|
34
|
+
// stop the user from typing invalid numbers
|
35
|
+
preventInvalidNumbers: false,
|
36
|
+
// specify the path to the libphonenumber script to enable validation/formatting
|
37
|
+
utilsScript: ""
|
38
|
+
}, keys = {
|
39
|
+
UP: 38,
|
40
|
+
DOWN: 40,
|
41
|
+
ENTER: 13,
|
42
|
+
ESC: 27,
|
43
|
+
PLUS: 43,
|
44
|
+
A: 65,
|
45
|
+
Z: 90,
|
46
|
+
ZERO: 48,
|
47
|
+
NINE: 57,
|
48
|
+
SPACE: 32,
|
49
|
+
BSPACE: 8,
|
50
|
+
DEL: 46,
|
51
|
+
CTRL: 17,
|
52
|
+
CMD1: 91,
|
53
|
+
// Chrome
|
54
|
+
CMD2: 224
|
55
|
+
}, windowLoaded = false;
|
56
|
+
// keep track of if the window.load event has fired as impossible to check after the fact
|
57
|
+
$(window).load(function() {
|
58
|
+
windowLoaded = true;
|
59
|
+
});
|
60
|
+
function Plugin(element, options) {
|
61
|
+
this.element = element;
|
62
|
+
this.options = $.extend({}, defaults, options);
|
63
|
+
this._defaults = defaults;
|
64
|
+
// event namespace
|
65
|
+
this.ns = "." + pluginName + id++;
|
66
|
+
// Chrome, FF, Safari, IE9+
|
67
|
+
this.isGoodBrowser = Boolean(element.setSelectionRange);
|
68
|
+
this.hadInitialPlaceholder = Boolean($(element).attr("placeholder"));
|
69
|
+
this._name = pluginName;
|
70
|
+
this.init();
|
71
|
+
}
|
72
|
+
Plugin.prototype = {
|
73
|
+
init: function() {
|
74
|
+
var that = this;
|
75
|
+
// if defaultCountry is set to "auto", we must do a lookup first
|
76
|
+
if (this.options.defaultCountry == "auto") {
|
77
|
+
// reset this in case lookup fails
|
78
|
+
this.options.defaultCountry = "";
|
79
|
+
var ipinfoURL = "//ipinfo.io";
|
80
|
+
if (this.options.ipinfoToken) {
|
81
|
+
ipinfoURL += "?token=" + this.options.ipinfoToken;
|
82
|
+
}
|
83
|
+
$.get(ipinfoURL, function(response) {
|
84
|
+
if (response && response.country) {
|
85
|
+
that.options.defaultCountry = response.country.toLowerCase();
|
86
|
+
}
|
87
|
+
}, "jsonp").always(function() {
|
88
|
+
that._ready();
|
89
|
+
});
|
90
|
+
} else {
|
91
|
+
this._ready();
|
92
|
+
}
|
93
|
+
},
|
94
|
+
_ready: function() {
|
95
|
+
// if in nationalMode, disable options relating to dial codes
|
96
|
+
if (this.options.nationalMode) {
|
97
|
+
this.options.autoHideDialCode = false;
|
98
|
+
}
|
99
|
+
// IE Mobile doesn't support the keypress event (see issue 68) which makes autoFormat impossible
|
100
|
+
if (navigator.userAgent.match(/IEMobile/i)) {
|
101
|
+
this.options.autoFormat = false;
|
102
|
+
}
|
103
|
+
// process all the data: onlyCountries, preferredCountries etc
|
104
|
+
this._processCountryData();
|
105
|
+
// generate the markup
|
106
|
+
this._generateMarkup();
|
107
|
+
// set the initial state of the input value and the selected flag
|
108
|
+
this._setInitialState();
|
109
|
+
// start all of the event listeners: autoHideDialCode, input keydown, selectedFlag click
|
110
|
+
this._initListeners();
|
111
|
+
},
|
112
|
+
/********************
|
113
|
+
* PRIVATE METHODS
|
114
|
+
********************/
|
115
|
+
// prepare all of the country data, including onlyCountries and preferredCountries options
|
116
|
+
_processCountryData: function() {
|
117
|
+
// set the instances country data objects
|
118
|
+
this._setInstanceCountryData();
|
119
|
+
// set the preferredCountries property
|
120
|
+
this._setPreferredCountries();
|
121
|
+
},
|
122
|
+
// add a country code to this.countryCodes
|
123
|
+
_addCountryCode: function(iso2, dialCode, priority) {
|
124
|
+
if (!(dialCode in this.countryCodes)) {
|
125
|
+
this.countryCodes[dialCode] = [];
|
126
|
+
}
|
127
|
+
var index = priority || 0;
|
128
|
+
this.countryCodes[dialCode][index] = iso2;
|
129
|
+
},
|
130
|
+
// process onlyCountries array if present, and generate the countryCodes map
|
131
|
+
_setInstanceCountryData: function() {
|
132
|
+
var i;
|
133
|
+
// process onlyCountries option
|
134
|
+
if (this.options.onlyCountries.length) {
|
135
|
+
this.countries = [];
|
136
|
+
for (i = 0; i < allCountries.length; i++) {
|
137
|
+
if ($.inArray(allCountries[i].iso2, this.options.onlyCountries) != -1) {
|
138
|
+
this.countries.push(allCountries[i]);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
} else {
|
142
|
+
this.countries = allCountries;
|
143
|
+
}
|
144
|
+
// generate countryCodes map
|
145
|
+
this.countryCodes = {};
|
146
|
+
for (i = 0; i < this.countries.length; i++) {
|
147
|
+
var c = this.countries[i];
|
148
|
+
this._addCountryCode(c.iso2, c.dialCode, c.priority);
|
149
|
+
// area codes
|
150
|
+
if (c.areaCodes) {
|
151
|
+
for (var j = 0; j < c.areaCodes.length; j++) {
|
152
|
+
// full dial code is country code + dial code
|
153
|
+
this._addCountryCode(c.iso2, c.dialCode + c.areaCodes[j]);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
},
|
158
|
+
// process preferred countries - iterate through the preferences,
|
159
|
+
// fetching the country data for each one
|
160
|
+
_setPreferredCountries: function() {
|
161
|
+
this.preferredCountries = [];
|
162
|
+
for (var i = 0; i < this.options.preferredCountries.length; i++) {
|
163
|
+
var countryCode = this.options.preferredCountries[i], countryData = this._getCountryData(countryCode, false, true);
|
164
|
+
if (countryData) {
|
165
|
+
this.preferredCountries.push(countryData);
|
166
|
+
}
|
167
|
+
}
|
168
|
+
},
|
169
|
+
// generate all of the markup for the plugin: the selected flag overlay, and the dropdown
|
170
|
+
_generateMarkup: function() {
|
171
|
+
// telephone input
|
172
|
+
this.telInput = $(this.element);
|
173
|
+
// prevent autocomplete as there's no safe, cross-browser event we can react to, so it can easily put the plugin in an inconsistent state e.g. the wrong flag selected for the autocompleted number, which on submit could mean the wrong number is saved (esp in nationalMode)
|
174
|
+
this.telInput.attr("autocomplete", "off");
|
175
|
+
// containers (mostly for positioning)
|
176
|
+
this.telInput.wrap($("<div>", {
|
177
|
+
"class": "intl-tel-input"
|
178
|
+
}));
|
179
|
+
var flagsContainer = $("<div>", {
|
180
|
+
"class": "flag-dropdown"
|
181
|
+
}).insertAfter(this.telInput);
|
182
|
+
// currently selected flag (displayed to left of input)
|
183
|
+
var selectedFlag = $("<div>", {
|
184
|
+
"class": "selected-flag"
|
185
|
+
}).appendTo(flagsContainer);
|
186
|
+
this.selectedFlagInner = $("<div>", {
|
187
|
+
"class": "iti-flag"
|
188
|
+
}).appendTo(selectedFlag);
|
189
|
+
// CSS triangle
|
190
|
+
$("<div>", {
|
191
|
+
"class": "arrow"
|
192
|
+
}).appendTo(this.selectedFlagInner);
|
193
|
+
// country list contains: preferred countries, then divider, then all countries
|
194
|
+
this.countryList = $("<ul>", {
|
195
|
+
"class": "country-list v-hide"
|
196
|
+
}).appendTo(flagsContainer);
|
197
|
+
if (this.preferredCountries.length) {
|
198
|
+
this._appendListItems(this.preferredCountries, "preferred");
|
199
|
+
$("<li>", {
|
200
|
+
"class": "divider"
|
201
|
+
}).appendTo(this.countryList);
|
202
|
+
}
|
203
|
+
this._appendListItems(this.countries, "");
|
204
|
+
// now we can grab the dropdown height, and hide it properly
|
205
|
+
this.dropdownHeight = this.countryList.outerHeight();
|
206
|
+
this.countryList.removeClass("v-hide").addClass("hide");
|
207
|
+
// on small screens make the dropdown the same width as the input
|
208
|
+
if (window.innerWidth < 500) {
|
209
|
+
this.countryList.outerWidth(this.telInput.outerWidth());
|
210
|
+
}
|
211
|
+
// this is useful in lots of places
|
212
|
+
this.countryListItems = this.countryList.children(".country");
|
213
|
+
},
|
214
|
+
// add a country <li> to the countryList <ul> container
|
215
|
+
_appendListItems: function(countries, className) {
|
216
|
+
// we create so many DOM elements, I decided it was faster to build a temp string
|
217
|
+
// and then add everything to the DOM in one go at the end
|
218
|
+
var tmp = "";
|
219
|
+
// for each country
|
220
|
+
for (var i = 0; i < countries.length; i++) {
|
221
|
+
var c = countries[i];
|
222
|
+
// open the list item
|
223
|
+
tmp += "<li class='country " + className + "' data-dial-code='" + c.dialCode + "' data-country-code='" + c.iso2 + "'>";
|
224
|
+
// add the flag
|
225
|
+
tmp += "<div class='iti-flag " + c.iso2 + "'></div>";
|
226
|
+
// and the country name and dial code
|
227
|
+
tmp += "<span class='country-name'>" + c.name + "</span>";
|
228
|
+
tmp += "<span class='dial-code'>+" + c.dialCode + "</span>";
|
229
|
+
// close the list item
|
230
|
+
tmp += "</li>";
|
231
|
+
}
|
232
|
+
this.countryList.append(tmp);
|
233
|
+
},
|
234
|
+
// set the initial state of the input value and the selected flag
|
235
|
+
_setInitialState: function() {
|
236
|
+
var val = this.telInput.val();
|
237
|
+
// if there is a number, and it's valid, we can go ahead and set the flag, else fall back to default
|
238
|
+
if (this._getDialCode(val)) {
|
239
|
+
this._updateFlagFromNumber(val);
|
240
|
+
} else {
|
241
|
+
var defaultCountry;
|
242
|
+
// check the defaultCountry option, else fall back to the first in the list
|
243
|
+
if (this.options.defaultCountry) {
|
244
|
+
defaultCountry = this._getCountryData(this.options.defaultCountry, false, false);
|
245
|
+
} else {
|
246
|
+
defaultCountry = this.preferredCountries.length ? this.preferredCountries[0] : this.countries[0];
|
247
|
+
}
|
248
|
+
this._selectFlag(defaultCountry.iso2);
|
249
|
+
// if empty, insert the default dial code (this function will check !nationalMode and !autoHideDialCode)
|
250
|
+
if (!val) {
|
251
|
+
this._updateDialCode(defaultCountry.dialCode, false);
|
252
|
+
}
|
253
|
+
}
|
254
|
+
// format
|
255
|
+
if (val) {
|
256
|
+
// this wont be run after _updateDialCode as that's only called if no val
|
257
|
+
this._updateVal(val, false);
|
258
|
+
}
|
259
|
+
},
|
260
|
+
// initialise the main event listeners: input keyup, and click selected flag
|
261
|
+
_initListeners: function() {
|
262
|
+
var that = this;
|
263
|
+
this._initKeyListeners();
|
264
|
+
// autoFormat prevents the change event from firing, so we need to check for changes between focus and blur in order to manually trigger it
|
265
|
+
if (this.options.autoHideDialCode || this.options.autoFormat) {
|
266
|
+
this._initFocusListeners();
|
267
|
+
}
|
268
|
+
// hack for input nested inside label: clicking the selected-flag to open the dropdown would then automatically trigger a 2nd click on the input which would close it again
|
269
|
+
var label = this.telInput.closest("label");
|
270
|
+
if (label.length) {
|
271
|
+
label.on("click" + this.ns, function(e) {
|
272
|
+
// if the dropdown is closed, then focus the input, else ignore the click
|
273
|
+
if (that.countryList.hasClass("hide")) {
|
274
|
+
that.telInput.focus();
|
275
|
+
} else {
|
276
|
+
e.preventDefault();
|
277
|
+
}
|
278
|
+
});
|
279
|
+
}
|
280
|
+
// toggle country dropdown on click
|
281
|
+
var selectedFlag = this.selectedFlagInner.parent();
|
282
|
+
selectedFlag.on("click" + this.ns, function(e) {
|
283
|
+
// only intercept this event if we're opening the dropdown
|
284
|
+
// else let it bubble up to the top ("click-off-to-close" listener)
|
285
|
+
// we cannot just stopPropagation as it may be needed to close another instance
|
286
|
+
if (that.countryList.hasClass("hide") && !that.telInput.prop("disabled") && !that.telInput.prop("readonly")) {
|
287
|
+
that._showDropdown();
|
288
|
+
}
|
289
|
+
});
|
290
|
+
// if the user has specified the path to the utils script, fetch it on window.load
|
291
|
+
if (this.options.utilsScript) {
|
292
|
+
// if the plugin is being initialised after the window.load event has already been fired
|
293
|
+
if (windowLoaded) {
|
294
|
+
this.loadUtils();
|
295
|
+
} else {
|
296
|
+
// wait until the load event so we don't block any other requests e.g. the flags image
|
297
|
+
$(window).load(function() {
|
298
|
+
that.loadUtils();
|
299
|
+
});
|
300
|
+
}
|
301
|
+
}
|
302
|
+
},
|
303
|
+
_initKeyListeners: function() {
|
304
|
+
var that = this;
|
305
|
+
if (this.options.autoFormat) {
|
306
|
+
// format number and update flag on keypress
|
307
|
+
// use keypress event as we want to ignore all input except for a select few keys,
|
308
|
+
// but we dont want to ignore the navigation keys like the arrows etc.
|
309
|
+
// NOTE: no point in refactoring this to only bind these listeners on focus/blur because then you would need to have those 2 listeners running the whole time anyway...
|
310
|
+
this.telInput.on("keypress" + this.ns, function(e) {
|
311
|
+
// 32 is space, and after that it's all chars (not meta/nav keys)
|
312
|
+
// this fix is needed for Firefox, which triggers keypress event for some meta/nav keys
|
313
|
+
// Update: also ignore if this is a metaKey e.g. FF and Safari trigger keypress on the v of Ctrl+v
|
314
|
+
// Update: also check that we have utils before we do any autoFormat stuff
|
315
|
+
if (e.which >= keys.SPACE && !e.metaKey && window.intlTelInputUtils && !that.telInput.prop("readonly")) {
|
316
|
+
e.preventDefault();
|
317
|
+
// allowed keys are just numeric keys and plus
|
318
|
+
// we must allow plus for the case where the user does select-all and then hits plus to start typing a new number. we could refine this logic to first check that the selection contains a plus, but that wont work in old browsers, and I think it's overkill anyway
|
319
|
+
var isAllowedKey = e.which >= keys.ZERO && e.which <= keys.NINE || e.which == keys.PLUS, input = that.telInput[0], noSelection = that.isGoodBrowser && input.selectionStart == input.selectionEnd, max = that.telInput.attr("maxlength"), val = that.telInput.val(), // assumes that if max exists, it is >0
|
320
|
+
isBelowMax = max ? val.length < max : true;
|
321
|
+
// first: ensure we dont go over maxlength. we must do this here to prevent adding digits in the middle of the number
|
322
|
+
// still reformat even if not an allowed key as they could by typing a formatting char, but ignore if there's a selection as doesn't make sense to replace selection with illegal char and then immediately remove it
|
323
|
+
if (isBelowMax && (isAllowedKey || noSelection)) {
|
324
|
+
var newChar = isAllowedKey ? String.fromCharCode(e.which) : null;
|
325
|
+
that._handleInputKey(newChar, true);
|
326
|
+
// if something has changed, trigger the input event (which was otherwised squashed by the preventDefault)
|
327
|
+
if (val != that.telInput.val()) {
|
328
|
+
that.telInput.trigger("input");
|
329
|
+
}
|
330
|
+
}
|
331
|
+
if (!isAllowedKey) {
|
332
|
+
that.telInput.trigger("invalidkey");
|
333
|
+
}
|
334
|
+
}
|
335
|
+
});
|
336
|
+
}
|
337
|
+
// handle keyup event
|
338
|
+
// for autoFormat: we use keyup to catch cut/paste events and also delete events (after the fact)
|
339
|
+
this.telInput.on("keyup" + this.ns, function(e) {
|
340
|
+
// the "enter" key event from selecting a dropdown item is triggered here on the input, because the document.keydown handler that initially handles that event triggers a focus on the input, and so the keyup for that same key event gets triggered here. weird, but just make sure we dont bother doing any re-formatting in this case (we've already done preventDefault in the keydown handler, so it wont actually submit the form or anything).
|
341
|
+
// ALSO: ignore keyup if readonly
|
342
|
+
if (e.which == keys.ENTER || that.telInput.prop("readonly")) {} else if (that.options.autoFormat && window.intlTelInputUtils) {
|
343
|
+
var isCtrl = e.which == keys.CTRL || e.which == keys.CMD1 || e.which == keys.CMD2, input = that.telInput[0], // noSelection defaults to false for bad browsers, else would be reformatting on all ctrl keys e.g. select-all/copy
|
344
|
+
noSelection = that.isGoodBrowser && input.selectionStart == input.selectionEnd, // cursorAtEnd defaults to false for bad browsers else they would never get a reformat on delete
|
345
|
+
cursorAtEnd = that.isGoodBrowser && input.selectionStart == that.telInput.val().length;
|
346
|
+
// if delete in the middle: reformat with no suffix (no need to reformat if delete at end)
|
347
|
+
// if backspace: reformat with no suffix (need to reformat if at end to remove any lingering suffix - this is a feature)
|
348
|
+
// if ctrl and no selection (i.e. could have just been a paste): reformat (if cursorAtEnd: add suffix)
|
349
|
+
if (e.which == keys.DEL && !cursorAtEnd || e.which == keys.BSPACE || isCtrl && noSelection) {
|
350
|
+
// important to remember never to add suffix on any delete key as can fuck up in ie8 so you can never delete a formatting char at the end
|
351
|
+
that._handleInputKey(null, isCtrl && cursorAtEnd);
|
352
|
+
}
|
353
|
+
// prevent deleting the plus (if not in nationalMode)
|
354
|
+
if (!that.options.nationalMode) {
|
355
|
+
var val = that.telInput.val();
|
356
|
+
if (val.substr(0, 1) != "+") {
|
357
|
+
// newCursorPos is current pos + 1 to account for the plus we are about to add
|
358
|
+
var newCursorPos = that.isGoodBrowser ? input.selectionStart + 1 : 0;
|
359
|
+
that.telInput.val("+" + val);
|
360
|
+
if (that.isGoodBrowser) {
|
361
|
+
input.setSelectionRange(newCursorPos, newCursorPos);
|
362
|
+
}
|
363
|
+
}
|
364
|
+
}
|
365
|
+
} else {
|
366
|
+
// if no autoFormat, just update flag
|
367
|
+
that._updateFlagFromNumber(that.telInput.val());
|
368
|
+
}
|
369
|
+
});
|
370
|
+
},
|
371
|
+
// when autoFormat is enabled: handle various key events on the input: the 2 main situations are 1) adding a new number character, which will replace any selection, reformat, and preserve the cursor position. and 2) reformatting on backspace, or paste event (etc)
|
372
|
+
_handleInputKey: function(newNumericChar, addSuffix) {
|
373
|
+
var val = this.telInput.val(), numericBefore = this._getNumeric(val), originalLeftChar, // raw DOM element
|
374
|
+
input = this.telInput[0], digitsOnRight = 0;
|
375
|
+
if (this.isGoodBrowser) {
|
376
|
+
// cursor strategy: maintain the number of digits on the right. we use the right instead of the left so that A) we dont have to account for the new digit (or digits if paste event), and B) we're always on the right side of formatting suffixes
|
377
|
+
digitsOnRight = this._getDigitsOnRight(val, input.selectionEnd);
|
378
|
+
// if handling a new number character: insert it in the right place
|
379
|
+
if (newNumericChar) {
|
380
|
+
// replace any selection they may have made with the new char
|
381
|
+
val = val.substr(0, input.selectionStart) + newNumericChar + val.substring(input.selectionEnd, val.length);
|
382
|
+
} else {
|
383
|
+
// here we're not handling a new char, we're just doing a re-format (e.g. on delete/backspace/paste, after the fact), but we still need to maintain the cursor position. so make note of the char on the left, and then after the re-format, we'll count in the same number of digits from the right, and then keep going through any formatting chars until we hit the same left char that we had before.
|
384
|
+
originalLeftChar = val.charAt(input.selectionStart - 1);
|
385
|
+
}
|
386
|
+
} else if (newNumericChar) {
|
387
|
+
val += newNumericChar;
|
388
|
+
}
|
389
|
+
// update the number and flag
|
390
|
+
this.setNumber(val, addSuffix);
|
391
|
+
val = this.telInput.val();
|
392
|
+
var numericAfter = this._getNumeric(val), numericIsSame = numericBefore == numericAfter;
|
393
|
+
if (this.options.preventInvalidNumbers && newNumericChar) {
|
394
|
+
if (numericIsSame) {
|
395
|
+
// if we're trying to add a new numeric char and the numeric digits haven't changed, then trigger invalid
|
396
|
+
this.telInput.trigger("invalidkey");
|
397
|
+
} else if (numericBefore.length == numericAfter.length) {
|
398
|
+
// preventInvalidNumbers edge case: adding digit in middle of full number, so a digit gets dropped from the end (numeric digits have changed but are same length)
|
399
|
+
digitsOnRight--;
|
400
|
+
}
|
401
|
+
}
|
402
|
+
// update the cursor position
|
403
|
+
if (this.isGoodBrowser) {
|
404
|
+
var newCursor;
|
405
|
+
// if it was at the end, keep it there
|
406
|
+
if (!digitsOnRight) {
|
407
|
+
newCursor = val.length;
|
408
|
+
} else {
|
409
|
+
// else count in the same number of digits from the right
|
410
|
+
newCursor = this._getCursorFromDigitsOnRight(val, digitsOnRight);
|
411
|
+
// but if delete/paste etc, keep going left until hit the same left char as before
|
412
|
+
if (!newNumericChar) {
|
413
|
+
newCursor = this._getCursorFromLeftChar(val, newCursor, originalLeftChar);
|
414
|
+
}
|
415
|
+
}
|
416
|
+
// set the new cursor
|
417
|
+
input.setSelectionRange(newCursor, newCursor);
|
418
|
+
}
|
419
|
+
},
|
420
|
+
// we start from the position in guessCursor, and work our way left until we hit the originalLeftChar or a number to make sure that after reformatting the cursor has the same char on the left in the case of a delete etc
|
421
|
+
_getCursorFromLeftChar: function(val, guessCursor, originalLeftChar) {
|
422
|
+
for (var i = guessCursor; i > 0; i--) {
|
423
|
+
var leftChar = val.charAt(i - 1);
|
424
|
+
if (leftChar == originalLeftChar || $.isNumeric(leftChar)) {
|
425
|
+
return i;
|
426
|
+
}
|
427
|
+
}
|
428
|
+
return 0;
|
429
|
+
},
|
430
|
+
// after a reformat we need to make sure there are still the same number of digits to the right of the cursor
|
431
|
+
_getCursorFromDigitsOnRight: function(val, digitsOnRight) {
|
432
|
+
for (var i = val.length - 1; i >= 0; i--) {
|
433
|
+
if ($.isNumeric(val.charAt(i))) {
|
434
|
+
if (--digitsOnRight === 0) {
|
435
|
+
return i;
|
436
|
+
}
|
437
|
+
}
|
438
|
+
}
|
439
|
+
return 0;
|
440
|
+
},
|
441
|
+
// get the number of numeric digits to the right of the cursor so we can reposition the cursor correctly after the reformat has happened
|
442
|
+
_getDigitsOnRight: function(val, selectionEnd) {
|
443
|
+
var digitsOnRight = 0;
|
444
|
+
for (var i = selectionEnd; i < val.length; i++) {
|
445
|
+
if ($.isNumeric(val.charAt(i))) {
|
446
|
+
digitsOnRight++;
|
447
|
+
}
|
448
|
+
}
|
449
|
+
return digitsOnRight;
|
450
|
+
},
|
451
|
+
// listen for focus and blur
|
452
|
+
_initFocusListeners: function() {
|
453
|
+
var that = this;
|
454
|
+
if (this.options.autoHideDialCode) {
|
455
|
+
// mousedown decides where the cursor goes, so if we're focusing we must preventDefault as we'll be inserting the dial code, and we want the cursor to be at the end no matter where they click
|
456
|
+
this.telInput.on("mousedown" + this.ns, function(e) {
|
457
|
+
if (!that.telInput.is(":focus") && !that.telInput.val()) {
|
458
|
+
e.preventDefault();
|
459
|
+
// but this also cancels the focus, so we must trigger that manually
|
460
|
+
that.telInput.focus();
|
461
|
+
}
|
462
|
+
});
|
463
|
+
}
|
464
|
+
this.telInput.on("focus" + this.ns, function(e) {
|
465
|
+
var value = that.telInput.val();
|
466
|
+
// save this to compare on blur
|
467
|
+
that.telInput.data("focusVal", value);
|
468
|
+
// on focus: if empty, insert the dial code for the currently selected flag
|
469
|
+
if (that.options.autoHideDialCode && !value && !that.telInput.prop("readonly")) {
|
470
|
+
that._updateVal("+" + that.selectedCountryData.dialCode, true);
|
471
|
+
// after auto-inserting a dial code, if the first key they hit is '+' then assume they are entering a new number, so remove the dial code. use keypress instead of keydown because keydown gets triggered for the shift key (required to hit the + key), and instead of keyup because that shows the new '+' before removing the old one
|
472
|
+
that.telInput.one("keypress.plus" + that.ns, function(e) {
|
473
|
+
if (e.which == keys.PLUS) {
|
474
|
+
// if autoFormat is enabled, this key event will have already have been handled by another keypress listener (hence we need to add the "+"). if disabled, it will be handled after this by a keyup listener (hence no need to add the "+").
|
475
|
+
var newVal = that.options.autoFormat && window.intlTelInputUtils ? "+" : "";
|
476
|
+
that.telInput.val(newVal);
|
477
|
+
}
|
478
|
+
});
|
479
|
+
// after tabbing in, make sure the cursor is at the end we must use setTimeout to get outside of the focus handler as it seems the selection happens after that
|
480
|
+
setTimeout(function() {
|
481
|
+
var input = that.telInput[0];
|
482
|
+
if (that.isGoodBrowser) {
|
483
|
+
var len = that.telInput.val().length;
|
484
|
+
input.setSelectionRange(len, len);
|
485
|
+
}
|
486
|
+
});
|
487
|
+
}
|
488
|
+
});
|
489
|
+
this.telInput.on("blur" + this.ns, function() {
|
490
|
+
if (that.options.autoHideDialCode) {
|
491
|
+
// on blur: if just a dial code then remove it
|
492
|
+
var value = that.telInput.val(), startsPlus = value.substr(0, 1) == "+";
|
493
|
+
if (startsPlus) {
|
494
|
+
var numeric = that._getNumeric(value);
|
495
|
+
// if just a plus, or if just a dial code
|
496
|
+
if (!numeric || that.selectedCountryData.dialCode == numeric) {
|
497
|
+
that.telInput.val("");
|
498
|
+
}
|
499
|
+
}
|
500
|
+
// remove the keypress listener we added on focus
|
501
|
+
that.telInput.off("keypress.plus" + that.ns);
|
502
|
+
}
|
503
|
+
// if autoFormat, we must manually trigger change event if value has changed
|
504
|
+
if (that.options.autoFormat && window.intlTelInputUtils && that.telInput.val() != that.telInput.data("focusVal")) {
|
505
|
+
that.telInput.trigger("change");
|
506
|
+
}
|
507
|
+
});
|
508
|
+
},
|
509
|
+
// extract the numeric digits from the given string
|
510
|
+
_getNumeric: function(s) {
|
511
|
+
return s.replace(/\D/g, "");
|
512
|
+
},
|
513
|
+
// show the dropdown
|
514
|
+
_showDropdown: function() {
|
515
|
+
this._setDropdownPosition();
|
516
|
+
// update highlighting and scroll to active list item
|
517
|
+
var activeListItem = this.countryList.children(".active");
|
518
|
+
this._highlightListItem(activeListItem);
|
519
|
+
// show it
|
520
|
+
this.countryList.removeClass("hide");
|
521
|
+
this._scrollTo(activeListItem);
|
522
|
+
// bind all the dropdown-related listeners: mouseover, click, click-off, keydown
|
523
|
+
this._bindDropdownListeners();
|
524
|
+
// update the arrow
|
525
|
+
this.selectedFlagInner.children(".arrow").addClass("up");
|
526
|
+
},
|
527
|
+
// decide where to position dropdown (depends on position within viewport, and scroll)
|
528
|
+
_setDropdownPosition: function() {
|
529
|
+
var inputTop = this.telInput.offset().top, windowTop = $(window).scrollTop(), // dropdownFitsBelow = (dropdownBottom < windowBottom)
|
530
|
+
dropdownFitsBelow = inputTop + this.telInput.outerHeight() + this.dropdownHeight < windowTop + $(window).height(), dropdownFitsAbove = inputTop - this.dropdownHeight > windowTop;
|
531
|
+
// dropdownHeight - 1 for border
|
532
|
+
var cssTop = !dropdownFitsBelow && dropdownFitsAbove ? "-" + (this.dropdownHeight - 1) + "px" : "";
|
533
|
+
this.countryList.css("top", cssTop);
|
534
|
+
},
|
535
|
+
// we only bind dropdown listeners when the dropdown is open
|
536
|
+
_bindDropdownListeners: function() {
|
537
|
+
var that = this;
|
538
|
+
// when mouse over a list item, just highlight that one
|
539
|
+
// we add the class "highlight", so if they hit "enter" we know which one to select
|
540
|
+
this.countryList.on("mouseover" + this.ns, ".country", function(e) {
|
541
|
+
that._highlightListItem($(this));
|
542
|
+
});
|
543
|
+
// listen for country selection
|
544
|
+
this.countryList.on("click" + this.ns, ".country", function(e) {
|
545
|
+
that._selectListItem($(this));
|
546
|
+
});
|
547
|
+
// click off to close
|
548
|
+
// (except when this initial opening click is bubbling up)
|
549
|
+
// we cannot just stopPropagation as it may be needed to close another instance
|
550
|
+
var isOpening = true;
|
551
|
+
$("html").on("click" + this.ns, function(e) {
|
552
|
+
if (!isOpening) {
|
553
|
+
that._closeDropdown();
|
554
|
+
}
|
555
|
+
isOpening = false;
|
556
|
+
});
|
557
|
+
// listen for up/down scrolling, enter to select, or letters to jump to country name.
|
558
|
+
// use keydown as keypress doesn't fire for non-char keys and we want to catch if they
|
559
|
+
// just hit down and hold it to scroll down (no keyup event).
|
560
|
+
// listen on the document because that's where key events are triggered if no input has focus
|
561
|
+
var query = "", queryTimer = null;
|
562
|
+
$(document).on("keydown" + this.ns, function(e) {
|
563
|
+
// prevent down key from scrolling the whole page,
|
564
|
+
// and enter key from submitting a form etc
|
565
|
+
e.preventDefault();
|
566
|
+
if (e.which == keys.UP || e.which == keys.DOWN) {
|
567
|
+
// up and down to navigate
|
568
|
+
that._handleUpDownKey(e.which);
|
569
|
+
} else if (e.which == keys.ENTER) {
|
570
|
+
// enter to select
|
571
|
+
that._handleEnterKey();
|
572
|
+
} else if (e.which == keys.ESC) {
|
573
|
+
// esc to close
|
574
|
+
that._closeDropdown();
|
575
|
+
} else if (e.which >= keys.A && e.which <= keys.Z || e.which == keys.SPACE) {
|
576
|
+
// upper case letters (note: keyup/keydown only return upper case letters)
|
577
|
+
// jump to countries that start with the query string
|
578
|
+
if (queryTimer) {
|
579
|
+
clearTimeout(queryTimer);
|
580
|
+
}
|
581
|
+
query += String.fromCharCode(e.which);
|
582
|
+
that._searchForCountry(query);
|
583
|
+
// if the timer hits 1 second, reset the query
|
584
|
+
queryTimer = setTimeout(function() {
|
585
|
+
query = "";
|
586
|
+
}, 1e3);
|
587
|
+
}
|
588
|
+
});
|
589
|
+
},
|
590
|
+
// highlight the next/prev item in the list (and ensure it is visible)
|
591
|
+
_handleUpDownKey: function(key) {
|
592
|
+
var current = this.countryList.children(".highlight").first();
|
593
|
+
var next = key == keys.UP ? current.prev() : current.next();
|
594
|
+
if (next.length) {
|
595
|
+
// skip the divider
|
596
|
+
if (next.hasClass("divider")) {
|
597
|
+
next = key == keys.UP ? next.prev() : next.next();
|
598
|
+
}
|
599
|
+
this._highlightListItem(next);
|
600
|
+
this._scrollTo(next);
|
601
|
+
}
|
602
|
+
},
|
603
|
+
// select the currently highlighted item
|
604
|
+
_handleEnterKey: function() {
|
605
|
+
var currentCountry = this.countryList.children(".highlight").first();
|
606
|
+
if (currentCountry.length) {
|
607
|
+
this._selectListItem(currentCountry);
|
608
|
+
}
|
609
|
+
},
|
610
|
+
// find the first list item whose name starts with the query string
|
611
|
+
_searchForCountry: function(query) {
|
612
|
+
for (var i = 0; i < this.countries.length; i++) {
|
613
|
+
if (this._startsWith(this.countries[i].name, query)) {
|
614
|
+
var listItem = this.countryList.children("[data-country-code=" + this.countries[i].iso2 + "]").not(".preferred");
|
615
|
+
// update highlighting and scroll
|
616
|
+
this._highlightListItem(listItem);
|
617
|
+
this._scrollTo(listItem, true);
|
618
|
+
break;
|
619
|
+
}
|
620
|
+
}
|
621
|
+
},
|
622
|
+
// check if (uppercase) string a starts with string b
|
623
|
+
_startsWith: function(a, b) {
|
624
|
+
return a.substr(0, b.length).toUpperCase() == b;
|
625
|
+
},
|
626
|
+
// update the input's value to the given val
|
627
|
+
// if autoFormat=true, format it first according to the country-specific formatting rules
|
628
|
+
_updateVal: function(val, addSuffix) {
|
629
|
+
var formatted;
|
630
|
+
if (this.options.autoFormat && window.intlTelInputUtils) {
|
631
|
+
formatted = intlTelInputUtils.formatNumber(val, this.selectedCountryData.iso2, addSuffix, this.options.preventInvalidNumbers);
|
632
|
+
// ensure we dont go over maxlength. we must do this here to truncate any formatting suffix, and also handle paste events
|
633
|
+
var max = this.telInput.attr("maxlength");
|
634
|
+
if (max && formatted.length > max) {
|
635
|
+
formatted = formatted.substr(0, max);
|
636
|
+
}
|
637
|
+
} else {
|
638
|
+
// no autoFormat, so just insert the original value
|
639
|
+
formatted = val;
|
640
|
+
}
|
641
|
+
this.telInput.val(formatted);
|
642
|
+
},
|
643
|
+
// check if need to select a new flag based on the given number
|
644
|
+
_updateFlagFromNumber: function(number) {
|
645
|
+
// if we're in nationalMode and we're on US/Canada, make sure the number starts with a +1 so _getDialCode will be able to extract the area code
|
646
|
+
// update: if we dont yet have selectedCountryData, but we're here (trying to update the flag from the number), that means we're initialising the plugin with a number that already has a dial code, so fine to ignore this bit
|
647
|
+
if (this.options.nationalMode && this.selectedCountryData && this.selectedCountryData.dialCode == "1" && number.substr(0, 1) != "+") {
|
648
|
+
number = "+1" + number;
|
649
|
+
}
|
650
|
+
// try and extract valid dial code from input
|
651
|
+
var dialCode = this._getDialCode(number);
|
652
|
+
if (dialCode) {
|
653
|
+
// check if one of the matching countries is already selected
|
654
|
+
var countryCodes = this.countryCodes[this._getNumeric(dialCode)], alreadySelected = false;
|
655
|
+
if (this.selectedCountryData) {
|
656
|
+
for (var i = 0; i < countryCodes.length; i++) {
|
657
|
+
if (countryCodes[i] == this.selectedCountryData.iso2) {
|
658
|
+
alreadySelected = true;
|
659
|
+
}
|
660
|
+
}
|
661
|
+
}
|
662
|
+
// if a matching country is not already selected (or this is an unknown NANP area code): choose the first in the list
|
663
|
+
if (!alreadySelected || this._isUnknownNanp(number, dialCode)) {
|
664
|
+
// if using onlyCountries option, countryCodes[0] may be empty, so we must find the first non-empty index
|
665
|
+
for (var j = 0; j < countryCodes.length; j++) {
|
666
|
+
if (countryCodes[j]) {
|
667
|
+
this._selectFlag(countryCodes[j]);
|
668
|
+
break;
|
669
|
+
}
|
670
|
+
}
|
671
|
+
}
|
672
|
+
}
|
673
|
+
},
|
674
|
+
// check if the given number contains an unknown area code from the North American Numbering Plan i.e. the only dialCode that could be extracted was +1 but the actual number's length is >=4
|
675
|
+
_isUnknownNanp: function(number, dialCode) {
|
676
|
+
return dialCode == "+1" && this._getNumeric(number).length >= 4;
|
677
|
+
},
|
678
|
+
// remove highlighting from other list items and highlight the given item
|
679
|
+
_highlightListItem: function(listItem) {
|
680
|
+
this.countryListItems.removeClass("highlight");
|
681
|
+
listItem.addClass("highlight");
|
682
|
+
},
|
683
|
+
// find the country data for the given country code
|
684
|
+
// the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
|
685
|
+
_getCountryData: function(countryCode, ignoreOnlyCountriesOption, allowFail) {
|
686
|
+
var countryList = ignoreOnlyCountriesOption ? allCountries : this.countries;
|
687
|
+
for (var i = 0; i < countryList.length; i++) {
|
688
|
+
if (countryList[i].iso2 == countryCode) {
|
689
|
+
return countryList[i];
|
690
|
+
}
|
691
|
+
}
|
692
|
+
if (allowFail) {
|
693
|
+
return null;
|
694
|
+
} else {
|
695
|
+
throw new Error("No country data for '" + countryCode + "'");
|
696
|
+
}
|
697
|
+
},
|
698
|
+
// select the given flag, update the placeholder and the active list item
|
699
|
+
_selectFlag: function(countryCode) {
|
700
|
+
// do this first as it will throw an error and stop if countryCode is invalid
|
701
|
+
this.selectedCountryData = this._getCountryData(countryCode, false, false);
|
702
|
+
this.selectedFlagInner.attr("class", "iti-flag " + countryCode);
|
703
|
+
// update the selected country's title attribute
|
704
|
+
var title = this.selectedCountryData.name + ": +" + this.selectedCountryData.dialCode;
|
705
|
+
this.selectedFlagInner.parent().attr("title", title);
|
706
|
+
// and the input's placeholder
|
707
|
+
this._updatePlaceholder();
|
708
|
+
// update the active list item
|
709
|
+
var listItem = this.countryListItems.children(".iti-flag." + countryCode).first().parent();
|
710
|
+
this.countryListItems.removeClass("active");
|
711
|
+
listItem.addClass("active");
|
712
|
+
},
|
713
|
+
// update the input placeholder to an example number from the currently selected country
|
714
|
+
_updatePlaceholder: function() {
|
715
|
+
if (window.intlTelInputUtils && !this.hadInitialPlaceholder) {
|
716
|
+
var iso2 = this.selectedCountryData.iso2, numberType = intlTelInputUtils.numberType[this.options.numberType || "FIXED_LINE"], placeholder = intlTelInputUtils.getExampleNumber(iso2, this.options.nationalMode, numberType);
|
717
|
+
this.telInput.attr("placeholder", placeholder);
|
718
|
+
}
|
719
|
+
},
|
720
|
+
// called when the user selects a list item from the dropdown
|
721
|
+
_selectListItem: function(listItem) {
|
722
|
+
// update selected flag and active list item
|
723
|
+
var countryCode = listItem.attr("data-country-code");
|
724
|
+
this._selectFlag(countryCode);
|
725
|
+
this._closeDropdown();
|
726
|
+
this._updateDialCode(listItem.attr("data-dial-code"), true);
|
727
|
+
// always fire the change event as even if nationalMode=true (and we haven't updated the input val), the system as a whole has still changed - see country-sync example. think of it as making a selection from a select element.
|
728
|
+
this.telInput.trigger("change");
|
729
|
+
// focus the input
|
730
|
+
this.telInput.focus();
|
731
|
+
},
|
732
|
+
// close the dropdown and unbind any listeners
|
733
|
+
_closeDropdown: function() {
|
734
|
+
this.countryList.addClass("hide");
|
735
|
+
// update the arrow
|
736
|
+
this.selectedFlagInner.children(".arrow").removeClass("up");
|
737
|
+
// unbind key events
|
738
|
+
$(document).off(this.ns);
|
739
|
+
// unbind click-off-to-close
|
740
|
+
$("html").off(this.ns);
|
741
|
+
// unbind hover and click listeners
|
742
|
+
this.countryList.off(this.ns);
|
743
|
+
},
|
744
|
+
// check if an element is visible within it's container, else scroll until it is
|
745
|
+
_scrollTo: function(element, middle) {
|
746
|
+
var container = this.countryList, containerHeight = container.height(), containerTop = container.offset().top, containerBottom = containerTop + containerHeight, elementHeight = element.outerHeight(), elementTop = element.offset().top, elementBottom = elementTop + elementHeight, newScrollTop = elementTop - containerTop + container.scrollTop(), middleOffset = containerHeight / 2 - elementHeight / 2;
|
747
|
+
if (elementTop < containerTop) {
|
748
|
+
// scroll up
|
749
|
+
if (middle) {
|
750
|
+
newScrollTop -= middleOffset;
|
751
|
+
}
|
752
|
+
container.scrollTop(newScrollTop);
|
753
|
+
} else if (elementBottom > containerBottom) {
|
754
|
+
// scroll down
|
755
|
+
if (middle) {
|
756
|
+
newScrollTop += middleOffset;
|
757
|
+
}
|
758
|
+
var heightDifference = containerHeight - elementHeight;
|
759
|
+
container.scrollTop(newScrollTop - heightDifference);
|
760
|
+
}
|
761
|
+
},
|
762
|
+
// replace any existing dial code with the new one (if not in nationalMode)
|
763
|
+
// also we need to know if we're focusing for a couple of reasons e.g. if so, we want to add any formatting suffix, also if the input is empty and we're not in nationalMode, then we want to insert the dial code
|
764
|
+
_updateDialCode: function(newDialCode, focusing) {
|
765
|
+
var inputVal = this.telInput.val(), newNumber;
|
766
|
+
// save having to pass this every time
|
767
|
+
newDialCode = "+" + newDialCode;
|
768
|
+
if (this.options.nationalMode && inputVal.substr(0, 1) != "+") {
|
769
|
+
// if nationalMode, we just want to re-format
|
770
|
+
newNumber = inputVal;
|
771
|
+
} else if (inputVal) {
|
772
|
+
// if the previous number contained a valid dial code, replace it
|
773
|
+
// (if more than just a plus character)
|
774
|
+
var prevDialCode = this._getDialCode(inputVal);
|
775
|
+
if (prevDialCode.length > 1) {
|
776
|
+
newNumber = inputVal.replace(prevDialCode, newDialCode);
|
777
|
+
} else {
|
778
|
+
// if the previous number didn't contain a dial code, we should persist it
|
779
|
+
var existingNumber = inputVal.substr(0, 1) != "+" ? $.trim(inputVal) : "";
|
780
|
+
newNumber = newDialCode + existingNumber;
|
781
|
+
}
|
782
|
+
} else {
|
783
|
+
newNumber = !this.options.autoHideDialCode || focusing ? newDialCode : "";
|
784
|
+
}
|
785
|
+
this._updateVal(newNumber, focusing);
|
786
|
+
},
|
787
|
+
// try and extract a valid international dial code from a full telephone number
|
788
|
+
// Note: returns the raw string inc plus character and any whitespace/dots etc
|
789
|
+
_getDialCode: function(number) {
|
790
|
+
var dialCode = "";
|
791
|
+
// only interested in international numbers (starting with a plus)
|
792
|
+
if (number.charAt(0) == "+") {
|
793
|
+
var numericChars = "";
|
794
|
+
// iterate over chars
|
795
|
+
for (var i = 0; i < number.length; i++) {
|
796
|
+
var c = number.charAt(i);
|
797
|
+
// if char is number
|
798
|
+
if ($.isNumeric(c)) {
|
799
|
+
numericChars += c;
|
800
|
+
// if current numericChars make a valid dial code
|
801
|
+
if (this.countryCodes[numericChars]) {
|
802
|
+
// store the actual raw string (useful for matching later)
|
803
|
+
dialCode = number.substr(0, i + 1);
|
804
|
+
}
|
805
|
+
// longest dial code is 4 chars
|
806
|
+
if (numericChars.length == 4) {
|
807
|
+
break;
|
808
|
+
}
|
809
|
+
}
|
810
|
+
}
|
811
|
+
}
|
812
|
+
return dialCode;
|
813
|
+
},
|
814
|
+
/********************
|
815
|
+
* PUBLIC METHODS
|
816
|
+
********************/
|
817
|
+
// remove plugin
|
818
|
+
destroy: function() {
|
819
|
+
// make sure the dropdown is closed (and unbind listeners)
|
820
|
+
this._closeDropdown();
|
821
|
+
// key events, and focus/blur events if autoHideDialCode=true
|
822
|
+
this.telInput.off(this.ns);
|
823
|
+
// click event to open dropdown
|
824
|
+
this.selectedFlagInner.parent().off(this.ns);
|
825
|
+
// label click hack
|
826
|
+
this.telInput.closest("label").off(this.ns);
|
827
|
+
// remove markup
|
828
|
+
var container = this.telInput.parent();
|
829
|
+
container.before(this.telInput).remove();
|
830
|
+
},
|
831
|
+
// format the number to the given type
|
832
|
+
getNumber: function(type) {
|
833
|
+
if (window.intlTelInputUtils) {
|
834
|
+
return intlTelInputUtils.formatNumberByType(this.telInput.val(), this.selectedCountryData.iso2, type);
|
835
|
+
}
|
836
|
+
return "";
|
837
|
+
},
|
838
|
+
// get the type of the entered number e.g. landline/mobile
|
839
|
+
getNumberType: function() {
|
840
|
+
if (window.intlTelInputUtils) {
|
841
|
+
return intlTelInputUtils.getNumberType(this.telInput.val(), this.selectedCountryData.iso2);
|
842
|
+
}
|
843
|
+
return -99;
|
844
|
+
},
|
845
|
+
// get the country data for the currently selected flag
|
846
|
+
getSelectedCountryData: function() {
|
847
|
+
// if this is undefined, the plugin will return it's instance instead, so in that case an empty object makes more sense
|
848
|
+
return this.selectedCountryData || {};
|
849
|
+
},
|
850
|
+
// get the validation error
|
851
|
+
getValidationError: function() {
|
852
|
+
if (window.intlTelInputUtils) {
|
853
|
+
return intlTelInputUtils.getValidationError(this.telInput.val(), this.selectedCountryData.iso2);
|
854
|
+
}
|
855
|
+
return -99;
|
856
|
+
},
|
857
|
+
// validate the input val - assumes the global function isValidNumber (from utilsScript)
|
858
|
+
isValidNumber: function() {
|
859
|
+
var val = $.trim(this.telInput.val()), countryCode = this.options.nationalMode ? this.selectedCountryData.iso2 : "", // libphonenumber allows alpha chars, but in order to allow that, we'd need a method to retrieve the processed number, with letters replaced with numbers
|
860
|
+
containsAlpha = /[a-zA-Z]/.test(val);
|
861
|
+
if (!containsAlpha && window.intlTelInputUtils) {
|
862
|
+
return intlTelInputUtils.isValidNumber(val, countryCode);
|
863
|
+
}
|
864
|
+
return false;
|
865
|
+
},
|
866
|
+
// load the utils script
|
867
|
+
loadUtils: function(path) {
|
868
|
+
var utilsScript = path || this.options.utilsScript;
|
869
|
+
if (!$.fn[pluginName].loadedUtilsScript && utilsScript) {
|
870
|
+
// don't do this twice! (dont just check if the global intlTelInputUtils exists as if init plugin multiple times in quick succession, it may not have finished loading yet)
|
871
|
+
$.fn[pluginName].loadedUtilsScript = true;
|
872
|
+
// dont use $.getScript as it prevents caching
|
873
|
+
$.ajax({
|
874
|
+
url: utilsScript,
|
875
|
+
success: function() {
|
876
|
+
// tell all instances the utils are ready
|
877
|
+
$(".intl-tel-input input").intlTelInput("utilsLoaded");
|
878
|
+
},
|
879
|
+
dataType: "script",
|
880
|
+
cache: true
|
881
|
+
});
|
882
|
+
}
|
883
|
+
},
|
884
|
+
// update the selected flag, and update the input val accordingly
|
885
|
+
selectCountry: function(countryCode) {
|
886
|
+
// check if already selected
|
887
|
+
if (!this.selectedFlagInner.hasClass(countryCode)) {
|
888
|
+
this._selectFlag(countryCode);
|
889
|
+
this._updateDialCode(this.selectedCountryData.dialCode, false);
|
890
|
+
}
|
891
|
+
},
|
892
|
+
// set the input value and update the flag
|
893
|
+
setNumber: function(number, addSuffix) {
|
894
|
+
// ensure starts with plus
|
895
|
+
if (!this.options.nationalMode && number.substr(0, 1) != "+") {
|
896
|
+
number = "+" + number;
|
897
|
+
}
|
898
|
+
// we must update the flag first, which updates this.selectedCountryData, which is used later for formatting the number before displaying it
|
899
|
+
this._updateFlagFromNumber(number);
|
900
|
+
this._updateVal(number, addSuffix);
|
901
|
+
},
|
902
|
+
// this is called when the utils are ready
|
903
|
+
utilsLoaded: function() {
|
904
|
+
// if autoFormat is enabled and there's an initial value in the input, then format it
|
905
|
+
if (this.options.autoFormat && this.telInput.val()) {
|
906
|
+
this._updateVal(this.telInput.val());
|
907
|
+
}
|
908
|
+
this._updatePlaceholder();
|
909
|
+
}
|
910
|
+
};
|
911
|
+
// adapted to allow public functions
|
912
|
+
// using https://github.com/jquery-boilerplate/jquery-boilerplate/wiki/Extending-jQuery-Boilerplate
|
913
|
+
$.fn[pluginName] = function(options) {
|
914
|
+
var args = arguments;
|
915
|
+
// Is the first parameter an object (options), or was omitted,
|
916
|
+
// instantiate a new instance of the plugin.
|
917
|
+
if (options === undefined || typeof options === "object") {
|
918
|
+
return this.each(function() {
|
919
|
+
if (!$.data(this, "plugin_" + pluginName)) {
|
920
|
+
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
|
921
|
+
}
|
922
|
+
});
|
923
|
+
} else if (typeof options === "string" && options[0] !== "_" && options !== "init") {
|
924
|
+
// If the first parameter is a string and it doesn't start
|
925
|
+
// with an underscore or "contains" the `init`-function,
|
926
|
+
// treat this as a call to a public method.
|
927
|
+
// Cache the method call to make it possible to return a value
|
928
|
+
var returns;
|
929
|
+
this.each(function() {
|
930
|
+
var instance = $.data(this, "plugin_" + pluginName);
|
931
|
+
// Tests that there's already a plugin-instance
|
932
|
+
// and checks that the requested public method exists
|
933
|
+
if (instance instanceof Plugin && typeof instance[options] === "function") {
|
934
|
+
// Call the method of our plugin instance,
|
935
|
+
// and pass it the supplied arguments.
|
936
|
+
returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
|
937
|
+
}
|
938
|
+
// Allow instances to be destroyed via the 'destroy' method
|
939
|
+
if (options === "destroy") {
|
940
|
+
$.data(this, "plugin_" + pluginName, null);
|
941
|
+
}
|
942
|
+
});
|
943
|
+
// If the earlier cached method gives a value back return the value,
|
944
|
+
// otherwise return this to preserve chainability.
|
945
|
+
return returns !== undefined ? returns : this;
|
946
|
+
}
|
947
|
+
};
|
948
|
+
/********************
|
949
|
+
* STATIC METHODS
|
950
|
+
********************/
|
951
|
+
// get the country data object
|
952
|
+
$.fn[pluginName].getCountryData = function() {
|
953
|
+
return allCountries;
|
954
|
+
};
|
955
|
+
// Tell JSHint to ignore this warning: "character may get silently deleted by one or more browsers"
|
956
|
+
// jshint -W100
|
957
|
+
// Array of country objects for the flag dropdown.
|
958
|
+
// Each contains a name, country code (ISO 3166-1 alpha-2) and dial code.
|
959
|
+
// Originally from https://github.com/mledoze/countries
|
960
|
+
// then modified using the following JavaScript (NOW OUT OF DATE):
|
961
|
+
/*
|
962
|
+
var result = [];
|
963
|
+
_.each(countries, function(c) {
|
964
|
+
// ignore countries without a dial code
|
965
|
+
if (c.callingCode[0].length) {
|
966
|
+
result.push({
|
967
|
+
// var locals contains country names with localised versions in brackets
|
968
|
+
n: _.findWhere(locals, {
|
969
|
+
countryCode: c.cca2
|
970
|
+
}).name,
|
971
|
+
i: c.cca2.toLowerCase(),
|
972
|
+
d: c.callingCode[0]
|
973
|
+
});
|
974
|
+
}
|
975
|
+
});
|
976
|
+
JSON.stringify(result);
|
977
|
+
*/
|
978
|
+
// then with a couple of manual re-arrangements to be alphabetical
|
979
|
+
// then changed Kazakhstan from +76 to +7
|
980
|
+
// and Vatican City from +379 to +39 (see issue 50)
|
981
|
+
// and Caribean Netherlands from +5997 to +599
|
982
|
+
// and Curacao from +5999 to +599
|
983
|
+
// Removed: Åland Islands, Christmas Island, Cocos Islands, Guernsey, Isle of Man, Jersey, Kosovo, Mayotte, Pitcairn Islands, South Georgia, Svalbard, Western Sahara
|
984
|
+
// Update: converted objects to arrays to save bytes!
|
985
|
+
// Update: added "priority" for countries with the same dialCode as others
|
986
|
+
// Update: added array of area codes for countries with the same dialCode as others
|
987
|
+
// So each country array has the following information:
|
988
|
+
// [
|
989
|
+
// Country name,
|
990
|
+
// iso2 code,
|
991
|
+
// International dial code,
|
992
|
+
// Order (if >1 country with same dial code),
|
993
|
+
// Area codes (if >1 country with same dial code)
|
994
|
+
// ]
|
995
|
+
var allCountries = [ [ "Afghanistan (افغانستان)", "af", "93" ], [ "Albania (Shqipëri)", "al", "355" ], [ "Algeria (الجزائر)", "dz", "213" ], [ "American Samoa", "as", "1684" ], [ "Andorra", "ad", "376" ], [ "Angola", "ao", "244" ], [ "Anguilla", "ai", "1264" ], [ "Antigua and Barbuda", "ag", "1268" ], [ "Argentina", "ar", "54" ], [ "Armenia (Հայաստան)", "am", "374" ], [ "Aruba", "aw", "297" ], [ "Australia", "au", "61" ], [ "Austria (Österreich)", "at", "43" ], [ "Azerbaijan (Azərbaycan)", "az", "994" ], [ "Bahamas", "bs", "1242" ], [ "Bahrain (البحرين)", "bh", "973" ], [ "Bangladesh (বাংলাদেশ)", "bd", "880" ], [ "Barbados", "bb", "1246" ], [ "Belarus (Беларусь)", "by", "375" ], [ "Belgium (België)", "be", "32" ], [ "Belize", "bz", "501" ], [ "Benin (Bénin)", "bj", "229" ], [ "Bermuda", "bm", "1441" ], [ "Bhutan (འབྲུག)", "bt", "975" ], [ "Bolivia", "bo", "591" ], [ "Bosnia and Herzegovina (Босна и Херцеговина)", "ba", "387" ], [ "Botswana", "bw", "267" ], [ "Brazil (Brasil)", "br", "55" ], [ "British Indian Ocean Territory", "io", "246" ], [ "British Virgin Islands", "vg", "1284" ], [ "Brunei", "bn", "673" ], [ "Bulgaria (България)", "bg", "359" ], [ "Burkina Faso", "bf", "226" ], [ "Burundi (Uburundi)", "bi", "257" ], [ "Cambodia (កម្ពុជា)", "kh", "855" ], [ "Cameroon (Cameroun)", "cm", "237" ], [ "Canada", "ca", "1", 1, [ "204", "226", "236", "249", "250", "289", "306", "343", "365", "387", "403", "416", "418", "431", "437", "438", "450", "506", "514", "519", "548", "579", "581", "587", "604", "613", "639", "647", "672", "705", "709", "742", "778", "780", "782", "807", "819", "825", "867", "873", "902", "905" ] ], [ "Cape Verde (Kabu Verdi)", "cv", "238" ], [ "Caribbean Netherlands", "bq", "599", 1 ], [ "Cayman Islands", "ky", "1345" ], [ "Central African Republic (République centrafricaine)", "cf", "236" ], [ "Chad (Tchad)", "td", "235" ], [ "Chile", "cl", "56" ], [ "China (中国)", "cn", "86" ], [ "Colombia", "co", "57" ], [ "Comoros (جزر القمر)", "km", "269" ], [ "Congo (DRC) (Jamhuri ya Kidemokrasia ya Kongo)", "cd", "243" ], [ "Congo (Republic) (Congo-Brazzaville)", "cg", "242" ], [ "Cook Islands", "ck", "682" ], [ "Costa Rica", "cr", "506" ], [ "Côte d’Ivoire", "ci", "225" ], [ "Croatia (Hrvatska)", "hr", "385" ], [ "Cuba", "cu", "53" ], [ "Curaçao", "cw", "599", 0 ], [ "Cyprus (Κύπρος)", "cy", "357" ], [ "Czech Republic (Česká republika)", "cz", "420" ], [ "Denmark (Danmark)", "dk", "45" ], [ "Djibouti", "dj", "253" ], [ "Dominica", "dm", "1767" ], [ "Dominican Republic (República Dominicana)", "do", "1", 2, [ "809", "829", "849" ] ], [ "Ecuador", "ec", "593" ], [ "Egypt (مصر)", "eg", "20" ], [ "El Salvador", "sv", "503" ], [ "Equatorial Guinea (Guinea Ecuatorial)", "gq", "240" ], [ "Eritrea", "er", "291" ], [ "Estonia (Eesti)", "ee", "372" ], [ "Ethiopia", "et", "251" ], [ "Falkland Islands (Islas Malvinas)", "fk", "500" ], [ "Faroe Islands (Føroyar)", "fo", "298" ], [ "Fiji", "fj", "679" ], [ "Finland (Suomi)", "fi", "358" ], [ "France", "fr", "33" ], [ "French Guiana (Guyane française)", "gf", "594" ], [ "French Polynesia (Polynésie française)", "pf", "689" ], [ "Gabon", "ga", "241" ], [ "Gambia", "gm", "220" ], [ "Georgia (საქართველო)", "ge", "995" ], [ "Germany (Deutschland)", "de", "49" ], [ "Ghana (Gaana)", "gh", "233" ], [ "Gibraltar", "gi", "350" ], [ "Greece (Ελλάδα)", "gr", "30" ], [ "Greenland (Kalaallit Nunaat)", "gl", "299" ], [ "Grenada", "gd", "1473" ], [ "Guadeloupe", "gp", "590", 0 ], [ "Guam", "gu", "1671" ], [ "Guatemala", "gt", "502" ], [ "Guinea (Guinée)", "gn", "224" ], [ "Guinea-Bissau (Guiné Bissau)", "gw", "245" ], [ "Guyana", "gy", "592" ], [ "Haiti", "ht", "509" ], [ "Honduras", "hn", "504" ], [ "Hong Kong (香港)", "hk", "852" ], [ "Hungary (Magyarország)", "hu", "36" ], [ "Iceland (Ísland)", "is", "354" ], [ "India (भारत)", "in", "91" ], [ "Indonesia", "id", "62" ], [ "Iran (ایران)", "ir", "98" ], [ "Iraq (العراق)", "iq", "964" ], [ "Ireland", "ie", "353" ], [ "Israel (ישראל)", "il", "972" ], [ "Italy (Italia)", "it", "39", 0 ], [ "Jamaica", "jm", "1876" ], [ "Japan (日本)", "jp", "81" ], [ "Jordan (الأردن)", "jo", "962" ], [ "Kazakhstan (Казахстан)", "kz", "7", 1 ], [ "Kenya", "ke", "254" ], [ "Kiribati", "ki", "686" ], [ "Kuwait (الكويت)", "kw", "965" ], [ "Kyrgyzstan (Кыргызстан)", "kg", "996" ], [ "Laos (ລາວ)", "la", "856" ], [ "Latvia (Latvija)", "lv", "371" ], [ "Lebanon (لبنان)", "lb", "961" ], [ "Lesotho", "ls", "266" ], [ "Liberia", "lr", "231" ], [ "Libya (ليبيا)", "ly", "218" ], [ "Liechtenstein", "li", "423" ], [ "Lithuania (Lietuva)", "lt", "370" ], [ "Luxembourg", "lu", "352" ], [ "Macau (澳門)", "mo", "853" ], [ "Macedonia (FYROM) (Македонија)", "mk", "389" ], [ "Madagascar (Madagasikara)", "mg", "261" ], [ "Malawi", "mw", "265" ], [ "Malaysia", "my", "60" ], [ "Maldives", "mv", "960" ], [ "Mali", "ml", "223" ], [ "Malta", "mt", "356" ], [ "Marshall Islands", "mh", "692" ], [ "Martinique", "mq", "596" ], [ "Mauritania (موريتانيا)", "mr", "222" ], [ "Mauritius (Moris)", "mu", "230" ], [ "Mexico (México)", "mx", "52" ], [ "Micronesia", "fm", "691" ], [ "Moldova (Republica Moldova)", "md", "373" ], [ "Monaco", "mc", "377" ], [ "Mongolia (Монгол)", "mn", "976" ], [ "Montenegro (Crna Gora)", "me", "382" ], [ "Montserrat", "ms", "1664" ], [ "Morocco (المغرب)", "ma", "212" ], [ "Mozambique (Moçambique)", "mz", "258" ], [ "Myanmar (Burma) (မြန်မာ)", "mm", "95" ], [ "Namibia (Namibië)", "na", "264" ], [ "Nauru", "nr", "674" ], [ "Nepal (नेपाल)", "np", "977" ], [ "Netherlands (Nederland)", "nl", "31" ], [ "New Caledonia (Nouvelle-Calédonie)", "nc", "687" ], [ "New Zealand", "nz", "64" ], [ "Nicaragua", "ni", "505" ], [ "Niger (Nijar)", "ne", "227" ], [ "Nigeria", "ng", "234" ], [ "Niue", "nu", "683" ], [ "Norfolk Island", "nf", "672" ], [ "North Korea (조선 민주주의 인민 공화국)", "kp", "850" ], [ "Northern Mariana Islands", "mp", "1670" ], [ "Norway (Norge)", "no", "47" ], [ "Oman (عُمان)", "om", "968" ], [ "Pakistan (پاکستان)", "pk", "92" ], [ "Palau", "pw", "680" ], [ "Palestine (فلسطين)", "ps", "970" ], [ "Panama (Panamá)", "pa", "507" ], [ "Papua New Guinea", "pg", "675" ], [ "Paraguay", "py", "595" ], [ "Peru (Perú)", "pe", "51" ], [ "Philippines", "ph", "63" ], [ "Poland (Polska)", "pl", "48" ], [ "Portugal", "pt", "351" ], [ "Puerto Rico", "pr", "1", 3, [ "787", "939" ] ], [ "Qatar (قطر)", "qa", "974" ], [ "Réunion (La Réunion)", "re", "262" ], [ "Romania (România)", "ro", "40" ], [ "Russia (Россия)", "ru", "7", 0 ], [ "Rwanda", "rw", "250" ], [ "Saint Barthélemy (Saint-Barthélemy)", "bl", "590", 1 ], [ "Saint Helena", "sh", "290" ], [ "Saint Kitts and Nevis", "kn", "1869" ], [ "Saint Lucia", "lc", "1758" ], [ "Saint Martin (Saint-Martin (partie française))", "mf", "590", 2 ], [ "Saint Pierre and Miquelon (Saint-Pierre-et-Miquelon)", "pm", "508" ], [ "Saint Vincent and the Grenadines", "vc", "1784" ], [ "Samoa", "ws", "685" ], [ "San Marino", "sm", "378" ], [ "São Tomé and Príncipe (São Tomé e Príncipe)", "st", "239" ], [ "Saudi Arabia (المملكة العربية السعودية)", "sa", "966" ], [ "Senegal (Sénégal)", "sn", "221" ], [ "Serbia (Србија)", "rs", "381" ], [ "Seychelles", "sc", "248" ], [ "Sierra Leone", "sl", "232" ], [ "Singapore", "sg", "65" ], [ "Sint Maarten", "sx", "1721" ], [ "Slovakia (Slovensko)", "sk", "421" ], [ "Slovenia (Slovenija)", "si", "386" ], [ "Solomon Islands", "sb", "677" ], [ "Somalia (Soomaaliya)", "so", "252" ], [ "South Africa", "za", "27" ], [ "South Korea (대한민국)", "kr", "82" ], [ "South Sudan (جنوب السودان)", "ss", "211" ], [ "Spain (España)", "es", "34" ], [ "Sri Lanka (ශ්රී ලංකාව)", "lk", "94" ], [ "Sudan (السودان)", "sd", "249" ], [ "Suriname", "sr", "597" ], [ "Swaziland", "sz", "268" ], [ "Sweden (Sverige)", "se", "46" ], [ "Switzerland (Schweiz)", "ch", "41" ], [ "Syria (سوريا)", "sy", "963" ], [ "Taiwan (台灣)", "tw", "886" ], [ "Tajikistan", "tj", "992" ], [ "Tanzania", "tz", "255" ], [ "Thailand (ไทย)", "th", "66" ], [ "Timor-Leste", "tl", "670" ], [ "Togo", "tg", "228" ], [ "Tokelau", "tk", "690" ], [ "Tonga", "to", "676" ], [ "Trinidad and Tobago", "tt", "1868" ], [ "Tunisia (تونس)", "tn", "216" ], [ "Turkey (Türkiye)", "tr", "90" ], [ "Turkmenistan", "tm", "993" ], [ "Turks and Caicos Islands", "tc", "1649" ], [ "Tuvalu", "tv", "688" ], [ "U.S. Virgin Islands", "vi", "1340" ], [ "Uganda", "ug", "256" ], [ "Ukraine (Україна)", "ua", "380" ], [ "United Arab Emirates (الإمارات العربية المتحدة)", "ae", "971" ], [ "United Kingdom", "gb", "44" ], [ "United States", "us", "1", 0 ], [ "Uruguay", "uy", "598" ], [ "Uzbekistan (Oʻzbekiston)", "uz", "998" ], [ "Vanuatu", "vu", "678" ], [ "Vatican City (Città del Vaticano)", "va", "39", 1 ], [ "Venezuela", "ve", "58" ], [ "Vietnam (Việt Nam)", "vn", "84" ], [ "Wallis and Futuna", "wf", "681" ], [ "Yemen (اليمن)", "ye", "967" ], [ "Zambia", "zm", "260" ], [ "Zimbabwe", "zw", "263" ] ];
|
996
|
+
// loop over all of the countries above
|
997
|
+
for (var i = 0; i < allCountries.length; i++) {
|
998
|
+
var c = allCountries[i];
|
999
|
+
allCountries[i] = {
|
1000
|
+
name: c[0],
|
1001
|
+
iso2: c[1],
|
1002
|
+
dialCode: c[2],
|
1003
|
+
priority: c[3] || 0,
|
1004
|
+
areaCodes: c[4] || null
|
1005
|
+
};
|
1006
|
+
}
|
1007
|
+
});
|