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