Source: directives/cameras/cameras.js

(function () {
    'use strict';

    camerasCtrl.$inject = ['$scope', 'Preset', '$timeout', 'VideoStreamService'];

    /**
     * @ngdoc directives
     * @name Cameras
     * @module s4c.directives.cameras.Cameras
     *
     * @description
     * `camerasCtrl` Controller do módulo de cameras no sistema
     *
     *@example
     *   <s4c-cameras>
     *   </s4c-cameras>
     */

    function camerasCtrl($scope, Preset, $timeout, VideoStreamService) {

       /**
        * Abre a camera  
        *
        * @method abrirCamera
		*        
        * @param url {String} Url da Camera
        *  
		*               
        */ 
        function abrirCamera(url) {
            console.log('abrindo camera => ', url);
            $scope.$api.status = true;
            $scope.$api.numero = numero;
            $scope.nome = nome;
            $scope.url = url;
        }

       /**
        * Inicia o Streaming do Vídeo  
        *
        * @method openVideo
		*        
        * @param link {String} Url do Streaming
        *  
		*               
        */ 
        function openVideo(link) {

            $scope.modulo.extras = { url: link };
            VideoStreamService.open($scope.videoDivId, link);
            Preset.salvar();
        }


        var modulos = Preset.obter().PresetModulos;
        $scope.presetId = Preset.obter().id;

        $scope.videoDivId = Math.random().toString(36).substring(2, 15);
        $timeout(function () {
            for (var index in modulos) {
                if ((modulos[index].template == 'app/components/modulos/modulo-cameras.html' ||
                    (modulos[index].Face && modulos[index].Face.template == 'app/components/modulos/modulo-cameras.html')) && !modulos[index].usado) {

                    modulos[index].usado = true;
                    modulos[index].index = 1;
                    $scope.modulo = modulos[index];
                    openVideo(modulos[index].Face.endereco);
                    break;
                }
            }
        }, 300);

        $scope.$api = {
            abrirCamera: abrirCamera,
            status: false,
            numero: -1
        };
    }

    s4cCameras.$inject = [
        'CamerasManager'
    ]

    function s4cCameras(CamerasManager) {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/cameras/cameras.html',
            controller: camerasCtrl,
            link: function ($scope) {
                if ($scope.$parent.CamerasManager)
                    $scope.$parent.CamerasManager.cameras.push($scope.$api);

                $scope.$on('$destroy', function () {
                    var index = _.findIndex(CamerasManager, $scope.$api);
                    if (index > -1) {
                        CamerasManager.cameras.splice(index, 1);
                    }
                });
            }
        };
    }

    angular.module('s4c.components.cameras', [])
        .directive('s4cCameras', s4cCameras);
}());