Commit 0a622cf988bfe42ffb1b4910e806ab0090b69e13

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