UNPKG

angular-mask

Version:
119 lines (106 loc) 3.59 kB
'use strict'; angular.module('ui.utils.masks.br.ie', []) .directive('uiBrIeMask', ['$parse', function($parse) { var ieMasks = { 'AC': [{mask: new StringMask('00.000.000/000-00')}], 'AL': [{mask: new StringMask('000000000')}], 'AM': [{mask: new StringMask('00.000.000-0')}], 'AP': [{mask: new StringMask('000000000')}], 'BA': [{chars: 8, mask: new StringMask('000000-00')}, {mask: new StringMask('0000000-00')}], 'CE': [{mask: new StringMask('00000000-0')}], 'DF': [{mask: new StringMask('00000000000-00')}], 'ES': [{mask: new StringMask('00000000-0')}], 'GO': [{mask: new StringMask('00.000.000-0')}], 'MA': [{mask: new StringMask('000000000')}], 'MG': [{mask: new StringMask('000.000.000/0000')}], 'MS': [{mask: new StringMask('000000000')}], 'MT': [{mask: new StringMask('0000000000-0')}], 'PA': [{mask: new StringMask('00-000000-0')}], 'PB': [{mask: new StringMask('00000000-0')}], 'PE': [{chars: 9, mask: new StringMask('0000000-00')}, {mask: new StringMask('00.0.000.0000000-0')}], 'PI': [{mask: new StringMask('000000000')}], 'PR': [{mask: new StringMask('000.00000-00')}], 'RJ': [{mask: new StringMask('00.000.00-0')}], 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')}, {mask: new StringMask('00.0.000.000-0')}], 'RO': [{mask: new StringMask('0000000000000-0')}], 'RR': [{mask: new StringMask('00000000-0')}], 'RS': [{mask: new StringMask('000/0000000')}], 'SC': [{mask: new StringMask('000.000.000')}], 'SE': [{mask: new StringMask('00000000-0')}], 'SP': [{mask: new StringMask('000.000.000.000')}, {mask: new StringMask('-00000000.0/000')}], 'TO': [{mask: new StringMask('00000000000')}] }; function clearValue (value) { if(!value) { return value; } return value.replace(/[^0-9]/g, ''); } function getMask(uf, value) { if(!uf || !ieMasks[uf]) { return undefined; } var _uf = uf.toUpperCase(); if (_uf === 'SP' && /^P/i.test(value)) { return ieMasks.SP[1].mask; } var masks = ieMasks[uf]; var i = 0; while(masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) { i++; } return masks[i].mask; } function applyIEMask (value, uf, ctrl) { var mask = getMask(uf, value); if(!value || !mask) { ctrl.$setValidity('ie', true); return value; } var processed = mask.process(clearValue(value)); ctrl.$setValidity('ie', BrV.ie(uf).validate(value)); var formatedValue = processed.result; if (uf && uf.toUpperCase() === 'SP' && /^p/i.test(value)) { return 'P'+(formatedValue ? formatedValue.trim().replace(/[^0-9]$/, '') : ''); } if(!formatedValue) { return formatedValue; } return formatedValue.trim().replace(/[^0-9]$/, ''); } return { restrict: 'A', require: '?ngModel', link: function(scope, element, attrs, ctrl) { var state = $parse(attrs.uiBrIeMask)(scope); if (!ctrl) { return; } scope.$watch(attrs.uiBrIeMask, function(newState) { state = newState; applyIEMask(ctrl.$viewValue, state, ctrl); }); ctrl.$formatters.push(function(value) { return applyIEMask(value, state, ctrl); }); ctrl.$parsers.push(function(value) { if (!value) { return applyIEMask(value, state, ctrl); } var formatedValue = applyIEMask(value, state, ctrl); if (ctrl.$viewValue !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } if (state && state.toUpperCase() === 'SP' && /^p/i.test(value)) { return 'P'+clearValue(formatedValue); } return clearValue(formatedValue); }); } }; }]);