Commit 7e9d15313a18bdf2e91f76e3d96d6aae52126888

Authored by Marcelo Puebla
1 parent 21dbaa7385
Exists in master and in 1 other branch develop

agregado salto de linea.

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