Commit 0b377ec13fef10e67d4b5355921442b8610801be
1 parent
a5ff1d469c
Exists in
master
Codigo identado.
Showing
1 changed file
with
34 additions
and
44 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 | '$timeout', | 4 | '$timeout', | 
| 5 | '$filter', | 5 | '$filter', | 
| 6 | '$scope', | 6 | '$scope', | 
| 7 | '$uibModalInstance', | 7 | '$uibModalInstance', | 
| 8 | 'parametroProducto', | 8 | 'parametroProducto', | 
| 9 | 'focaBusquedaProductosService', | 9 | 'focaBusquedaProductosService', | 
| 10 | function($timeout, $filter, $scope, $uibModalInstance, parametroProducto, | 10 | function ($timeout, $filter, $scope, $uibModalInstance, parametroProducto, | 
| 11 | focaBusquedaProductosService | 11 | focaBusquedaProductosService | 
| 12 | ) { | 12 | ) { | 
| 13 | 13 | ||
| 14 | $scope.simbolo = parametroProducto.simbolo; | 14 | $scope.simbolo = parametroProducto.simbolo; | 
| 15 | $scope.filters = ''; | 15 | $scope.filters = ''; | 
| 16 | $scope.productos = []; | 16 | $scope.productos = []; | 
| 17 | $scope.primerBusqueda = false; | 17 | $scope.primerBusqueda = false; | 
| 18 | $scope.searchLoading = false; | 18 | $scope.searchLoading = false; | 
| 19 | // pagination | 19 | $scope.soloMostrar = parametroProducto.soloMostrar; | 
| 20 | //#region pagination variables | ||
| 20 | $scope.numPerPage = 10; | 21 | $scope.numPerPage = 10; | 
| 21 | $scope.currentPage = 1; | 22 | $scope.currentPage = 1; | 
| 22 | $scope.filteredProductos = []; | 23 | $scope.filteredProductos = []; | 
| 23 | $scope.currentPageProductos = []; | 24 | $scope.currentPageProductos = []; | 
| 24 | $scope.selectedProducto = -1; | 25 | $scope.selectedProducto = -1; | 
| 26 | //#endregion | ||
| 25 | 27 | ||
| 26 | $scope.soloMostrar = parametroProducto.soloMostrar; | ||
| 27 | |||
| 28 | //METODOS | 28 | //METODOS | 
| 29 | $scope.busquedaPress = function(key) { | 29 | $scope.busquedaPress = function (key) { | 
| 30 | if (key === 13) { | 30 | if (key === 13) { | 
| 31 | $scope.searchLoading = true; | 31 | $scope.searchLoading = true; | 
| 32 | if(parametroProducto.idLista > 0) { | 32 | if (parametroProducto.idLista > 0) { | 
| 33 | focaBusquedaProductosService | 33 | focaBusquedaProductosService.getProductosByIdLista(parametroProducto.idLista, $scope.filters) | 
| 34 | .getProductosByIdLista(parametroProducto.idLista, $scope.filters) | ||
| 35 | .then(llenarDatos); | 34 | .then(llenarDatos); | 
| 36 | } else if(parametroProducto.idLista === -1) { | 35 | } else if (parametroProducto.idLista === -1) { | 
| 37 | focaBusquedaProductosService.getProductos() | 36 | focaBusquedaProductosService.getProductos() | 
| 38 | .then(llenarDatos); | 37 | .then(llenarDatos); | 
| 39 | } | 38 | } | 
| 40 | } | 39 | } | 
| 41 | }; | 40 | }; | 
| 42 | function llenarDatos(res) { | 41 | function llenarDatos(res) { | 
| 43 | for(var i = 0; i < res.data.length; i++) { | 42 | for (var i = 0; i < res.data.length; i++) { | 
| 44 | res.data[i].precio = res.data[i].precio / parametroProducto.cotizacion; | 43 | res.data[i].precio = res.data[i].precio / parametroProducto.cotizacion; | 
| 45 | } | 44 | } | 
| 46 | $scope.searchLoading = false; | 45 | $scope.searchLoading = false; | 
| 47 | $scope.primerBusqueda = true; | 46 | $scope.primerBusqueda = true; | 
| 48 | $scope.productos = res.data; | 47 | $scope.productos = res.data; | 
| 49 | $scope.search(true); | 48 | $scope.search(true); | 
| 50 | primera(); | 49 | primera(); | 
| 51 | } | 50 | } | 
| 52 | $scope.search = function(pressed) { | 51 | $scope.search = function (pressed) { | 
| 53 | if($scope.productos.length > 0) { | 52 | if ($scope.productos.length > 0) { | 
| 54 | $scope.filteredProductos = $filter('filter')( | 53 | $scope.filteredProductos = $filter('filter')( | 
| 55 | $scope.productos, | 54 | $scope.productos, | 
| 56 | {$: $scope.filters} | 55 | { $: $scope.filters } | 
| 57 | ); | 56 | ); | 
| 58 | 57 | ||
| 59 | $scope.lastPage = Math.ceil( | 58 | $scope.lastPage = Math.ceil( | 
| 60 | $scope.filteredProductos.length / $scope.numPerPage | 59 | $scope.filteredProductos.length / $scope.numPerPage | 
| 61 | ); | 60 | ); | 
| 62 | 61 | ||
| 63 | $scope.resetPage(); | 62 | $scope.resetPage(); | 
| 64 | }else if(pressed) { | 63 | } else if (pressed) { | 
| 65 | $timeout(function() { | 64 | $timeout(function () { | 
| 66 | angular.element('#search')[0].focus(); | 65 | angular.element('#search')[0].focus(); | 
| 67 | $scope.filters = ''; | 66 | $scope.filters = ''; | 
| 68 | }); | 67 | }); | 
| 69 | } | 68 | } | 
| 70 | }; | 69 | }; | 
| 71 | 70 | $scope.resetPage = function () { | |
| 72 | $scope.resetPage = function() { | ||
| 73 | $scope.currentPage = 1; | 71 | $scope.currentPage = 1; | 
| 74 | $scope.selectPage(1); | 72 | $scope.selectPage(1); | 
| 75 | }; | 73 | }; | 
| 76 | 74 | $scope.selectPage = function (page) { | |
| 77 | $scope.selectPage = function(page) { | ||
| 78 | var start = (page - 1) * $scope.numPerPage; | 75 | var start = (page - 1) * $scope.numPerPage; | 
| 79 | var end = start + $scope.numPerPage; | 76 | var end = start + $scope.numPerPage; | 
| 80 | $scope.paginas = []; | 77 | $scope.paginas = []; | 
| 81 | $scope.paginas = calcularPages(page); | 78 | $scope.paginas = calcularPages(page); | 
| 82 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); | 79 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); | 
| 83 | $scope.currentPage = page; | 80 | $scope.currentPage = page; | 
| 84 | }; | 81 | }; | 
| 85 | 82 | $scope.select = function (producto) { | |
| 86 | $scope.select = function(producto) { | ||
| 87 | $uibModalInstance.close(producto); | 83 | $uibModalInstance.close(producto); | 
| 88 | }; | 84 | }; | 
| 89 | 85 | $scope.cancel = function () { | |
| 90 | $scope.cancel = function() { | ||
| 91 | $uibModalInstance.dismiss('cancel'); | 86 | $uibModalInstance.dismiss('cancel'); | 
| 92 | }; | 87 | }; | 
| 93 | 88 | $scope.busquedaDown = function (key) { | |
| 94 | $scope.busquedaDown = function(key) { | ||
| 95 | if (key === 40) { | 89 | if (key === 40) { | 
| 96 | primera(key); | 90 | primera(key); | 
| 97 | } | 91 | } | 
| 98 | }; | 92 | }; | 
| 99 | 93 | $scope.itemProducto = function (key) { | |
| 100 | $scope.itemProducto = function(key) { | ||
| 101 | if (key === 38) { | 94 | if (key === 38) { | 
| 102 | anterior(key); | 95 | anterior(key); | 
| 103 | } | 96 | } | 
| 104 | 97 | ||
| 105 | if (key === 40) { | 98 | if (key === 40) { | 
| 106 | siguiente(key); | 99 | siguiente(key); | 
| 107 | } | 100 | } | 
| 108 | 101 | ||
| 109 | if (key === 37) { | 102 | if (key === 37) { | 
| 110 | retrocederPagina(); | 103 | retrocederPagina(); | 
| 111 | } | 104 | } | 
| 112 | 105 | ||
| 113 | if (key === 39) { | 106 | if (key === 39) { | 
| 114 | avanzarPagina(); | 107 | avanzarPagina(); | 
| 115 | } | 108 | } | 
| 116 | }; | 109 | }; | 
| 117 | 110 | ||
| 111 | //#region Paginador | ||
| 118 | function calcularPages(paginaActual) { | 112 | function calcularPages(paginaActual) { | 
| 119 | var paginas = []; | 113 | var paginas = []; | 
| 120 | paginas.push(paginaActual); | 114 | paginas.push(paginaActual); | 
| 121 | 115 | ||
| 122 | if (paginaActual - 1 > 1) { | 116 | if (paginaActual - 1 > 1) { | 
| 123 | 117 | ||
| 124 | paginas.unshift(paginaActual - 1); | 118 | paginas.unshift(paginaActual - 1); | 
| 125 | if (paginaActual - 2 > 1) { | 119 | if (paginaActual - 2 > 1) { | 
| 126 | paginas.unshift(paginaActual - 2); | 120 | paginas.unshift(paginaActual - 2); | 
| 127 | } | 121 | } | 
| 128 | } | 122 | } | 
| 129 | 123 | ||
| 130 | if (paginaActual + 1 < $scope.lastPage) { | 124 | if (paginaActual + 1 < $scope.lastPage) { | 
| 131 | paginas.push(paginaActual + 1); | 125 | paginas.push(paginaActual + 1); | 
| 132 | if (paginaActual + 2 < $scope.lastPage) { | 126 | if (paginaActual + 2 < $scope.lastPage) { | 
| 133 | paginas.push(paginaActual + 2); | 127 | paginas.push(paginaActual + 2); | 
| 134 | } | 128 | } | 
| 135 | } | 129 | } | 
| 136 | 130 | ||
| 137 | if (paginaActual !== 1) { | 131 | if (paginaActual !== 1) { | 
| 138 | paginas.unshift(1); | 132 | paginas.unshift(1); | 
| 139 | } | 133 | } | 
| 140 | 134 | ||
| 141 | if (paginaActual !== $scope.lastPage) { | 135 | if (paginaActual !== $scope.lastPage) { | 
| 142 | paginas.push($scope.lastPage); | 136 | paginas.push($scope.lastPage); | 
| 143 | } | 137 | } | 
| 144 | 138 | ||
| 145 | return paginas; | 139 | return paginas; | 
| 146 | } | 140 | }; | 
| 147 | |||
| 148 | function primera() { | 141 | function primera() { | 
| 149 | $scope.selectedProducto = 0; | 142 | $scope.selectedProducto = 0; | 
| 150 | } | 143 | }; | 
| 151 | |||
| 152 | function anterior() { | 144 | function anterior() { | 
| 153 | if ($scope.selectedProducto === 0 && $scope.currentPage > 1) { | 145 | if ($scope.selectedProducto === 0 && $scope.currentPage > 1) { | 
| 154 | retrocederPagina(); | 146 | retrocederPagina(); | 
| 155 | } else { | 147 | } else { | 
| 156 | $scope.selectedProducto--; | 148 | $scope.selectedProducto--; | 
| 157 | } | 149 | } | 
| 158 | } | 150 | }; | 
| 159 | |||
| 160 | function siguiente() { | 151 | function siguiente() { | 
| 161 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { | 152 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1) { | 
| 162 | $scope.selectedProducto++; | 153 | $scope.selectedProducto++; | 
| 163 | } else { | 154 | } else { | 
| 164 | avanzarPagina(); | 155 | avanzarPagina(); | 
| 165 | } | 156 | } | 
| 166 | } | 157 | }; | 
| 167 | |||
| 168 | function retrocederPagina() { | 158 | function retrocederPagina() { | 
| 169 | if ($scope.currentPage > 1) { | 159 | if ($scope.currentPage > 1) { | 
| 170 | $scope.selectPage($scope.currentPage - 1); | 160 | $scope.selectPage($scope.currentPage - 1); | 
| 171 | $scope.selectedProducto = $scope.numPerPage - 1; | 161 | $scope.selectedProducto = $scope.numPerPage - 1; | 
| 172 | } | 162 | } | 
| 173 | } | 163 | }; | 
| 174 | |||
| 175 | function avanzarPagina() { | 164 | function avanzarPagina() { | 
| 176 | if ($scope.currentPage < $scope.lastPage) { | 165 | if ($scope.currentPage < $scope.lastPage) { | 
| 177 | $scope.selectPage($scope.currentPage + 1); | 166 | $scope.selectPage($scope.currentPage + 1); | 
| 178 | $scope.selectedProducto = 0; | 167 | $scope.selectedProducto = 0; | 
| 179 | } | 168 | } | 
| 180 | } | 169 | }; | 
| 170 | //#endregion | ||
| 181 | 171 | ||
| 182 | $scope.busquedaPress(13); | 172 | $scope.busquedaPress(13); |