Source: directives/terminal/terminal.js

/**
 * @ngdoc directives
 * @name Terminal
 * @module s4c.directives.terminal.Terminal
 *
 * @description
 * `Terminal` Controller do módulo de Terminal
 * 
 * 
 */
(function () {
    'use strict';

    function s4cTerminal() {
        return {
            restrict: 'EA',
            templateUrl: 'app/directives/terminal/terminal.html',
            controller: terminalCtrl,
            link: function ($scope, $elem, $attrs) {
                $scope.$parent.$directives = $scope.$parent.$directives || {};

                if ($scope.$parent.$directives['terminal']) {
                    throw 'Directive name already in use!';
                }

                $scope.$parent.$directives['terminal'] = $scope.$api;

                $scope.$on('$destroy', function () {
                    $scope.$parent.$directives['terminal'] = undefined;
                });
            }
        };
    }




    function terminalCtrl(
        $rootScope,
        $scope,
        MainState,
        $filter,
        $mdDialog,
        MapaService) {

        /**
         * @method abrir
         * @param {*} terminal 
         */
        function abrir(terminal) {
            $scope.connectionStatusText = "";
            $scope.terminal = terminal;
        }

        function fechar() {

        }

        /**
         * @method tarefa
         * @param {*} tarefa 
         */
        function editarTarefa(tarefa) {
            var tarefaCopy = angular.copy(tarefa);

            if (tarefaCopy.dataInicio)
                tarefaCopy.dataInicio = $filter('timestamp2datetime')(tarefaCopy.dataInicio);

            if (tarefaCopy.dataFim)
                tarefaCopy.dataFim = $filter('timestamp2datetime')(tarefaCopy.dataFim);

            var EditPTarefaManager = MainState.getManager('EditPTarefaManager');
            EditPTarefaManager.abrirTarefa(tarefaCopy, true);
        }

        /**
         * @method updateTarefa
         * @param {*} tarefa 
         */
        function updateTarefa(tarefa) {
            if ($scope.terminal.Tarefas === undefined)
                return;

            // pop previus if exists:
            var findBy = { 'id': tarefa.id };
            var tarefaIndex = _.findIndex($scope.terminal.Tarefas, findBy);
            if (tarefaIndex >= 0) {
                $scope.terminal.Tarefas.splice(tarefaIndex, 1);
                $scope.terminal.Tarefas.push(tarefa);
            }
        }

        /**
         * @method abrirVideo
         * @param {*} url 
         */
        function abrirVideo(url) {
            $mdDialog.show({
                template:
                    '<md-dialog aria-label="Detalhes video" style="max-with: 660px; max-height: 600px; overflow: scroll;">' +
                    '<md-dialog-content>' +
                    '<video width="640" height="480" controls autoplay style="margin:10px;">' +
                    '<source src="' + url + '" type="video/mp4">' +
                    '<source src="' + url + '" type="video/ogg">' +
                    '<source src="' + url + '" type="video/webm">' +
                    'Your browser does not support the video tag.' +
                    '</video>' +
                    '</md-dialog-content>' +
                    '<div class="md-actions">' +
                    '<md-button ng-click="closeDialog()" class="md-primary">Fechar</md-button>' +
                    '</div>' +
                    '</md-dialog>',
                locals: {
                    items: $scope.items
                },
                controller: function ($scope, $mdDialog) {
                    $scope.closeDialog = function () {
                        $mdDialog.hide();
                    };
                }
            });
        };

        /**
         * @method voarParaLocalizacao
         */
        function voarParaLocalizacao(geojson) {
            geojson = JSON.parse(geojson);

            MapaService.piscarAzul({
                lat: geojson.coordinates[1],
                lng: geojson.coordinates[0],
            });

            MapaService.flyTo(geojson);
        }

        /**
         * @method setConnectionStatusTextCb
         * @param {*} text 
         */
        function setConnectionStatusTextCb(text) {
            $scope.connectionStatusText = text;
        }

        /**
         * @method abrirCamera
         */
        function abrirCamera() {
            $scope.connectionStatusText = "Conectando...";
            abrirCameraNova($scope.terminal.chave_estrangeira, $scope.terminal.geojson, abrirVideo, setConnectionStatusTextCb);
        }

        angular.extend($scope, {
            terminal: {},
            editarTarefa: editarTarefa,
            abrirVideo: abrirVideo,
            voarParaLocalizacao: voarParaLocalizacao,
            abrirCamera: abrirCamera,
            $api: {
                abrir: abrir,
                fechar: fechar,
                updateTarefa: updateTarefa
            }
        });

        MainState.registerDirective('terminal', $scope.$api);
        $scope.$on('$destroy', function () {
            MainState.unregisterDirective('terminal');
        });
    }



    /* Injects */

    s4cTerminal.$inject = [];

    terminalCtrl.$inject = [
        '$rootScope',
        '$scope',
        'MainState',
        '$filter',
        '$mdDialog',
        'MapaService'
    ];

    angular.module('s4c.components.terminal', [])
        .directive('s4cTerminal', s4cTerminal);

})();