UNPKG

angular-input-masks

Version:
48 lines (39 loc) 1.32 kB
'use strict'; 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); }; }); } }; }; };