Commit 0b377ec13fef10e67d4b5355921442b8610801be

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

Codigo identado.

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