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