<commit_before>'use strict';


// ------------------------------------------------------
//                  MainCrtl
// ------------------------------------------------------

angular.module('anyfetchFrontApp')
.controller('MainCtrl', function ($scope, $rootScope, $location, $http, $q, AuthService, DocumentTypesService, ProvidersService) {
  $scope.logout = function() {
    AuthService.logout(function() {
      $location.path('/login');
    });
  };

  $scope.getRes = function (query, start, limit) {
    var deferred = $q.defer();

    var apiQuery = 'http://api.anyfetch.com/documents?search='+query+'&start='+start+'&limit='+limit;

    $http({method: 'GET', url: apiQuery})
      .success(function(data) {
        DocumentTypesService.updateSearchCounts(data.document_types);
        ProvidersService.updateSearchCounts(data.tokens);
        $scope.loading = false;

        if (data.datas.length === limit) {
          $scope.lastRes = start+limit;
          $scope.moreResult = true;
        } else {
          $scope.lastRes = start+data.datas.length;
          $scope.moreResult = false;
        }
        deferred.resolve(data);
      })
      .error(function() {
        $scope.display_error('Error while searching '+ query +'. Please reload.');
        deferred.reject();
      });

    return deferred.promise;
  };

  $scope.searchLaunch = function(query) {
    console.log('Search Launched!');
    $location.search({q: query});
    $scope.searchUpdate();
  };

  $scope.searchUpdate = function() {
    console.log('Search updating!');
    $scope.query = $location.search().q || '';
    $scope.search();
  };

  $scope.search = function() {
    $scope.loading = true;
    $scope.results = [];

    if ($scope.query.length) {
      $scope.getRes($scope.query, 0, 5)
        .then(function(data) {
          $scope.results = data.datas;
          $scope.loading = false;
        });
    } else {
      $scope.query = '';
      $location.search({});
      $scope.loading = false;
      DocumentTypesService.updateSearchCounts([]);
      ProvidersService.updateSearchCounts([]);
      $scope.moreResult = false;
    }
  };

  $scope.focusSearch = function() {
    $('#search').focus();
  };

  $scope.loadMore = function() {
    $scope.loading = true;

    $scope.getRes($scope.query, $scope.lastRes, 5)
      .then(function(data) {
        $scope.results = $scope.results.concat(data.datas);
      });
  };

  $scope.displayFull = function(id) {
    var actualSearch = $location.search();
    actualSearch.id = id;
    $location.search(actualSearch);
  };

  $scope.loadFull = function() {
    if ($scope.id) {
      var apiQuery = 'http://api.anyfetch.com/documents/'+ $scope.id;
      if ($scope.query) {
        apiQuery += '?search=' + $scope.query;
      }

      $scope.modalShow = true;
      //LOCK SCROLL MAIN!!!
      $scope.full = null;

      $http({method: 'GET', url: apiQuery})
        .success(function(data) {
          if($location.search().id) {
            $scope.full = data;
            $scope.modalShow = true;
            $scope.modalLoading = false;
          }
        })
        .error(function() {
          $scope.display_error('Error while loading full preview of the document '+$scope.id);
          $scope.searchLaunch($scope.query);
        });
    }
    else {
      console.log('Nothing to display in full.');
    }
  };

  $scope.close_similar = function(){
    if ($location.search().similar_to) {<commit_msg>Bugfix to add search when querying document full directly from url<commit_after>'use strict';


// ------------------------------------------------------
//                  MainCrtl
// ------------------------------------------------------

angular.module('anyfetchFrontApp')
.controller('MainCtrl', function ($scope, $rootScope, $location, $http, $q, AuthService, DocumentTypesService, ProvidersService) {
  $scope.logout = function() {
    AuthService.logout(function() {
      $location.path('/login');
    });
  };

  $scope.getRes = function (query, start, limit) {
    var deferred = $q.defer();

    var apiQuery = 'http://api.anyfetch.com/documents?search='+query+'&start='+start+'&limit='+limit;

    $http({method: 'GET', url: apiQuery})
      .success(function(data) {
        DocumentTypesService.updateSearchCounts(data.document_types);
        ProvidersService.updateSearchCounts(data.tokens);
        $scope.loading = false;

        if (data.datas.length === limit) {
          $scope.lastRes = start+limit;
          $scope.moreResult = true;
        } else {
          $scope.lastRes = start+data.datas.length;
          $scope.moreResult = false;
        }
        deferred.resolve(data);
      })
      .error(function() {
        $scope.display_error('Error while searching '+ query +'. Please reload.');
        deferred.reject();
      });

    return deferred.promise;
  };

  $scope.searchLaunch = function(query) {
    console.log('Search Launched!');
    $location.search({q: query});
    $scope.searchUpdate();
  };

  $scope.searchUpdate = function() {
    console.log('Search updating!');
    $scope.query = $location.search().q || '';
    $scope.search();
  };

  $scope.search = function() {
    $scope.loading = true;
    $scope.results = [];

    if ($scope.query.length) {
      $scope.getRes($scope.query, 0, 5)
        .then(function(data) {
          $scope.results = data.datas;
          $scope.loading = false;
        });
    } else {
      $scope.query = '';
      $location.search({});
      $scope.loading = false;
      DocumentTypesService.updateSearchCounts([]);
      ProvidersService.updateSearchCounts([]);
      $scope.moreResult = false;
    }
  };

  $scope.focusSearch = function() {
    $('#search').focus();
  };

  $scope.loadMore = function() {
    $scope.loading = true;

    $scope.getRes($scope.query, $scope.lastRes, 5)
      .then(function(data) {
        $scope.results = $scope.results.concat(data.datas);
      });
  };

  $scope.displayFull = function(id) {
    var actualSearch = $location.search();
    actualSearch.id = id;
    $location.search(actualSearch);
  };

  $scope.loadFull = function() {
    if ($scope.id) {
      var apiQuery = 'http://api.anyfetch.com/documents/'+ $scope.id;
      if ($scope.query) {
        apiQuery += '?search=' + $scope.query;
      } else if ($location.search().q) {
        apiQuery += '?search=' + $location.search().q;
      } else {
        $scope.display_error('No query passed in the URL. Please resart your search.');
        $location.search({});
      }

      $scope.modalShow = true;
      //LOCK SCROLL MAIN!!!
      $scope.full = null;

      $http({method: 'GET', url: apiQuery})
        .success(function(data) {
          if($location.search().id) {
            $scope.full = data;
            $scope.modalShow = true;
            $scope.modalLoading = false;
          }
        })
        .error(function() {
          $scope.display_error('Error while loading full preview of the document '+$scope.id);
          $scope.searchLaunch($scope.query);
        });
    }
    else {
      console.log('Nothing to display in full.');
    }
  };

  $scope.close_similar = function(){
    if ($location.search().similar_to) {
{'fraction_non_alphanumeric': 0.19206217041354426, 'fraction_numerical': 0.0008326394671107411, 'mean_word_length': 2.2073876279483757, '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': '101528', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2130, 'n_words': 374}