<commit_before>function CulturalCtrl($scope, searchModelService, Variable, CodeDescription, ContinuousVariable, DatasetSources, getCategories) {
   var linkModel = function() {
        // Model/state lives in searchModelService
        $scope.traits = [searchModelService.getModel().getCulturalTraits()];
    };
    
    
    $scope.$on('searchModelReset', linkModel); // When model is reset, update our model
    linkModel();
    //triggered by the view when a source is changed
    $scope.sourceChanged = function(trait) {
        trait.source_categories = [];
        trait.source_categories = getCategories.query({query: {source: trait.selectedSource.id}});
    };

    // triggered by the view when a category is changed
    $scope.categoryChanged = function(trait) {
        trait.indexVariables = Variable.query({index_categories: trait.selectedCategory.id, source: trait.selectedSource.id});
		trait.codes = [];
        trait.selectedCode = "";
    };

    // triggered by the view when a trait is changed in the picker
    $scope.traitChanged = function(trait) {
        trait.selectedCode = "";
        if (trait.selectedVariable.data_type == 'Continuous') {
            trait.codes = ContinuousVariable.query({query: {bf_id: trait.selectedVariable.id}});
        } else
            trait.codes = CodeDescription.query({variable: trait.selectedVariable.id });
        
        //make select all the default
        trait.codes.isSelected = true;
        trait.codes.$promise.then(function(result) {
            result.forEach(function(code) {
               code.isSelected = true;
                if (trait.selected.indexOf(code) == -1) {
                    trait.selected.push(code);
                }
            });
            trait.badgeValue = trait.selected.filter(function(code) { return code.isSelected; }).length;
        });
    };

    // used before searching to extract the codes from the search selection
    // Expects an array of CulturalTraitModel objects
	// this only gets the selected codes for the currently selected cultural trait
    $scope.getSelectedTraitCodes = function() {
		traits = $scope.traits;
		var allCodes = Array.prototype.concat.apply([], traits.map( function(trait) { 
			return trait.codes; 
		}));
		selectedCodes = allCodes.filter( function(c) { return c.isSelected; }).map( function(c) { return c; });
	   return selectedCodes;
    };

    $scope.traitCodeSelectionChanged = function(trait) {
		currentSelection = $scope.getSelectedTraitCodes();
        if (currentSelection.length == trait.codes.length) trait.codes.isSelected = true;
        else trait.codes.isSelected = false;
		currentSelection.forEach(function(code) {
			if (trait.selected.indexOf(code) == -1) {
			//if selected trait code is not already in the array of selected codes, add it to the array
				trait.selected.push(code);
			}
		});
		trait.badgeValue = trait.selected.filter(function(code) { return code.isSelected; }).length;
	};
	
	$scope.selectAllChanged = function(trait) {
		if (trait.codes.isSelected) {
			trait.codes.forEach(function(code){ code.isSelected = true;
			if (trait.selected.indexOf(code) == -1) {
				trait.selected.push(code);
			}
			});
		} else { trait.codes.forEach(function(code){ code.isSelected = false; });}

		trait.badgeValue = trait.selected.filter(function(code) { return code.isSelected; }).length;

	};

    // wired to the search button. Gets the code ids, adds cultural to the query, and invokes the search
    $scope.doSearch = function() {
        $scope.search();
    };
}<commit_msg>Cultural search saves user's search state instead of resetting each time variable is selected<commit_after>function CulturalCtrl($scope, searchModelService, Variable, CodeDescription, ContinuousVariable, DatasetSources, getCategories) {
   var linkModel = function() {
        // Model/state lives in searchModelService
        $scope.traits = [searchModelService.getModel().getCulturalTraits()];
        $scope.alreadySelected = []; //keeps track of traits the user has already selected
    };
    
    
    $scope.$on('searchModelReset', linkModel); // When model is reset, update our model
    linkModel();
    //triggered by the view when a source is changed
    $scope.sourceChanged = function(trait) {
        trait.source_categories = [];
        trait.source_categories = getCategories.query({query: {source: trait.selectedSource.id}});
    };

    // triggered by the view when a category is changed
    $scope.categoryChanged = function(trait) {
        trait.indexVariables = Variable.query({index_categories: trait.selectedCategory.id, source: trait.selectedSource.id});
		trait.codes = [];
        trait.selectedCode = "";
    };

    // triggered by the view when a trait is changed in the picker
    $scope.traitChanged = function(trait) {
        trait.selectedCode = "";
        if (trait.selectedVariable.data_type == 'Continuous') {
            trait.codes = ContinuousVariable.query({query: {bf_id: trait.selectedVariable.id}});
        } else
            trait.codes = CodeDescription.query({variable: trait.selectedVariable.id });
        trait.selected = trait.selected.filter(function(code) { return code.isSelected; });

        //make select all the default
        if ($scope.alreadySelected.indexOf(trait.selectedVariable.id) == -1) {
            trait.codes.isSelected = true;
            trait.codes.$promise.then(function(result) {
                result.forEach(function(code) {
                   code.isSelected = true;
                   if (trait.selected.map(function(code) { return code.id; }).indexOf(code.id) == -1) {
                        trait.selected.push(code);
                   }
                });
                trait.badgeValue = trait.selected.filter(function(code) { return code.isSelected; }).length;
            });
            $scope.alreadySelected.push(trait.selectedVariable.id);
        } else {
            trait.codes.$promise.then(function(result) {
                result.forEach(function(code) {
                    if (trait.selected.map(function(code) { return code.id; }).indexOf(code.id) != -1) {
                        code.isSelected = true;
                   }
                });
                trait.badgeValue = trait.selected.length;
            });
        }
    };

    // used before searching to extract the codes from the search selection
    // Expects an array of CulturalTraitModel objects
	// this only gets the selected codes for the currently selected cultural trait
    $scope.getSelectedTraitCodes = function() {
		traits = $scope.traits;
		var allCodes = Array.prototype.concat.apply([], traits.map( function(trait) { 
			return trait.codes; 
		}));
		return allCodes.filter( function(c) { return c.isSelected; });
    };
    
    function removeCode(trait, code) {
        var index = -1;
        for (var i = 0; i < trait.selected.length; i++) {
            if (trait.selected[i].id == code.id) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            trait.selected.splice(index, 1);
        }
    };

    $scope.traitCodeSelectionChanged = function(trait) {
		currentSelection = $scope.getSelectedTraitCodes();
        if (currentSelection.length == trait.codes.length) trait.codes.isSelected = true;
        else trait.codes.isSelected = false;
        trait.selected = trait.selected.filter(function(code) { return code.isSelected; });
        trait.codes.forEach(function(code) {
            if (code.isSelected) {
                if (trait.selected.map(function(code) { return code.id; }).indexOf(code.id) == -1) {
                    trait.selected.push(code);
                }
            } else {
                removeCode(trait, code);
            }
        });
		trait.badgeValue = trait.selected.length;
	};
	
	$scope.selectAllChanged = function(trait) {
        trait.selected = trait.selected.filter(function(code) { return code.isSelected; });
		if (trait.codes.isSelected) {
			trait.codes.forEach(function(code){ 
                code.isSelected = true;
                if (trait.selected.map(function(code) { return code.id; }).indexOf(code.id) == -1) trait.selected.push(code);
			});
		} else { 
            trait.codes.forEach(function(code){ 
                code.isSelected = false; 
                removeCode(trait, code);
            });
        }
		trait.badgeValue = trait.selected.length;
	};

    // wired to the search button. Gets the code ids, adds cultural to the query, and invokes the search
    $scope.doSearch = function() {
        $scope.search();
    };
}
{'fraction_non_alphanumeric': 0.12241579558652729, 'fraction_numerical': 0.0013937282229965157, 'mean_word_length': 2.4876468205751316, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7053644', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2378, 'n_words': 540}