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