controller.js 3.75 KB
angular.module('focaModalDetalles')
    .controller('focaModalDetallesController', 
        [
            '$scope',
            '$uibModalInstance',
            'focaModalService',
            'APP',
            '$timeout',
            'parametros',
            function($scope, $uibModalInstance, focaModalService, APP, $timeout, parametros) {

                $scope.mobile = APP === 'cobranza';
                $scope.detalles = parametros;

                $scope.$watch('detalles.files', function() {
                    validarExtSize();
                });

                $scope.eliminarImg = function(key) {
                    $scope.detalles.files.splice(key, 1);
                };

                $scope.cancel = function() {
                    $uibModalInstance.dismiss('cancel');
                };

                $scope.aceptar = function() {

                    $scope.detalles.importe = parseFloat($scope.detalles.importe);
                    $uibModalInstance.close($scope.detalles);
                };

                $scope.seleccionarFoto = function() {
                    var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
                    var options = setOptions(srcType);
                    navigator.camera.getPicture(cameraSuccess, cameraError, options);
                };

                $scope.tomarFoto = function() {
                    navigator.camera.getPicture(cameraSuccess, cameraError,
                        {
                            destinationType: Camera.DestinationType.DATA_URL,
                            quality: 100
                        });
                };

                function cameraSuccess(imageBase64) {

                    var fecha = new Date();
                    var size = (imageBase64.length * (3/4));

                    var imagen = {
                        name: 'imagen_' + fecha.getTime() + '.jpg',
                        size: size,
                        base64: imageBase64,
                    };
                    $timeout(function() {
                        $scope.detalles.files.push(imagen);
                    });
                }
                function cameraError(err) {
                    console.log(err);
                }
                function setOptions(srcType) {
                    var options = {
                        // Some common settings are 20, 50, and 100
                        quality: 100,
                        destinationType: Camera.DestinationType.DATA_URL,
                        sourceType: srcType,
                        encodingType: Camera.EncodingType.JPEG,
                        mediaType: Camera.MediaType.PICTURE,
                        allowEdit: true,
                        correctOrientation: true
                    };
                    return options;
                }
                function validarExtSize() {
                    var totalSize = 0;
                    var invalidExt = false;
                    $scope.detalles.files.forEach(function(file) {
                        //CONVIERTO BYTES A MB
                        totalSize += file.size / 1000000;
                        var fileExt = file.name.split('.').pop();

                        if(fileExt != 'jpg' && fileExt != 'png') {
                            invalidExt = true;
                        }
                    });

                    if (totalSize > 150) {
                        focaModalService.alert('El/los archivo/s exceden la capacidad máxima');
                        $scope.detalles.files = [];
                    } else if (invalidExt) {
                        focaModalService.alert('Ha ingresado un archivo con extensión inválida');
                        $scope.detalles.files = [];
                    }
                }
            }
        ]
    );