Commit d881022791155c3a5a0ce941bb9d99becf5d4048

Authored by Eric Fernandez
1 parent 9166ecf84f
Exists in master

ordeno variables

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