<commit_before>angular.module('app.search', [])
  .factory 'SearchResponse', ->
    class SearchResponse
      constructor: (scope, response) ->
        scope.hits = @hits(response)
        console.log _.first scope.hits
        scope.info = @info(response)
        scope.page = @page(response)
        console.log scope.page

      info: (data) ->
        start_index = data.page * data.hitsPerPage
        stop_index = Math.min(start_index + data.hitsPerPage, data.nbHits)
        resp =
          start: Math.min(start_index + 1, data.nbHits)
          stop: stop_index
          time: data.processingTimeMS
          total: data.nbHits

      hits: (data) ->
        data.hits

      page: (data) ->
        current = data.page
        resp =
          current: current
          prev: current - 1
          hasPrev: current != 0
          hasNext: current < data.nbPages - 1
          next: current + 1

  .service 'Library', (AlgoliaIndex, $location, SearchResponse) ->
    index = new AlgoliaIndex('jsdelivr')

    class Library
      get: (id, params) ->
        index.getObject(id, params)
      search: (scope) ->
        state = $location.search()
        params =
          hitsPerPage: 25
          facets: ['*']
          maxValuesPerFacet: 10
          facetFilters: []
          attributesToRetrieve: ['description', 'name', 'lastversion']
          q: state.q

        params.page = state.page if state.page
        index.search(params).then (response) ->
          new SearchResponse(scope, response)
    new Library()

  .controller 'DetailCtrl', ($scope, $stateParams, Library) ->
    $scope.copy_as = 'url'
    Library.get($stateParams.name).then (res) ->
      console.log  res
      res.selected_version = res.lastversion
      res.assets = _.indexBy(res.assets, 'version')
      $scope.lib = res

    $scope.zip_url = ->
      lib = @lib || {}
      "//cdn.jsdelivr.net/#{lib.name}/#{lib.selected_version}/#{lib.zip}" if lib.zip

    $scope.file_url = ->
      "//cdn.jsdelivr.net/#{@lib.name}/#{@lib.selected_version}/#{@file}"


  .controller 'SearchCtrl', ($scope, $location, Library) ->
    $scope.search = $location.search().q
    Library.search($scope)

    $scope.reset = ->
      $location.search({})
      Library.search($scope)

    $scope.select = (param_key, value) ->
      $location.search('page', null)
      value = null if value == ""
      $location.search(param_key, value)
      Library.search($scope)

    $scope.select_page = (page) ->
      return if page < 0
      page = null if page == 0
      $location.search('page', page)
      Library.search($scope)<commit_msg>Scroll to top after pressing next_page<commit_after>angular.module('app.search', [])
  .factory 'SearchResponse', ->
    class SearchResponse
      constructor: (scope, response) ->
        scope.hits = @hits(response)
        console.log _.first scope.hits
        scope.info = @info(response)
        scope.page = @page(response)
        console.log scope.page

      info: (data) ->
        start_index = data.page * data.hitsPerPage
        stop_index = Math.min(start_index + data.hitsPerPage, data.nbHits)
        resp =
          start: Math.min(start_index + 1, data.nbHits)
          stop: stop_index
          time: data.processingTimeMS
          total: data.nbHits

      hits: (data) ->
        data.hits

      page: (data) ->
        current = data.page
        resp =
          current: current
          prev: current - 1
          hasPrev: current != 0
          hasNext: current < data.nbPages - 1
          next: current + 1

  .service 'Library', (AlgoliaIndex, $location, SearchResponse) ->
    index = new AlgoliaIndex('jsdelivr')

    class Library
      get: (id, params) ->
        index.getObject(id, params)
      search: (scope) ->
        state = $location.search()
        params =
          hitsPerPage: 25
          facets: ['*']
          maxValuesPerFacet: 10
          facetFilters: []
          attributesToRetrieve: ['description', 'name', 'lastversion']
          q: state.q

        params.page = state.page if state.page
        index.search(params).then (response) ->
          new SearchResponse(scope, response)
    new Library()

  .controller 'DetailCtrl', ($scope, $stateParams, Library) ->
    $scope.copy_as = 'url'
    Library.get($stateParams.name).then (res) ->
      console.log  res
      res.selected_version = res.lastversion
      res.assets = _.indexBy(res.assets, 'version')
      $scope.lib = res

    $scope.zip_url = ->
      lib = @lib || {}
      "//cdn.jsdelivr.net/#{lib.name}/#{lib.selected_version}/#{lib.zip}" if lib.zip

    $scope.file_url = ->
      "//cdn.jsdelivr.net/#{@lib.name}/#{@lib.selected_version}/#{@file}"


  .controller 'SearchCtrl', ($scope, $location, Library, $window) ->
    $scope.search = $location.search().q
    Library.search($scope)

    $scope.reset = ->
      $location.search({})
      Library.search($scope)

    $scope.select = (param_key, value) ->
      $location.search('page', null)
      value = null if value == ""
      $location.search(param_key, value)
      Library.search($scope)

    $scope.select_page = (page) ->
      return if page < 0
      page = null if page == 0
      $location.search('page', page)
      Library.search($scope).then ->
        $window.scrollTo(0, 0)
{'fraction_non_alphanumeric': 0.14218366962096926, 'fraction_numerical': 0.004525740147086555, 'mean_word_length': 2.1665671641791047, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, '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': '5738791', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1627, 'n_words': 343}