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