Commit 83e62217006066f77284a2ecd092b73ac84c9dfa

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

teclado virtual funciona, focus input si no hay resultados

See merge request !2
src/js/controller.js
1 angular.module('focaModalVehiculo') 1 angular.module('focaModalVehiculo')
2 .controller('focaModalVehiculoController', 2 .controller('focaModalVehiculoController',
3 [ 3 [
4 '$timeout',
4 '$filter', 5 '$filter',
5 '$scope', 6 '$scope',
6 '$uibModalInstance', 7 '$uibModalInstance',
7 'focaModalVehiculoService', 8 'focaModalVehiculoService',
8 function($filter, $scope, $uibModalInstance, 9 function($timeout, $filter, $scope, $uibModalInstance,
9 focaModalVehiculoService 10 focaModalVehiculoService
10 ) { 11 ) {
11 12
12 $scope.filters = ''; 13 $scope.filters = '';
13 $scope.vehiculos = []; 14 $scope.vehiculos = [];
14 $scope.primerBusqueda = false; 15 $scope.primerBusqueda = false;
15 $scope.searchLoading = false; 16 $scope.searchLoading = false;
16 // pagination 17 // pagination
17 $scope.numPerPage = 10; 18 $scope.numPerPage = 10;
18 $scope.currentPage = 1; 19 $scope.currentPage = 1;
19 $scope.filteredVehiculos = []; 20 $scope.filteredVehiculos = [];
20 $scope.currentPageVehiculos = []; 21 $scope.currentPageVehiculos = [];
21 $scope.selectedVehiculo = -1; 22 $scope.selectedVehiculo = -1;
22 23
23 //METODOS 24 //METODOS
24 $scope.busquedaPress = function(key) { 25 $scope.busquedaPress = function(key) {
25 if (key === 13) { 26 if (key === 13) {
26 $scope.searchLoading = true; 27 $scope.searchLoading = true;
27 focaModalVehiculoService.getVehiculos().then(llenarDatos); 28 focaModalVehiculoService.getVehiculos().then(llenarDatos);
28 } 29 }
29 }; 30 };
30 function llenarDatos(res) { 31 function llenarDatos(res) {
31 $scope.searchLoading = false; 32 $scope.searchLoading = false;
32 $scope.primerBusqueda = true; 33 $scope.primerBusqueda = true;
33 $scope.vehiculos = res.data; 34 $scope.vehiculos = res.data;
34 $scope.search(); 35 $scope.search(true);
35 primera(); 36 primera();
36 } 37 }
37 $scope.search = function() { 38 $scope.search = function(pressed) {
38 if($scope.vehiculos.length > 0) { 39 if($scope.vehiculos.length > 0) {
39 $scope.filteredVehiculos = $filter('filter')( 40 $scope.filteredVehiculos = $filter('filter')(
40 $scope.vehiculos, 41 $scope.vehiculos,
41 {$: $scope.filters} 42 {$: $scope.filters}
42 ); 43 );
43 44
45 if(pressed && $scope.filteredVehiculos.length === 0) {
46 $timeout(function() {
47 angular.element('#search')[0].focus();
48 $scope.filters = '';
49 });
50 }
51
44 $scope.lastPage = Math.ceil( 52 $scope.lastPage = Math.ceil(
45 $scope.filteredVehiculos.length / $scope.numPerPage 53 $scope.filteredVehiculos.length / $scope.numPerPage
46 ); 54 );
47 55
48 $scope.resetPage(); 56 $scope.resetPage();
49 } 57 }
50 }; 58 };
51 59
52 $scope.resetPage = function() { 60 $scope.resetPage = function() {
53 $scope.currentPage = 1; 61 $scope.currentPage = 1;
54 $scope.selectPage(1); 62 $scope.selectPage(1);
55 }; 63 };
56 64
57 $scope.selectPage = function(page) { 65 $scope.selectPage = function(page) {
58 var start = (page - 1) * $scope.numPerPage; 66 var start = (page - 1) * $scope.numPerPage;
59 var end = start + $scope.numPerPage; 67 var end = start + $scope.numPerPage;
60 $scope.paginas = []; 68 $scope.paginas = [];
61 $scope.paginas = calcularPages(page); 69 $scope.paginas = calcularPages(page);
62 $scope.currentPageVehiculos = 70 $scope.currentPageVehiculos =
63 $scope.filteredVehiculos.slice(start, end); 71 $scope.filteredVehiculos.slice(start, end);
64 $scope.currentPage = page; 72 $scope.currentPage = page;
65 }; 73 };
66 74
67 $scope.select = function(vehiculo) { 75 $scope.select = function(vehiculo) {
68 $uibModalInstance.close(vehiculo); 76 $uibModalInstance.close(vehiculo);
69 }; 77 };
70 78
71 $scope.cancel = function() { 79 $scope.cancel = function() {
72 $uibModalInstance.dismiss('cancel'); 80 $uibModalInstance.dismiss('cancel');
73 }; 81 };
74 82
75 $scope.busquedaDown = function(key) { 83 $scope.busquedaDown = function(key) {
76 if (key === 40) { 84 if (key === 40) {
77 primera(key); 85 primera(key);
78 } 86 }
79 }; 87 };
80 88
81 $scope.itemVehiculo = function(key) { 89 $scope.itemVehiculo = function(key) {
82 if (key === 38) { 90 if (key === 38) {
83 anterior(key); 91 anterior(key);
84 } 92 }
85 93
86 if (key === 40) { 94 if (key === 40) {
87 siguiente(key); 95 siguiente(key);
88 } 96 }
89 97
90 if (key === 37) { 98 if (key === 37) {
91 retrocederPagina(); 99 retrocederPagina();
92 } 100 }
93 101
94 if (key === 39) { 102 if (key === 39) {
95 avanzarPagina(); 103 avanzarPagina();
96 } 104 }
97 }; 105 };
98 106
99 function calcularPages(paginaActual) { 107 function calcularPages(paginaActual) {
100 var paginas = []; 108 var paginas = [];
101 paginas.push(paginaActual); 109 paginas.push(paginaActual);
102 110
103 if (paginaActual - 1 > 1) { 111 if (paginaActual - 1 > 1) {
104 112
105 paginas.unshift(paginaActual - 1); 113 paginas.unshift(paginaActual - 1);
106 if (paginaActual - 2 > 1) { 114 if (paginaActual - 2 > 1) {
107 paginas.unshift(paginaActual - 2); 115 paginas.unshift(paginaActual - 2);
108 } 116 }
109 } 117 }
110 118
111 if (paginaActual + 1 < $scope.lastPage) { 119 if (paginaActual + 1 < $scope.lastPage) {
112 paginas.push(paginaActual + 1); 120 paginas.push(paginaActual + 1);
113 if (paginaActual + 2 < $scope.lastPage) { 121 if (paginaActual + 2 < $scope.lastPage) {
114 paginas.push(paginaActual + 2); 122 paginas.push(paginaActual + 2);
115 } 123 }
116 } 124 }
117 125
118 if (paginaActual !== 1) { 126 if (paginaActual !== 1) {
119 paginas.unshift(1); 127 paginas.unshift(1);
120 } 128 }
121 129
122 if (paginaActual !== $scope.lastPage) { 130 if (paginaActual !== $scope.lastPage) {
123 paginas.push($scope.lastPage); 131 paginas.push($scope.lastPage);
124 } 132 }
125 133
126 return paginas; 134 return paginas;
127 } 135 }
128 136
129 function primera() { 137 function primera() {
130 $scope.selectedVehiculo = 0; 138 $scope.selectedVehiculo = 0;
131 } 139 }
132 140
133 function anterior() { 141 function anterior() {
134 if ($scope.selectedVehiculo === 0 && $scope.currentPage > 1) { 142 if ($scope.selectedVehiculo === 0 && $scope.currentPage > 1) {
135 retrocederPagina(); 143 retrocederPagina();
136 } else { 144 } else {
137 $scope.selectedVehiculo--; 145 $scope.selectedVehiculo--;
138 } 146 }
139 } 147 }
140 148
141 function siguiente() { 149 function siguiente() {
142 if ( 150 if (
143 $scope.selectedVehiculo < $scope.currentPageVehiculos.length - 1 151 $scope.selectedVehiculo < $scope.currentPageVehiculos.length - 1
144 ) { 152 ) {
145 $scope.selectedVehiculo++; 153 $scope.selectedVehiculo++;
146 } else { 154 } else {
147 avanzarPagina(); 155 avanzarPagina();
148 } 156 }
149 } 157 }
150 158
151 function retrocederPagina() { 159 function retrocederPagina() {
152 if ($scope.currentPage > 1) { 160 if ($scope.currentPage > 1) {
153 $scope.selectPage($scope.currentPage - 1); 161 $scope.selectPage($scope.currentPage - 1);
154 $scope.selectedVehiculo = $scope.numPerPage - 1; 162 $scope.selectedVehiculo = $scope.numPerPage - 1;
155 } 163 }
156 } 164 }
157 165
158 function avanzarPagina() { 166 function avanzarPagina() {
159 if ($scope.currentPage < $scope.lastPage) { 167 if ($scope.currentPage < $scope.lastPage) {
160 $scope.selectPage($scope.currentPage + 1); 168 $scope.selectPage($scope.currentPage + 1);
161 $scope.selectedVehiculo = 0; 169 $scope.selectedVehiculo = 0;
162 } 170 }
163 } 171 }
164 } 172 }
165 ] 173 ]
166 ); 174 );
167 175
src/views/modal-vehiculo.html
1 <div class="modal-header py-1"> 1 <div class="modal-header py-1">
2 <div class="row w-100"> 2 <div class="row w-100">
3 <div class="col-lg-6"> 3 <div class="col-lg-6">
4 <h5 class="modal-title my-1">Busqueda de Vehiculo</h5> 4 <h5 class="modal-title my-1">Búsqueda de Vehiculo</h5>
5 </div> 5 </div>
6 <div class="input-group col-lg-6 pr-0 my-2"> 6 <div class="input-group col-lg-6 pr-0 my-2">
7 <input 7 <input
8 ladda="searchLoading" 8 ladda="searchLoading"
9 type="text" 9 type="text"
10 class="form-control" 10 class="form-control"
11 placeholder="Busqueda" 11 id="search"
12 placeholder="Búsqueda"
12 ng-model="filters" 13 ng-model="filters"
13 ng-change="search()" 14 ng-change="search()"
14 ng-keydown="busquedaDown($event.keyCode)" 15 ng-keydown="busquedaDown($event.keyCode)"
15 ng-keypress="busquedaPress($event.keyCode)" 16 ng-keypress="busquedaPress($event.keyCode)"
16 foca-focus="selectedVehiculo == -1" 17 foca-focus="selectedVehiculo == -1"
17 ng-focus="selectedVehiculo = -1" 18 ng-focus="selectedVehiculo = -1"
19 teclado-virtual
18 > 20 >
19 <div class="input-group-append"> 21 <div class="input-group-append">
20 <button 22 <button
21 ladda="searchLoading" 23 ladda="searchLoading"
22 class="btn btn-outline-secondary" 24 class="btn btn-outline-secondary"
23 type="button" 25 type="button"
24 ng-click="busquedaPress(13)" 26 ng-click="busquedaPress(13)"
25 > 27 >
26 <i class="fa fa-search" aria-hidden="true"></i> 28 <i class="fa fa-search" aria-hidden="true"></i>
27 </button> 29 </button>
28 </div> 30 </div>
29 </div> 31 </div>
30 </div> 32 </div>
31 </div> 33 </div>
32 <div class="modal-body" id="modal-body"> 34 <div class="modal-body" id="modal-body">
33 35
34 <div ng-show="!primerBusqueda"> 36 <div ng-show="!primerBusqueda">
35 Debe realizar una primer búsqueda. 37 Debe realizar una primer búsqueda.
36 </div> 38 </div>
37 39
38 <table ng-show="primerBusqueda" class="table table-striped table-sm"> 40 <table ng-show="primerBusqueda" class="table table-striped table-sm">
39 <thead> 41 <thead>
40 <tr> 42 <tr>
41 <th>Código</th> 43 <th>Código</th>
42 <th>Tractor</th> 44 <th>Tractor</th>
43 <th>Semi</th> 45 <th>Semi</th>
44 <th>Capacidad</th> 46 <th>Capacidad</th>
45 <th>Cisternado</th> 47 <th>Cisternado</th>
46 </tr> 48 </tr>
47 </thead> 49 </thead>
48 <tbody> 50 <tbody>
49 <tr ng-show="currentPageVehiculos.length == 0 && primerBusqueda"> 51 <tr ng-show="currentPageVehiculos.length == 0 && primerBusqueda">
50 <td colspan="5"> 52 <td colspan="5">
51 No se encontraron resultados. 53 No se encontraron resultados.
52 </td> 54 </td>
53 </tr> 55 </tr>
54 <tr class="selectable" 56 <tr class="selectable"
55 ng-repeat="(key,vehiculo) in currentPageVehiculos" 57 ng-repeat="(key,vehiculo) in currentPageVehiculos"
56 ng-click="select(vehiculo)"> 58 ng-click="select(vehiculo)">
57 <td ng-bind="vehiculo.id"></td> 59 <td ng-bind="vehiculo.id"></td>
58 <td ng-bind="vehiculo.tractor"></td> 60 <td ng-bind="vehiculo.tractor"></td>
59 <td ng-bind="vehiculo.semi"></td> 61 <td ng-bind="vehiculo.semi"></td>
60 <td ng-bind="vehiculo.capacidad"></td> 62 <td ng-bind="vehiculo.capacidad"></td>
61 <td ng-bind="vehiculo.cisternado"></td> 63 <td ng-bind="vehiculo.cisternado"></td>
62 <td> 64 <td>
63 <button 65 <button
64 type="button" 66 type="button"
65 class="btn btn-xs p-1 float-right" 67 class="btn btn-xs p-1 float-right"
66 ng-class="{ 68 ng-class="{
67 'btn-secondary': selectedVehiculo != key, 69 'btn-secondary': selectedVehiculo != key,
68 'btn-primary': selectedVehiculo == key 70 'btn-primary': selectedVehiculo == key
69 }" 71 }"
70 foca-focus="selectedVehiculo == {{key}}" 72 foca-focus="selectedVehiculo == {{key}}"
71 ng-keydown="itemVehiculo($event.keyCode)" 73 ng-keydown="itemVehiculo($event.keyCode)"
72 > 74 >
73 <i class="fa fa-circle-thin" aria-hidden="true"></i> 75 <i class="fa fa-circle-thin" aria-hidden="true"></i>
74 </button> 76 </button>
75 </td> 77 </td>
76 </tr> 78 </tr>
77 </tbody> 79 </tbody>
78 </table> 80 </table>
79 81
80 </div> 82 </div>
81 <div class="modal-footer py-1"> 83 <div class="modal-footer py-1">
82 <nav ng-show="currentPageVehiculos.length > 0 && primerBusqueda" class="mr-auto"> 84 <nav ng-show="currentPageVehiculos.length > 0 && primerBusqueda" class="mr-auto">
83 <ul class="pagination pagination-sm justify-content mb-0"> 85 <ul class="pagination pagination-sm justify-content mb-0">
84 <li class="page-item" ng-class="{'disabled': currentPage == 1}"> 86 <li class="page-item" ng-class="{'disabled': currentPage == 1}">
85 <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage - 1)"> 87 <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage - 1)">
86 <span aria-hidden="true">&laquo;</span> 88 <span aria-hidden="true">&laquo;</span>
87 <span class="sr-only">Anterior</span> 89 <span class="sr-only">Anterior</span>
88 </a> 90 </a>
89 </li> 91 </li>
90 <li 92 <li
91 class="page-item" 93 class="page-item"
92 ng-repeat="pagina in paginas" 94 ng-repeat="pagina in paginas"
93 ng-class="{'active': pagina == currentPage}" 95 ng-class="{'active': pagina == currentPage}"
94 > 96 >
95 <a 97 <a
96 class="page-link" 98 class="page-link"
97 href="javascript:void();" 99 href="javascript:void();"
98 ng-click="selectPage(pagina)" 100 ng-click="selectPage(pagina)"
99 ng-bind="pagina" 101 ng-bind="pagina"
100 ></a> 102 ></a>
101 </li> 103 </li>
102 <li class="page-item" ng-class="{'disabled': currentPage == lastPage}"> 104 <li class="page-item" ng-class="{'disabled': currentPage == lastPage}">
103 <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage + 1)"> 105 <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage + 1)">
104 <span aria-hidden="true">&raquo;</span> 106 <span aria-hidden="true">&raquo;</span>
105 <span class="sr-only">Siguiente</span> 107 <span class="sr-only">Siguiente</span>
106 </a> 108 </a>
107 </li> 109 </li>
108 </ul> 110 </ul>
109 </nav> 111 </nav>
110 <button class="btn btn-sm btn-secondary" type="button" ng-click="cancel()">Cancelar</button> 112 <button class="btn btn-sm btn-secondary" type="button" ng-click="cancel()">Cancelar</button>
111 </div> 113 </div>
112 114