angular.module('focaModalListaPrecio') .controller('focaModalListaPrecioCtrl', [ '$timeout', '$filter', '$scope', '$uibModalInstance', 'focaModalListaPrecioService', function($timeout, $filter, $scope, $uibModalInstance, focaModalListaPrecioService) { $scope.filters = ''; $scope.listasPrecio = []; $scope.primerBusqueda = false; $scope.searchLoading = false; // pagination $scope.numPerPage = 10; $scope.currentPage = 1; $scope.filteredListasPrecio = []; $scope.currentPageListasPrecio = []; $scope.selectedListaPrecio = -1; $scope.busquedaPress = function(key) { if(key === 13) { $scope.searchLoading = true; focaModalListaPrecioService.getListasPrecio($scope.filters).then( function(res) { $scope.searchLoading = false; $scope.primerBusqueda = true; $scope.listasPrecio = res.data; $scope.search(true); primera(); } ); } }; $scope.busquedaPress(13); $scope.search = function(pressed) { if($scope.listasPrecio.length > 0) { $scope.filteredListasPrecio = $filter('filter')( $scope.listasPrecio, { $: $scope.filters } ); $scope.lastPage = Math.ceil( $scope.filteredListasPrecio.length / $scope.numPerPage ); $scope.resetPage(); }else if(pressed) { $timeout(function() { angular.element('#search')[0].focus(); $scope.filters = ''; }); } }; $scope.resetPage = function() { $scope.currentPage = 1; $scope.selectPage(1); }; $scope.selectPage = function(page) { var start = (page - 1) * $scope.numPerPage; var end = start + $scope.numPerPage; $scope.paginas = []; $scope.paginas = calcularPages(page); $scope.currentPageListasPrecio = $scope.filteredListasPrecio.slice(start, end); $scope.currentPage = page; }; $scope.select = function(vendedor) { $uibModalInstance.close(vendedor); }; $scope.cancel = function() { $uibModalInstance.dismiss('cancel'); }; $scope.busquedaDown = function(key) { if(key === 40) { primera(key); } }; $scope.itemListaPrecio = function(key) { if(key === 38) { anterior(key); } if(key === 40) { siguiente(key); } if(key === 37) { retrocederPagina(); } if(key === 39) { avanzarPagina(); } }; function calcularPages(paginaActual) { var paginas = []; paginas.push(paginaActual); if(paginaActual - 1 > 1) { paginas.unshift(paginaActual - 1); if(paginaActual - 2 > 1) { paginas.unshift(paginaActual - 2); } } if(paginaActual + 1 < $scope.lastPage) { paginas.push(paginaActual + 1); if(paginaActual + 2 < $scope.lastPage) { paginas.push(paginaActual + 2); } } if(paginaActual !== 1) { paginas.unshift(1); } if(paginaActual !== $scope.lastPage) { paginas.push($scope.lastPage); } return paginas; } function primera() { $scope.selectedListaPrecio = 0; } function anterior() { if($scope.selectedListaPrecio === 0 && $scope.currentPage > 1) { retrocederPagina(); }else { $scope.selectedListaPrecio--; } } function siguiente() { if($scope.selectedListaPrecio < $scope.currentPageListasPrecio.length - 1) { $scope.selectedListaPrecio++; }else { avanzarPagina(); } } function retrocederPagina() { if($scope.currentPage > 1) { $scope.selectPage($scope.currentPage - 1); $scope.selectedListaPrecio = $scope.numPerPage - 1; } } function avanzarPagina() { if($scope.currentPage < $scope.lastPage) { $scope.selectPage($scope.currentPage + 1); $scope.selectedListaPrecio = 0; } } }] );