controller.js 2.48 KB
angular.module('appWrapperDemo')
    .controller('appWrapperDemoController', [
        '$scope',
        '$rootScope',
        '$timeout',
        'focaSqliteService',
        'focaModalService',
        'API_ENDPOINT',
        '$localStorage',
        function($scope, $rootScope, $timeout, focaSqliteService, focaModalService, API_ENDPOINT, $localStorage) {
            document.addEventListener('deviceready', function() {
                focaSqliteService.openDataBase();
            });

            if (!API_ENDPOINT.URL && !$localStorage.urlEndPoint) {
                API_ENDPOINT.getUrl().then(success, err);
            } else if ($localStorage.urlEndPoint && !API_ENDPOINT.URL) {
                API_ENDPOINT.setUrl($localStorage.urlEndPoint);
            }

            function success(res) {

                if (res.data === 'terminal_not_found') {
                    err();
                    return;
                }
                API_ENDPOINT.setUrl(res.data);
            }

            function err() {

                $timeout(function() {
                    focaModalService.alert('Terminal no configurada: \n' + $localStorage.terminalKey);
                }, 1000);
            }

            $scope.usarTeclado = false;
            $rootScope.$broadcast('usarTeclado', false);
            $scope.mostrarTeclado = false;
            //Envía broadcast para avisar que el teclado está en funcionamiento o no
            //para su uso cambiar ng-click del boton por esta función
            $scope.cambioUsoTeclado = function() {
                if($scope.usarTeclado) {
                    $scope.usarTeclado = false;
                    $rootScope.$broadcast('usarTeclado', false);
                    return;
                }
                $scope.usarTeclado = true;
                $rootScope.$broadcast('usarTeclado', true);
            };

            $rootScope.$on('focus', function() {
                if(!$scope.usarTeclado) {
                    return;
                }
                $scope.mostrarTeclado = true;
                $timeout.cancel($scope.timeout);
                if(!$scope.$$phase) {
                    $scope.$apply();
                }
            });
            $rootScope.$on('blur', function() {
                $scope.timeout = $timeout(function() {
                    $scope.mostrarTeclado = false;
                    if(!$scope.$$phase) {
                        $scope.$apply();
                    }
                }, 150);
            });
        }
    ]);