<commit_before>var ash = angular.module('ash', []).directive("ngInfiniteScroll", function ($timeout, Data, Resource) {

  return {
    restrict: 'A',
    scope: {
      options: '=?',
      items: '=?'
    },
    link: function ($scope, element) {
      $scope.options = angular.extend({
        limit: 10,
        threshold: 50,
        data: [],
        onDocument: false
      }, $scope.options ? $scope.options : {});

      $scope.lastRemain = undefined;
      $scope.offset = 0;
      $scope.inProcess = false;
      $scope.hasItems = true;

      if (!$scope.options.resource && !Array.isArray($scope.options.data)) {
        $scope.options.data = [$scope.options.data];
      }
      $scope.strategy = $scope.options.resource ? Resource : Data;
      $scope.strategy.addItems($scope);

      var checkRemainSpace = function(remain) {
        if (remain < $scope.options.threshold && (!$scope.lastRemain || (remain - $scope.lastRemain) < 0) && $scope.hasItems && !$scope.inProcess) {
          $scope.$apply(function() {
            $scope.strategy.addItems($scope);
          });
        }

        $scope.lastRemain = remain;
      };

      if ($scope.options.onDocument) {
        $(document).bind('scroll', function () {

          var remain = document.documentElement.scrollHeight - (document.documentElement.clientHeight + document.documentElement.scrollTop);
          checkRemainSpace(remain);
        });
      } else {
        element.bind('scroll', function () {
          var remain = element[0].scrollHeight - (element[0].clientHeight + element[0].scrollTop);
          checkRemainSpace(remain);
        });
      }
    }
  }

});

ash.factory('Data', function() {
  return {
    addItems: function($scope) {
      $scope.inProcess = true;

      var from = $scope.offset * $scope.options.limit;
      if (from < $scope.options.data.length) {
        var to = from + $scope.options.limit;
        to = to > $scope.options.data.length ? $scope.options.data.length : to;

        for (var i = from; i < to; i++) {
          $scope.items = $scope.items.concat($scope.options.data[i]);
        }

        $scope.offset++;
      } else {
        $scope.hasItems = false;
      }

      $scope.inProcess = false;
    }
  };
});

ash.factory('Resource', function() {
  return {
    addItems: function($scope) {
      $scope.inProcess = true;
      $scope.options.resource.query(
        { offset: $scope.offset * $scope.options.limit, limit: $scope.options.limit },
        function (data) {
          if (data.length == 0) {
            $scope.hasItems = false;
          } else {
            for (var i = 0; i < data.length; i++) {
              $scope.items = $scope.items.concat(data[i]);
            }
          }

          $scope.inProcess = false;<commit_msg>added filter for resource<commit_after>var ash = angular.module('ash', []).directive("ngInfiniteScroll", function ($timeout, Data, Resource) {

  return {
    restrict: 'A',
    scope: {
      options: '=?',
      items: '=?'
    },
    link: function ($scope, element) {
      $scope.options = angular.extend({
        limit: 10,
        threshold: 50,
        data: [],
        onDocument: false
      }, $scope.options ? $scope.options : {});

      $scope.lastRemain = undefined;
      $scope.offset = 0;
      $scope.inProcess = false;
      $scope.hasItems = true;

      if (!$scope.options.data.resource && !Array.isArray($scope.options.data)) {
        $scope.options.data = [$scope.options.data];
      }
      if ($scope.options.data.resource) {
        $scope.strategy = Resource;
        $scope.$watch('options.data.filter', function() {
          console.log('filter');
          $scope.items = [];
          $scope.lastRemain = undefined;
          $scope.offset = 0;
          $scope.inProcess = false;
          $scope.hasItems = true;
          $scope.strategy.addItems($scope);
        }, true);
      } else {
        $scope.strategy = Data;
      }
      $scope.strategy.addItems($scope);

      var checkRemainSpace = function(remain) {
        if (remain < $scope.options.threshold && (!$scope.lastRemain || (remain - $scope.lastRemain) < 0) && $scope.hasItems && !$scope.inProcess) {
          $scope.$apply(function() {
            $scope.strategy.addItems($scope);
          });
        }

        $scope.lastRemain = remain;
      };

      if ($scope.options.onDocument) {
        $(document).bind('scroll', function () {
          var remain = document.documentElement.scrollHeight - (document.documentElement.clientHeight + document.documentElement.scrollTop);
          checkRemainSpace(remain);
        });
      } else {
        element.bind('scroll', function () {
          var remain = element[0].scrollHeight - (element[0].clientHeight + element[0].scrollTop);
          checkRemainSpace(remain);
        });
      }
    }
  }

});

ash.factory('Data', function() {
  return {
    addItems: function($scope) {
      $scope.inProcess = true;

      var from = $scope.offset * $scope.options.limit;
      if (from < $scope.options.data.length) {
        var to = from + $scope.options.limit;
        to = to > $scope.options.data.length ? $scope.options.data.length : to;

        for (var i = from; i < to; i++) {
          $scope.items = $scope.items.concat($scope.options.data[i]);
        }

        $scope.offset++;
      } else {
        $scope.hasItems = false;
      }

      $scope.inProcess = false;
    }
  };
});

ash.factory('Resource', function() {
  return {
    addItems: function($scope) {
      $scope.inProcess = true;
      var params = angular.extend(
        { offset: $scope.offset * $scope.options.limit, limit: $scope.options.limit },
        $scope.options.data.filter
      );
      
      $scope.options.data.resource.query(
        params,
        function (data) {
          if (data.length == 0) {
            $scope.hasItems = false;
          } else {
            for (var i = 0; i < data.length; i++) {
              $scope.items = $scope.items.concat(data[i]);
            }
            $scope.offset++;
          }

          $scope.inProcess = false;
{'fraction_non_alphanumeric': 0.15842070773263434, 'fraction_numerical': 0.0037680209698558323, 'mean_word_length': 2.0616850551654964, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '3355147', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1829, 'n_words': 329}