Commit e7ad1a7a5abd6a900493198ed241029be3e3b922
1 parent
5ae8cec1c5
Exists in
master
code review
Showing
1 changed file
with
5 additions
and
12 deletions
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 | 'columnas', | 8 | 'columnas', |
| 9 | 'query', | 9 | 'query', |
| 10 | 'titulo', | 10 | 'titulo', |
| 11 | function($timeout, $filter, $scope, $uibModalInstance, focaModalService, | 11 | function($timeout, $filter, $scope, $uibModalInstance, focaModalService, |
| 12 | columnas, query, titulo) { | 12 | columnas, query, titulo) { |
| 13 | 13 | ||
| 14 | $scope.filters = ''; | 14 | $scope.filters = ''; |
| 15 | $scope.columnas = columnas; | 15 | $scope.columnas = columnas; |
| 16 | $scope.titulo = titulo | 16 | $scope.titulo = titulo |
| 17 | $scope.entidades = []; | 17 | $scope.entidades = []; |
| 18 | $scope.primerBusqueda = false; | 18 | $scope.primerBusqueda = false; |
| 19 | $scope.searchLoading = false; | 19 | $scope.searchLoading = false; |
| 20 | // pagination | 20 | // pagination |
| 21 | $scope.numPerPage = 10; | 21 | $scope.numPerPage = 10; |
| 22 | $scope.currentPage = 1; | 22 | $scope.currentPage = 1; |
| 23 | $scope.filteredEntidades = []; | 23 | $scope.filteredEntidades = []; |
| 24 | $scope.currentPageEntidades = []; | 24 | $scope.currentPageEntidades = []; |
| 25 | $scope.selectedEntidad = -1; | 25 | $scope.selectedEntidad = -1; |
| 26 | 26 | ||
| 27 | $scope.busquedaPress = function(key) { | 27 | $scope.busquedaPress = function(key) { |
| 28 | if (key === 13) { | 28 | if(key === 13) { |
| 29 | $scope.searchLoading = true; | 29 | $scope.searchLoading = true; |
| 30 | focaModalService.getEntidad($scope.filters, query).then( | 30 | focaModalService.getEntidad($scope.filters, query).then( |
| 31 | function(res) { | 31 | function(res) { |
| 32 | $scope.searchLoading = false; | 32 | $scope.searchLoading = false; |
| 33 | $scope.primerBusqueda = true; | 33 | $scope.primerBusqueda = true; |
| 34 | $scope.entidades = res.data; | 34 | $scope.entidades = res.data; |
| 35 | $scope.search(true); | 35 | $scope.search(true); |
| 36 | primera(); | 36 | primera(); |
| 37 | } | 37 | } |
| 38 | ); | 38 | ); |
| 39 | } | 39 | } |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| 42 | $scope.search = function(pressed) { | 42 | $scope.search = function(pressed) { |
| 43 | if($scope.entidades.length > 0) { | 43 | if($scope.entidades.length > 0) { |
| 44 | $scope.filteredEntidades = $filter('filter')( | 44 | $scope.filteredEntidades = $filter('filter')( |
| 45 | $scope.entidades, { $: $scope.filters } | 45 | $scope.entidades, { $: $scope.filters } |
| 46 | ); | 46 | ); |
| 47 | 47 | ||
| 48 | $scope.lastPage = Math.ceil( | 48 | $scope.lastPage = Math.ceil( |
| 49 | $scope.filteredEntidades.length / $scope.numPerPage | 49 | $scope.filteredEntidades.length / $scope.numPerPage |
| 50 | ); | 50 | ); |
| 51 | $scope.resetPage(); | 51 | $scope.resetPage(); |
| 52 | }else if(pressed){ | 52 | }else if(pressed) { |
| 53 | $timeout(function() { | 53 | $timeout(function() { |
| 54 | angular.element('#search')[0].focus(); | 54 | angular.element('#search')[0].focus(); |
| 55 | $scope.filters = ''; | 55 | $scope.filters = ''; |
| 56 | }); | 56 | }); |
| 57 | } | 57 | } |
| 58 | }; | 58 | }; |
| 59 | 59 | ||
| 60 | $scope.resetPage = function() { | 60 | $scope.resetPage = function() { |
| 61 | $scope.currentPage = 1; | 61 | $scope.currentPage = 1; |
| 62 | $scope.selectPage(1); | 62 | $scope.selectPage(1); |
| 63 | }; | 63 | }; |
| 64 | 64 | ||
| 65 | $scope.selectPage = function(page) { | 65 | $scope.selectPage = function(page) { |
| 66 | var start = (page - 1) * $scope.numPerPage; | 66 | var start = (page - 1) * $scope.numPerPage; |
| 67 | var end = start + $scope.numPerPage; | 67 | var end = start + $scope.numPerPage; |
| 68 | $scope.paginas = []; | 68 | $scope.paginas = []; |
| 69 | $scope.paginas = calcularPages(page); | 69 | $scope.paginas = calcularPages(page); |
| 70 | $scope.currentPageEntidades = $scope.filteredEntidades.slice(start, end); | 70 | $scope.currentPageEntidades = $scope.filteredEntidades.slice(start, end); |
| 71 | $scope.currentPage = page; | 71 | $scope.currentPage = page; |
| 72 | }; | 72 | }; |
| 73 | 73 | ||
| 74 | $scope.select = function(vendedor) { | 74 | $scope.select = function(vendedor) { |
| 75 | $uibModalInstance.close(vendedor); | 75 | $uibModalInstance.close(vendedor); |
| 76 | }; | 76 | }; |
| 77 | 77 | ||
| 78 | $scope.cancel = function() { | 78 | $scope.cancel = function() { |
| 79 | $uibModalInstance.dismiss('cancel'); | 79 | $uibModalInstance.dismiss('cancel'); |
| 80 | }; | 80 | }; |
| 81 | 81 | ||
| 82 | $scope.busquedaDown = function(key) { | 82 | $scope.busquedaDown = function(key) { |
| 83 | if (key === 40) { | 83 | if (key === 40) { |
| 84 | primera(key); | 84 | primera(key); |
| 85 | } | 85 | } |
| 86 | }; | 86 | }; |
| 87 | 87 | ||
| 88 | $scope.itemEntidad = function(key) { | 88 | $scope.itemEntidad = function(key) { |
| 89 | if (key === 38) { | 89 | if (key === 38) { |
| 90 | anterior(key); | 90 | anterior(key); |
| 91 | } | 91 | } |
| 92 | |||
| 93 | if (key === 40) { | 92 | if (key === 40) { |
| 94 | siguiente(key); | 93 | siguiente(key); |
| 95 | } | 94 | } |
| 96 | |||
| 97 | if (key === 37) { | 95 | if (key === 37) { |
| 98 | retrocederPagina(); | 96 | retrocederPagina(); |
| 99 | } | 97 | } |
| 100 | |||
| 101 | if (key === 39) { | 98 | if (key === 39) { |
| 102 | avanzarPagina(); | 99 | avanzarPagina(); |
| 103 | } | 100 | } |
| 104 | }; | 101 | }; |
| 105 | 102 | ||
| 106 | function calcularPages(paginaActual) { | 103 | function calcularPages(paginaActual) { |
| 107 | var paginas = []; | 104 | var paginas = []; |
| 108 | paginas.push(paginaActual); | 105 | paginas.push(paginaActual); |
| 109 | 106 | ||
| 110 | if (paginaActual - 1 > 1) { | 107 | if (paginaActual - 1 > 1) { |
| 111 | 108 | ||
| 112 | paginas.unshift(paginaActual - 1); | 109 | paginas.unshift(paginaActual - 1); |
| 113 | if (paginaActual - 2 > 1) { | 110 | if (paginaActual - 2 > 1) { |
| 114 | paginas.unshift(paginaActual - 2); | 111 | paginas.unshift(paginaActual - 2); |
| 115 | } | 112 | } |
| 116 | } | 113 | } |
| 117 | |||
| 118 | if (paginaActual + 1 < $scope.lastPage) { | 114 | if (paginaActual + 1 < $scope.lastPage) { |
| 119 | paginas.push(paginaActual + 1); | 115 | paginas.push(paginaActual + 1); |
| 120 | if (paginaActual + 2 < $scope.lastPage) { | 116 | if (paginaActual + 2 < $scope.lastPage) { |
| 121 | paginas.push(paginaActual + 2); | 117 | paginas.push(paginaActual + 2); |
| 122 | } | 118 | } |
| 123 | } | 119 | } |
| 124 | |||
| 125 | if (paginaActual !== 1) { | 120 | if (paginaActual !== 1) { |
| 126 | paginas.unshift(1); | 121 | paginas.unshift(1); |
| 127 | } | 122 | } |
| 128 | |||
| 129 | if (paginaActual !== $scope.lastPage) { | 123 | if (paginaActual !== $scope.lastPage) { |
| 130 | paginas.push($scope.lastPage); | 124 | paginas.push($scope.lastPage); |
| 131 | } | 125 | } |
| 132 | |||
| 133 | return paginas; | 126 | return paginas; |
| 134 | } | 127 | } |
| 135 | 128 | ||
| 136 | function primera() { | 129 | function primera() { |
| 137 | $scope.selectedEntidad = 0; | 130 | $scope.selectedEntidad = 0; |
| 138 | } | 131 | } |
| 139 | 132 | ||
| 140 | function anterior() { | 133 | function anterior() { |
| 141 | if ($scope.selectedEntidad === 0 && $scope.currentPage > 1) { | 134 | if($scope.selectedEntidad === 0 && $scope.currentPage > 1) { |
| 142 | retrocederPagina(); | 135 | retrocederPagina(); |
| 143 | } else { | 136 | } else { |
| 144 | $scope.selectedEntidad--; | 137 | $scope.selectedEntidad--; |
| 145 | } | 138 | } |
| 146 | } | 139 | } |
| 147 | 140 | ||
| 148 | function siguiente() { | 141 | function siguiente() { |
| 149 | if ($scope.selectedEntidad < $scope.currentPageEntidades.length - 1) { | 142 | if($scope.selectedEntidad < $scope.currentPageEntidades.length - 1) { |
| 150 | $scope.selectedEntidad++; | 143 | $scope.selectedEntidad++; |
| 151 | } else { | 144 | } else { |
| 152 | avanzarPagina(); | 145 | avanzarPagina(); |
| 153 | } | 146 | } |
| 154 | } | 147 | } |
| 155 | 148 | ||
| 156 | function retrocederPagina() { | 149 | function retrocederPagina() { |
| 157 | if ($scope.currentPage > 1) { | 150 | if ($scope.currentPage > 1) { |
| 158 | $scope.selectPage($scope.currentPage - 1); | 151 | $scope.selectPage($scope.currentPage - 1); |
| 159 | $scope.selectedEntidad = $scope.numPerPage - 1; | 152 | $scope.selectedEntidad = $scope.numPerPage - 1; |
| 160 | } | 153 | } |
| 161 | } | 154 | } |
| 162 | 155 | ||
| 163 | function avanzarPagina() { | 156 | function avanzarPagina() { |
| 164 | if ($scope.currentPage < $scope.lastPage) { | 157 | if($scope.currentPage < $scope.lastPage) { |
| 165 | $scope.selectPage($scope.currentPage + 1); | 158 | $scope.selectPage($scope.currentPage + 1); |
| 166 | $scope.selectedEntidad = 0; | 159 | $scope.selectedEntidad = 0; |
| 167 | } | 160 | } |
| 168 | } | 161 | } |
| 169 | }] | 162 | }] |
| 170 | ); | 163 | ); |
| 171 | 164 |