Skip to content

Instantly share code, notes, and snippets.

@gonzaloruizdevilla
Last active December 18, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzaloruizdevilla/5763846 to your computer and use it in GitHub Desktop.
Save gonzaloruizdevilla/5763846 to your computer and use it in GitHub Desktop.
// Directives
(function (angular) {
'use strict';
var filtroVisor = "filtrovisor";
function FilterVisor($timeout, $http, $q) {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, element, attr, controller, http) {
var requestCanceler,
buscadorFocus = 0,
elemetParent = element.parent(),
sbPredictivo = $('.predictivoPoliza');
scope.sbStatusPredictivo = 0;
elemetParent.append('<span class="iconSearch" ng-show="_iconSearch"></span>');
element.bind('keyup', function(evt) {
var suppressKeyPressRepeat = ~$.inArray(evt.which, [40,38,9,13,27]);
if (!suppressKeyPressRepeat) callServices();
if (suppressKeyPressRepeat) {
scope.$apply(handleKeypress(evt, evt.which));
}
});
var timeStamp = 200 ;
element.bind('focusout',function(evt){
console.log(scope.callPoliza);
if(requestCanceler)
requestCanceler.resolve();
$timeout(function(){
sbPredictivo.hide()
}, timeStamp );
iconSearch(false);
})
function cancelaTimeOut() {
$timeout.cancel(buscadorFocus);
}
var LImiteCarateres = 3,
tiempoDePeticion = 1200;
function callServices(){
sbPredictivo.hide()
validaCamposBusqueda();
if (element.val().length >= LImiteCarateres) {
console.log('entro');
cancelaTimeOut();
buscadorFocus = $timeout(bucarPoliza, tiempoDePeticion);
};
}
function scrollPredictive(id, tipo){
console.log(id);
var factorLimit = 4 ,
factor = id %= factorLimit;
if(tipo=='baja' && factor==0){
sbPredictivo.scrollTop(sbPredictivo.find('li').eq(0).outerHeight(true) * scope.sbStatusPredictivo);
}
if (tipo=='sube' && factor==0) {
sbPredictivo.scrollTop(sbPredictivo.find('li').eq(0).outerHeight(true) * (scope.sbStatusPredictivo - 3));
};
}
function handleKeypress (evt, key) {
// console.log(key)
// baja
if (key == 40 && scope.sbStatusPredictivo < scope.model.listaInformacionPolizas.length - 1){
scope.sbStatusPredictivo += 1;
scrollPredictive(scope.sbStatusPredictivo, 'baja');
}
//sube
if (key == 38 && scope.sbStatusPredictivo > 0) {
scope.sbStatusPredictivo -=1 ;
scrollPredictive(scope.sbStatusPredictivo, 'sube');
}
if (key == 38 && scope.sbStatusPredictivo < 0){
scope.sbStatusPredictivo = scope.model.listaInformacionPolizas.length - 1 ;
scrollPredictive(scope.sbStatusPredictivo, 'sube');
}
//enter
if (key == 13|| key == 39){
scope.completaPoliza(scope.model.listaInformacionPolizas[scope.sbStatusPredictivo]);
sbPredictivo.hide();
}
//salir
if (key == 27) sbPredictivo.hide();
};
// busca la poliza cunado se cumple los campos del filtro
function bucarPoliza () {
if (validaCamposBusqueda()) {
scope.tipoDeBusqueda = attr.filtrovisor;
iconSearch(true);
// scope.callPoliza = $q.defer();
requestCanceler = $q.defer();
scope.callPoliza = $http.post(
dynPath + '/api/visor/consultas/consultaPolizaSeguro/' + attr.filtrovisor,
scope.filtroConsulta,
{timeout: requestCanceler.promise}
);
scope.callPoliza.then(dataPoliza);
// scope.callPoliza
console.log(scope.callPoliza);
scope.sbStatusPredictivotive = 0;
};
}
var dataPoliza = function (response) {
scope.model = response.data;
if(scope.model.errorFormDto) {
angular.showError(scope,{msg:scope.model.errorFormDto.operacionMensaje[0]});
scope.sbClean(false);
scope.muestraDetalle = false;
} else {
iconSearch(false);
sbPredictivo.show();
}
}
// Valida los compos sean correctos en el filtro y el producto de la consulta
function validaCamposBusqueda () {
scope.showCuenta = false;
if (scope.filtroConsulta.producto == undefined) {
angular.showError(scope,{msg:'Selecciona un producto para poder continuar.'})
return false;
} else {
if (attr.filtrovisor=='consultaPorCuenta') scope.showCuenta = true
return true
}
}
function iconSearch (tipo){
var icon = element.parent().find('span.iconSearch'),
iconTop = element.offset().top + (element.height() / 2) - 8,
iconLeft = element.offset().left + (element.width() - 4),
predictivoTop = element.offset().top + (element.height() + 2);
icon.css({top:iconTop, left:iconLeft});
sbPredictivo.css({top:predictivoTop})
if(tipo){
icon.show();
}else{
icon.hide();
}
}
}
}
}
FilterVisor.$inject = ['$timeout', '$http', '$q']
angular.module('directiveVisor', [])
.directive(filtroVisor, FilterVisor)
})(angular);
@gonzaloruizdevilla
Copy link
Author

Creo que quiere cancelar en timeout, por eso cancelo en

if(requestCanceler)
  requestCanceler.resolve();

Al crear la petición, genero la promesa de cancelación:

   requestCanceler = $q.defer();

que uso en la petición:

$http.post(
   dynPath + '/api/visor/consultas/consultaPolizaSeguro/' + attr.filtrovisor, 
   scope.filtroConsulta,
    {timeout: requestCanceler.promise}
);

No lo he probado, lo he escrito directamente sin ejecutarlo, no me responsabilizo de los fallos :)

Por otra parte, no deberías guardar cosas en el scope que no debas, mételas en variables locales de la función link. AngularJs hace 'dirty checks' para detectar cambios en el scope, y revisa esos objetos cuando no hace falta.

@Yarov
Copy link

Yarov commented Jun 12, 2013

OK. Gracias, lo pruebo, reviso lo que me comentas
y te aviso . Slds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment