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 | angular.module('focaModalRemito') | 1 | angular.module('focaModalRemito') |
2 | .controller('focaModalRemitoController', | 2 | .controller('focaModalRemitoController', |
3 | [ | 3 | [ |
4 | '$timeout', | 4 | '$timeout', '$filter', '$scope', '$uibModalInstance', |
5 | '$filter', | 5 | 'focaModalRemitoService', 'usadoPor', 'focaModalService', |
6 | '$scope', | 6 | function ($timeout, $filter, $scope, $uibModalInstance, |
7 | '$uibModalInstance', | 7 | focaModalRemitoService, usadoPor, focaModalService |
8 | 'focaModalRemitoService', | 8 | ) { |
9 | 'usadoPor', | 9 | var fecha = new Date(); |
10 | 'focaModalService', | 10 | $scope.fechaHasta = new Date(); |
11 | function ($timeout, $filter, $scope, $uibModalInstance, | 11 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); |
12 | focaModalRemitoService, usadoPor, focaModalService | 12 | $scope.filtersCliente = ''; |
13 | ) { | 13 | $scope.filtersRemito = ''; |
14 | var fecha = new Date(); | 14 | $scope.remitos = []; |
15 | $scope.fechaHasta = new Date(); | 15 | $scope.isCollapsed = false; |
16 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | 16 | $scope.verProductos = {}; |
17 | $scope.filtersCliente = ''; | 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 | $scope.filtersRemito = ''; | 75 | $scope.filtersRemito = ''; |
19 | $scope.remitos = []; | 76 | }); |
20 | $scope.isCollapsed = false; | 77 | } |
21 | $scope.verProductos = {}; | 78 | |
22 | $scope.primerBusqueda = false; | 79 | $scope.lastPage = Math.ceil( |
23 | $scope.searchLoading = false; | 80 | $scope.filteredRemitos.length / $scope.numPerPage |
24 | // pagination | 81 | ); |
25 | $scope.numPerPage = 10; | 82 | |
26 | $scope.currentPage = 1; | 83 | $scope.resetPage(); |
27 | $scope.filteredRemitos = []; | 84 | } |
28 | $scope.currentPageRemitos = []; | 85 | } |
29 | $scope.selectedRemito = -1; | 86 | |
30 | 87 | $scope.searchCliente = function (pressed) { | |
31 | //METODOS | 88 | if ($scope.remitos.length > 0) { |
32 | $scope.busquedaPress = function (key) { | 89 | $scope.filteredRemitos = $filter('filter')( |
33 | if (key === 13) { | 90 | $scope.remitos, |
34 | if (!$scope.fechaDesde) { | 91 | { nombreCliente: $scope.filtersCliente } |
35 | focaModalService | 92 | ); |
36 | .alert('INGRESE FECHA DESDE'); | 93 | |
37 | return; | 94 | if (pressed && $scope.filteredRemitos.length === 0) { |
38 | } | 95 | $timeout(function () { |
39 | if (!$scope.fechaHasta) { | 96 | angular.element('#search')[0].focus(); |
40 | focaModalService | 97 | $scope.filtersCliente = ''; |
41 | .alert('INGRESE FECHA HASTA'); | 98 | }); |
42 | return; | 99 | } |
43 | } | 100 | |
44 | if ($scope.fechaDesde > $scope.fechaHasta) { | 101 | $scope.lastPage = Math.ceil( |
45 | focaModalService | 102 | $scope.filteredRemitos.length / $scope.numPerPage |
46 | .alert('La fecha desde no puede ser mayor a la fecha hasta'); | 103 | ); |
47 | return; | 104 | |
48 | } | 105 | $scope.resetPage(); |
49 | $scope.searchLoading = true; | 106 | } |
50 | //TODO: usar filtros en vez de toISOString | 107 | }; |
51 | focaModalRemitoService | 108 | |
52 | .getRemitos(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], | 109 | $scope.resetPage = function () { |
53 | $scope.fechaHasta.toISOString().split('.')[0]) | 110 | $scope.currentPage = 1; |
54 | .then(llenarDatos); | 111 | $scope.selectPage(1); |
55 | } | 112 | }; |
56 | }; | 113 | |
57 | 114 | $scope.selectPage = function (page) { | |
58 | function llenarDatos(res) { | 115 | var start = (page - 1) * $scope.numPerPage; |
59 | $scope.remitos = []; | 116 | var end = start + $scope.numPerPage; |
60 | $scope.filteredRemitos = []; | 117 | $scope.paginas = []; |
61 | $scope.currentPageRemitos = []; | 118 | $scope.paginas = calcularPages(page); |
62 | $scope.selectedRemito = -1; | 119 | $scope.currentPageRemitos = $scope.filteredRemitos.slice(start, end); |
63 | $scope.searchLoading = false; | 120 | $scope.currentPage = page; |
64 | $scope.primerBusqueda = true; | 121 | }; |
65 | $scope.remitos = res.data; | 122 | |
66 | $scope.searchRemito(true); | 123 | $scope.select = function (remito) { |
67 | primera(); | 124 | $uibModalInstance.close(remito); |
68 | } | 125 | }; |
69 | 126 | ||
70 | $scope.searchRemito = function (pressed) { | 127 | $scope.cancel = function () { |
71 | if ($scope.remitos.length > 0) { | 128 | $uibModalInstance.dismiss('cancel'); |
72 | $scope.filteredRemitos = $filter('filter')( | 129 | }; |
73 | $scope.remitos, | 130 | |
74 | { numeroRemito: $scope.filtersRemito } | 131 | $scope.busquedaDown = function (key) { |
75 | ); | 132 | if (key === 40) { |
76 | 133 | primera(key); | |
77 | if (pressed && $scope.filteredRemitos.length === 0) { | 134 | } |
78 | $timeout(function () { | 135 | }; |
79 | angular.element('#search')[0].focus(); | 136 | |
80 | $scope.filtersRemito = ''; | 137 | $scope.itemRemito = function (key) { |
81 | }); | 138 | switch (key) { |
82 | } | 139 | case 38: |
83 | 140 | anterior(key); | |
84 | $scope.lastPage = Math.ceil( | 141 | break; |
85 | $scope.filteredRemitos.length / $scope.numPerPage | 142 | case 40: |
86 | ); | 143 | siguiente(key); |
87 | 144 | break; | |
88 | $scope.resetPage(); | 145 | case 37: |
89 | } | 146 | retrocederPagina(); |
90 | } | 147 | break; |
91 | 148 | case 39: | |
92 | $scope.searchCliente = function (pressed) { | 149 | avanzarPagina(); |
93 | if ($scope.remitos.length > 0) { | 150 | break; |
94 | $scope.filteredRemitos = $filter('filter')( | 151 | } |
95 | $scope.remitos, | 152 | }; |
96 | { nombreCliente: $scope.filtersCliente } | 153 | |
97 | ); | 154 | function calcularPages(paginaActual) { |
98 | 155 | var paginas = []; | |
99 | if (pressed && $scope.filteredRemitos.length === 0) { | 156 | paginas.push(paginaActual); |
100 | $timeout(function () { | 157 | |
101 | angular.element('#search')[0].focus(); | 158 | if (paginaActual - 1 > 1) { |
102 | $scope.filtersCliente = ''; | 159 | |
103 | }); | 160 | paginas.unshift(paginaActual - 1); |
104 | } | 161 | if (paginaActual - 2 > 1) { |
105 | 162 | paginas.unshift(paginaActual - 2); | |
106 | $scope.lastPage = Math.ceil( | 163 | } |
107 | $scope.filteredRemitos.length / $scope.numPerPage | 164 | } |
108 | ); | 165 | |
109 | 166 | if (paginaActual + 1 < $scope.lastPage) { | |
110 | $scope.resetPage(); | 167 | paginas.push(paginaActual + 1); |
111 | } | 168 | if (paginaActual + 2 < $scope.lastPage) { |
112 | }; | 169 | paginas.push(paginaActual + 2); |
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 | } |