UNPKG

hurbis-ui-mascara-v1

Version:

Biblioteca contendo componentes visuais de máscaras, validadores e formatadores para AngularJS em TypeScript.

952 lines 48.9 kB
/** * @license hurbis-mascara-v1 v1.4.1 * (c) 2018 Hurbis Tecnologia da Informação Ltda. https://www.hurbis.com.br * License: MIT */ var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var Modulo = /** @class */ (function () { function Modulo() { } Modulo.inicializar = function () { //hurbis.comum.GestorModulo.configurarModulo({ // moduloDefinicao: Modulo.DEFINICAO, // objeto: [ // "$provide", // ($provide: angular.auto.IProvideService) => { // $provide.decorator("mdDatepickerDirective", ($delegate: any) => { // let directive = (<angular.IDirective>$delegate[0]); // let template = <any>directive.template; // directive.template = (tElement, tAttrs) => { // var originalTemplate = template.apply(this, arguments); // if (R.has('osMask', tAttrs)) { // var element = angular.element(originalTemplate); // element.find('input').attr('mask', tAttrs.osMask); // element.find('input').attr('ng-model', "ctrl.dateInput");//ng-model is required by ngMask // return R.map(R.prop('outerHTML'), R.values(element)).join(""); // } // return originalTemplate; // }; // return $delegate; // }); // }] //}); hurbis.comum.GestorModulo.registrarModulo(Modulo.DEFINICAO); }; Modulo.DEFINICAO = { nome: "hurbis.ui.mascara", requisitos: [ "hurbis.comum" ] }; /// propriedade obrigatória para inicialização return Modulo; }()); mascara.Modulo = Modulo; Modulo.inicializar(); })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara_1) { "use strict"; /** * Adaptações do componente StringMask * Copyright (c) 2014 Daniel Campos (The Darc - darc.tec@gmail.com) */ var Formatador = /** @class */ (function () { function Formatador(mascara, opcoes) { if (opcoes === void 0) { opcoes = {}; } this.mascara = mascara; this.opcoes = opcoes; this.tokens = { "d": { pattern: /\d/, _default: "0" }, "o": { pattern: /\d/, opcional: true }, "#": { pattern: /\d/, opcional: true, recursivo: true }, "A": { pattern: /[a-zA-Z0-9]/ }, "S": { pattern: /[a-zA-Z]/ }, "U": { pattern: /[a-zA-Z]/, transformar: function (c) { return c.toLocaleUpperCase(); } }, "L": { pattern: /[a-zA-Z]/, transformar: function (c) { return c.toLocaleLowerCase(); } }, "$": { escape: true }, "0": { pattern: /[0]/ }, "1": { pattern: /[0-1]/ }, "2": { pattern: /[0-2]/ }, "3": { pattern: /[0-3]/ }, "4": { pattern: /[0-4]/ }, "5": { pattern: /[0-5]/ }, "6": { pattern: /[0-6]/ }, "7": { pattern: /[0-7]/ }, "8": { pattern: /[0-8]/ }, "9": { pattern: /[0-9]/ } }; this.opcoes = { reverso: this.opcoes.reverso || false, utilizarPadrao: this.opcoes.utilizarPadrao || this.opcoes.reverso }; } Formatador.prototype.escape = function (mascara, posicao) { var contadorEscape = 0; var i = posicao - 1; var token = { escape: true }; while (i >= 0 && token && token.escape) { token = this.tokens[mascara.charAt(i)]; contadorEscape += token && token.escape ? 1 : 0; i--; } return contadorEscape > 0 && contadorEscape % 2 === 1; }; Formatador.prototype.calcularQuantidadeNumeroOpcional = function (mascara, texto) { var quantidadeMascara = mascara.replace(/[^o]/g, "").length; var quantidadeTexto = texto.replace(/[^\d]/g, "").length; return quantidadeTexto - quantidadeMascara; }; Formatador.prototype.concatenarCaracter = function (texto, caracter, options, token) { if (token && typeof token.transformar === "function") { caracter = token.transformar(caracter); } if (options.reverso) { return caracter + texto; } return texto + caracter; }; Formatador.prototype.existeTokenDefinido = function (token) { return this.tokens[token] != null; }; Formatador.prototype.existeToken = function (mascara, posicao, incremento) { var pc = mascara.charAt(posicao); var token = this.tokens[pc]; if (pc === "") { return false; } return token && !token.escape ? true : this.existeToken(mascara, posicao + incremento, incremento); }; Formatador.prototype.existeTokenRecursivo = function (mascara, posicao, incremento) { var pc = mascara.charAt(posicao); var token = this.tokens[pc]; if (pc === "") { return false; } return token && token.recursivo ? true : this.existeTokenRecursivo(mascara, posicao + incremento, incremento); }; Formatador.prototype.inserirCaracter = function (texto, caracter, posicao) { var t = texto.split(""); t.splice(posicao, 0, caracter); return t.join(""); }; Formatador.prototype.processar = function (texto) { var _this = this; if (!texto) { return { resultado: "", valido: false }; } texto = texto + ""; var mascaraAux = this.mascara; var valido = true; var textoFormatado = ""; var posicaoTexto = this.opcoes.reverso ? texto.length - 1 : 0; var posicaoMascara = 0; var quantidadeNumeroOpcionalParaUso = this.calcularQuantidadeNumeroOpcional(mascaraAux, texto); var proximoEscape = false; var recursivo = []; // Salva útimo caracter da máscara para que seja reinserido no final da máscara. var modoRecursivo = false; var passos = { inicio: this.opcoes.reverso ? mascaraAux.length - 1 : 0, fim: this.opcoes.reverso ? -1 : mascaraAux.length, incremento: this.opcoes.reverso ? -1 : 1 }; var continueCondition = function (opcoes) { if (!modoRecursivo && !recursivo.length && _this.existeToken(mascaraAux, posicaoMascara, passos.incremento)) { return true; } else if (!modoRecursivo && recursivo.length && _this.existeTokenRecursivo(mascaraAux, posicaoMascara, passos.incremento)) { return true; } else if (!modoRecursivo) { modoRecursivo = recursivo.length > 0; } //Rotina para manter máscaras dinâmicas (Token recursivo #) if (modoRecursivo) { var pc = recursivo.shift(); recursivo.push(pc); if (opcoes.reverso && posicaoTexto >= 0) { posicaoMascara++; mascaraAux = _this.inserirCaracter(mascaraAux, pc, posicaoMascara); return true; } else if (!opcoes.reverso && posicaoTexto < texto.length) { mascaraAux = _this.inserirCaracter(mascaraAux, pc, posicaoMascara); return true; } } return posicaoMascara < mascaraAux.length && posicaoMascara >= 0; }; /** * Percorre todos os caracteres da máscara informada e realiza o parse/verificação * para cada caracter informado na respectiva posição (digitável) da máscara. * Caso a máscara contenha token recursivo, a formatação será feita indeterminadamente enquanto o usuário informar algum valor. */ for (posicaoMascara = passos.inicio; continueCondition(this.opcoes); posicaoMascara = posicaoMascara + passos.incremento) { var caracterTexto = texto.charAt(posicaoTexto); var caracterMascara = mascaraAux.charAt(posicaoMascara); var token = this.tokens[caracterMascara]; if (recursivo.length && token && !token.recursivo) { token = null; } // 1. Verifica máscara com Escape if (!modoRecursivo || caracterTexto) { if (this.opcoes.reverso && this.escape(mascaraAux, posicaoMascara)) { textoFormatado = this.concatenarCaracter(textoFormatado, caracterMascara, this.opcoes, token); posicaoMascara = posicaoMascara + passos.incremento; // pula o token Escape continue; } else if (!this.opcoes.reverso && proximoEscape) { textoFormatado = this.concatenarCaracter(textoFormatado, caracterMascara, this.opcoes, token); proximoEscape = false; continue; } else if (!this.opcoes.reverso && token && token.escape) { proximoEscape = true; continue; } } // 2. Verifica máscara dinâmica (com token recursivo #) if (!modoRecursivo && token && token.recursivo) { recursivo.push(caracterMascara); } else if (modoRecursivo && !caracterTexto) { textoFormatado = this.concatenarCaracter(textoFormatado, caracterMascara, this.opcoes, token); continue; } else if (!modoRecursivo && recursivo.length > 0 && !caracterTexto) { continue; } // 3. Verifica o valor informado if (!token) { textoFormatado = this.concatenarCaracter(textoFormatado, caracterMascara, this.opcoes, token); if (!modoRecursivo && recursivo.length) { recursivo.push(caracterMascara); } } else if (token.opcional) { if (token.pattern.test(caracterTexto) && quantidadeNumeroOpcionalParaUso) { textoFormatado = this.concatenarCaracter(textoFormatado, caracterTexto, this.opcoes, token); posicaoTexto = posicaoTexto + passos.incremento; quantidadeNumeroOpcionalParaUso--; } else if (recursivo.length > 0 && caracterTexto) { valido = false; break; } } else if (token.pattern.test(caracterTexto)) { // Caso não seja opcional, o texto obrigatoriamente deverá estar de acordo com a máscara textoFormatado = this.concatenarCaracter(textoFormatado, caracterTexto, this.opcoes, token); posicaoTexto = posicaoTexto + passos.incremento; } else if (!caracterTexto && token._default && this.opcoes.utilizarPadrao) { // Caso não seja opcional e não seja válido, adicionar o texto padrão textoFormatado = this.concatenarCaracter(textoFormatado, token._default, this.opcoes, token); } else { // O texto informado não está de acordo com a máscara valido = false; break; } } return { resultado: textoFormatado, valido: valido }; }; Formatador.prototype.aplicar = function (texto) { return this.processar(texto).resultado; }; Formatador.prototype.validar = function (texto) { return this.processar(texto).valido; }; Formatador.processar = function (texto, mascara, opcoes) { return new Formatador(mascara, opcoes).processar(texto); }; Formatador.aplicar = function (texto, mascara, opcoes) { return new Formatador(mascara, opcoes).aplicar(texto); }; Formatador.validar = function (texto, mascara, opcoes) { return new Formatador(mascara, opcoes).validar(texto); }; return Formatador; }()); mascara_1.Formatador = Formatador; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara_2) { "use strict"; var Generica = /** @class */ (function () { function Generica(mascara) { this.mascara = mascara; this.formatadorMascara = new mascara_2.Formatador(this.mascara); } Generica.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } if (manterMascara) { return texto; } return this.limparTexto(texto); }; ; Generica.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, ""); }; Generica.prototype.formatar = function (texto) { var valorLimpo = this.limparTexto(texto); var valorFormatado = this.formatadorMascara.aplicar(valorLimpo); var tamanhoTextoDigitado = valorFormatado.length; if (tamanhoTextoDigitado > 0 && !this.formatadorMascara.existeTokenDefinido(valorFormatado[tamanhoTextoDigitado - 1]) && valorFormatado[tamanhoTextoDigitado - 1] == this.mascara[tamanhoTextoDigitado - 1]) { valorFormatado = valorFormatado.slice(0, tamanhoTextoDigitado - 1); } return valorFormatado; }; Generica.prototype.configurarValidador = function (scope, attributes, controller) { // Não se aplica }; ; return Generica; }()); mascara_2.Generica = Generica; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var Data = /** @class */ (function () { function Data(configuracao) { if (configuracao === void 0) { configuracao = {}; } this.mascara = "DD/MM/YYYY"; if (configuracao && configuracao.formato) { this.mascara = configuracao.formato.toLocaleUpperCase(); } var mascaraAux = this.mascara.replace(/DD/g, "39").replace(/MM/g, "19").replace(/YYYY/g, "9999").replace(/YY/g, "99"); this.formatadorMascara = new mascara.Formatador(mascaraAux); } Data.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } var m = moment(texto, this.mascara, true); return m.isValid() ? m.toDate() : new Date(NaN); }; ; Data.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, this.mascara.length) || ""; }; Data.prototype.formatar = function (valor) { var valorLimpo = this.limparTexto(valor.toString()); return (this.formatadorMascara.aplicar(valorLimpo) || "").replace(/[^0-9]$/, ""); }; Data.prototype.configurarValidador = function (scope, attributes, controller) { var _this = this; controller.$validators["data"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var m = moment(modelValue, _this.mascara, true); return m.isValid(); }; }; ; return Data; }()); mascara.Data = Data; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var Hora = /** @class */ (function () { function Hora(reduzida) { if (reduzida === void 0) { reduzida = true; } this.mascara = "29:59"; if (!reduzida) { this.mascara = "29:59:59"; } this.formatadorMascara = new mascara.Formatador(this.mascara); } Hora.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } return texto; }; ; Hora.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, this.mascara.replace(/:/g, "").length) || ""; }; Hora.prototype.formatar = function (texto) { var valorLimpo = this.limparTexto(texto); return (this.formatadorMascara.aplicar(valorLimpo) || "").replace(/[^0-9]$/, ""); }; Hora.prototype.configurarValidador = function (scope, attributes, controller) { controller.$validators["hora"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var regex = new RegExp(/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/); return regex.test(modelValue); }; }; ; return Hora; }()); mascara.Hora = Hora; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara_3) { "use strict"; var Numero = /** @class */ (function () { function Numero($locale, configuracao) { if (configuracao === void 0) { configuracao = {}; } this.configuracao = configuracao; this.separadorSimbolo = " "; this.backspacePressed = false; if (configuracao.separadorDecimal == null) { configuracao.separadorDecimal = $locale.NUMBER_FORMATS.DECIMAL_SEP; } if (configuracao.separadorMilhar == null) { configuracao.separadorMilhar = $locale.NUMBER_FORMATS.GROUP_SEP; } if (configuracao.simbolo == null) { configuracao.simbolo = $locale.NUMBER_FORMATS.CURRENCY_SYM; } if (configuracao.quantidadeCasaDecimal == null) { configuracao.quantidadeCasaDecimal = 0; } if (!configuracao.exibeSimbolo) { configuracao.simbolo = ""; } if (!configuracao.exibeSeparadorMilhar) { configuracao.separadorMilhar = ""; } this.formatadorMascara = this.configurarMascara(); this.formatadorModel = this.configurarMascara(true); } Numero.prototype.configurarMascara = function (model) { if (model === void 0) { model = false; } var separadorDecimal = (model) ? "." : this.configuracao.separadorDecimal; var mascaraDecimal = this.configuracao.quantidadeCasaDecimal > 0 ? separadorDecimal + new Array(this.configuracao.quantidadeCasaDecimal + 1).join("d") : ""; if (model) { return new mascara_3.Formatador("#d" + mascaraDecimal, { reverso: true }); } var mascara = "#" + this.configuracao.separadorMilhar + "##d" + mascaraDecimal; if (angular.isDefined(this.configuracao.exibeSimboloAposNumero)) { mascara += ((this.configuracao.simbolo) ? this.separadorSimbolo + this.configuracao.simbolo : ""); } else { mascara = ((this.configuracao.simbolo) ? this.configuracao.simbolo + this.separadorSimbolo : "") + mascara; } return new mascara_3.Formatador(mascara, { reverso: true }); }; Numero.prototype.limparTexto = function (texto) { var valorLimpo = texto.replace(/[^0-9]/g, "").replace(/^0*/, ""); return valorLimpo; }; Numero.prototype.formatarValorNegativo = function (texto, model) { var _this = this; var valorLimpo = this.limparTexto(texto); var recuperarValorFormatado = function () { return _this.formatadorMascara.aplicar(valorLimpo); }; var recuperarValorFormatadoModel = function () { var valorAux = _this.formatadorModel.aplicar(valorLimpo); return (_this.configuracao.quantidadeCasaDecimal == 0) ? parseInt(valorAux) : parseFloat(valorAux); }; if (!this.configuracao.permiteNegativo) { return (model) ? recuperarValorFormatadoModel() : recuperarValorFormatado(); } var valorFormatado = recuperarValorFormatado(); var valorModel = recuperarValorFormatadoModel(); var negativo = (texto[0] === "-"); var sinalNegativoInvertido = (texto.slice(-1) === "-"); if ((!sinalNegativoInvertido && negativo) || (sinalNegativoInvertido && !negativo)) { valorModel *= -1; valorFormatado = "-" + ((valorModel !== 0) ? valorFormatado : ""); } return (model) ? valorModel : valorFormatado; }; Numero.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } return this.formatarValorNegativo(texto, true); }; Numero.prototype.formatar = function (valor) { var valorAux; if (typeof valor == "number") { valorAux = valor.toFixed(this.configuracao.quantidadeCasaDecimal); } else { valorAux = valor.toString(); } return this.formatarValorNegativo(valorAux, false); }; Numero.prototype.configurarValidador = function (scope, attributes, controller) { if (attributes["min"]) { var valorMinimo_1 = parseFloat(attributes["min"]); controller.$validators["min"] = function (modelValue) { return controller.$isEmpty(modelValue) || isNaN(valorMinimo_1) || modelValue >= valorMinimo_1; }; scope.$watch(attributes["min"], function (value) { valorMinimo_1 = parseFloat(value); controller.$validate(); }); } if (attributes["max"]) { var valorMaximo_1 = parseFloat(attributes["max"]); controller.$validators["max"] = function (modelValue) { return controller.$isEmpty(modelValue) || isNaN(valorMaximo_1) || modelValue <= valorMaximo_1; }; scope.$watch(attributes["max"], function (value) { valorMaximo_1 = parseFloat(value); controller.$validate(); }); } }; ; return Numero; }()); mascara_3.Numero = Numero; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var CPFCNPJ = /** @class */ (function () { function CPFCNPJ(tipo) { if (tipo === void 0) { tipo = "cpfcnpj"; } this.tipo = tipo; this.mascaraCPF = "999.999.999-99"; this.mascaraCNPJ = "99.999.999\/9999-99"; switch (tipo) { case "cpf": this.formatadorMascaraCPF = new mascara.Formatador(this.mascaraCPF); break; case "cnpj": this.formatadorMascaraCNPJ = new mascara.Formatador(this.mascaraCNPJ); break; default: this.formatadorMascaraCPF = new mascara.Formatador(this.mascaraCPF); this.formatadorMascaraCNPJ = new mascara.Formatador(this.mascaraCNPJ); break; } } CPFCNPJ.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, 14); }; CPFCNPJ.prototype.formatar = function (valor) { var valorLimpo = this.limparTexto(valor); var valorFormatado; if (this.formatadorMascaraCNPJ && valorLimpo.length > 11) { valorFormatado = this.formatadorMascaraCNPJ.aplicar(valorLimpo); } else { valorFormatado = this.formatadorMascaraCPF.aplicar(valorLimpo) || ''; } return valorFormatado.trim().replace(/[^0-9]$/, ""); }; ; CPFCNPJ.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } if (manterMascara) { return texto; } return this.limparTexto(texto); }; ; CPFCNPJ.prototype.configurarValidador = function (scope, attributes, controller) { var _this = this; switch (this.tipo) { case "cpf": controller.$validators["cpf"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var valorLimpo = _this.limparTexto(modelValue); return hurbis.comum.util.validador.CPF.validar(valorLimpo); }; break; case "cnpj": controller.$validators["cnpj"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var valorLimpo = _this.limparTexto(modelValue); return hurbis.comum.util.validador.CNPJ.validar(valorLimpo); }; break; default: controller.$validators["cpfcnpj"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var valorLimpo = _this.limparTexto(modelValue); if (valorLimpo.length > 11) { return hurbis.comum.util.validador.CNPJ.validar(valorLimpo); } else { return hurbis.comum.util.validador.CPF.validar(valorLimpo); } }; break; } }; ; return CPFCNPJ; }()); mascara.CPFCNPJ = CPFCNPJ; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var CartaoCredito = /** @class */ (function () { function CartaoCredito() { this.mascara = "9999 9999 9999 9999"; this.mascaraAmex = "9999 999999 99999"; this.formatadorMascara = new mascara.Formatador(this.mascara); this.formatadorMascaraAmex = new mascara.Formatador(this.mascaraAmex); } CartaoCredito.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, 16); }; CartaoCredito.prototype.formatar = function (valor) { var valorLimpo = this.limparTexto(valor); var valorFormatado; if (valorLimpo.length > 15) { valorFormatado = this.formatadorMascara.aplicar(valorLimpo); } else { valorFormatado = this.formatadorMascaraAmex.aplicar(valorLimpo); } return valorFormatado.trim(); }; ; CartaoCredito.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } if (manterMascara) { return texto; } return this.limparTexto(texto); }; ; CartaoCredito.prototype.configurarValidador = function (scope, attributes, controller) { var _this = this; controller.$validators["cartaoCredito"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } return _this.limparTexto(modelValue).length > 14; }; }; ; return CartaoCredito; }()); mascara.CartaoCredito = CartaoCredito; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var CEP = /** @class */ (function () { function CEP() { this.mascara = "99999-999"; this.formatadorMascara = new mascara.Formatador(this.mascara); } CEP.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } if (manterMascara) { return texto; } return this.limparTexto(texto); }; ; CEP.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, 8); }; CEP.prototype.formatar = function (texto) { var valorLimpo = this.limparTexto(texto); return this.formatadorMascara.aplicar(valorLimpo).trim().replace(/[^0-9]$/, ""); }; CEP.prototype.configurarValidador = function (scope, attributes, controller) { var _this = this; controller.$validators["cep"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var valorLimpo = _this.limparTexto(modelValue); return valorLimpo.length == 8; }; }; ; return CEP; }()); mascara.CEP = CEP; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara) { "use strict"; var Telefone = /** @class */ (function () { function Telefone() { this.mascara8Digitos = { codigoPais: new mascara.Formatador("+dd (dd) dddd-dddd"), codigoArea: new mascara.Formatador("(dd) dddd-dddd"), simples: new mascara.Formatador("dddd-dddd") }; this.mascara9Digitos = { codigoPais: new mascara.Formatador("+dd (dd) ddddd-dddd"), codigoArea: new mascara.Formatador("(dd) ddddd-dddd"), simples: new mascara.Formatador("ddddd-dddd") }; this.mascara0800 = { simples: new mascara.Formatador("dddd-ddd-dddd") }; } Telefone.prototype.limparTexto = function (texto) { return texto.replace(/[^0-9]/g, "").slice(0, 13); }; Telefone.prototype.formatar = function (valor) { var valorLimpo = this.limparTexto(valor); var valorFormatado; if (valorLimpo.indexOf("0800") === 0) { valorFormatado = this.mascara0800.simples.aplicar(valorLimpo); } else if (valorLimpo.length < 9) { valorFormatado = this.mascara8Digitos.simples.aplicar(valorLimpo) || ""; } else if (valorLimpo.length < 10) { valorFormatado = this.mascara9Digitos.simples.aplicar(valorLimpo); } else if (valorLimpo.length < 11) { valorFormatado = this.mascara8Digitos.codigoArea.aplicar(valorLimpo); } else if (valorLimpo.length < 12) { valorFormatado = this.mascara9Digitos.codigoArea.aplicar(valorLimpo); } else if (valorLimpo.length < 13) { valorFormatado = this.mascara8Digitos.codigoPais.aplicar(valorLimpo); } else { valorFormatado = this.mascara9Digitos.codigoPais.aplicar(valorLimpo); } return valorFormatado.trim().replace(/[^0-9]$/, ""); }; ; Telefone.prototype.converterParaModel = function (texto, manterMascara) { if (manterMascara === void 0) { manterMascara = false; } if (manterMascara) { return texto; } return this.limparTexto(texto); }; Telefone.prototype.configurarValidador = function (scope, attributes, controller) { var _this = this; controller.$validators["telefone"] = function (modelValue, viewValue) { if (controller.$isEmpty(modelValue)) { return true; } var tamanho = _this.limparTexto(modelValue).length; return tamanho >= 8 && tamanho <= 13; }; }; return Telefone; }()); mascara.Telefone = Telefone; })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); var hurbis; (function (hurbis) { var ui; (function (ui) { var mascara; (function (mascara_4) { "use strict"; var Mascara = /** @class */ (function () { function Mascara($locale) { var _this = this; this.$locale = $locale; this.restrict = "A"; this.require = "ngModel"; this.priority = 100; this.link = function (scope, element, attributes, controller) { var opcao; var parametro = attributes["hbsMascara"]; var input = element[0]; input.autocomplete = "off"; // verifica se o valor informado é um objeto. Ex.: {'tipo': 'Valor', 'manterMascara': true, 'numero': {...}} if (parametro.search(/^\{.*\}$/g) >= 0) { opcao = scope.$eval(parametro); } else { opcao = { tipo: parametro }; } var mascara = _this.instanciarMascara(opcao); var keydownHandler = function (e) { if (e.which !== 32) { return; } e.preventDefault(); }; var pasteHandler = function (ev) { var evento = ev.originalEvent; var texto = evento.clipboardData.getData("text\/plain").trim(); if (texto.length == 0) { ev.preventDefault(); } }; element.bind("keydown", keydownHandler); element.bind("paste", pasteHandler); var formatador = function (valor) { if (controller.$isEmpty(valor)) { return valor; } return mascara.formatar(valor); }; controller.$formatters.push(formatador); controller.$parsers.push(function (valor) { if (controller.$isEmpty(valor)) { input.value = ""; return valor; } var viewValue = mascara.formatar(valor); if (controller.$viewValue !== viewValue || controller.$viewValue !== input.value) { var viewValueAntigo = controller.$viewValue; controller.$setViewValue(viewValue); //TODO: Melhorar inteligência para uso da posição do cursor //let posicaoCursor: number = input.selectionStart; //guarda a última posição do cursor // Necessário validar/recuperar informações do cursor antes que seja renderizado. if ((viewValueAntigo.length - input.value.length) != 0 && (mascara.limparTexto(input.value).length - mascara.limparTexto(viewValueAntigo).length) == 0) { setTimeout(function () { input.setSelectionRange(viewValue.length, viewValue.length); }, 0); } controller.$render(); } return mascara.converterParaModel(viewValue, opcao.manterMascara); }); mascara.configurarValidador(scope, attributes, controller); }; } Mascara.prototype.instanciarMascara = function (opcao) { var tipo = opcao.tipo.toLocaleLowerCase(); switch (tipo) { case "hora": case "horareduzida": return new mascara_4.Hora(tipo == "horareduzida"); case "data": return new mascara_4.Data(opcao.data); case "cpf": case "cnpj": case "cpfcnpj": return new mascara_4.CPFCNPJ(tipo); case "cartaocredito": return new mascara_4.CartaoCredito(); case "valor": case "valortrescasas": case "inteiro": if (tipo == "valor") { if (opcao.numero == null) { opcao.numero = {}; } if (opcao.numero.exibeSimbolo == null) { opcao.numero.exibeSimbolo = false; } if (opcao.numero.exibeSeparadorMilhar == null) { opcao.numero.exibeSeparadorMilhar = true; } if (opcao.numero.quantidadeCasaDecimal == null) { opcao.numero.quantidadeCasaDecimal = 2; } } if (tipo == "valortrescasas") { if (opcao.numero == null) { opcao.numero = {}; } if (opcao.numero.exibeSimbolo == null) { opcao.numero.exibeSimbolo = false; } if (opcao.numero.exibeSeparadorMilhar == null) { opcao.numero.exibeSeparadorMilhar = true; } if (opcao.numero.quantidadeCasaDecimal == null) { opcao.numero.quantidadeCasaDecimal = 3; } } return new mascara_4.Numero(this.$locale, opcao.numero); case "cep": return new mascara_4.CEP(); case "telefone": return new mascara_4.Telefone(); default: // neste caso o tipo informado é uma máscara. Obs.: Recupero o tipo original da máscara. return new mascara_4.Generica(opcao.tipo); } }; Mascara.inicializar = function () { hurbis.comum.GestorModulo.registrarFuncionalidade({ moduloDefinicao: ui.mascara.Modulo.DEFINICAO, nome: "hbsMascara", tipo: hurbis.comum.FuncionalidadeTipo.Diretiva, objeto: ["$locale", function ($locale) { return new Mascara($locale); }] }); }; return Mascara; }()); mascara_4.Mascara = Mascara; Mascara.inicializar(); })(mascara = ui.mascara || (ui.mascara = {})); })(ui = hurbis.ui || (hurbis.ui = {})); })(hurbis || (hurbis = {})); //# sourceMappingURL=hurbis-ui-mascara-v1.js.map