Skip to content

Commit

Permalink
Merge pull request #55 from pra85/release
Browse files Browse the repository at this point in the history
initialize $viewValue and Make release files minification friendly
  • Loading branch information
mareczek committed Jul 14, 2015
2 parents d1c30c8 + 2f7a74b commit 6514e87
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
bower_components
200 changes: 111 additions & 89 deletions releases/international-phone-number.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,119 @@
(function() {
"use strict";
angular.module("internationalPhoneNumber", []).directive('internationalPhoneNumber', function($timeout) {
return {
restrict: 'A',
require: '^ngModel',
scope: {
ngModel: '=',
defaultCountry: '@'
},
link: function(scope, element, attrs, ctrl) {
var handleWhatsSupposedToBeAnArray, options, read, watchOnce;
read = function() {
return ctrl.$setViewValue(element.val());
};
handleWhatsSupposedToBeAnArray = function(value) {
if (value instanceof Array) {
return value;
} else {
return value.toString().replace(/[ ]/g, '').split(',');
}
};
options = {
autoFormat: true,
autoHideDialCode: true,
defaultCountry: '',
nationalMode: false,
numberType: '',
onlyCountries: void 0,
preferredCountries: ['us', 'gb'],
responsiveDropdown: false,
utilsScript: ""
};
angular.forEach(options, function(value, key) {
var option;
if (!(attrs.hasOwnProperty(key) && angular.isDefined(attrs[key]))) {
return;
}
option = attrs[key];
if (key === 'preferredCountries') {
return options.preferredCountries = handleWhatsSupposedToBeAnArray(option);
} else if (key === 'onlyCountries') {
return options.onlyCountries = handleWhatsSupposedToBeAnArray(option);
} else if (typeof value === "boolean") {
return options[key] = option === "true";
} else {
return options[key] = option;
angular.module("internationalPhoneNumber", []).directive('internationalPhoneNumber', [
'$timeout', function($timeout) {
return {
restrict: 'A',
require: '^ngModel',
scope: {
ngModel: '=',
defaultCountry: '@'
},
link: function(scope, element, attrs, ctrl) {
var handleWhatsSupposedToBeAnArray, options, read, watchOnce;
if (ctrl) {
if (element.val() !== '') {
$timeout(function() {
element.intlTelInput('setNumber', element.val());
return ctrl.$setViewValue(element.val());
}, 0);
}
}
});
watchOnce = scope.$watch('ngModel', function(newValue) {
return scope.$$postDigest(function() {
options.defaultCountry = scope.defaultCountry;
if (newValue !== null && newValue !== void 0 && newValue !== '') {
element.val(newValue);
read = function() {
return ctrl.$setViewValue(element.val());
};
handleWhatsSupposedToBeAnArray = function(value) {
if (value instanceof Array) {
return value;
} else {
return value.toString().replace(/[ ]/g, '').split(',');
}
};
options = {
autoFormat: true,
autoHideDialCode: true,
defaultCountry: '',
nationalMode: false,
numberType: '',
onlyCountries: void 0,
preferredCountries: ['us', 'gb'],
responsiveDropdown: false,
utilsScript: ""
};
angular.forEach(options, function(value, key) {
var option;
if (!(attrs.hasOwnProperty(key) && angular.isDefined(attrs[key]))) {
return;
}
element.intlTelInput(options);
if (!(attrs.skipUtilScriptDownload !== void 0 || options.utilsScript)) {
element.intlTelInput('loadUtils', '/bower_components/intl-tel-input/lib/libphonenumber/build/utils.js');
option = attrs[key];
if (key === 'preferredCountries') {
return options.preferredCountries = handleWhatsSupposedToBeAnArray(option);
} else if (key === 'onlyCountries') {
return options.onlyCountries = handleWhatsSupposedToBeAnArray(option);
} else if (typeof value === "boolean") {
return options[key] = option === "true";
} else {
return options[key] = option;
}
return watchOnce();
});
});
ctrl.$formatters.push(function(value) {
if (!value) {
return value;
} else {
$timeout(function() {
return element.intlTelInput('setNumber', value);
}, 0);
return element.val();
}
});
ctrl.$parsers.push(function(value) {
if (!value) {
return value;
}
return value.replace(/[^\d]/g, '');
});
ctrl.$validators.internationalPhoneNumber = function(value) {
if (!value) {
return value;
} else {
return element.intlTelInput("isValidNumber");
}
};
element.on('blur keyup change', function(event) {
return scope.$apply(read);
});
return element.on('$destroy', function() {
element.intlTelInput('destroy');
return element.off('blur keyup change');
});
}
};
});
watchOnce = scope.$watch('ngModel', function(newValue) {
return scope.$$postDigest(function() {
options.defaultCountry = scope.defaultCountry;
if (newValue !== null && newValue !== void 0 && newValue !== '') {
element.val(newValue);
}
element.intlTelInput(options);
if (!(attrs.skipUtilScriptDownload !== void 0 || options.utilsScript)) {
element.intlTelInput('loadUtils', '/bower_components/intl-tel-input/lib/libphonenumber/build/utils.js');
}
return watchOnce();
});
});
ctrl.$formatters.push(function(value) {
if (!value) {
return value;
} else {
$timeout(function() {
return element.intlTelInput('setNumber', value);
}, 0);
return element.val();
}
});
ctrl.$parsers.push(function(value) {
if (!value) {
return value;
}
return value.replace(/[^\d]/g, '');
});
ctrl.$validators.internationalPhoneNumber = function(value) {
if (element.attr('required')) {
if (!value) {
return false;
} else {
return element.intlTelInput("isValidNumber");
}
} else {
if (element.intlTelInput("getSelectedCountryData").dialCode === value) {
return true;
} else {
if (!value) {
return true;
} else {
return element.intlTelInput("isValidNumber");
}
}
}
};
element.on('blur keyup change', function(event) {
return scope.$apply(read);
});
return element.on('$destroy', function() {
element.intlTelInput('destroy');
return element.off('blur keyup change');
});
}
};
}
]);

}).call(this);
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.

23 changes: 20 additions & 3 deletions src/international-phone-number.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ angular.module("internationalPhoneNumber", []).directive 'internationalPhoneNumb

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

if ctrl
if element.val() != ''
$timeout () ->
element.intlTelInput 'setNumber', element.val()
ctrl.$setViewValue element.val()
, 0


read = () ->
ctrl.$setViewValue element.val()

Expand Down Expand Up @@ -78,10 +86,19 @@ angular.module("internationalPhoneNumber", []).directive 'internationalPhoneNumb
value.replace(/[^\d]/g, '')

ctrl.$validators.internationalPhoneNumber = (value) ->
if !value
return value
if element.attr('required')
if !value
return false
else
return element.intlTelInput("isValidNumber")
else
return element.intlTelInput("isValidNumber")
if element.intlTelInput("getSelectedCountryData").dialCode == value
return true
else
if !value
return true
else
return element.intlTelInput("isValidNumber")


element.on 'blur keyup change', (event) ->
Expand Down

0 comments on commit 6514e87

Please sign in to comment.