angular-input-masks
Version:
Personalized input masks for AngularJS
48 lines (39 loc) • 1.32 kB
JavaScript
module.exports = function maskFactory(maskDefinition) {
return function MaskDirective() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
ctrl.$formatters.push(function formatter(value) {
if (ctrl.$isEmpty(value)) {
return value;
}
var cleanValue = maskDefinition.clearValue(value.toString());
return maskDefinition.format(cleanValue);
});
ctrl.$parsers.push(function parser(value) {
if (ctrl.$isEmpty(value)) {
return value;
}
var cleanValue = maskDefinition.clearValue(value.toString());
var formattedValue = maskDefinition.format(cleanValue);
if (ctrl.$viewValue !== formattedValue) {
ctrl.$setViewValue(formattedValue);
ctrl.$render();
}
if (angular.isUndefined(maskDefinition.getModelValue)) {
return cleanValue;
}
var actualModelType = typeof ctrl.$modelValue;
return maskDefinition.getModelValue(formattedValue, actualModelType);
});
angular.forEach(maskDefinition.validations, function(validatorFn, validationErrorKey) {
ctrl.$validators[validationErrorKey] = function validator(modelValue, viewValue) {
return ctrl.$isEmpty(modelValue) || validatorFn(modelValue, viewValue);
};
});
}
};
};
};
;