Source: directives/mensageria/informacoes-grupo/informacoes-grupo.js

/**
 * @ngdoc directives
 * @name MensageriaInformacoesGrupo
 * @module s4c.directives.mensageria.informacoesgrupo.MensageriaInformacoesGrupo
 *
 * @description
 * `MensageriaInformacoesGrupo` Gerenciamento das informações dos grupos do módulo de mensageria 
 * 
 */
(function () {
    angular.module('s4c.components.collaboration')
        .directive('mensageriaInformacoesGrupo', MensageriaInformacoesGrupo);

    MensageriaInformacoesGrupo.$inject = ['MensageriaService', '$mdDialog', 'toasty', 'AuthService'];

    function MensageriaInformacoesGrupo(MensageriaService, $mdDialog, toasty, AuthService) {
        return {
            restrict: 'E',
            templateUrl: 'app/directives/mensageria/informacoes-grupo/informacoes-grupo.html',
            replace: true,
            scope: {
                grupo: '=',
                usuarios: '=',
                isInformacoesActive: '=informacoesActive',
                deletarGrupo: '=deletarGrupo'
            },
            link: function ($scope, $elem, $attrs) {
                $scope.res = $scope.$root.res;
                $scope.$watch('grupo', function () {
                    if (!$scope.grupo) {
                        return;
                    }
                    MensageriaService.findGrupoParticipantes($scope.grupo.id)
                        .then(function (participantes) {
                            $scope.participantes = participantes;
                        });
                }, true);

                $scope.voltarSelecao = function () {
                    $scope.isInformacoesActive = false;
                };

                $scope.grupo = $scope.$parent.grupo;
            },
            controller: function ($scope, toasty) {
                $scope.res = $scope.$root.res;

                /**
                 * @method editarGrupo
                 * @param {*} evt 
                 */
                function editarGrupo(evt) {
                    $mdDialog.show({
                        targetEvent: evt,
                        template:
                            '<md-dialog>' +
                            '<md-content>' +
                            '<div>' + $scope.res('EDITAR_GRUPO') + '</div>' +
                            '<md-input-container>' +
                            '   <label>' + $scope.res('COMUM_NOME') + '</label>' +
                            '   <input type="text" ng-model="grupo.nome" required />' +
                            '</md-input-container>' +
                            '</md-content>' +
                            '<div class="md-actions">' +
                            '  <md-button ng-click="salvar()">' + $scope.res('COMUM_SALVAR') + '</md-button>' +
                            '  <md-button ng-click="cancelar()">' + $scope.res('COMUM_CANCELAR') + '</md-button>' +
                            '</div>' +
                            '</md-dialog>',
                        locals: {
                            grupo: $scope.grupo
                        },
                        controller: function ($scope, grupo) {
                            $scope.res = $scope.$root.res;
                            $scope.grupo = grupo;
                            var nomeAntigo = $scope.grupo.nome;

                            /**
                             * @method cancelar
                             */
                            function cancelar() {
                                $scope.grupo.nome = nomeAntigo;
                                $mdDialog.hide();
                            }

                            /**
                             * @method salvar
                             */
                            function salvar() {
                                MensageriaService.atualizarGrupoUsuarios($scope.grupo)
                                    .then(function (data) {
                                        $scope.grupo = data;
                                        $mdDialog
                                            .show($mdDialog.alert()
                                                .title($scope.res('COMUM_SUCESSO'))
                                                .content($scope.res('GRUPO_ATUALIZADO_SUCESSO'))
                                                .ok($scope.res('COMUM_OK')));
                                    }, function (err) {
                                        toasty({
                                            msg: err.data.message
                                        });
                                    });
                            };

                            angular.extend($scope, {
                                cancelar: cancelar,
                                salvar: salvar
                            })
                        }
                    });
                }

                /**
                 * @method participa
                 * @param {*} id 
                 */
                function participa(id) {
                    return AuthService.user.info.id == id;
                }

                /**
                 * @method removerParticipanteGrupo
                 * @param {*} participante 
                 */
                function removerParticipanteGrupo(participante) {

                    var confirm = $mdDialog.confirm()
                        .title($scope.res('COMUM_CONFIRMACAO'))
                        .content($scope.res('MENSAGERIA_CERTEZA_SAIR_GRUPO'))
                        .ariaLabel($scope.res('MENSAGERIA_SAIR_GRUPO'))
                        .ok($scope.res('COMUM_SIM'))
                        .cancel($scope.res('COMUM_NAO'));

                    $mdDialog.show(confirm)
                        .then(function () {
                            MensageriaService.deleteParticipanteGrupo($scope.grupo.id, participante.id, participante.nome)
                                .then(function (result) {
                                    $scope.grupo.participantes = result;
                                    participante.isAdded = false;

                                    var parentScope = $scope.$parent.$parent;
                                    if (AuthService.user.info.id != $scope.grupo.UsuarioId) {
                                        $scope.isInformacoesActive = false;
                                        parentScope.state.chatGrupoAtivo = false;
                                    }
                                });
                        });
                }

                angular.extend($scope, {
                    participa: participa,
                    removerParticipanteGrupo: removerParticipanteGrupo,
                    editarGrupo: editarGrupo
                })

            }
        }
    }
}());