Commit f7b32803c859c905ee3d6db431ee90a04b8e513b

Authored by Nicolás Guarnieri
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !1
src/js/controller.js
1 angular.module('focaModalBanco') 1 angular.module('focaModalBanco')
2 .controller('focaModalBancoController', [ 2 .controller('focaModalBancoController', [
3 '$filter', 3 '$filter',
4 '$scope', 4 '$scope',
5 '$uibModalInstance', 5 '$uibModalInstance',
6 'focaModalBancoService', 6 'focaModalBancoService',
7 'filters', 7 'filters',
8 function($filter, $scope, $uibModalInstance, focaModalBancoService, filters) { 8 function($filter, $scope, $uibModalInstance, focaModalBancoService, filters) {
9 9
10 $scope.filters = filters; 10 $scope.filters = filters;
11 $scope.bancos = []; 11 $scope.bancos = [];
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.filteredBancos = []; 17 $scope.filteredBancos = [];
18 $scope.currentPageBancos = []; 18 $scope.currentPageBancos = [];
19 $scope.selectedBanco = -1; 19 $scope.selectedBanco = -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 focaModalBancoService.getBancos($scope.filters).then( 24 focaModalBancoService.getBancos().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.bancos = res.data; 28 $scope.bancos = res.data;
29 $scope.search(); 29 $scope.search();
30 primera(); 30 primera();
31 } 31 }
32 ); 32 );
33 } 33 }
34 }; 34 };
35 35
36 $scope.search = function() { 36 $scope.search = function() {
37 if($scope.bancos.length > 0) { 37 if($scope.bancos.length > 0) {
38 $scope.filteredBancos = $filter('filter')( 38 $scope.filteredBancos = $filter('filter')(
39 $scope.bancos, { $: $scope.filters } 39 $scope.bancos, { $: $scope.filters }
40 ); 40 );
41 $scope.lastPage = Math.ceil( 41 $scope.lastPage = Math.ceil(
42 $scope.filteredBancos.length / $scope.numPerPage 42 $scope.filteredBancos.length / $scope.numPerPage
43 ); 43 );
44 $scope.resetPage(); 44 $scope.resetPage();
45 } 45 }
46 }; 46 };
47 47
48 $scope.resetPage = function() { 48 $scope.resetPage = function() {
49 $scope.currentPage = 1; 49 $scope.currentPage = 1;
50 $scope.selectPage(1); 50 $scope.selectPage(1);
51 }; 51 };
52 52
53 $scope.selectPage = function(page) { 53 $scope.selectPage = function(page) {
54 var start = (page - 1) * $scope.numPerPage; 54 var start = (page - 1) * $scope.numPerPage;
55 var end = start + $scope.numPerPage; 55 var end = start + $scope.numPerPage;
56 $scope.paginas = []; 56 $scope.paginas = [];
57 $scope.paginas = calcularPages(page); 57 $scope.paginas = calcularPages(page);
58 $scope.currentPageBancos = $scope.filteredBancos.slice(start, end); 58 $scope.currentPageBancos = $scope.filteredBancos.slice(start, end);
59 $scope.currentPage = page; 59 $scope.currentPage = page;
60 }; 60 };
61 61
62 $scope.select = function(vendedor) { 62 $scope.select = function(vendedor) {
63 $uibModalInstance.close(vendedor); 63 $uibModalInstance.close(vendedor);
64 }; 64 };
65 65
66 $scope.cancel = function() { 66 $scope.cancel = function() {
67 $uibModalInstance.dismiss('cancel'); 67 $uibModalInstance.dismiss('cancel');
68 }; 68 };
69 69
70 $scope.busquedaDown = function(key) { 70 $scope.busquedaDown = function(key) {
71 if (key === 40) { 71 if (key === 40) {
72 primera(key); 72 primera(key);
73 } 73 }
74 }; 74 };
75 75
76 $scope.itemBanco = function(key) { 76 $scope.itemBanco = function(key) {
77 if (key === 38) { 77 if (key === 38) {
78 anterior(key); 78 anterior(key);
79 } 79 }
80 80
81 if (key === 40) { 81 if (key === 40) {
82 siguiente(key); 82 siguiente(key);
83 } 83 }
84 84
85 if (key === 37) { 85 if (key === 37) {
86 retrocederPagina(); 86 retrocederPagina();
87 } 87 }
88 88
89 if (key === 39) { 89 if (key === 39) {
90 avanzarPagina(); 90 avanzarPagina();
91 } 91 }
92 }; 92 };
93 93
94 function calcularPages(paginaActual) { 94 function calcularPages(paginaActual) {
95 var paginas = []; 95 var paginas = [];
96 paginas.push(paginaActual); 96 paginas.push(paginaActual);
97 97
98 if (paginaActual - 1 > 1) { 98 if (paginaActual - 1 > 1) {
99 99
100 paginas.unshift(paginaActual - 1); 100 paginas.unshift(paginaActual - 1);
101 if (paginaActual - 2 > 1) { 101 if (paginaActual - 2 > 1) {
102 paginas.unshift(paginaActual - 2); 102 paginas.unshift(paginaActual - 2);
103 } 103 }
104 } 104 }
105 105
106 if (paginaActual + 1 < $scope.lastPage) { 106 if (paginaActual + 1 < $scope.lastPage) {
107 paginas.push(paginaActual + 1); 107 paginas.push(paginaActual + 1);
108 if (paginaActual + 2 < $scope.lastPage) { 108 if (paginaActual + 2 < $scope.lastPage) {
109 paginas.push(paginaActual + 2); 109 paginas.push(paginaActual + 2);
110 } 110 }
111 } 111 }
112 112
113 if (paginaActual !== 1) { 113 if (paginaActual !== 1) {
114 paginas.unshift(1); 114 paginas.unshift(1);
115 } 115 }
116 116
117 if (paginaActual !== $scope.lastPage) { 117 if (paginaActual !== $scope.lastPage) {
118 paginas.push($scope.lastPage); 118 paginas.push($scope.lastPage);
119 } 119 }
120 120
121 return paginas; 121 return paginas;
122 } 122 }
123 123
124 function primera() { 124 function primera() {
125 $scope.selectedBanco = 0; 125 $scope.selectedBanco = 0;
126 } 126 }
127 127
128 function anterior() { 128 function anterior() {
129 if ($scope.selectedBanco === 0 && $scope.currentPage > 1) { 129 if ($scope.selectedBanco === 0 && $scope.currentPage > 1) {
130 retrocederPagina(); 130 retrocederPagina();
131 } else { 131 } else {
132 $scope.selectedBanco--; 132 $scope.selectedBanco--;
133 } 133 }
134 } 134 }
135 135
136 function siguiente() { 136 function siguiente() {
137 if ($scope.selectedBanco < $scope.currentPageBancos.length - 1) { 137 if ($scope.selectedBanco < $scope.currentPageBancos.length - 1) {
138 $scope.selectedBanco++; 138 $scope.selectedBanco++;
139 } else { 139 } else {
140 avanzarPagina(); 140 avanzarPagina();
141 } 141 }
142 } 142 }
143 143
144 function retrocederPagina() { 144 function retrocederPagina() {
145 if ($scope.currentPage > 1) { 145 if ($scope.currentPage > 1) {
146 $scope.selectPage($scope.currentPage - 1); 146 $scope.selectPage($scope.currentPage - 1);
147 $scope.selectedBanco = $scope.numPerPage - 1; 147 $scope.selectedBanco = $scope.numPerPage - 1;
148 } 148 }
149 } 149 }
150 150
151 function avanzarPagina() { 151 function avanzarPagina() {
152 if ($scope.currentPage < $scope.lastPage) { 152 if ($scope.currentPage < $scope.lastPage) {
153 $scope.selectPage($scope.currentPage + 1); 153 $scope.selectPage($scope.currentPage + 1);
154 $scope.selectedBanco = 0; 154 $scope.selectedBanco = 0;
155 } 155 }
156 } 156 }
157 157
158 $scope.busquedaPress(13); 158 $scope.busquedaPress(13);
159 }] 159 }]
160 ); 160 );
161 161
1 angular.module('focaModalBanco') 1 angular.module('focaModalBanco')
2 .factory('focaModalBancoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) { 2 .factory('focaModalBancoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) {
3 return { 3 return {
4 getBancos: function() { 4 getBancos: function() {
5 //TODO: Pasar filtro
5 return $http.get(API_ENDPOINT.URL + '/banco'); 6 return $http.get(API_ENDPOINT.URL + '/banco');
6 } 7 }
7 }; 8 };
8 }]); 9 }]);
9 10