Commit 07923ceeebf1652a4c577a6266e2fa8b067609cc

Authored by Eric Fernandez
Exists in master

Merge branch 'develop' into 'master'

Develop

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