Commit 465f3c869d702a4ab1e3f0d655470014a204785b
1 parent
93354f3ec5
Exists in
develop
Change
Codigo reordenado
Showing
1 changed file
with
216 additions
and
222 deletions
Show diff stats
src/js/controller.js
1 | 1 | angular.module('focaModalRemito') |
2 | - .controller('focaModalRemitoController', | |
3 | - [ | |
4 | - '$timeout', | |
5 | - '$filter', | |
6 | - '$scope', | |
7 | - '$uibModalInstance', | |
8 | - 'focaModalRemitoService', | |
9 | - 'usadoPor', | |
10 | - 'focaModalService', | |
11 | - function ($timeout, $filter, $scope, $uibModalInstance, | |
12 | - focaModalRemitoService, usadoPor, focaModalService | |
13 | - ) { | |
14 | - var fecha = new Date(); | |
15 | - $scope.fechaHasta = new Date(); | |
16 | - $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | |
17 | - $scope.filtersCliente = ''; | |
2 | + .controller('focaModalRemitoController', | |
3 | + [ | |
4 | + '$timeout', '$filter', '$scope', '$uibModalInstance', | |
5 | + 'focaModalRemitoService', 'usadoPor', 'focaModalService', | |
6 | + function ($timeout, $filter, $scope, $uibModalInstance, | |
7 | + focaModalRemitoService, usadoPor, focaModalService | |
8 | + ) { | |
9 | + var fecha = new Date(); | |
10 | + $scope.fechaHasta = new Date(); | |
11 | + $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | |
12 | + $scope.filtersCliente = ''; | |
13 | + $scope.filtersRemito = ''; | |
14 | + $scope.remitos = []; | |
15 | + $scope.isCollapsed = false; | |
16 | + $scope.verProductos = {}; | |
17 | + $scope.primerBusqueda = false; | |
18 | + $scope.searchLoading = false; | |
19 | + // pagination | |
20 | + $scope.numPerPage = 10; | |
21 | + $scope.currentPage = 1; | |
22 | + $scope.filteredRemitos = []; | |
23 | + $scope.currentPageRemitos = []; | |
24 | + $scope.selectedRemito = -1; | |
25 | + | |
26 | + //METODOS | |
27 | + $scope.busquedaPress = function (key) { | |
28 | + if (key === 13) { | |
29 | + if (!$scope.fechaDesde) { | |
30 | + focaModalService | |
31 | + .alert('INGRESE FECHA DESDE'); | |
32 | + return; | |
33 | + } | |
34 | + if (!$scope.fechaHasta) { | |
35 | + focaModalService | |
36 | + .alert('INGRESE FECHA HASTA'); | |
37 | + return; | |
38 | + } | |
39 | + if ($scope.fechaDesde > $scope.fechaHasta) { | |
40 | + focaModalService | |
41 | + .alert('La fecha desde no puede ser mayor a la fecha hasta'); | |
42 | + return; | |
43 | + } | |
44 | + $scope.searchLoading = true; | |
45 | + //TODO: usar filtros en vez de toISOString | |
46 | + focaModalRemitoService | |
47 | + .getRemitos(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], | |
48 | + $scope.fechaHasta.toISOString().split('.')[0]) | |
49 | + .then(llenarDatos); | |
50 | + } | |
51 | + }; | |
52 | + | |
53 | + function llenarDatos(res) { | |
54 | + $scope.remitos = []; | |
55 | + $scope.filteredRemitos = []; | |
56 | + $scope.currentPageRemitos = []; | |
57 | + $scope.selectedRemito = -1; | |
58 | + $scope.searchLoading = false; | |
59 | + $scope.primerBusqueda = true; | |
60 | + $scope.remitos = res.data; | |
61 | + $scope.searchRemito(true); | |
62 | + primera(); | |
63 | + } | |
64 | + | |
65 | + $scope.searchRemito = function (pressed) { | |
66 | + if ($scope.remitos.length > 0) { | |
67 | + $scope.filteredRemitos = $filter('filter')( | |
68 | + $scope.remitos, | |
69 | + { numeroRemito: $scope.filtersRemito } | |
70 | + ); | |
71 | + | |
72 | + if (pressed && $scope.filteredRemitos.length === 0) { | |
73 | + $timeout(function () { | |
74 | + angular.element('#search')[0].focus(); | |
18 | 75 | $scope.filtersRemito = ''; |
19 | - $scope.remitos = []; | |
20 | - $scope.isCollapsed = false; | |
21 | - $scope.verProductos = {}; | |
22 | - $scope.primerBusqueda = false; | |
23 | - $scope.searchLoading = false; | |
24 | - // pagination | |
25 | - $scope.numPerPage = 10; | |
26 | - $scope.currentPage = 1; | |
27 | - $scope.filteredRemitos = []; | |
28 | - $scope.currentPageRemitos = []; | |
29 | - $scope.selectedRemito = -1; | |
30 | - | |
31 | - //METODOS | |
32 | - $scope.busquedaPress = function (key) { | |
33 | - if (key === 13) { | |
34 | - if (!$scope.fechaDesde) { | |
35 | - focaModalService | |
36 | - .alert('INGRESE FECHA DESDE'); | |
37 | - return; | |
38 | - } | |
39 | - if (!$scope.fechaHasta) { | |
40 | - focaModalService | |
41 | - .alert('INGRESE FECHA HASTA'); | |
42 | - return; | |
43 | - } | |
44 | - if ($scope.fechaDesde > $scope.fechaHasta) { | |
45 | - focaModalService | |
46 | - .alert('La fecha desde no puede ser mayor a la fecha hasta'); | |
47 | - return; | |
48 | - } | |
49 | - $scope.searchLoading = true; | |
50 | - //TODO: usar filtros en vez de toISOString | |
51 | - focaModalRemitoService | |
52 | - .getRemitos(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], | |
53 | - $scope.fechaHasta.toISOString().split('.')[0]) | |
54 | - .then(llenarDatos); | |
55 | - } | |
56 | - }; | |
57 | - | |
58 | - function llenarDatos(res) { | |
59 | - $scope.remitos = []; | |
60 | - $scope.filteredRemitos = []; | |
61 | - $scope.currentPageRemitos = []; | |
62 | - $scope.selectedRemito = -1; | |
63 | - $scope.searchLoading = false; | |
64 | - $scope.primerBusqueda = true; | |
65 | - $scope.remitos = res.data; | |
66 | - $scope.searchRemito(true); | |
67 | - primera(); | |
68 | - } | |
69 | - | |
70 | - $scope.searchRemito = function (pressed) { | |
71 | - if ($scope.remitos.length > 0) { | |
72 | - $scope.filteredRemitos = $filter('filter')( | |
73 | - $scope.remitos, | |
74 | - { numeroRemito: $scope.filtersRemito } | |
75 | - ); | |
76 | - | |
77 | - if (pressed && $scope.filteredRemitos.length === 0) { | |
78 | - $timeout(function () { | |
79 | - angular.element('#search')[0].focus(); | |
80 | - $scope.filtersRemito = ''; | |
81 | - }); | |
82 | - } | |
83 | - | |
84 | - $scope.lastPage = Math.ceil( | |
85 | - $scope.filteredRemitos.length / $scope.numPerPage | |
86 | - ); | |
87 | - | |
88 | - $scope.resetPage(); | |
89 | - } | |
90 | - } | |
91 | - | |
92 | - $scope.searchCliente = function (pressed) { | |
93 | - if ($scope.remitos.length > 0) { | |
94 | - $scope.filteredRemitos = $filter('filter')( | |
95 | - $scope.remitos, | |
96 | - { nombreCliente: $scope.filtersCliente } | |
97 | - ); | |
98 | - | |
99 | - if (pressed && $scope.filteredRemitos.length === 0) { | |
100 | - $timeout(function () { | |
101 | - angular.element('#search')[0].focus(); | |
102 | - $scope.filtersCliente = ''; | |
103 | - }); | |
104 | - } | |
105 | - | |
106 | - $scope.lastPage = Math.ceil( | |
107 | - $scope.filteredRemitos.length / $scope.numPerPage | |
108 | - ); | |
109 | - | |
110 | - $scope.resetPage(); | |
111 | - } | |
112 | - }; | |
113 | - | |
114 | - $scope.resetPage = function () { | |
115 | - $scope.currentPage = 1; | |
116 | - $scope.selectPage(1); | |
117 | - }; | |
118 | - | |
119 | - $scope.selectPage = function (page) { | |
120 | - var start = (page - 1) * $scope.numPerPage; | |
121 | - var end = start + $scope.numPerPage; | |
122 | - $scope.paginas = []; | |
123 | - $scope.paginas = calcularPages(page); | |
124 | - $scope.currentPageRemitos = $scope.filteredRemitos.slice(start, end); | |
125 | - $scope.currentPage = page; | |
126 | - }; | |
127 | - | |
128 | - $scope.select = function (remito) { | |
129 | - $uibModalInstance.close(remito); | |
130 | - }; | |
131 | - | |
132 | - $scope.cancel = function () { | |
133 | - $uibModalInstance.dismiss('cancel'); | |
134 | - }; | |
135 | - | |
136 | - $scope.busquedaDown = function (key) { | |
137 | - if (key === 40) { | |
138 | - primera(key); | |
139 | - } | |
140 | - }; | |
141 | - | |
142 | - $scope.itemRemito = function (key) { | |
143 | - if (key === 38) { | |
144 | - anterior(key); | |
145 | - } | |
146 | - | |
147 | - if (key === 40) { | |
148 | - siguiente(key); | |
149 | - } | |
150 | - | |
151 | - if (key === 37) { | |
152 | - retrocederPagina(); | |
153 | - } | |
154 | - | |
155 | - if (key === 39) { | |
156 | - avanzarPagina(); | |
157 | - } | |
158 | - }; | |
159 | - | |
160 | - function calcularPages(paginaActual) { | |
161 | - var paginas = []; | |
162 | - paginas.push(paginaActual); | |
163 | - | |
164 | - if (paginaActual - 1 > 1) { | |
165 | - | |
166 | - paginas.unshift(paginaActual - 1); | |
167 | - if (paginaActual - 2 > 1) { | |
168 | - paginas.unshift(paginaActual - 2); | |
169 | - } | |
170 | - } | |
171 | - | |
172 | - if (paginaActual + 1 < $scope.lastPage) { | |
173 | - paginas.push(paginaActual + 1); | |
174 | - if (paginaActual + 2 < $scope.lastPage) { | |
175 | - paginas.push(paginaActual + 2); | |
176 | - } | |
177 | - } | |
178 | - | |
179 | - if (paginaActual !== 1) { | |
180 | - paginas.unshift(1); | |
181 | - } | |
182 | - | |
183 | - if (paginaActual !== $scope.lastPage) { | |
184 | - paginas.push($scope.lastPage); | |
185 | - } | |
186 | - | |
187 | - return paginas; | |
188 | - } | |
189 | - | |
190 | - function primera() { | |
191 | - $scope.selectedRemito = 0; | |
192 | - } | |
193 | - | |
194 | - function anterior() { | |
195 | - if ($scope.selectedRemito === 0 && $scope.currentPage > 1) { | |
196 | - retrocederPagina(); | |
197 | - } else { | |
198 | - $scope.selectedRemito--; | |
199 | - } | |
200 | - } | |
201 | - | |
202 | - function siguiente() { | |
203 | - if ($scope.selectedRemito < $scope.currentPageRemitos.length - 1) { | |
204 | - $scope.selectedRemito++; | |
205 | - } else { | |
206 | - avanzarPagina(); | |
207 | - } | |
208 | - } | |
209 | - | |
210 | - function retrocederPagina() { | |
211 | - if ($scope.currentPage > 1) { | |
212 | - $scope.selectPage($scope.currentPage - 1); | |
213 | - $scope.selectedRemito = $scope.numPerPage - 1; | |
214 | - } | |
215 | - } | |
216 | - | |
217 | - function avanzarPagina() { | |
218 | - if ($scope.currentPage < $scope.lastPage) { | |
219 | - $scope.selectPage($scope.currentPage + 1); | |
220 | - $scope.selectedRemito = 0; | |
221 | - } | |
222 | - } | |
76 | + }); | |
77 | + } | |
78 | + | |
79 | + $scope.lastPage = Math.ceil( | |
80 | + $scope.filteredRemitos.length / $scope.numPerPage | |
81 | + ); | |
82 | + | |
83 | + $scope.resetPage(); | |
84 | + } | |
85 | + } | |
86 | + | |
87 | + $scope.searchCliente = function (pressed) { | |
88 | + if ($scope.remitos.length > 0) { | |
89 | + $scope.filteredRemitos = $filter('filter')( | |
90 | + $scope.remitos, | |
91 | + { nombreCliente: $scope.filtersCliente } | |
92 | + ); | |
93 | + | |
94 | + if (pressed && $scope.filteredRemitos.length === 0) { | |
95 | + $timeout(function () { | |
96 | + angular.element('#search')[0].focus(); | |
97 | + $scope.filtersCliente = ''; | |
98 | + }); | |
99 | + } | |
100 | + | |
101 | + $scope.lastPage = Math.ceil( | |
102 | + $scope.filteredRemitos.length / $scope.numPerPage | |
103 | + ); | |
104 | + | |
105 | + $scope.resetPage(); | |
106 | + } | |
107 | + }; | |
108 | + | |
109 | + $scope.resetPage = function () { | |
110 | + $scope.currentPage = 1; | |
111 | + $scope.selectPage(1); | |
112 | + }; | |
113 | + | |
114 | + $scope.selectPage = function (page) { | |
115 | + var start = (page - 1) * $scope.numPerPage; | |
116 | + var end = start + $scope.numPerPage; | |
117 | + $scope.paginas = []; | |
118 | + $scope.paginas = calcularPages(page); | |
119 | + $scope.currentPageRemitos = $scope.filteredRemitos.slice(start, end); | |
120 | + $scope.currentPage = page; | |
121 | + }; | |
122 | + | |
123 | + $scope.select = function (remito) { | |
124 | + $uibModalInstance.close(remito); | |
125 | + }; | |
126 | + | |
127 | + $scope.cancel = function () { | |
128 | + $uibModalInstance.dismiss('cancel'); | |
129 | + }; | |
130 | + | |
131 | + $scope.busquedaDown = function (key) { | |
132 | + if (key === 40) { | |
133 | + primera(key); | |
134 | + } | |
135 | + }; | |
136 | + | |
137 | + $scope.itemRemito = function (key) { | |
138 | + switch (key) { | |
139 | + case 38: | |
140 | + anterior(key); | |
141 | + break; | |
142 | + case 40: | |
143 | + siguiente(key); | |
144 | + break; | |
145 | + case 37: | |
146 | + retrocederPagina(); | |
147 | + break; | |
148 | + case 39: | |
149 | + avanzarPagina(); | |
150 | + break; | |
151 | + } | |
152 | + }; | |
153 | + | |
154 | + function calcularPages(paginaActual) { | |
155 | + var paginas = []; | |
156 | + paginas.push(paginaActual); | |
157 | + | |
158 | + if (paginaActual - 1 > 1) { | |
159 | + | |
160 | + paginas.unshift(paginaActual - 1); | |
161 | + if (paginaActual - 2 > 1) { | |
162 | + paginas.unshift(paginaActual - 2); | |
163 | + } | |
164 | + } | |
165 | + | |
166 | + if (paginaActual + 1 < $scope.lastPage) { | |
167 | + paginas.push(paginaActual + 1); | |
168 | + if (paginaActual + 2 < $scope.lastPage) { | |
169 | + paginas.push(paginaActual + 2); | |
223 | 170 | } |
224 | - ] | |
225 | - ); | |
171 | + } | |
172 | + | |
173 | + if (paginaActual !== 1) { | |
174 | + paginas.unshift(1); | |
175 | + } | |
176 | + | |
177 | + if (paginaActual !== $scope.lastPage) { | |
178 | + paginas.push($scope.lastPage); | |
179 | + } | |
180 | + | |
181 | + return paginas; | |
182 | + } | |
183 | + | |
184 | + function primera() { | |
185 | + $scope.selectedRemito = 0; | |
186 | + } | |
187 | + | |
188 | + function anterior() { | |
189 | + if ($scope.selectedRemito === 0 && $scope.currentPage > 1) { | |
190 | + retrocederPagina(); | |
191 | + } else { | |
192 | + $scope.selectedRemito--; | |
193 | + } | |
194 | + } | |
195 | + | |
196 | + function siguiente() { | |
197 | + if ($scope.selectedRemito < $scope.currentPageRemitos.length - 1) { | |
198 | + $scope.selectedRemito++; | |
199 | + } else { | |
200 | + avanzarPagina(); | |
201 | + } | |
202 | + } | |
203 | + | |
204 | + function retrocederPagina() { | |
205 | + if ($scope.currentPage > 1) { | |
206 | + $scope.selectPage($scope.currentPage - 1); | |
207 | + $scope.selectedRemito = $scope.numPerPage - 1; | |
208 | + } | |
209 | + } | |
210 | + | |
211 | + function avanzarPagina() { | |
212 | + if ($scope.currentPage < $scope.lastPage) { | |
213 | + $scope.selectPage($scope.currentPage + 1); | |
214 | + $scope.selectedRemito = 0; | |
215 | + } | |
216 | + } | |
217 | + } | |
218 | + ] | |
219 | + ); |