diff --git a/README.md b/README.md index 4d68070..ff28d22 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,4 @@ Y despues consiguiendo el resultado de esta forma: // funcion ejecutada cuando se cancela el modal } ); - \ No newline at end of file + diff --git a/index.html b/index.html index f3157be..51b08f3 100644 --- a/index.html +++ b/index.html @@ -12,6 +12,7 @@ + diff --git a/package.json b/package.json index 4a25376..ec041fa 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "foca-navegacion-doble", + "name": "foca-modal-busqueda-productos", "version": "0.0.1", "description": "Menu de navegacion de doble entrada", "main": "index.js", @@ -32,11 +32,13 @@ "gulp-replace": "^1.0.0", "gulp-uglify": "^3.0.1", "jquery": "^3.3.1", - "pump": "^3.0.0" + "pump": "^3.0.0", + "foca-directivas": "git+https://192.168.0.11/modulos-npm/foca-directivas", }, "devDependencies": { "angular": "^1.7.4", "bootstrap": "^4.1.3", + "foca-directivas": "git+https://192.168.0.11/modulos-npm/foca-directivas", "font-awesome": "^4.7.0", "gulp": "^3.9.1", "gulp-angular-templatecache": "^2.2.1", diff --git a/src/js/app.js b/src/js/app.js index 9677048..e449b68 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -1 +1 @@ -angular.module('focaBusquedaProductos', ['ui.bootstrap']); +angular.module('focaBusquedaProductos', ['ui.bootstrap','focaDirectivas']); diff --git a/src/js/controller.js b/src/js/controller.js index 240d1fc..ac87308 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -8,7 +8,7 @@ angular.module('focaBusquedaProductos') function($filter, $scope, $uibModalInstance, focaBusquedaProductosService) { focaBusquedaProductosService.getProductos().then( function(res) { - $scope.productos = res; + $scope.productos = res.data; $scope.search(); } ); @@ -18,12 +18,12 @@ angular.module('focaBusquedaProductos') $scope.currentPage = 1; $scope.filteredProductos = []; $scope.currentPageProductos = []; - $scope.selectProducto = 0; + $scope.selectedProducto = -1; - //METODOS $scope.search = function() { $scope.filteredProductos = $filter('filter')($scope.productos, {$: $scope.filters}); + $scope.lastPage = Math.ceil($scope.filteredProductos.length / $scope.numPerPage); $scope.resetPage(); } @@ -35,7 +35,10 @@ angular.module('focaBusquedaProductos') $scope.selectPage = function(page) { var start = (page - 1) * $scope.numPerPage; var end = start + $scope.numPerPage; + $scope.paginas = []; + $scope.paginas = calcularPages(page); $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); + $scope.currentPage = page; } $scope.select = function(producto) { @@ -46,11 +49,100 @@ angular.module('focaBusquedaProductos') $uibModalInstance.dismiss('cancel'); } - $scope.enter = function(key) { + $scope.busquedaDown = function(key) { + if (key === 40) { + primera(key); + } + } + + $scope.busquedaPress = function(key) { if (key === 13) { - console.table($scope.currentPageProductos); + primera(key); + } + } + + $scope.itemProducto = function(key) { + console.info(key); + if (key == 38) { + anterior(key); + } + + if (key == 40) { + siguiente(key); + } + + if (key == 37) { + retrocederPagina(); + } + + if (key == 39) { + avanzarPagina(); + } + } + + function calcularPages(paginaActual) { + var paginas = []; + paginas.push(paginaActual); + + if (paginaActual - 1 > 1) { + + paginas.unshift(paginaActual - 1); + if (paginaActual - 2 > 1) { + paginas.unshift(paginaActual - 2); + } + } + + if (paginaActual + 1 < $scope.lastPage) { + paginas.push(paginaActual + 1); + if (paginaActual + 2 < $scope.lastPage) { + paginas.push(paginaActual + 2); + } + } + + if (paginaActual !== 1) { + paginas.unshift(1); + } + + if (paginaActual !== $scope.lastPage) { + paginas.push($scope.lastPage); + } + + return paginas; + } + + function primera(key) { + $scope.selectedProducto = 0; + } + + function anterior(key) { + if ($scope.selectedProducto === 0) { + anteriorPagina(); + } else { + $scope.selectedProducto--; + } + } + + function siguiente(key) { + if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { + $scope.selectedProducto++; + } else { + avanzarPagina(); + } + } + + function retrocederPagina() { + if ($scope.currentPage > 1) { + $scope.selectPage($scope.currentPage - 1); + $scope.selectedProducto = $scope.numPerPage - 1; + } + } + + function avanzarPagina() { + if ($scope.currentPage < $scope.lastPage) { + $scope.selectPage($scope.currentPage + 1); + $scope.selectedProducto = 0; } } } ] - ) \ No newline at end of file + ) diff --git a/src/js/service.js b/src/js/service.js index c7e87e5..352d279 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -1,37 +1,8 @@ angular.module('focaBusquedaProductos') - .service('focaBusquedaProductosService', ['$q', function($q) { - return { - getProductos: function(filtro) { - var deferred = $q.defer(); - - deferred.resolve([ - { - idProducto: 1, - codigo: 1, - sector: 1, - descripcion: 'Nafta Comun', - codsecfilter: '1-1', - precio: 35 - }, - { - idProducto: 2, - codigo: 2, - sector: 1, - descripcion: 'Diesel', - codsecfilter: '1-2', - precio: 40 - }, - { - idProducto: 3, - codigo: 1, - sector: 2, - descripcion: 'Aceite', - codsecfilter: '2-1', - precio: 95 - } - ]); - - return deferred.promise; - } - } - }]) \ No newline at end of file + .service('focaBusquedaProductosService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) { + return { + getProductos: function(filtro) { + return $http.get(API_ENDPOINT.URL + '/articulos'); + } + } + }]) diff --git a/src/views/modal-busqueda-productos.html b/src/views/modal-busqueda-productos.html index 8888290..b520e32 100644 --- a/src/views/modal-busqueda-productos.html +++ b/src/views/modal-busqueda-productos.html @@ -9,7 +9,10 @@ placeholder="Busqueda" ng-model="filters" ng-change="search()" - ng-keypress="enter($event.keyCode)" + ng-keydown="busquedaDown($event.keyCode)" + ng-keypress="busquedaPress($event.keyCode)" + foca-focus="selectedProducto == -1" + ng-focus="selectedProducto = -1" >
- |