Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use intl-tel-input 7.0.* PLUS read only field option added #86

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ with countries only:
<input type="text" international-phone-number only-countries ng-model="phone">
```

read only field:
```html
<input type="text" international-phone-number ng-model="phone" readonly="true">
```

Feel free to mix options together:
```html
<input type="text" international-phone-number only-countries="pl, de, en, es" default-country="pl" preferred-countries="pl, de" ng-model="phone">
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "international-phone-number",
"version": "1.0.0",
"main": "releases/international-phone-number.js",
"description": "AngularJS directive for intl-tel-input jQuery plugin",
"author": "Marek Pietrucha (http://enginearch.com)",
"license": "MIT",
"dependencies": {
"angular": ">1.3.0",
"intl-tel-input": "~5.1.0"
"intl-tel-input": "~7.0.0"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "international-phone-number",
"version": "0.0.11",
"version": "1.0.0",
"description": "AngularJS directive for intl-tel-input jQuery plugin",
"keywords": [
"international",
Expand Down
30 changes: 26 additions & 4 deletions releases/international-phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
autoHideDialCode: true,
autoPlaceholder: true,
customPlaceholder: null,
defaultCountry: "",
initialCountry: "",
geoIpLookup: null,
nationalMode: true,
numberType: "MOBILE",
Expand All @@ -21,10 +21,11 @@
require: '^ngModel',
scope: {
ngModel: '=',
country: '='
country: '=',
geoIpLookup: '&'
},
link: function(scope, element, attrs, ctrl) {
var handleWhatsSupposedToBeAnArray, options, read, watchOnce;
var checkReadOnly, handleWhatsSupposedToBeAnArray, options, read, watchOnce;
if (ctrl) {
if (element.val() !== '') {
$timeout(function() {
Expand All @@ -43,6 +44,25 @@
return value.toString().replace(/[ ]/g, '').split(',');
}
};
checkReadOnly = function() {
var divIntlTelInput, readOnly, readOnlyClass, readOnlySpan;
readOnly = attrs.readonly;
if (readOnly) {
readOnlyClass = 'intl-tel-input-read-only';
divIntlTelInput = element.parents('.intl-tel-input:first');
divIntlTelInput.find('select:first').attr('disabled', true);
divIntlTelInput.find('.iti-arrow').hide();
readOnlySpan = divIntlTelInput.find('span.' + readOnlyClass);
if (readOnlySpan.length === 0 && element.val() !== '') {
readOnlySpan = angular.element('<span></span>');
readOnlySpan.attr('class', readOnlyClass);
readOnlySpan.attr('style', 'padding-left: 38px');
element.hide();
element.after(readOnlySpan);
}
readOnlySpan.text(element.val());
}
};
options = angular.copy(ipnConfig);
angular.forEach(options, function(value, key) {
var option;
Expand All @@ -60,6 +80,7 @@
return options[key] = option;
}
});
options['geoIpLookup'] = scope.geoIpLookup ? scope.geoIpLookup() : null;
watchOnce = scope.$watch('ngModel', function(newValue) {
return scope.$$postDigest(function() {
if (newValue !== null && newValue !== void 0 && newValue.length > 0) {
Expand All @@ -77,14 +98,15 @@
});
scope.$watch('country', function(newValue) {
if (newValue !== null && newValue !== void 0 && newValue !== '') {
return element.intlTelInput("selectCountry", newValue);
return element.intlTelInput("setCountry", newValue);
}
});
ctrl.$formatters.push(function(value) {
if (!value) {
return value;
}
element.intlTelInput('setNumber', value);
checkReadOnly();
return element.val();
});
ctrl.$parsers.push(function(value) {
Expand Down
4 changes: 2 additions & 2 deletions releases/international-phone-number.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions src/international-phone-number.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module("internationalPhoneNumber", [])
autoHideDialCode: true
autoPlaceholder: true
customPlaceholder: null
defaultCountry: ""
initialCountry: ""
geoIpLookup: null
nationalMode: true
numberType: "MOBILE"
Expand All @@ -27,6 +27,7 @@ angular.module("internationalPhoneNumber", [])
scope:
ngModel: '='
country: '='
geoIpLookup: '&'

link: (scope, element, attrs, ctrl) ->

Expand All @@ -47,6 +48,23 @@ angular.module("internationalPhoneNumber", [])
else
value.toString().replace(/[ ]/g, '').split(',')

checkReadOnly = () ->
readOnly = attrs.readonly
if readOnly
readOnlyClass = 'intl-tel-input-read-only'
divIntlTelInput = element.parents('.intl-tel-input:first')
divIntlTelInput.find('select:first').attr('disabled', true)
divIntlTelInput.find('.iti-arrow').hide()
readOnlySpan = divIntlTelInput.find('span.' + readOnlyClass)
if readOnlySpan.length == 0 and element.val() != ''
readOnlySpan = angular.element('<span></span>')
readOnlySpan.attr('class', readOnlyClass)
readOnlySpan.attr('style', 'padding-left: 38px')
element.hide()
element.after(readOnlySpan)
readOnlySpan.text(element.val())
return

options = angular.copy(ipnConfig)

angular.forEach options, (value, key) ->
Expand All @@ -60,7 +78,7 @@ angular.module("internationalPhoneNumber", [])
options[key] = (option == "true")
else
options[key] = option

options['geoIpLookup'] = if scope.geoIpLookup then scope.geoIpLookup() else null
# Wait for ngModel to be set
watchOnce = scope.$watch('ngModel', (newValue) ->
# Wait to see if other scope variables were set at the same time
Expand All @@ -84,14 +102,15 @@ angular.module("internationalPhoneNumber", [])

scope.$watch('country', (newValue) ->
if newValue != null && newValue != undefined && newValue != ''
element.intlTelInput("selectCountry", newValue)
element.intlTelInput("setCountry", newValue)
)

ctrl.$formatters.push (value) ->
if !value
return value

element.intlTelInput 'setNumber', value
checkReadOnly()
element.val()

ctrl.$parsers.push (value) ->
Expand Down