<commit_before>###
# Created by <NAME> on 26/10/15.
###

$ = require "./util/jquery"
qs = require "qs"

###
Controller
  
This class uses the client to
to retrieve the data and the widgets
to paint them.
###  
class Controller
  ###
  Controller constructor
  
  @param {doofinder.Client} client
  @param {doofinder.widget | Array} widgets
  @param {Object} searchParams
  @api public
  ###
  constructor: (client, widgets, @searchParams = {}) ->
    @client = client
    @hashid = client.hashid # Publish hashid
    @widgets = []
    if widgets instanceof Array
      for widget in widgets
        @addWidget(widget)

    else if widgets
      @addWidget(widgets)

    @status = $.extend true,
      {},
      params: @searchParams
    
    @reset()

  ###
  __search
  this method invokes Client's search method for
  retrieving the data and use widget's replace or 
  append to show them.
  
  @param {String} event: the event name
  @param {Array} params: the params will be passed
    to the listeners
  @api private
  ###
  __search: (next=false) ->

    # To avoid past queries
    # we'll check the query_counter
    @status.params.query_counter++
    
    params = $.extend true,
      {},
      @status.params || {}
    
    params.page = @status.currentPage
    _this = this
    
    @client.search params.query, params, (err, res) ->
      # I check if I reached the last page.    
      if res.results.length < _this.status.params.rpp
        _this.status.lastPageReached = true
      # I set the query_name till next query
      if not _this.searchParams.query_name
        _this.status.params.query_name = res.query_name
      # Triggers results_received
      _this.trigger "df:results_received", [res]
      # Whe show the results only when query counter
      # belongs to a the present request
      if res.query_counter == _this.status.params.query_counter
        for widget in _this.widgets
          if next
            widget.renderNext res
          else
            widget.render res
      
  ### 
  __search wrappers
  ###

  ###
  search
  
  Takes a new query, initializes status and performs
  a search

  @param {String} query: the query term
  @api public
  ###
  search: (query, params={}) ->
    if query
      searchParams = $.extend true,
        {},
        @searchParams
      # Saves current query counter
      queryCounter = @status.params.query_counter
      # Reset @status.params
      @status.params = $.extend true,
        {},
        params
      @status.params = $.extend true, searchParams, params
      @status.params.query = query
      @status.params.filters = $.extend true,
        {},
        @searchParams.filters || {}
      @status.params.query_counter = queryCounter

      if not @searchParams.query_name
        delete @status.params.query_name
      
      @status.currentPage = 1
      @status.firstQueryTriggered = true
      @status.lastPageReached = false
      @__search()
    
    @trigger "df:search", [@status.params]

  ###
  nextPage
  
  Increments the currentPage and performs a search. Takes
  the next page results and shows them.

  @api public
  ###
  nextPage: (replace = false) ->
    if @status.firstQueryTriggered and @status.currentPage > 0 and not @status.lastPageReached   
      @trigger "df:next_page"   
      @status.currentPage++
      @__search(true)

  ###
  getPage
  
  Set the currentPage with a given value and performs a search.<commit_msg> according to doc, the controller.search method should be able to send every search param in the search API. that includes filter<commit_after>###
# Created by <NAME> on 26/10/15.
###

$ = require "./util/jquery"
qs = require "qs"

###
Controller
  
This class uses the client to
to retrieve the data and the widgets
to paint them.
###  
class Controller
  ###
  Controller constructor
  
  @param {doofinder.Client} client
  @param {doofinder.widget | Array} widgets
  @param {Object} searchParams
  @api public
  ###
  constructor: (client, widgets, @searchParams = {}) ->
    @client = client
    @hashid = client.hashid # Publish hashid
    @widgets = []
    if widgets instanceof Array
      for widget in widgets
        @addWidget(widget)

    else if widgets
      @addWidget(widgets)

    @status = $.extend true,
      {},
      params: @searchParams
    
    @reset()

  ###
  __search
  this method invokes Client's search method for
  retrieving the data and use widget's replace or 
  append to show them.
  
  @param {String} event: the event name
  @param {Array} params: the params will be passed
    to the listeners
  @api private
  ###
  __search: (next=false) ->

    # To avoid past queries
    # we'll check the query_counter
    @status.params.query_counter++
    
    params = $.extend true,
      {},
      @status.params || {}
    
    params.page = @status.currentPage
    _this = this
    
    @client.search params.query, params, (err, res) ->
      # I check if I reached the last page.    
      if res.results.length < _this.status.params.rpp
        _this.status.lastPageReached = true
      # I set the query_name till next query
      if not _this.searchParams.query_name
        _this.status.params.query_name = res.query_name
      # Triggers results_received
      _this.trigger "df:results_received", [res]
      # Whe show the results only when query counter
      # belongs to a the present request
      if res.query_counter == _this.status.params.query_counter
        for widget in _this.widgets
          if next
            widget.renderNext res
          else
            widget.render res
      
  ### 
  __search wrappers
  ###

  ###
  search
  
  Takes a new query, initializes status and performs
  a search

  @param {String} query: the query term
  @api public
  ###
  search: (query, params={}) ->
    if query
      searchParams = $.extend true,
        {},
        @searchParams
      # Saves current query counter
      queryCounter = @status.params.query_counter
      # Reset @status.params
      @status.params = $.extend true,
        {},
        params
      @status.params = $.extend true, searchParams, params
      @status.params.query = query
      @status.params.filters = $.extend true,
        {},
        @searchParams.filters || {},
        params.filters
      @status.params.query_counter = queryCounter

      if not @searchParams.query_name
        delete @status.params.query_name
      
      @status.currentPage = 1
      @status.firstQueryTriggered = true
      @status.lastPageReached = false
      @__search()
    
    @trigger "df:search", [@status.params]

  ###
  nextPage
  
  Increments the currentPage and performs a search. Takes
  the next page results and shows them.

  @api public
  ###
  nextPage: (replace = false) ->
    if @status.firstQueryTriggered and @status.currentPage > 0 and not @status.lastPageReached   
      @trigger "df:next_page"   
      @status.currentPage++
      @__search(true)

  ###
  getPage
  
  Set the currentPage with a given value and performs a search.
{'fraction_non_alphanumeric': 0.09620866590649943, 'fraction_numerical': 0.002280501710376283, 'mean_word_length': 2.4096209912536444, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '731318', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2036, 'n_words': 641}