diff --git a/src/js/controller.js b/src/js/controller.js index 663383f..804fadd 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -154,7 +154,7 @@ angular.module('focaAbmVehiculo') }; function salir() { - if (!$scope.formVehiculo.$pristine) { + if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { focaModalService.confirm( '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' ).then(function(data) { @@ -181,8 +181,8 @@ angular.module('focaAbmVehiculo') if (key === 13) { - if ($scope.formVehiculo.$pristine) { - $scope.cancelar(); + if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { + salir(); return; } @@ -299,7 +299,7 @@ angular.module('focaAbmVehiculo') total += parseInt(cisterna.capacidad); } }); - return $scope.vehiculo.capacidad === total; + return $scope.vehiculo.capacidad == total; } function guardarCisternas() { @@ -316,5 +316,6 @@ angular.module('focaAbmVehiculo') return focaAbmVehiculoService.guardarCisternas(cisternas); } + } ]); diff --git a/src/js/controllerCisterna.js b/src/js/controllerCisterna.js index f43ca56..0cb3562 100644 --- a/src/js/controllerCisterna.js +++ b/src/js/controllerCisterna.js @@ -1,9 +1,9 @@ angular.module('focaAbmVehiculo') .controller('focaAbmVehiculoCisternaController', [ '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', - 'focaModalService', 'focaBotoneraLateralService', '$timeout','$localStorage', '$filter', + 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$window', function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, - focaModalService, focaBotoneraLateralService, $timeout, $localStorage, $filter) { + focaModalService, focaBotoneraLateralService, $timeout, $window) { $scope.nuevo = ($routeParams.idx > -1) ? false : true; $scope.editar = false; $scope.now = new Date(); @@ -66,6 +66,8 @@ angular.module('focaAbmVehiculo') $location.path('/vehiculo'); } }); + } else { + $window.history.back(); } }; @@ -74,9 +76,7 @@ angular.module('focaAbmVehiculo') if ($scope.formCisterna.$pristine) { $scope.cancelar(); return; - } - - if (!$scope.cisterna.codigo) { + } else if (!$scope.cisterna.codigo) { focaModalService.alert('Ingrese codigo de cisterna'); return; } else if (!$scope.cisterna.capacidad) { @@ -130,7 +130,7 @@ angular.module('focaAbmVehiculo') cisternas.forEach(function(cisterna, idx) { //SI EL CODIGO YA EXISTE if (cisterna.codigo === $scope.cisterna.codigo && - idx !== $routeParams.idx && + idx != $routeParams.idx && !cisterna.desactivado) { reject('Código de cisterna existente'); } diff --git a/src/js/service.js b/src/js/service.js index b582f48..b9295b8 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -1,6 +1,7 @@ angular.module('focaAbmVehiculo') .factory('focaAbmVehiculoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) { var cisternas = []; + var cisternasPristine = true; return { getVehiculos: function() { return $http.get(API_ENDPOINT.URL + '/vehiculo'); @@ -18,9 +19,11 @@ angular.module('focaAbmVehiculo') return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id); }, getCisternas: function(idVehiculo) { - if(cisternas.length) { + if (cisternas.length) { + cisternasPristine = true; return Promise.resolve(angular.copy(cisternas)); - }else { + } else { + cisternasPristine = true; return new Promise(function(resolve) { $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo) .then(function(res) { @@ -31,12 +34,14 @@ angular.module('focaAbmVehiculo') } }, guardarCisterna: function(cisterna, idx) { - if(idx !== '-1') { + if (idx !== '-1') { //update cisternas[idx] = cisterna; - }else { + cisternasPristine = false; + } else { //insert cisternas.push(cisterna); + cisternasPristine = false; } }, guardarCisternas: function(cisternas) { @@ -44,6 +49,7 @@ angular.module('focaAbmVehiculo') }, deleteCisterna: function(idx) { cisternas[idx].desactivado = true; + cisternasPristine = false; }, cleanCisternas: function() { cisternas = [];