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