Source: directives/participantes/participantes.js

/**
 * @ngdoc directives
 * @name Participantes
 * @module s4c.directives.participantes.Participantes
 *
 * @description
 * `Participantes` Controller do módulo Participantes 
 * 
 * @example <s4c-participantes></s4c-participantes>
 */
(function () {
    'use strict';

    function participantesCtrl($scope, ParticipantesService, $rootScope, localize) {

        $scope.res = $rootScope.res;

        $scope.$watch('usuariosParticipantes', function () {
            ParticipantesService.categorizarResultado($scope.usuariosParticipantes, 'usuario');
        });

        $scope.$watch('gruposUsuariosParticipantes', function () {
            ParticipantesService.categorizarResultado($scope.gruposUsuariosParticipantes, 'grupo');
        });

        $scope.$watch('agenciasParticipantes', function () {
            ParticipantesService.categorizarResultado($scope.agenciasParticipantes, 'agencia');
        });

        /**
         * @method pesquisar
         * @param {*} texto 
         */
        function pesquisar(texto) {
            return ParticipantesService.pesquisar(texto);
        }

        /**
         * @method onSelected
         * @param {*} item 
         */
        function onSelected(item) {
            console.log(item);
            $(this).click(function () {
                $(this).parent().hide();
            })
        }

        /**
         * @method selecionar
         * @param {*} item 
         */
        function selecionar(item) {
            $scope.searchTerm = '';

            if (item) {
                if (item._tipo == 'usuario') {
                    $scope.usuariosParticipantes.push(item);
                } else if (item._tipo == 'grupo') {
                    $scope.gruposUsuariosParticipantes.push(item);
                } else {
                    $scope.agenciasParticipantes.push(item);
                }
            }
        }

        /**
         * @method remover
         * @param {*} item 
         */
        function remover(item) {
            if (item) {
                if (item._tipo == 'usuario') {
                    _.remove($scope.usuariosParticipantes, { id: item.id });
                } else if (item._tipo == 'grupo') {
                    _.remove($scope.gruposUsuariosParticipantes, { id: item.id });
                } else {
                    _.remove($scope.agenciasParticipantes, { id: item.id });
                }
            }
        }

        angular.extend($scope, {
            pesquisar: pesquisar,
            selecionar: selecionar,
            remover: remover
        })

    }

    function s4cParticipantes() {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/participantes/participantes.html',
            replace: true,
            controller: participantesCtrl,
            scope: {
                'usuariosParticipantes': '=',
                'gruposUsuariosParticipantes': '=',
                'agenciasParticipantes': '=',
                'canDelete': '=?'
            },
            link: function (scope, elem, attrs) {
                if (!scope.canDelete) {
                    scope.canDelete = true;
                }

                scope.collapsible = attrs.collapsible == "true" ? true : (attrs.collapsible == "false" ? false : true);

                if (!scope.collapsible) {
                    scope.participantesHidden = false;
                }

                if (attrs.tema) {
                    scope.tema = attrs.tema;
                }

                if (attrs.buscar) {
                    scope.buscar = attrs.buscar;
                }
            }
        };
    }

    participantesCtrl.$inject = [
        '$scope',
        'ParticipantesService',
        '$rootScope',
        'localize'
    ]

    angular.module('s4c.components.participantes', [])
        .directive('s4cParticipantes', s4cParticipantes);
}());