Commit e80493aa58944b1421a1ea3eb66d266d5242c2c7
Exists in
master
Merge branch 'master' into 'develop'
Master See merge request !20
Showing
1 changed file
Show diff stats
src/js/controllerModal.js
1 | angular.module('focaModal') | 1 | angular.module('focaModal') |
2 | .controller('focaModalController', [ | 2 | .controller('focaModalController', [ |
3 | '$timeout', | 3 | '$timeout', |
4 | '$filter', | 4 | '$filter', |
5 | '$scope', | 5 | '$scope', |
6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
7 | 'focaModalService', | 7 | 'focaModalService', |
8 | 'parametrosModal', | 8 | 'parametrosModal', |
9 | function($timeout, $filter, $scope, $uibModalInstance, focaModalService, parametrosModal) { | 9 | function($timeout, $filter, $scope, $uibModalInstance, focaModalService, parametrosModal) { |
10 | 10 | ||
11 | $scope.parametrosModal = parametrosModal; | 11 | $scope.parametrosModal = parametrosModal; |
12 | $scope.filters = ''; | 12 | $scope.filters = parametrosModal.searchText || ''; |
13 | $scope.entidades = []; | 13 | $scope.entidades = []; |
14 | $scope.primerBusqueda = false; | 14 | $scope.primerBusqueda = false; |
15 | $scope.searchLoading = false; | 15 | $scope.searchLoading = false; |
16 | // pagination | 16 | // pagination |
17 | $scope.numPerPage = 10; | 17 | $scope.numPerPage = 10; |
18 | $scope.currentPage = 1; | 18 | $scope.currentPage = 1; |
19 | $scope.filteredEntidades = []; | 19 | $scope.filteredEntidades = []; |
20 | $scope.currentPageEntidades = []; | 20 | $scope.currentPageEntidades = []; |
21 | $scope.selectedEntidad = -1; | 21 | $scope.selectedEntidad = -1; |
22 | 22 | ||
23 | $scope.busquedaPress = function(key) { | 23 | $scope.busquedaPress = function(key) { |
24 | if (key === 13) { | 24 | if (key === 13) { |
25 | $scope.searchLoading = true; | 25 | $scope.searchLoading = true; |
26 | if (parametrosModal.query) { | 26 | if (parametrosModal.query) { |
27 | focaModalService.getEntidad( | 27 | focaModalService.getEntidad( |
28 | $scope.filters, | 28 | $scope.filters, |
29 | parametrosModal.query, | 29 | parametrosModal.query, |
30 | parametrosModal.tipo, | 30 | parametrosModal.tipo, |
31 | parametrosModal.json | 31 | parametrosModal.json |
32 | ).then( | 32 | ).then( |
33 | function(res) { | 33 | function(res) { |
34 | $scope.searchLoading = false; | 34 | $scope.searchLoading = false; |
35 | $scope.primerBusqueda = true; | 35 | $scope.primerBusqueda = true; |
36 | $scope.entidades = res.data; | 36 | $scope.entidades = res.data; |
37 | filtros($scope.entidades); | 37 | filtros($scope.entidades); |
38 | $scope.search(true); | 38 | $scope.search(true); |
39 | } | 39 | } |
40 | ); | 40 | ); |
41 | } else { | 41 | } else { |
42 | $scope.searchLoading = false; | 42 | $scope.searchLoading = false; |
43 | $scope.primerBusqueda = true; | 43 | $scope.primerBusqueda = true; |
44 | $scope.entidades = parametrosModal.data; | 44 | $scope.entidades = parametrosModal.data; |
45 | filtros($scope.entidades); | 45 | filtros($scope.entidades); |
46 | $scope.search(true); | 46 | $scope.search(true); |
47 | } | 47 | } |
48 | } | 48 | } |
49 | }; | 49 | }; |
50 | function filtros(entidades) { | 50 | function filtros(entidades) { |
51 | for (var i = 0; i < $scope.parametrosModal.columnas.length; i++) { | 51 | for (var i = 0; i < $scope.parametrosModal.columnas.length; i++) { |
52 | var filtro = $scope.parametrosModal.columnas[i].filtro; | 52 | var filtro = $scope.parametrosModal.columnas[i].filtro; |
53 | if (filtro) { | 53 | if (filtro) { |
54 | if (filtro.nombre === 'comprobante') { | 54 | if (filtro.nombre === 'comprobante') { |
55 | for (var j = 0; j < entidades.length; j++) { | 55 | for (var j = 0; j < entidades.length; j++) { |
56 | var array = []; | 56 | var array = []; |
57 | $scope.parametrosModal.columnas[i].propiedad | 57 | $scope.parametrosModal.columnas[i].propiedad |
58 | .forEach(function(prop) { | 58 | .forEach(function(prop) { |
59 | array.push(entidades[j][prop]); | 59 | array.push(entidades[j][prop]); |
60 | }); | 60 | }); |
61 | entidades[j][$scope.parametrosModal.columnas[i].propiedad] = | 61 | entidades[j][$scope.parametrosModal.columnas[i].propiedad] = |
62 | $filter('comprobante')(array); | 62 | $filter('comprobante')(array); |
63 | } | 63 | } |
64 | } else { | 64 | } else { |
65 | for (var j = 0; j < entidades.length; j++) { | 65 | for (var j = 0; j < entidades.length; j++) { |
66 | entidades[j][$scope.parametrosModal.columnas[i].propiedad] = | 66 | entidades[j][$scope.parametrosModal.columnas[i].propiedad] = |
67 | $filter(filtro.nombre)(entidades[j][$scope | 67 | $filter(filtro.nombre)(entidades[j][$scope |
68 | .parametrosModal.columnas[i].propiedad], filtro.parametro); | 68 | .parametrosModal.columnas[i].propiedad], filtro.parametro); |
69 | } | 69 | } |
70 | } | 70 | } |
71 | } | 71 | } |
72 | } | 72 | } |
73 | } | 73 | } |
74 | $scope.search = function(pressed) { | 74 | $scope.search = function(pressed) { |
75 | if ($scope.entidades.length > 0) { | 75 | if ($scope.entidades.length > 0) { |
76 | $scope.filteredEntidades = $filter('filter')( | 76 | $scope.filteredEntidades = $filter('filter')( |
77 | $scope.entidades, { $: $scope.filters } | 77 | $scope.entidades, { $: $scope.filters } |
78 | ); | 78 | ); |
79 | $scope.lastPage = Math.ceil( | 79 | $scope.lastPage = Math.ceil( |
80 | $scope.filteredEntidades.length / $scope.numPerPage | 80 | $scope.filteredEntidades.length / $scope.numPerPage |
81 | ); | 81 | ); |
82 | $scope.resetPage(); | 82 | $scope.resetPage(); |
83 | } else if (pressed) { | 83 | } else if (pressed) { |
84 | $timeout(function() { | 84 | $timeout(function() { |
85 | angular.element('#search')[0].focus(); | 85 | angular.element('#search')[0].focus(); |
86 | $scope.filters = ''; | 86 | $scope.filters = ''; |
87 | }); | 87 | }); |
88 | } | 88 | } |
89 | }; | 89 | }; |
90 | 90 | ||
91 | $scope.resetPage = function() { | 91 | $scope.resetPage = function() { |
92 | $scope.currentPage = 1; | 92 | $scope.currentPage = 1; |
93 | $scope.selectPage(1); | 93 | $scope.selectPage(1); |
94 | }; | 94 | }; |
95 | 95 | ||
96 | $scope.selectPage = function(page) { | 96 | $scope.selectPage = function(page) { |
97 | var start = (page - 1) * $scope.numPerPage; | 97 | var start = (page - 1) * $scope.numPerPage; |
98 | var end = start + $scope.numPerPage; | 98 | var end = start + $scope.numPerPage; |
99 | $scope.paginas = []; | 99 | $scope.paginas = []; |
100 | $scope.paginas = calcularPages(page); | 100 | $scope.paginas = calcularPages(page); |
101 | $scope.currentPageEntidades = $scope.filteredEntidades.slice(start, end); | 101 | $scope.currentPageEntidades = $scope.filteredEntidades.slice(start, end); |
102 | $scope.currentPage = page; | 102 | $scope.currentPage = page; |
103 | }; | 103 | }; |
104 | 104 | ||
105 | $scope.select = function(vendedor) { | 105 | $scope.select = function(vendedor) { |
106 | $uibModalInstance.close(vendedor); | 106 | $uibModalInstance.close(vendedor); |
107 | }; | 107 | }; |
108 | 108 | ||
109 | $scope.cancel = function() { | 109 | $scope.cancel = function() { |
110 | $uibModalInstance.dismiss('cancel'); | 110 | $uibModalInstance.dismiss('cancel'); |
111 | }; | 111 | }; |
112 | 112 | ||
113 | $scope.busquedaDown = function(key) { | 113 | $scope.busquedaDown = function(key) { |
114 | if (key === 40) { | 114 | if (key === 40) { |
115 | primera(key); | 115 | primera(key); |
116 | } | 116 | } |
117 | }; | 117 | }; |
118 | 118 | ||
119 | $scope.itemEntidad = function(key) { | 119 | $scope.itemEntidad = function(key) { |
120 | if (key === 38) { | 120 | if (key === 38) { |
121 | anterior(key); | 121 | anterior(key); |
122 | } | 122 | } |
123 | if (key === 40) { | 123 | if (key === 40) { |
124 | siguiente(key); | 124 | siguiente(key); |
125 | } | 125 | } |
126 | if (key === 37) { | 126 | if (key === 37) { |
127 | retrocederPagina(); | 127 | retrocederPagina(); |
128 | } | 128 | } |
129 | if (key === 39) { | 129 | if (key === 39) { |
130 | avanzarPagina(); | 130 | avanzarPagina(); |
131 | } | 131 | } |
132 | }; | 132 | }; |
133 | $scope.esFecha = function(fecha) { | 133 | $scope.esFecha = function(fecha) { |
134 | if (fecha.includes('fecha')) { | 134 | if (fecha.includes('fecha')) { |
135 | return true; | 135 | return true; |
136 | } | 136 | } |
137 | return false; | 137 | return false; |
138 | }; | 138 | }; |
139 | 139 | ||
140 | function calcularPages(paginaActual) { | 140 | function calcularPages(paginaActual) { |
141 | var paginas = []; | 141 | var paginas = []; |
142 | paginas.push(paginaActual); | 142 | paginas.push(paginaActual); |
143 | 143 | ||
144 | if (paginaActual - 1 > 1) { | 144 | if (paginaActual - 1 > 1) { |
145 | 145 | ||
146 | paginas.unshift(paginaActual - 1); | 146 | paginas.unshift(paginaActual - 1); |
147 | if (paginaActual - 2 > 1) { | 147 | if (paginaActual - 2 > 1) { |
148 | paginas.unshift(paginaActual - 2); | 148 | paginas.unshift(paginaActual - 2); |
149 | } | 149 | } |
150 | } | 150 | } |
151 | if (paginaActual + 1 < $scope.lastPage) { | 151 | if (paginaActual + 1 < $scope.lastPage) { |
152 | paginas.push(paginaActual + 1); | 152 | paginas.push(paginaActual + 1); |
153 | if (paginaActual + 2 < $scope.lastPage) { | 153 | if (paginaActual + 2 < $scope.lastPage) { |
154 | paginas.push(paginaActual + 2); | 154 | paginas.push(paginaActual + 2); |
155 | } | 155 | } |
156 | } | 156 | } |
157 | if (paginaActual !== 1) { | 157 | if (paginaActual !== 1) { |
158 | paginas.unshift(1); | 158 | paginas.unshift(1); |
159 | } | 159 | } |
160 | if (paginaActual !== $scope.lastPage) { | 160 | if (paginaActual !== $scope.lastPage) { |
161 | paginas.push($scope.lastPage); | 161 | paginas.push($scope.lastPage); |
162 | } | 162 | } |
163 | return paginas; | 163 | return paginas; |
164 | } | 164 | } |
165 | 165 | ||
166 | function primera() { | 166 | function primera() { |
167 | $scope.selectedEntidad = 0; | 167 | $scope.selectedEntidad = 0; |
168 | } | 168 | } |
169 | 169 | ||
170 | function anterior() { | 170 | function anterior() { |
171 | if ($scope.selectedEntidad === 0 && $scope.currentPage > 1) { | 171 | if ($scope.selectedEntidad === 0 && $scope.currentPage > 1) { |
172 | retrocederPagina(); | 172 | retrocederPagina(); |
173 | } else { | 173 | } else { |
174 | $scope.selectedEntidad--; | 174 | $scope.selectedEntidad--; |
175 | } | 175 | } |
176 | } | 176 | } |
177 | 177 | ||
178 | function siguiente() { | 178 | function siguiente() { |
179 | if($scope.selectedEntidad < $scope.currentPageEntidades.length - 1) { | 179 | if($scope.selectedEntidad < $scope.currentPageEntidades.length - 1) { |
180 | $scope.selectedEntidad++; | 180 | $scope.selectedEntidad++; |
181 | } else { | 181 | } else { |
182 | avanzarPagina(); | 182 | avanzarPagina(); |
183 | } | 183 | } |
184 | } | 184 | } |
185 | 185 | ||
186 | function retrocederPagina() { | 186 | function retrocederPagina() { |
187 | if ($scope.currentPage > 1) { | 187 | if ($scope.currentPage > 1) { |
188 | $scope.selectPage($scope.currentPage - 1); | 188 | $scope.selectPage($scope.currentPage - 1); |
189 | $scope.selectedEntidad = $scope.numPerPage - 1; | 189 | $scope.selectedEntidad = $scope.numPerPage - 1; |
190 | } | 190 | } |
191 | } | 191 | } |
192 | 192 | ||
193 | function avanzarPagina() { | 193 | function avanzarPagina() { |
194 | if ($scope.currentPage < $scope.lastPage) { | 194 | if ($scope.currentPage < $scope.lastPage) { |
195 | $scope.selectPage($scope.currentPage + 1); | 195 | $scope.selectPage($scope.currentPage + 1); |
196 | $scope.selectedEntidad = 0; | 196 | $scope.selectedEntidad = 0; |
197 | } | 197 | } |
198 | } | 198 | } |
199 | $scope.busquedaPress(13); | 199 | $scope.busquedaPress(13); |
200 | }] | 200 | }] |
201 | ); | 201 | ); |
202 | 202 |