Source: directives/legenda-incidente/legenda-incidente.js

// Após a execução do proceso S4C-449, que fez as legendas não desencadearem
// ato de filtragem, aparentemente essa diretiva não tem mais sentido em
// existir no sistema. A remoção deste arquivo é recomendada quando os testes
// do sistema não falharem na ausência deste. 
/**
 * @ngdoc directives
 * @name LegendaIncidente
 * @module s4c.directives.legendaincidente.legendaIncidente
 *
 * @description
 * `LegendaIncidente` exibe informações sobre a Legenda de Incidentes
 *
 *@example
 *   <legenda-incidente></legenda-incidente>
 *
 */
(function () {
    angular.module('s4c.components.legenda-incidente', ['angularMoment'])
        .directive('legendaIncidente', LegendaIncidente);

    LegendaIncidente.$inject = ['LegendaIncidenteFilter', 'IncidenteFilter', 'MsiFilter', 'MainState', 'IncidentesService', 'CommService', 'IncidenteFilter', 'localize'];

    function LegendaIncidente(LegendaIncidenteFilter, IncidenteFilter, MsiFilter, MainState, IncidentesService, CommService, IncidenteFilter, localize) {

        var filterTypes = {
            'incidentes': IncidenteFilter,
            'msi': MsiFilter
        };

        return {
            restrict: 'E',
            templateUrl: 'app/directives/legenda-incidente/legenda-incidente.html',
            scope: {
                relatedFilterId: '=',
                relatedFilterType: '='
            },
            link: function ($scope, $elem, $attrs) {
                angular.extend($scope, {
                    filtroAbertoAtivo: true,
                    filtroFechadoAtivo: false,
                    res: $scope.$root.res
                });


                /**
                 * Listener para aplicação da atualização da lista de incidentes disparados de outros componentes
                 * 
                 * @method filterChanged
                 * 
                 */                          
                IncidenteFilter.getInstance('filtro-incidentes')
                    .on('countByStatus', countByStatus);

                /**
                 * Listener para aplicação da inclusão de incidente na lista de incidentes disparados de outros componentes
                 * 
                 * @method incidentes_
                 * 
                 * @param incidente {Object} 
                 * 
                 */                       
                CommService.on('incidentes_', function (incidente) {
                    countByStatus();
                });
                /**
                 * Listener para aplicação da remoção de incidente da lista de incidentes disparados de outros componentes
                 * 
                 * @method removeIncidentes_
                 * 
                 * @param incidente {Object} 
                 * 
                 */                 
                CommService.on('removeIncidentes_', function (incidente) {
                    countByStatus();
                });

		        /**
		         * Atualiza a quantidade de Incidentes 
		         * 
		         * @method countByStatus
		         *
		         *
		         */                      
                function countByStatus() {
                    $scope.qtdIncidentesAbertos = 0;//inicializando
                    $scope.qtdIncidentesFechados = 0;//inicializando
                    var interval = setTimeout(function () {
                        var incidenteDirective = MainState.getDirective('incidentes');
                        var query = JSON.stringify(incidenteDirective.setQuery());
                        if (query != undefined || query != null || query != "") {
                            //clearInterval(interval);
                            IncidentesService.getCountStatus(query).then(function (data) {
                                data.forEach(function (e) {
                                    if (e.key == 'ABERTO') {
                                        $scope.qtdIncidentesAbertos = e.value;
                                    } else if (e.key == 'FECHADO') {
                                        $scope.qtdIncidentesFechados = e.value;
                                    }
                                });
                            });
                        }
                    }, 1000);
                }

		        /**
		         * Listener do atributo filtroAbertoAtivo
		         * 
		         * @method watch
		         *
		         *
		         */                   
                $scope.$watch('filtroAbertoAtivo', function () {
                    var status = LegendaIncidenteFilter.getInstance($attrs.id).statuses;

                    if ($scope.filtroAbertoAtivo) {
                        status.push('ABERTO');
                    } else {
                        _.remove(status, function (valor) {
                            return valor === 'ABERTO';
                        });
                    }

                    LegendaIncidenteFilter.getInstance($attrs.id).trigger('filterChanged', 'filtro');
                });

		        /**
		         * Listener do atributo filtroFechadoAtivo
		         * 
		         * @method watch
		         *
		         *
		         */                  
                $scope.$watch('filtroFechadoAtivo', function () {
                    var status = LegendaIncidenteFilter.getInstance($attrs.id).statuses;

                    if ($scope.filtroFechadoAtivo) {
                        status.push('FECHADO');
                    } else {
                        _.remove(status, function (valor) {
                            return valor === 'FECHADO';
                        });
                    }

                    LegendaIncidenteFilter.getInstance($attrs.id).trigger('filterChanged', 'filtro');
                });

                $scope.relatedFilter = filterTypes[$scope.relatedFilterType].getInstance($scope.relatedFilterId);

                $scope.changed = false;
                var dataRange = $scope.relatedFilter.dataRange;

		        /**
		         * Listener do atributo filterChanged
		         * 
		         * @method relatedFilter
		         *
		         *
		         */                   
                $scope.relatedFilter.on('filterChanged', function () {
                    var dataRangeFilter = $scope.relatedFilter.dataRange;

                    if (_hasChange(dataRangeFilter, dataRange)) {
                        $scope.changed = true;
                    }
                    dataRange = $scope.relatedFilter.dataRange;
                });

                /**
                 * Verifica se houve alteração na data
                 * 
                 * @method _hasChange
                 *
                 * @param newData {Date} 
                 * @param oldData {Date}
                 *  
                 */                    
                function _hasChange(newData, oldData) {
                    if (newData !== oldData && oldData !== undefined) return true;
                }

                $scope._ = _;

            }
        }
    }
}());