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