<commit_before>'use strict';

angular.module('ngDependencyGraph')
  .factory('currentView', function($rootScope, Const, util) {

    var service = {
      selectedNode: undefined,
      filters: {
        filterModules: Const.FilterModules.DEFAULT_FILTER,
        ignoreModules: Const.FilterModules.DEFAULT_IGNORE
      },
      componentsVisible: {
        service: true,
        controller: true
      },
      setGraphs: function(modulesGraph, componentsGraph) {
        this.modulesGraph = modulesGraph;
        this.componentsGraph = componentsGraph;
      },
      setScope: function(scope) {
        this.scope = scope;
        this.graph = (scope === Const.Scope.COMPONENTS ? this.componentsGraph : this.modulesGraph);

        if (scope === 'components') {
          this.setComponentsVisible(this.componentsVisible);
        }

        $rootScope.$broadcast(Const.Events.UPDATE_GRAPH);
      },
      chooseNode: function(node) {
        if (node.isModule === true && this.scope !== Const.Scope.MODULES) {
          this.setScope(Const.Scope.MODULES);
        } else if (node.isModule === false && this.scope !== Const.Scope.COMPONENTS) {
          this.setScope(Const.Scope.COMPONENTS);
        }
        
        this.selectedNode = node;
        $rootScope.$broadcast(Const.Events.CHOOSE_NODE, node);
      },
      applyFilters: function() {
        var masks;
        this.componentsGraph.resetFilter();
        this.modulesGraph.resetFilter();

        if (this.filters.componentsVisible) {
          this.componentsGraph.filterNodes(function(node) {
            var val = service.filters.componentsVisible[node.type];
            return val === true;
          });
        }

        // Apply ignore and filter masks to modules
        if (this.filters.ignoreModules) {
          masks = util.extractMasks(this.filters.ignoreModules);

          masks.forEach(function(mask) {
            service.modulesGraph.filterNodes(function(node) {
              return mask.test(node.name) === false;
            });
          });
        }

        if (this.filters.filterModules) {
          masks = util.extractMasks(this.filters.filterModules);
          
          masks.forEach(function(mask) {
            service.modulesGraph.filterNodes(function(node) {
              console.log(node.name, mask.test(node.name), mask);
              return mask.test(node.name);
            });
          });
        }

        // Now filter all components of excluded modules 
        this.componentsGraph.filterNodes(function(node) {
          return (service.modulesGraph.nodes.indexOf(node.module) !== -1);
        });


        $rootScope.$broadcast(Const.Events.UPDATE_GRAPH);
      },
      setIgnoreModules: function(ignoreModules, filterModules) {
        console.log(ignoreModules, filterModules);
        this.filters.ignoreModules = ignoreModules;
        this.filters.filterModules = filterModules;
        this.applyFilters();
      },
      setComponentsVisible: function(componentsVisible) {
        this.filters.componentsVisible = componentsVisible;
        this.applyFilters();
      },

      saveState: function() {
        throw 'not implemented';
      },
      loadState: function() {
        throw 'not implemented';
      }
    };

    return service;

  });
<commit_msg>clean up, comment<commit_after>'use strict';

/**
 * TODO this is doing too much: extract filters, serialisation
 *
 * Responsibilities:
 * - singleton holding currentView
 * - filtering of modules / components
 */
angular.module('ngDependencyGraph')
  .factory('currentView', function($rootScope, Const, util) {

    var service = {
      selectedNode: undefined,
      filters: {
        filterModules: Const.FilterModules.DEFAULT_FILTER,
        ignoreModules: Const.FilterModules.DEFAULT_IGNORE
      },
      componentsVisible: {
        service: true,
        controller: true
      },
      setGraphs: function(modulesGraph, componentsGraph) {
        this.modulesGraph = modulesGraph;
        this.componentsGraph = componentsGraph;
      },
      setScope: function(scope) {
        this.scope = scope;
        this.graph = (scope === Const.Scope.COMPONENTS ? this.componentsGraph : this.modulesGraph);

        if (scope === 'components') {
          this.setComponentsVisible(this.componentsVisible);
        }

        $rootScope.$broadcast(Const.Events.UPDATE_GRAPH);
      },
      chooseNode: function(node) {
        if (node.isModule === true && this.scope !== Const.Scope.MODULES) {
          this.setScope(Const.Scope.MODULES);
        } else if (node.isModule === false && this.scope !== Const.Scope.COMPONENTS) {
          this.setScope(Const.Scope.COMPONENTS);
        }
        
        this.selectedNode = node;
        $rootScope.$broadcast(Const.Events.CHOOSE_NODE, node);
      },
      applyFilters: function() {
        var masks;
        this.componentsGraph.resetFilter();
        this.modulesGraph.resetFilter();

        if (this.filters.componentsVisible) {
          this.componentsGraph.filterNodes(function(node) {
            var val = service.filters.componentsVisible[node.type];
            return val === true;
          });
        }

        // Apply ignore and filter masks to modules
        if (this.filters.ignoreModules) {
          masks = util.extractMasks(this.filters.ignoreModules);

          masks.forEach(function(mask) {
            service.modulesGraph.filterNodes(function(node) {
              return mask.test(node.name) === false;
            });
          });
        }

        if (this.filters.filterModules) {
          masks = util.extractMasks(this.filters.filterModules);
          
          masks.forEach(function(mask) {
            service.modulesGraph.filterNodes(function(node) {
              return mask.test(node.name);
            });
          });
        }

        // Now filter all components of excluded modules 
        this.componentsGraph.filterNodes(function(node) {
          return (service.modulesGraph.nodes.indexOf(node.module) !== -1);
        });


        $rootScope.$broadcast(Const.Events.UPDATE_GRAPH);
      },
      setIgnoreModules: function(ignoreModules, filterModules) {
        this.filters.ignoreModules = ignoreModules;
        this.filters.filterModules = filterModules;
        this.applyFilters();
      },
      setComponentsVisible: function(componentsVisible) {
        this.filters.componentsVisible = componentsVisible;
        this.applyFilters();
      },

      saveState: function() {
        throw 'not implemented';
      },
      loadState: function() {
        throw 'not implemented';
      }
    };

    return service;

  });

{'fraction_non_alphanumeric': 0.11620565243535778, 'fraction_numerical': 0.0003006614552014432, 'mean_word_length': 2.2249151720794957, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6250995', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1763, 'n_words': 298}