Commit d93be438b4af409159b787130ae20825071cfa9e
1 parent
92b7f3bfd2
Exists in
master
tab a espacios
Showing
1 changed file
with
156 additions
and
156 deletions
Show diff stats
src/js/controller.js
| 1 | 1 | angular.module('focaModalPetroleras') |
| 2 | - .controller('modalPetrolerasCtrl', [ | |
| 3 | - '$filter', | |
| 4 | - '$scope', | |
| 5 | - '$uibModalInstance', | |
| 6 | - 'focaPetrolerasService', | |
| 7 | - function ($filter, $scope, $uibModalInstance, focaPetrolerasService) { | |
| 8 | - var json = {razonCuitCod: ''}; | |
| 9 | - | |
| 10 | - focaPetrolerasService.getPetroleras(json).then( | |
| 11 | - function (res) { | |
| 12 | - for (var i = res.data.length - 1; i >= 0; i--) { | |
| 13 | - if (res.data[i].COD === 0) { | |
| 14 | - delete res.data[i]; | |
| 15 | - } | |
| 16 | - } | |
| 17 | - | |
| 18 | - $scope.petroleras = res.data; | |
| 19 | - $scope.search(); | |
| 20 | - }); | |
| 21 | - | |
| 22 | - // pagination | |
| 23 | - $scope.numPerPage = 10; | |
| 24 | - $scope.currentPage = 1; | |
| 25 | - $scope.filteredPetroleras = []; | |
| 26 | - $scope.currentPagePetroleras = []; | |
| 27 | - $scope.selectedPetroleras = -1; | |
| 28 | - | |
| 29 | - $scope.search = function () { | |
| 30 | - $scope.filteredPetroleras = $filter('filter')( | |
| 31 | - $scope.petroleras, {$: $scope.filters} | |
| 32 | - ); | |
| 33 | - | |
| 34 | - $scope.lastPage = Math.ceil( | |
| 35 | - $scope.filteredPetroleras.length / $scope.numPerPage | |
| 36 | - ); | |
| 37 | - | |
| 38 | - $scope.resetPage(); | |
| 39 | - }; | |
| 40 | - | |
| 41 | - $scope.resetPage = function () { | |
| 42 | - $scope.currentPage = 1; | |
| 43 | - $scope.selectPage(1); | |
| 44 | - }; | |
| 45 | - | |
| 46 | - $scope.selectPage = function (page) { | |
| 47 | - console.info(page); | |
| 48 | - var start = (page - 1) * $scope.numPerPage; | |
| 49 | - var end = start + $scope.numPerPage; | |
| 50 | - $scope.paginas = []; | |
| 51 | - $scope.paginas = calcularPages(page); | |
| 52 | - $scope.currentPagePetroleras = $scope.filteredPetroleras.slice(start, end); | |
| 2 | + .controller('modalPetrolerasCtrl', [ | |
| 3 | + '$filter', | |
| 4 | + '$scope', | |
| 5 | + '$uibModalInstance', | |
| 6 | + 'focaPetrolerasService', | |
| 7 | + function ($filter, $scope, $uibModalInstance, focaPetrolerasService) { | |
| 8 | + var json = {razonCuitCod: ''}; | |
| 9 | + | |
| 10 | + focaPetrolerasService.getPetroleras(json).then( | |
| 11 | + function (res) { | |
| 12 | + for (var i = res.data.length - 1; i >= 0; i--) { | |
| 13 | + if (res.data[i].COD === 0) { | |
| 14 | + delete res.data[i]; | |
| 15 | + } | |
| 16 | + } | |
| 17 | + | |
| 18 | + $scope.petroleras = res.data; | |
| 19 | + $scope.search(); | |
| 20 | + }); | |
| 21 | + | |
| 22 | + // pagination | |
| 23 | + $scope.numPerPage = 10; | |
| 24 | + $scope.currentPage = 1; | |
| 25 | + $scope.filteredPetroleras = []; | |
| 26 | + $scope.currentPagePetroleras = []; | |
| 27 | + $scope.selectedPetroleras = -1; | |
| 28 | + | |
| 29 | + $scope.search = function () { | |
| 30 | + $scope.filteredPetroleras = $filter('filter')( | |
| 31 | + $scope.petroleras, {$: $scope.filters} | |
| 32 | + ); | |
| 33 | + | |
| 34 | + $scope.lastPage = Math.ceil( | |
| 35 | + $scope.filteredPetroleras.length / $scope.numPerPage | |
| 36 | + ); | |
| 37 | + | |
| 38 | + $scope.resetPage(); | |
| 39 | + }; | |
| 40 | + | |
| 41 | + $scope.resetPage = function () { | |
| 42 | + $scope.currentPage = 1; | |
| 43 | + $scope.selectPage(1); | |
| 44 | + }; | |
| 45 | + | |
| 46 | + $scope.selectPage = function (page) { | |
| 47 | + console.info(page); | |
| 48 | + var start = (page - 1) * $scope.numPerPage; | |
| 49 | + var end = start + $scope.numPerPage; | |
| 50 | + $scope.paginas = []; | |
| 51 | + $scope.paginas = calcularPages(page); | |
| 52 | + $scope.currentPagePetroleras = $scope.filteredPetroleras.slice(start, end); | |
| 53 | 53 | $scope.currentPage = page; |
| 54 | - }; | |
| 55 | - | |
| 56 | - $scope.select = function(petrolera) { | |
| 57 | - $uibModalInstance.close(petrolera); | |
| 58 | - }; | |
| 59 | - | |
| 60 | - $scope.cancel = function() { | |
| 61 | - $uibModalInstance.dismiss('cancel'); | |
| 62 | - }; | |
| 63 | - | |
| 64 | - $scope.busquedaDown = function(key) { | |
| 65 | - if (key === 40) { | |
| 66 | - primera(key); | |
| 67 | - } | |
| 68 | - }; | |
| 69 | - | |
| 70 | - $scope.busquedaPress = function(key) { | |
| 71 | - if (key === 13) { | |
| 72 | - primera(key); | |
| 73 | - } | |
| 74 | - }; | |
| 75 | - | |
| 76 | - $scope.itemPetrolera = function(key) { | |
| 77 | - if (key === 38) { | |
| 78 | - anterior(key); | |
| 79 | - } | |
| 80 | - | |
| 81 | - if (key === 40) { | |
| 82 | - siguiente(key); | |
| 83 | - } | |
| 84 | - | |
| 85 | - if (key === 37) { | |
| 86 | - retrocederPagina(); | |
| 87 | - } | |
| 88 | - | |
| 89 | - if (key === 39) { | |
| 90 | - avanzarPagina(); | |
| 91 | - } | |
| 92 | - }; | |
| 93 | - | |
| 94 | - function calcularPages(paginaActual) { | |
| 95 | - var paginas = []; | |
| 96 | - paginas.push(paginaActual); | |
| 97 | - | |
| 98 | - if (paginaActual - 1 > 1) { | |
| 99 | - | |
| 100 | - paginas.unshift(paginaActual - 1); | |
| 101 | - if (paginaActual - 2 > 1) { | |
| 102 | - paginas.unshift(paginaActual - 2); | |
| 103 | - } | |
| 104 | - } | |
| 105 | - | |
| 106 | - if (paginaActual + 1 < $scope.lastPage) { | |
| 107 | - paginas.push(paginaActual + 1); | |
| 108 | - if (paginaActual + 2 < $scope.lastPage) { | |
| 109 | - paginas.push(paginaActual + 2); | |
| 110 | - } | |
| 111 | - } | |
| 112 | - | |
| 113 | - if (paginaActual !== 1) { | |
| 114 | - paginas.unshift(1); | |
| 115 | - } | |
| 116 | - | |
| 117 | - if (paginaActual !== $scope.lastPage) { | |
| 118 | - paginas.push($scope.lastPage); | |
| 119 | - } | |
| 120 | - | |
| 121 | - return paginas; | |
| 122 | - } | |
| 123 | - | |
| 124 | - function primera() { | |
| 125 | - $scope.selectedPetroleras = 0; | |
| 126 | - } | |
| 127 | - | |
| 128 | - function anterior() { | |
| 129 | - if ($scope.selectedPetroleras === 0 && $scope.currentPage > 1) { | |
| 130 | - retrocederPagina(); | |
| 131 | - } else { | |
| 132 | - $scope.selectedPetroleras--; | |
| 133 | - } | |
| 134 | - } | |
| 135 | - | |
| 136 | - function siguiente() { | |
| 137 | - if ($scope.selectedPetroleras < $scope.currentPagePetroleras.length - 1 ) { | |
| 138 | - $scope.selectedPetroleras++; | |
| 139 | - } else { | |
| 140 | - avanzarPagina(); | |
| 141 | - } | |
| 142 | - } | |
| 143 | - | |
| 144 | - function retrocederPagina() { | |
| 145 | - if ($scope.currentPage > 1) { | |
| 146 | - $scope.selectPage($scope.currentPage - 1); | |
| 147 | - $scope.selectedPetroleras = $scope.numPerPage - 1; | |
| 148 | - } | |
| 149 | - } | |
| 150 | - | |
| 151 | - function avanzarPagina() { | |
| 152 | - if ($scope.currentPage < $scope.lastPage) { | |
| 153 | - $scope.selectPage($scope.currentPage + 1); | |
| 154 | - $scope.selectedPetroleras = 0; | |
| 155 | - } | |
| 156 | - } | |
| 157 | - }] | |
| 158 | - ); | |
| 54 | + }; | |
| 55 | + | |
| 56 | + $scope.select = function(petrolera) { | |
| 57 | + $uibModalInstance.close(petrolera); | |
| 58 | + }; | |
| 59 | + | |
| 60 | + $scope.cancel = function() { | |
| 61 | + $uibModalInstance.dismiss('cancel'); | |
| 62 | + }; | |
| 63 | + | |
| 64 | + $scope.busquedaDown = function(key) { | |
| 65 | + if (key === 40) { | |
| 66 | + primera(key); | |
| 67 | + } | |
| 68 | + }; | |
| 69 | + | |
| 70 | + $scope.busquedaPress = function(key) { | |
| 71 | + if (key === 13) { | |
| 72 | + primera(key); | |
| 73 | + } | |
| 74 | + }; | |
| 75 | + | |
| 76 | + $scope.itemPetrolera = function(key) { | |
| 77 | + if (key === 38) { | |
| 78 | + anterior(key); | |
| 79 | + } | |
| 80 | + | |
| 81 | + if (key === 40) { | |
| 82 | + siguiente(key); | |
| 83 | + } | |
| 84 | + | |
| 85 | + if (key === 37) { | |
| 86 | + retrocederPagina(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if (key === 39) { | |
| 90 | + avanzarPagina(); | |
| 91 | + } | |
| 92 | + }; | |
| 93 | + | |
| 94 | + function calcularPages(paginaActual) { | |
| 95 | + var paginas = []; | |
| 96 | + paginas.push(paginaActual); | |
| 97 | + | |
| 98 | + if (paginaActual - 1 > 1) { | |
| 99 | + | |
| 100 | + paginas.unshift(paginaActual - 1); | |
| 101 | + if (paginaActual - 2 > 1) { | |
| 102 | + paginas.unshift(paginaActual - 2); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + if (paginaActual + 1 < $scope.lastPage) { | |
| 107 | + paginas.push(paginaActual + 1); | |
| 108 | + if (paginaActual + 2 < $scope.lastPage) { | |
| 109 | + paginas.push(paginaActual + 2); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + if (paginaActual !== 1) { | |
| 114 | + paginas.unshift(1); | |
| 115 | + } | |
| 116 | + | |
| 117 | + if (paginaActual !== $scope.lastPage) { | |
| 118 | + paginas.push($scope.lastPage); | |
| 119 | + } | |
| 120 | + | |
| 121 | + return paginas; | |
| 122 | + } | |
| 123 | + | |
| 124 | + function primera() { | |
| 125 | + $scope.selectedPetroleras = 0; | |
| 126 | + } | |
| 127 | + | |
| 128 | + function anterior() { | |
| 129 | + if ($scope.selectedPetroleras === 0 && $scope.currentPage > 1) { | |
| 130 | + retrocederPagina(); | |
| 131 | + } else { | |
| 132 | + $scope.selectedPetroleras--; | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + function siguiente() { | |
| 137 | + if ($scope.selectedPetroleras < $scope.currentPagePetroleras.length - 1 ) { | |
| 138 | + $scope.selectedPetroleras++; | |
| 139 | + } else { | |
| 140 | + avanzarPagina(); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + function retrocederPagina() { | |
| 145 | + if ($scope.currentPage > 1) { | |
| 146 | + $scope.selectPage($scope.currentPage - 1); | |
| 147 | + $scope.selectedPetroleras = $scope.numPerPage - 1; | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + function avanzarPagina() { | |
| 152 | + if ($scope.currentPage < $scope.lastPage) { | |
| 153 | + $scope.selectPage($scope.currentPage + 1); | |
| 154 | + $scope.selectedPetroleras = 0; | |
| 155 | + } | |
| 156 | + } | |
| 157 | + }] | |
| 158 | + ); |