<commit_before>/**
 * Created by jeanella on 3/26/2015.
 */

var ngSearchFilter = angular.module('ngSearchFilter', []);

ngSearchFilter.directive('searchFilter', function(){
    return {
        restrict: 'E',
        templateUrl: 'search-filter/filter.html',
        transclude: true,
        scope: {
            collection: '=',
            filters: '=',
            collectionType: '@'
        },
        controller: 'SearchFilterCtrl'
    };
});

ngSearchFilter.controller('SearchFilterCtrl', ['$scope','$filter',
    function($scope, $filter){
        $scope.searchKeyword = '';
        $scope.isActiveSearch = false;
        $scope.collectionFields = [];

        $scope.$watch('collection', function(newCollection){
            $scope.collectionFields = $scope.getFields(angular.copy(newCollection));
            $scope.countFilters();
        });

        $scope.getFields = function(collection){
            return _.keys(angular.copy(collection[0]));
        };

        $scope.getValues = function(item){
            return _.values(angular.copy(item));
        };

        $scope.toggleFilter = function(filter, value){
            var general = _.findWhere(filter.values, { isAll: true});

            if(value.isActive){
                if(!value.isAll){
                    if(_.where(filter.values, { isActive: true}).length <= 1){
                        general.isActive = true;
                    }
                    value.isActive = !value.isActive;
                }
            } else {
                if(value.isAll){
                    _.each(_.where(filter.values, { isAll: false}), function(filterValue){
                        filterValue.isActive = false;
                    });
                } else {
                    general.isActive = false;
                }
                value.isActive = !value.isActive;
            }

            $scope.isActiveSearch = (!general.isActive || value.isActive) && !value.isAll;
        };

        $scope.clearFilters = function(){
            $scope.searchKeyword = '';

            _.each($scope.filters, function(filter){
                _.each(filter.values, function(value){
                    value.isActive = value.isAll;
                });
            });
            $scope.isActiveSearch = false;
        };

        $scope.countFilters = function(){
            _.each($scope.filters, function(filter){
                _.each(filter.values, function(value){
                    value.count = $scope.countFilter($scope.collection, filter, value);
                });
            });
        };

        $scope.countFilter = function(filter, value){
            console.log(filter);
            var filters = [],
                values = [],
                valueToCount = angular.copy(value),
                filterToCount = angular.copy(filter);

            valueToCount.isActive = true;

            values.push(valueToCount);
            _.extend(filterToCount, { values: values });
            filters.push(filterToCount);

            return $filter('KeywordSearchFilter')($scope.collection, filters, filter.category).length;
        };

        $scope.init = function(){
            console.log($scope.collection);
            $scope.collectionFields = $scope.getFields($scope.collection);
        };

        $scope.init();
    }]);

ngSearchFilter.filter('FilterByCategory', function(){
    return function(collection, filters, category){
        var filteredCollection = [],
            filter = _.findWhere(filters, { category: category}),
            general = _.findWhere(filter.values, { isAll: true });

        if(!_.isUndefined(general)){
            if(general.isActive){
                filteredCollection = collection;
            }
        }

        var others = _.findWhere(filter.values, { isAll: false });
        if(!_.isUndefined(others)) {
            _.each(_.where(filter.values, { isActive: true, isAll: false }), function (value) {
                _.each(collection, function (item) {
                    if(_.isArray(item[filter.object])){
                        if(filter.which == 'collection'){
                            _.each(item[filter.object], function(objectItem){

                                if(_.isEqual(objectItem[filter.field].trim(),value.fieldEval.trim()) && !_.contains(filteredCollection, item)){
                                    filteredCollection.push(item);
                                }
                            });<commit_msg>Fixed bug: Displaying 0 instead of actual total count for each filter<commit_after>/**
 * Created by jeanella on 3/26/2015.
 */

var ngSearchFilter = angular.module('ngSearchFilter', []);

ngSearchFilter.directive('searchFilter', function(){
    return {
        restrict: 'E',
        templateUrl: 'search-filter/filter.html',
        transclude: true,
        scope: {
            collection: '=',
            filters: '=',
            collectionType: '@'
        },
        controller: 'SearchFilterCtrl'
    };
});

ngSearchFilter.controller('SearchFilterCtrl', ['$scope','$filter',
    function($scope, $filter){
        $scope.searchKeyword = '';
        $scope.isActiveSearch = false;
        $scope.collectionFields = [];

        $scope.$watch('collection', function(newCollection){
            $scope.collectionFields = $scope.getFields(angular.copy(newCollection));
            $scope.countFilters($scope.collection);
        });

        $scope.getFields = function(collection){
            return _.keys(angular.copy(collection[0]));
        };

        $scope.getValues = function(item){
            return _.values(angular.copy(item));
        };

        $scope.toggleFilter = function(filter, value){
            var general = _.findWhere(filter.values, { isAll: true});

            if(value.isActive){
                if(!value.isAll){
                    if(_.where(filter.values, { isActive: true}).length <= 1){
                        general.isActive = true;
                    }
                    value.isActive = !value.isActive;
                }
            } else {
                if(value.isAll){
                    _.each(_.where(filter.values, { isAll: false}), function(filterValue){
                        filterValue.isActive = false;
                    });
                } else {
                    general.isActive = false;
                }
                value.isActive = !value.isActive;
            }

            $scope.isActiveSearch = (!general.isActive || value.isActive) && !value.isAll;
        };

        $scope.clearFilters = function(){
            $scope.searchKeyword = '';

            _.each($scope.filters, function(filter){
                _.each(filter.values, function(value){
                    value.isActive = value.isAll;
                });
            });
            $scope.isActiveSearch = false;
        };

        $scope.countFilters = function(collection){
            _.each($scope.filters, function(filter){
                _.each(filter.values, function(value){
                    value.count = $scope.countFilter(collection, filter, value);
                });
            });
        };

        $scope.countFilter = function(collection, filter, value){
            console.log(filter);
            var filters = [],
                values = [],
                valueToCount = angular.copy(value),
                filterToCount = angular.copy(filter);

            valueToCount.isActive = true;

            values.push(valueToCount);
            _.extend(filterToCount, { values: values });
            filters.push(filterToCount);

            return $filter('FilterByCategory')(collection, filters, filter.category).length;
        };

        $scope.init();
    }]);

ngSearchFilter.filter('FilterByCategory', function(){
    return function(collection, filters, category){
        var filteredCollection = [],
            filter = _.findWhere(filters, { category: category}),
            general = _.findWhere(filter.values, { isAll: true });

        if(!_.isUndefined(general)){
            if(general.isActive){
                filteredCollection = collection;
            }
        }

        var others = _.findWhere(filter.values, { isAll: false });
        if(!_.isUndefined(others)) {
            _.each(_.where(filter.values, { isActive: true, isAll: false }), function (value) {
                _.each(collection, function (item) {
                    if(_.isArray(item[filter.object])){
                        if(filter.which == 'collection'){
                            _.each(item[filter.object], function(objectItem){

                                if(_.isEqual(objectItem[filter.field].trim(),value.fieldEval.trim()) && !_.contains(filteredCollection, item)){
                                    filteredCollection.push(item);
                                }
                            });
{'fraction_non_alphanumeric': 0.13217938630999213, 'fraction_numerical': 0.002135551309430145, 'mean_word_length': 1.6474263612020232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803082', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2173, 'n_words': 355}