Commit 132d8046f914255febad8e66c1177db984d828d9
1 parent
ecb228f8ab
Exists in
master
and in
1 other branch
fuera ToDo
Showing
1 changed file
with
0 additions
and
1 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalNotaPedido') | 1 | angular.module('focaModalNotaPedido') |
| 2 | .controller('focaModalNotaPedidoController', | 2 | .controller('focaModalNotaPedidoController', |
| 3 | [ | 3 | [ |
| 4 | '$timeout', | 4 | '$timeout', |
| 5 | '$filter', | 5 | '$filter', |
| 6 | '$scope', | 6 | '$scope', |
| 7 | '$uibModalInstance', | 7 | '$uibModalInstance', |
| 8 | 'focaModalNotaPedidoService', | 8 | 'focaModalNotaPedidoService', |
| 9 | 'usadoPor', | 9 | 'usadoPor', |
| 10 | 'focaModalService', | 10 | 'focaModalService', |
| 11 | function($timeout, $filter, $scope, $uibModalInstance, | 11 | function($timeout, $filter, $scope, $uibModalInstance, |
| 12 | focaModalNotaPedidoService, usadoPor, focaModalService | 12 | focaModalNotaPedidoService, usadoPor, focaModalService |
| 13 | ) { | 13 | ) { |
| 14 | var fecha = new Date(); | 14 | var fecha = new Date(); |
| 15 | $scope.fechaHasta = new Date(); | 15 | $scope.fechaHasta = new Date(); |
| 16 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | 16 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); |
| 17 | $scope.filters = ''; | 17 | $scope.filters = ''; |
| 18 | $scope.notasPedido = []; | 18 | $scope.notasPedido = []; |
| 19 | $scope.primerBusqueda = false; | 19 | $scope.primerBusqueda = false; |
| 20 | $scope.searchLoading = false; | 20 | $scope.searchLoading = false; |
| 21 | // pagination | 21 | // pagination |
| 22 | $scope.numPerPage = 10; | 22 | $scope.numPerPage = 10; |
| 23 | $scope.currentPage = 1; | 23 | $scope.currentPage = 1; |
| 24 | $scope.filteredNotasPedido = []; | 24 | $scope.filteredNotasPedido = []; |
| 25 | $scope.currentPageNotasPedido = []; | 25 | $scope.currentPageNotasPedido = []; |
| 26 | $scope.selectedNotaPedido = -1; | 26 | $scope.selectedNotaPedido = -1; |
| 27 | 27 | ||
| 28 | //METODOS | 28 | //METODOS |
| 29 | $scope.busquedaPress = function(key) { | 29 | $scope.busquedaPress = function(key) { |
| 30 | if (key === 13) { | 30 | if (key === 13) { |
| 31 | //TODO Validaciones con alertas | ||
| 32 | if(!$scope.fechaDesde) { | 31 | if(!$scope.fechaDesde) { |
| 33 | focaModalService | 32 | focaModalService |
| 34 | .alert('INGRESE FECHA DESDE'); | 33 | .alert('INGRESE FECHA DESDE'); |
| 35 | return; | 34 | return; |
| 36 | } | 35 | } |
| 37 | if(!$scope.fechaHasta) { | 36 | if(!$scope.fechaHasta) { |
| 38 | focaModalService | 37 | focaModalService |
| 39 | .alert('INGRESE FECHA HASTA'); | 38 | .alert('INGRESE FECHA HASTA'); |
| 40 | return; | 39 | return; |
| 41 | } | 40 | } |
| 42 | if($scope.fechaDesde > $scope.fechaHasta) { | 41 | if($scope.fechaDesde > $scope.fechaHasta) { |
| 43 | focaModalService | 42 | focaModalService |
| 44 | .alert('La fecha desde no puede ser mayor a la fecha hasta'); | 43 | .alert('La fecha desde no puede ser mayor a la fecha hasta'); |
| 45 | return; | 44 | return; |
| 46 | } | 45 | } |
| 47 | $scope.searchLoading = true; | 46 | $scope.searchLoading = true; |
| 48 | //TODO hacer filtro de fecha | 47 | //TODO hacer filtro de fecha |
| 49 | focaModalNotaPedidoService | 48 | focaModalNotaPedidoService |
| 50 | .getNotasPedido(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], | 49 | .getNotasPedido(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], |
| 51 | $scope.fechaHasta.toISOString().split('.')[0]) | 50 | $scope.fechaHasta.toISOString().split('.')[0]) |
| 52 | .then(llenarDatos); | 51 | .then(llenarDatos); |
| 53 | } | 52 | } |
| 54 | }; | 53 | }; |
| 55 | function llenarDatos(res) { | 54 | function llenarDatos(res) { |
| 56 | $scope.notasPedido = []; | 55 | $scope.notasPedido = []; |
| 57 | $scope.filteredNotasPedido = []; | 56 | $scope.filteredNotasPedido = []; |
| 58 | $scope.currentPageNotasPedido = []; | 57 | $scope.currentPageNotasPedido = []; |
| 59 | $scope.selectedNotaPedido = -1; | 58 | $scope.selectedNotaPedido = -1; |
| 60 | $scope.searchLoading = false; | 59 | $scope.searchLoading = false; |
| 61 | $scope.primerBusqueda = true; | 60 | $scope.primerBusqueda = true; |
| 62 | $scope.notasPedido = res.data; | 61 | $scope.notasPedido = res.data; |
| 63 | $scope.search(true); | 62 | $scope.search(true); |
| 64 | primera(); | 63 | primera(); |
| 65 | } | 64 | } |
| 66 | $scope.search = function(pressed) { | 65 | $scope.search = function(pressed) { |
| 67 | if($scope.notasPedido.length > 0) { | 66 | if($scope.notasPedido.length > 0) { |
| 68 | $scope.filteredNotasPedido = $filter('filter')( | 67 | $scope.filteredNotasPedido = $filter('filter')( |
| 69 | $scope.notasPedido, | 68 | $scope.notasPedido, |
| 70 | {$: $scope.filters} | 69 | {$: $scope.filters} |
| 71 | ); | 70 | ); |
| 72 | 71 | ||
| 73 | $scope.lastPage = Math.ceil( | 72 | $scope.lastPage = Math.ceil( |
| 74 | $scope.filteredNotasPedido.length / $scope.numPerPage | 73 | $scope.filteredNotasPedido.length / $scope.numPerPage |
| 75 | ); | 74 | ); |
| 76 | 75 | ||
| 77 | $scope.resetPage(); | 76 | $scope.resetPage(); |
| 78 | if(pressed && $scope.filteredNotasPedido.length === 0){ | 77 | if(pressed && $scope.filteredNotasPedido.length === 0){ |
| 79 | $timeout(function() { | 78 | $timeout(function() { |
| 80 | angular.element('#search')[0].focus(); | 79 | angular.element('#search')[0].focus(); |
| 81 | $scope.filters = ''; | 80 | $scope.filters = ''; |
| 82 | }); | 81 | }); |
| 83 | } | 82 | } |
| 84 | } | 83 | } |
| 85 | }; | 84 | }; |
| 86 | 85 | ||
| 87 | $scope.resetPage = function() { | 86 | $scope.resetPage = function() { |
| 88 | $scope.currentPage = 1; | 87 | $scope.currentPage = 1; |
| 89 | $scope.selectPage(1); | 88 | $scope.selectPage(1); |
| 90 | }; | 89 | }; |
| 91 | 90 | ||
| 92 | $scope.selectPage = function(page) { | 91 | $scope.selectPage = function(page) { |
| 93 | var start = (page - 1) * $scope.numPerPage; | 92 | var start = (page - 1) * $scope.numPerPage; |
| 94 | var end = start + $scope.numPerPage; | 93 | var end = start + $scope.numPerPage; |
| 95 | $scope.paginas = []; | 94 | $scope.paginas = []; |
| 96 | $scope.paginas = calcularPages(page); | 95 | $scope.paginas = calcularPages(page); |
| 97 | $scope.currentPageNotasPedido = $scope.filteredNotasPedido.slice(start, end); | 96 | $scope.currentPageNotasPedido = $scope.filteredNotasPedido.slice(start, end); |
| 98 | $scope.currentPage = page; | 97 | $scope.currentPage = page; |
| 99 | }; | 98 | }; |
| 100 | 99 | ||
| 101 | $scope.select = function(notaPedido) { | 100 | $scope.select = function(notaPedido) { |
| 102 | $uibModalInstance.close(notaPedido); | 101 | $uibModalInstance.close(notaPedido); |
| 103 | }; | 102 | }; |
| 104 | 103 | ||
| 105 | $scope.cancel = function() { | 104 | $scope.cancel = function() { |
| 106 | $uibModalInstance.dismiss('cancel'); | 105 | $uibModalInstance.dismiss('cancel'); |
| 107 | }; | 106 | }; |
| 108 | 107 | ||
| 109 | $scope.busquedaDown = function(key) { | 108 | $scope.busquedaDown = function(key) { |
| 110 | if (key === 40) { | 109 | if (key === 40) { |
| 111 | primera(key); | 110 | primera(key); |
| 112 | } | 111 | } |
| 113 | }; | 112 | }; |
| 114 | 113 | ||
| 115 | $scope.itemNotaPedido = function(key) { | 114 | $scope.itemNotaPedido = function(key) { |
| 116 | if (key === 38) { | 115 | if (key === 38) { |
| 117 | anterior(key); | 116 | anterior(key); |
| 118 | } | 117 | } |
| 119 | 118 | ||
| 120 | if (key === 40) { | 119 | if (key === 40) { |
| 121 | siguiente(key); | 120 | siguiente(key); |
| 122 | } | 121 | } |
| 123 | 122 | ||
| 124 | if (key === 37) { | 123 | if (key === 37) { |
| 125 | retrocederPagina(); | 124 | retrocederPagina(); |
| 126 | } | 125 | } |
| 127 | 126 | ||
| 128 | if (key === 39) { | 127 | if (key === 39) { |
| 129 | avanzarPagina(); | 128 | avanzarPagina(); |
| 130 | } | 129 | } |
| 131 | }; | 130 | }; |
| 132 | 131 | ||
| 133 | function calcularPages(paginaActual) { | 132 | function calcularPages(paginaActual) { |
| 134 | var paginas = []; | 133 | var paginas = []; |
| 135 | paginas.push(paginaActual); | 134 | paginas.push(paginaActual); |
| 136 | 135 | ||
| 137 | if (paginaActual - 1 > 1) { | 136 | if (paginaActual - 1 > 1) { |
| 138 | 137 | ||
| 139 | paginas.unshift(paginaActual - 1); | 138 | paginas.unshift(paginaActual - 1); |
| 140 | if (paginaActual - 2 > 1) { | 139 | if (paginaActual - 2 > 1) { |
| 141 | paginas.unshift(paginaActual - 2); | 140 | paginas.unshift(paginaActual - 2); |
| 142 | } | 141 | } |
| 143 | } | 142 | } |
| 144 | 143 | ||
| 145 | if (paginaActual + 1 < $scope.lastPage) { | 144 | if (paginaActual + 1 < $scope.lastPage) { |
| 146 | paginas.push(paginaActual + 1); | 145 | paginas.push(paginaActual + 1); |
| 147 | if (paginaActual + 2 < $scope.lastPage) { | 146 | if (paginaActual + 2 < $scope.lastPage) { |
| 148 | paginas.push(paginaActual + 2); | 147 | paginas.push(paginaActual + 2); |
| 149 | } | 148 | } |
| 150 | } | 149 | } |
| 151 | 150 | ||
| 152 | if (paginaActual !== 1) { | 151 | if (paginaActual !== 1) { |
| 153 | paginas.unshift(1); | 152 | paginas.unshift(1); |
| 154 | } | 153 | } |
| 155 | 154 | ||
| 156 | if (paginaActual !== $scope.lastPage) { | 155 | if (paginaActual !== $scope.lastPage) { |
| 157 | paginas.push($scope.lastPage); | 156 | paginas.push($scope.lastPage); |
| 158 | } | 157 | } |
| 159 | 158 | ||
| 160 | return paginas; | 159 | return paginas; |
| 161 | } | 160 | } |
| 162 | 161 | ||
| 163 | function primera() { | 162 | function primera() { |
| 164 | $scope.selectedNotaPedido = 0; | 163 | $scope.selectedNotaPedido = 0; |
| 165 | } | 164 | } |
| 166 | 165 | ||
| 167 | function anterior() { | 166 | function anterior() { |
| 168 | if ($scope.selectedNotaPedido === 0 && $scope.currentPage > 1) { | 167 | if ($scope.selectedNotaPedido === 0 && $scope.currentPage > 1) { |
| 169 | retrocederPagina(); | 168 | retrocederPagina(); |
| 170 | } else { | 169 | } else { |
| 171 | $scope.selectedNotaPedido--; | 170 | $scope.selectedNotaPedido--; |
| 172 | } | 171 | } |
| 173 | } | 172 | } |
| 174 | 173 | ||
| 175 | function siguiente() { | 174 | function siguiente() { |
| 176 | if ($scope.selectedNotaPedido < $scope.currentPageNotasPedido.length - 1 ) { | 175 | if ($scope.selectedNotaPedido < $scope.currentPageNotasPedido.length - 1 ) { |
| 177 | $scope.selectedNotaPedido++; | 176 | $scope.selectedNotaPedido++; |
| 178 | } else { | 177 | } else { |
| 179 | avanzarPagina(); | 178 | avanzarPagina(); |
| 180 | } | 179 | } |
| 181 | } | 180 | } |
| 182 | 181 | ||
| 183 | function retrocederPagina() { | 182 | function retrocederPagina() { |
| 184 | if ($scope.currentPage > 1) { | 183 | if ($scope.currentPage > 1) { |
| 185 | $scope.selectPage($scope.currentPage - 1); | 184 | $scope.selectPage($scope.currentPage - 1); |
| 186 | $scope.selectedNotaPedido = $scope.numPerPage - 1; | 185 | $scope.selectedNotaPedido = $scope.numPerPage - 1; |
| 187 | } | 186 | } |
| 188 | } | 187 | } |
| 189 | 188 | ||
| 190 | function avanzarPagina() { | 189 | function avanzarPagina() { |
| 191 | if ($scope.currentPage < $scope.lastPage) { | 190 | if ($scope.currentPage < $scope.lastPage) { |
| 192 | $scope.selectPage($scope.currentPage + 1); | 191 | $scope.selectPage($scope.currentPage + 1); |
| 193 | $scope.selectedNotaPedido = 0; | 192 | $scope.selectedNotaPedido = 0; |
| 194 | } | 193 | } |
| 195 | } | 194 | } |
| 196 | } | 195 | } |
| 197 | ] | 196 | ] |
| 198 | ); | 197 | ); |
| 199 | 198 |