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