Last active
March 3, 2016 12:53
-
-
Save kutyel/901c3ae0e57288e75fb1 to your computer and use it in GitHub Desktop.
Directiva sumaBuscarPersona
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="input-group"> | |
<input type="text" class="form-control" | |
ng-model="search.texto" | |
ng-blur="search.buscar($event)" | |
ng-keyup="$event.keyCode === 13 ? search.buscar($event) : null" | |
placeholder="NIF, nombre..." suma-validar-cadena="mayuscula"> | |
<span class="input-group-btn"> | |
<button class="btn btn-primary" type="button" | |
ng-click="search.buscar($event)" | |
ng-disabled="!search.texto"> | |
<span class="glyphicon glyphicon-search"></span> | |
</button> | |
</span> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
/** | |
* @ngdoc directive | |
* @name sumaBuscarPersona | |
* | |
* @description | |
* Directiva que centraliza la comunicación con la búsqueda de personas, recibe un callback | |
* y al volver de la búsqueda de personas se gestionan en el callback los datos de la persona. | |
* | |
* @element ANY | |
* @param {expression} sumaBuscarPersona {@link guide/expression Expression} to evaluate. | |
* | |
* @example | |
<example module="bindHtmlExample" deps="angular-sanitize.js"> | |
<file name="index.html"> | |
<suma-buscar-persona ng-model="model.persona" callback="callbackPersona"></suma-buscar-persona> | |
</file> | |
</example> | |
*/ | |
angular.module('app.directivas').directive('sumaBuscarPersona', function() { | |
return { | |
restrict: 'E', | |
scope: { | |
'ngModel': '=', | |
'callback': '=', | |
'noMostrarAvisos': '=?', | |
'mostrarResultado': '@?', | |
'idBusqueda': '@?' | |
}, | |
templateUrl: 'js/directivas/buscar-persona/buscar-persona.html', | |
controllerAs: 'search', | |
bindToController: true, | |
controller: [ | |
'util', | |
'core', | |
'_', | |
function(util, core, _) { | |
var search = this; | |
(function(search) { | |
search.buscar = buscar; | |
init(); | |
})(this); | |
function init() { | |
search.texto = ''; | |
search.evento = ''; | |
search.results = []; | |
search.mensajeError = ''; | |
search.lastSearch = null; | |
obtenerBusquedas(); | |
} | |
function buscarResultados(str) { | |
var resultados = []; | |
search.results = []; | |
// Recorro las búsquedas y las añado al array si alguna de sus expresiones pasa el test | |
search.busquedas.forEach((busqueda) => { | |
JSON.parse(busqueda.F7713_EXPRESION_REGULAR).forEach((regexp) => { | |
if (new RegExp(regexp).test(str) && | |
!_.contains(resultados, busqueda.F7713_BUSQUEDA_ID) && busqueda.F7713_ORDEN) { | |
resultados.push({ | |
'id': busqueda.F7713_BUSQUEDA_ID, | |
'nombre': busqueda.F7713_NOMBRE, | |
'sufijo': busqueda.F7713_SUFIJO, | |
'orden': busqueda.F7713_ORDEN, | |
'desc': `Búsqueda por ${busqueda.F7713_NOMBRE} en ${busqueda.F7713_SUFIJO}` | |
}); | |
} | |
}); | |
}); | |
// Si hay más de un resultado, priorizo el DNI | |
search.opcion = (resultados.length > 1) ? | |
_.findWhere(resultados, {'nombre': 'NIF'}) : resultados[0]; | |
} | |
function obtenerBusquedas() { | |
return util.getCache('get', 'E7713_RFX2_BUSCADOR') | |
.success(function(data) { | |
search.busquedas = _.where(data, {'F7713_SUFIJO': 'Contribuyentes'}); | |
return search.busquedas; | |
}); | |
} | |
function buscarPersonas(criterio, texto) { | |
var req = { | |
'pe_entorno': util.getEntorno(), | |
'pe_codusuario': util.getUserConex(), | |
'pe_codaplicacion': util.getCodAplicacion(), | |
'pe_tipo_busqueda': criterio, | |
'pe_idpersona': '', | |
'pe_nif': criterio === 'NIF' ? texto : '', | |
'pe_nombre': criterio !== 'NIF' ? texto : '', | |
'pe_lista_ids': '', | |
'pe_codrelacion': '', | |
'pe_dominio': 'G', | |
'pe_codzona': 0 | |
}; | |
return util.runFacade('PERSONAS_V4001', 'BuscarPersonas', req) | |
.success((res) => { | |
if (res.RETVAL !== -1) { | |
if (!search.noMostrarAvisos) { | |
return Promise.reject({'val': true, 'mensaje': res.PS_ERROR}); | |
} | |
search.mensajeError = res.PS_ERROR; | |
} | |
search.personas = res.PS_DATOS; | |
return search.personas; | |
}); | |
} | |
function buscar(evento) { | |
search.ngModel = null; | |
// Permitir borrar la persona | |
if (!search.texto && search.lastSearch) { | |
if (search.callback) { | |
search.callback({ | |
'persona': null, | |
'aviso': search.mensajeError, | |
'id': search.idBusqueda | |
}); | |
} | |
} | |
// Gestionar posibles eventos múltiples | |
if (search.texto && !search.evento && search.texto !== search.lastSearch) { | |
search.evento = evento.type; | |
util.establecerCargando(true, true); | |
buscarResultados(search.texto); | |
buscarPersonas(search.opcion.nombre, search.texto) | |
.success(() => { | |
// Si la búsqueda devuelve más de un resultado, redirijo a la búsqueda (v4001) | |
if (search.personas && search.personas.length > 1) { | |
util.crearVista('personas/V4001_personas', search.opcion.desc, { | |
'modoSeleccion': true, | |
'cache': search.personas, | |
'callback': search.callback, | |
'aviso': search.mensajeError, | |
'textoBusqueda': search.texto, | |
'idBusqueda': search.idBusqueda, | |
'opcionBusqueda': search.opcion.id, | |
'cambiarTexto': search.cambiarTexto | |
}); | |
} else { | |
search.cambiarTexto(search.personas[0]); | |
// Si está definido el callback, lo ejecuto | |
if (search.callback) { | |
search.callback({ | |
'persona': search.personas[0], | |
'aviso': search.mensajeError, | |
'id': search.idBusqueda | |
}); | |
} | |
} | |
}) | |
.error((data, status) => (data.val) ? util.mostrarAviso(data) | |
: util.lanzarError({'mensaje': 'No se ha podido realizar la búsqueda de personas.', | |
'data': data, 'status': status})) | |
.finally(() => { | |
search.evento = ''; | |
util.establecerCargando(false, true); | |
}); | |
} | |
} | |
} | |
], | |
link(scope, el, attrs, ctrl) { | |
function cambiarTexto(persona, apply) { | |
if (persona) { | |
scope.search.texto = scope.search.mostrarResultado ? | |
persona[scope.search.mostrarResultado.toUpperCase()] : | |
persona.NOMBRE; | |
scope.search.lastSearch = scope.search.texto; | |
if (apply) { scope.$apply(); } | |
} | |
} | |
ctrl.cambiarTexto = cambiarTexto; | |
scope.$watch(scope.search.ngModel, () => setTimeout(() => | |
cambiarTexto(scope.search.ngModel, true), 100)); | |
} | |
}; | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Insert into E7713_RFX2_BUSCADOR | |
(F7713_BUSQUEDA_ID, F6246_CODAPLICACION, F7713_BUSQUEDA_COD, F7713_NOMBRE, F7713_SUFIJO, | |
F7715_URL, F7713_EXPRESION_REGULAR, F7713_ORDEN) | |
Values | |
(26, 1, 'Buscar_Personas_NIF', 'NIF', 'sumaBuscarPersona', | |
'/personas/V4001_personas', '["^[\\w{1}|\\s]\\s{0,5}?\\d{1,8}\\w{1}$"]', 10); | |
Insert into E7713_RFX2_BUSCADOR | |
(F7713_BUSQUEDA_ID, F6246_CODAPLICACION, F7713_BUSQUEDA_COD, F7713_NOMBRE, F7713_SUFIJO, | |
F7715_URL, F7713_EXPRESION_REGULAR, F7713_ORDEN) | |
Values | |
(27, 1, 'Buscar_Personas_Nombre', 'Nombre', 'sumaBuscarPersona', | |
'/personas/V4001_personas', '["^[\\w\\s\"&ñÑ]*$"]', 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment