1(function (window, angular) { 2 'use strict'; 3 4 angular 5 .module('RFPe') 6 .directive("errorMessages", ErrorMessages) 7 .directive("validateCpf", ValidateCPF) 8 .directive("validateCnpj", ValidateCNPJ) 9 .directive("checkUsername", CheckUsername) 10 .directive("checkEmail", CheckEmail) 11 .directive("checkPassword", CheckPassword); 12 13 /* 14 Adiciona mensagens de erro ao input 15 */ 16 function ErrorMessages() { 17 return { 18 compile: function (element) { 19 20 console.log('ErrorMessages.compile'); 21 22 var formName = $(element).data('formname'); 23 if (!formName) { 24 formName = $(element).closest('form').attr('name'); 25 } 26 var inputName = $(element).attr('name'); 27 var stringConcat = formName + "." + inputName; 28 var isInputGroup = $(element).parent().hasClass("input-group"); 29 var stringMessages = '<div class="error-messages" ng-messages="' + stringConcat + '.$error" ng-if="' + formName + '.$submitted || ' + stringConcat + '.$touched">' 30 + '<p ng-message="required">Campo obrigatório</p>' 31 + '<p ng-message="minlength">Este campo é muito curto</p>' 32 + '<p ng-message="maxlength">Este campo é muito longo</p>' 33 + '<p ng-message="email">Este não é um e-mail válido</p>' 34 + '<p ng-message="validateCpf">CPF inválido</p>' 35 + '<p ng-message="validateCnpj">CNPJ inválido</p>' 36 + '<p ng-message="checkUsername">Este nome de usuário já está em uso</p>' 37 + '<p ng-message="checkEmail">Este email já está em uso</p>' 38 + '<p ng-message="checkPassword">As senhas não conferem</p>' 39 + '<p ng-message="customerSearch">Selecione o cliente</p>' 40 + '<p ng-message="cep">CEP inválido</p>' 41 + '<p ng-message="brPhoneNumber">Formato de telefone inválido</p>' 42 + '</div>'; 43 //$(element).parent().attr('ng-class', "{ 'has-error': " + stringConcat + ".$dirty && " + stringConcat + ".$invalid }"); 44 if (isInputGroup) { 45 $(element).parent().after(stringMessages); 46 } 47 else { 48 $(element).after(stringMessages); 49 } 50 51 } 52 } 53 }; 54 55 /* 56 Valida CPF para diretivas de apresentação de formulário utilizar setar propriedade showfields 57 */ 58 59 function ValidateCPF() { 60 return { 61 require: 'ngModel', 62 link: function (scope, elm, attrs, ctrl) { 63 ctrl.$validators.validateCpf = function (modelValue, viewValue) { 64 if (ctrl.$isEmpty(modelValue)) { 65 // consider empty models to be valid 66 return true; 67 } 68 var cpf = modelValue; 69 cpf = cpf.replace(/[^\d]+/g, ''); 70 if (cpf == '') return false; 71 // Elimina CPFs invalidos conhecidos 72 if (cpf.length != 11 || 73 cpf == "00000000000" || 74 cpf == "11111111111" || 75 cpf == "22222222222" || 76 cpf == "33333333333" || 77 cpf == "44444444444" || 78 cpf == "55555555555" || 79 cpf == "66666666666" || 80 cpf == "77777777777" || 81 cpf == "88888888888" || 82 cpf == "99999999999") { 83 return false; 84 } 85 86 // Valida 1o digito 87 var add = 0; 88 for (var i = 0; i < 9; i++) 89 add += parseInt(cpf.charAt(i)) * (10 - i); 90 var rev = 11 - (add % 11); 91 if (rev == 10 || rev == 11) 92 rev = 0; 93 if (rev != parseInt(cpf.charAt(9))) 94 return false;
95 // Valida 2o digito 96 add = 0; 97 for (var i = 0; i < 10; i++) 98 add += parseInt(cpf.charAt(i)) * (11 - i); 99 rev = 11 - (add % 11); 100 if (rev == 10 || rev == 11) 101 rev = 0; 102 if (rev != parseInt(cpf.charAt(10))) 103 return false; 104 return true; 105 }; 106 } 107 }; 108 }; 109 110 /* 111 Valida CNPJ 112 */ 113 114 function ValidateCNPJ() { 115 return { 116 require: 'ngModel', 117 link: function (scope, elm, attrs, ctrl) { 118 ctrl.$validators.validateCnpj = function (modelValue, viewValue) { 119 if (ctrl.$isEmpty(modelValue)) { 120 // consider empty models to be valid 121 return true; 122 } 123 var cnpj = modelValue; 124 cnpj = cnpj.replace(/[^\d]+/g, ''); 125 126 if (cnpj == '') return false; 127 128 if (cnpj.length != 14) 129 return false; 130 131 // Elimina CNPJs invalidos conhecidos 132 if (cnpj == "00000000000000" || 133 cnpj == "11111111111111" || 134 cnpj == "22222222222222" || 135 cnpj == "33333333333333" || 136 cnpj == "44444444444444" || 137 cnpj == "55555555555555" || 138 cnpj == "66666666666666" || 139 cnpj == "77777777777777" || 140 cnpj == "88888888888888" || 141 cnpj == "99999999999999") 142 return false; 143 144 // Valida DVs 145 var tamanho = cnpj.length - 2 146 var numeros = cnpj.substring(0, tamanho); 147 var digitos = cnpj.substring(tamanho); 148 var soma = 0; 149 var pos = tamanho - 7; 150 for (var i = tamanho; i >= 1; i--) { 151 soma += numeros.charAt(tamanho - i) * pos--; 152 if (pos < 2) 153 pos = 9; 154 } 155 var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 156 if (resultado != digitos.charAt(0)) 157 return false; 158 159 tamanho = tamanho + 1; 160 numeros = cnpj.substring(0, tamanho); 161 soma = 0; 162 pos = tamanho - 7; 163 for (var i = tamanho; i >= 1; i--) { 164 soma += numeros.charAt(tamanho - i) * pos--; 165 if (pos < 2) 166 pos = 9; 167 } 168 resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 169 if (resultado != digitos.charAt(1)) 170 return false; 171 172 return true; 173 }; 174 } 175 }; 176 }; 177 178 /* 179 Verifica se já existe um usuário cadastrado 180 */ 181 182 CheckUsername.$inject = ['userService', '$q']; 183 function CheckUsername(userService, $q) { 184 return { 185 require: 'ngModel', 186 link: function (scope, elm, attrs, ctrl) { 187 ctrl.$asyncValidators.checkUsername = function (modelValue, viewValue) { 188 var result = null; 189 var deferred = $q.defer(); 190 userService.checkUsername(modelValue, scope.user.id) 191 .success(function () { 192 deferred.resolve(); 193 }) 194 .error(function () { 195 deferred.reject(); 196 }); 197 return deferred.promise; 198 }; 199 } 200 }; 201 }; 202 203 /* 204 Verifica se já existe um email cadastrado 205 */ 206 207 CheckEmail.$inject = ['userService', '$q']; 208 function CheckEmail(userService, $q) { 209 return { 210 require: 'ngModel', 211 link: function (scope, elm, attrs, ctrl) { 212 ctrl.$asyncValidators.checkEmail = function (modelValue, viewValue) { 213 var result = null; 214 var deferred = $q.defer(); 215 userService.checkEmail(modelValue, scope.user.id) 216 .success(function (data) { 217 deferred.resolve(); 218 }) 219 .error(function () { 220 deferred.reject(); 221 }); 222 223 return deferred.promise; 224 }; 225 } 226 }; 227 }; 228 229 function CheckPassword() { 230 return { 231 require: "ngModel", 232 scope: { 233 otherModelValue: "=checkPassword" 234 }, 235 link: function (scope, element, attributes, ngModel) { 236 237 ngModel.$validators.checkPassword = function (modelValue, viewValue) { 238 return modelValue == scope.otherModelValue; 239 }; 240 241 scope.$watch("otherModelValue", function () { 242 ngModel.$validate(); 243 }); 244 } 245 }; 246 } 247 248})(window, angular);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.