Commit 51adf0b8a3767008939c7581e4adcf877f44a2f7

Authored by Eric Fernandez
Exists in master and in 1 other branch develop

Merge branch 'master' into 'master'

Master

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