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