Commit 6e5546fc00f251b31fc4b24840ddb7183c8cafa0
1 parent
76bcf36f2f
Exists in
master
quito console info
Showing
1 changed file
with
0 additions
and
1 deletions
 
Show diff stats
src/js/controller.js
| 1 | angular.module('focaBusquedaCliente') | 1 | angular.module('focaBusquedaCliente') | 
| 2 | .controller('focaBusquedaClienteModalController', [ | 2 | .controller('focaBusquedaClienteModalController', [ | 
| 3 | '$uibModalInstance', 'focaBusquedaClienteService', '$scope', '$filter', | 3 | '$uibModalInstance', 'focaBusquedaClienteService', '$scope', '$filter', | 
| 4 | function($uibModalInstance, focaBusquedaClienteService, $scope, $filter) { | 4 | function($uibModalInstance, focaBusquedaClienteService, $scope, $filter) { | 
| 5 | 5 | ||
| 6 | $scope.filters = ''; | 6 | $scope.filters = ''; | 
| 7 | $scope.primerBusqueda = false; | 7 | $scope.primerBusqueda = false; | 
| 8 | // pagination | 8 | // pagination | 
| 9 | $scope.numPerPage = 10; | 9 | $scope.numPerPage = 10; | 
| 10 | $scope.currentPage = 1; | 10 | $scope.currentPage = 1; | 
| 11 | $scope.filteredClientes = []; | 11 | $scope.filteredClientes = []; | 
| 12 | $scope.currentPageClientes = []; | 12 | $scope.currentPageClientes = []; | 
| 13 | $scope.selectedClientes = -1; | 13 | $scope.selectedClientes = -1; | 
| 14 | 14 | ||
| 15 | $scope.busquedaPress = function(key) { | 15 | $scope.busquedaPress = function(key) { | 
| 16 | if (key === 13) { | 16 | if (key === 13) { | 
| 17 | $scope.searchLoading = true; | 17 | $scope.searchLoading = true; | 
| 18 | focaBusquedaClienteService.obtenerClientesPorNombreOCuit($scope.filters).then( | 18 | focaBusquedaClienteService.obtenerClientesPorNombreOCuit($scope.filters).then( | 
| 19 | function(res) { | 19 | function(res) { | 
| 20 | $scope.primerBusqueda = true; | 20 | $scope.primerBusqueda = true; | 
| 21 | $scope.clientes = res.data; | 21 | $scope.clientes = res.data; | 
| 22 | $scope.search(); | 22 | $scope.search(); | 
| 23 | primera(); | 23 | primera(); | 
| 24 | $scope.searchLoading = false; | 24 | $scope.searchLoading = false; | 
| 25 | }); | 25 | }); | 
| 26 | } | 26 | } | 
| 27 | }; | 27 | }; | 
| 28 | 28 | ||
| 29 | $scope.search = function () { | 29 | $scope.search = function () { | 
| 30 | if($scope.primerBusqueda) { | 30 | if($scope.primerBusqueda) { | 
| 31 | $scope.filteredClientes = $filter('filter')( | 31 | $scope.filteredClientes = $filter('filter')( | 
| 32 | $scope.clientes, {$: $scope.filters} | 32 | $scope.clientes, {$: $scope.filters} | 
| 33 | ); | 33 | ); | 
| 34 | 34 | ||
| 35 | $scope.lastPage = Math.ceil( | 35 | $scope.lastPage = Math.ceil( | 
| 36 | $scope.filteredClientes.length / $scope.numPerPage | 36 | $scope.filteredClientes.length / $scope.numPerPage | 
| 37 | ); | 37 | ); | 
| 38 | 38 | ||
| 39 | $scope.resetPage(); | 39 | $scope.resetPage(); | 
| 40 | } | 40 | } | 
| 41 | }; | 41 | }; | 
| 42 | 42 | ||
| 43 | $scope.resetPage = function () { | 43 | $scope.resetPage = function () { | 
| 44 | $scope.currentPage = 1; | 44 | $scope.currentPage = 1; | 
| 45 | $scope.selectPage(1); | 45 | $scope.selectPage(1); | 
| 46 | }; | 46 | }; | 
| 47 | 47 | ||
| 48 | $scope.selectPage = function (page) { | 48 | $scope.selectPage = function (page) { | 
| 49 | console.info(page); | ||
| 50 | var start = (page - 1) * $scope.numPerPage; | 49 | var start = (page - 1) * $scope.numPerPage; | 
| 51 | var end = start + $scope.numPerPage; | 50 | var end = start + $scope.numPerPage; | 
| 52 | $scope.paginas = []; | 51 | $scope.paginas = []; | 
| 53 | $scope.paginas = calcularPages(page); | 52 | $scope.paginas = calcularPages(page); | 
| 54 | $scope.currentPageClientes = $scope.filteredClientes.slice(start, end); | 53 | $scope.currentPageClientes = $scope.filteredClientes.slice(start, end); | 
| 55 | $scope.currentPage = page; | 54 | $scope.currentPage = page; | 
| 56 | }; | 55 | }; | 
| 57 | 56 | ||
| 58 | $scope.select = function(cliente) { | 57 | $scope.select = function(cliente) { | 
| 59 | $uibModalInstance.close(cliente); | 58 | $uibModalInstance.close(cliente); | 
| 60 | }; | 59 | }; | 
| 61 | 60 | ||
| 62 | $scope.cancel = function() { | 61 | $scope.cancel = function() { | 
| 63 | $uibModalInstance.dismiss('cancel'); | 62 | $uibModalInstance.dismiss('cancel'); | 
| 64 | }; | 63 | }; | 
| 65 | 64 | ||
| 66 | $scope.busquedaDown = function(key) { | 65 | $scope.busquedaDown = function(key) { | 
| 67 | if (key === 40) { | 66 | if (key === 40) { | 
| 68 | primera(key); | 67 | primera(key); | 
| 69 | } | 68 | } | 
| 70 | }; | 69 | }; | 
| 71 | 70 | ||
| 72 | $scope.itemCliente = function(key) { | 71 | $scope.itemCliente = function(key) { | 
| 73 | if (key === 38) { | 72 | if (key === 38) { | 
| 74 | anterior(key); | 73 | anterior(key); | 
| 75 | } | 74 | } | 
| 76 | 75 | ||
| 77 | if (key === 40) { | 76 | if (key === 40) { | 
| 78 | siguiente(key); | 77 | siguiente(key); | 
| 79 | } | 78 | } | 
| 80 | 79 | ||
| 81 | if (key === 37) { | 80 | if (key === 37) { | 
| 82 | retrocederPagina(); | 81 | retrocederPagina(); | 
| 83 | } | 82 | } | 
| 84 | 83 | ||
| 85 | if (key === 39) { | 84 | if (key === 39) { | 
| 86 | avanzarPagina(); | 85 | avanzarPagina(); | 
| 87 | } | 86 | } | 
| 88 | }; | 87 | }; | 
| 89 | 88 | ||
| 90 | function calcularPages(paginaActual) { | 89 | function calcularPages(paginaActual) { | 
| 91 | var paginas = []; | 90 | var paginas = []; | 
| 92 | paginas.push(paginaActual); | 91 | paginas.push(paginaActual); | 
| 93 | 92 | ||
| 94 | if (paginaActual - 1 > 1) { | 93 | if (paginaActual - 1 > 1) { | 
| 95 | 94 | ||
| 96 | paginas.unshift(paginaActual - 1); | 95 | paginas.unshift(paginaActual - 1); | 
| 97 | if (paginaActual - 2 > 1) { | 96 | if (paginaActual - 2 > 1) { | 
| 98 | paginas.unshift(paginaActual - 2); | 97 | paginas.unshift(paginaActual - 2); | 
| 99 | } | 98 | } | 
| 100 | } | 99 | } | 
| 101 | 100 | ||
| 102 | if (paginaActual + 1 < $scope.lastPage) { | 101 | if (paginaActual + 1 < $scope.lastPage) { | 
| 103 | paginas.push(paginaActual + 1); | 102 | paginas.push(paginaActual + 1); | 
| 104 | if (paginaActual + 2 < $scope.lastPage) { | 103 | if (paginaActual + 2 < $scope.lastPage) { | 
| 105 | paginas.push(paginaActual + 2); | 104 | paginas.push(paginaActual + 2); | 
| 106 | } | 105 | } | 
| 107 | } | 106 | } | 
| 108 | 107 | ||
| 109 | if (paginaActual !== 1) { | 108 | if (paginaActual !== 1) { | 
| 110 | paginas.unshift(1); | 109 | paginas.unshift(1); | 
| 111 | } | 110 | } | 
| 112 | 111 | ||
| 113 | if (paginaActual !== $scope.lastPage) { | 112 | if (paginaActual !== $scope.lastPage) { | 
| 114 | paginas.push($scope.lastPage); | 113 | paginas.push($scope.lastPage); | 
| 115 | } | 114 | } | 
| 116 | 115 | ||
| 117 | return paginas; | 116 | return paginas; | 
| 118 | } | 117 | } | 
| 119 | 118 | ||
| 120 | function primera() { | 119 | function primera() { | 
| 121 | $scope.selectedClientes = 0; | 120 | $scope.selectedClientes = 0; | 
| 122 | } | 121 | } | 
| 123 | 122 | ||
| 124 | function anterior() { | 123 | function anterior() { | 
| 125 | if ($scope.selectedClientes === 0 && $scope.currentPage > 1) { | 124 | if ($scope.selectedClientes === 0 && $scope.currentPage > 1) { | 
| 126 | retrocederPagina(); | 125 | retrocederPagina(); | 
| 127 | } else { | 126 | } else { | 
| 128 | $scope.selectedClientes--; | 127 | $scope.selectedClientes--; | 
| 129 | } | 128 | } | 
| 130 | } | 129 | } | 
| 131 | 130 | ||
| 132 | function siguiente() { | 131 | function siguiente() { | 
| 133 | if ($scope.selectedClientes < $scope.currentPageClientes.length - 1 ) { | 132 | if ($scope.selectedClientes < $scope.currentPageClientes.length - 1 ) { | 
| 134 | $scope.selectedClientes++; | 133 | $scope.selectedClientes++; | 
| 135 | } else { | 134 | } else { | 
| 136 | avanzarPagina(); | 135 | avanzarPagina(); | 
| 137 | } | 136 | } | 
| 138 | } | 137 | } | 
| 139 | 138 | ||
| 140 | function retrocederPagina() { | 139 | function retrocederPagina() { | 
| 141 | if ($scope.currentPage > 1) { | 140 | if ($scope.currentPage > 1) { | 
| 142 | $scope.selectPage($scope.currentPage - 1); | 141 | $scope.selectPage($scope.currentPage - 1); | 
| 143 | $scope.selectedClientes = $scope.numPerPage - 1; | 142 | $scope.selectedClientes = $scope.numPerPage - 1; | 
| 144 | } | 143 | } | 
| 145 | } | 144 | } | 
| 146 | 145 | ||
| 147 | function avanzarPagina() { | 146 | function avanzarPagina() { | 
| 148 | if ($scope.currentPage < $scope.lastPage) { | 147 | if ($scope.currentPage < $scope.lastPage) { | 
| 149 | $scope.selectPage($scope.currentPage + 1); | 148 | $scope.selectPage($scope.currentPage + 1); | 
| 150 | $scope.selectedClientes = 0; | 149 | $scope.selectedClientes = 0; | 
| 151 | } | 150 | } | 
| 152 | } | 151 | } | 
| 153 | } | 152 | } | 
| 154 | ]); | 153 | ]); | 
| 155 | 154 |