<commit_before># AutoComplete
#
# TODO: - there's a bug with accented characters, they aren't being highlighted
#         (http://frightanic.com/projects/jquery-highlight/ as an example)
#       - abstract the XHR code to a separate library
#
# Arguments:
#   _args (An object containing)
#     id                      : [string] The target form element
#     uri                     : [string] The search endpoint
#     scope                   : [string] (Optional) Value to specify as the scope of the search
#     threshold               : [number] (Optional) The number of characters required before searching
#     throttle                : [number] (Optional) Time in ms to throttle requests to search endpoint
#     resultsClass            : [string] (Optional) Class name for results list element
#     resultItemClass         : [string] (Optional) Class name for result list item element
#     resultLinkClass         : [string] (Optional) Class name for result list link element
#     resultItemHoveredClass  : [string] (Optional) Class name for hovered
#     map                     : [object] (Optional) Endpoint mappings
#     inputCallback           : [function] (Optional) when something has been entered in the input element
#     selectCallback          : [function] (Optional) when an item has been selected
#     showResultsCallback     : [function] (Optional) when the results list has been displayed
#     removeResultsCallback   : [function] (Optional) when the results list has been removed
#
# Map:
#   Maps an endpoint key to the component's expected key
#     title : [string]
#     type  : [string]
#     uri   : [string]
#     data  : [array of strings] adds data attribute on the list item for each value
#
# Example:
#  args =
#    id: 'my_search'
#    uri: '/search'
#
#  new AutoComplete(args)
#
# Dependencies:
#   None

define [], ->

  class AutoComplete

    DEFAULTS =
      threshold: 3
      resultsClass: 'autocomplete__results'
      resultItemClass: 'autocomplete__result'
      resultLinkClass: 'autocomplete__result__link'
      resultItemHoveredClass: 'autocomplete__current'
      throttle: 200
      map:
        title: 'title',
        type: 'type',
        uri: 'uri'

    KEY =
      tab: 9,
      enter: 13,
      esc: 27,
      up: 38,
      down: 40

    constructor: (args) ->
      @_init args if args.id and args.uri

    _init: (args) ->
      @config = @_updateConfig args
      @el = document.getElementById @config.id
      @xhr = @_setupXHR()
      @_addEventHandlers()
      @results = @_buildResults()

    _updateConfig: (args) ->
      newConfig = {}
      newConfig[key] = value for own key, value of DEFAULTS
      newConfig[key] = value for own key, value of args
      newConfig

    _addEventHandlers: ->
      @_on(@el, 'input', (e) =>
        @_searchFor e.currentTarget.value
      )
      @_on(@el, 'keydown', (e) =>
        @_keypressHandler e
      )

    _setupXHR: ->
      xhr = new XMLHttpRequest()
      @_on(xhr, 'readystatechange', =>
        if @xhr.readyState == 4
          if @xhr.status == 200
            @_populateResults JSON.parse(@xhr.responseText), @currentSearch
      )
      xhr

    _buildResults: ->
      results = document.createElement 'UL'
      @_addClass results, @config.resultsClass
      @_addClass results, "#{@config.resultsClass}--#{@config.classModifier}" if @config.classModifier

      @_on(results, 'click', (e) =>
        @_resultsClick e
      , false)

      @_on(results, 'mouseover', (e) =>
        @_resultsMouseOver e.target
      , false)

      @_on(results, 'mouseout', (e) =>
        @_resultsMouseOut e.target
      , false)
      results

    # event handlers
    _keypressHandler: (e) ->
      switch e.keyCode
        when KEY.up
          if @results.displayed
            e.preventDefault()
            @_highlightUp()
        when KEY.down
          if @results.displayed
            e.preventDefault()
            @_highlightDown()
        when KEY.tab
          if @results.displayed
            e.preventDefault()
            if e.shiftKey
              @_highlightUp()
            else
              @_highlightDown()
        when KEY.enter
          if @results.highlighted
            e.preventDefault()
            @_handleEnter()
        when KEY.esc
          @_handleCancel()

    _resultsClick: (e) ->
      target = e.target

      # if the target was a b, check its parent
      target = target.parentNode if target.tagName == 'B'

      # if the real target was an anchor, prevent the event from bubbling so the text isn't selected
      if target.tagName == 'A'
        e.stopPropagation()
      else if target.tagName == 'LI'
        @results.highlighted = target
        @_selectCurrent()

    _resultsMouseOver: (target) ->
      until target.tagName is 'LI'
        target = target.parentNode

      @_clearHighlight()
      @results.highlighted = target<commit_msg>Improved comment<commit_after># AutoComplete
#
# TODO: - there's a bug with accented characters, they aren't being highlighted
#         (http://frightanic.com/projects/jquery-highlight/ as an example)
#       - abstract the XHR code to a separate library
#
# Arguments:
#   _args (An object containing)
#     id                      : [string] The target form element
#     uri                     : [string] The search endpoint
#     scope                   : [string] (Optional) Value to specify as the scope of the search
#     threshold               : [number] (Optional) The number of characters required before searching
#     throttle                : [number] (Optional) Time in ms to throttle requests to search endpoint
#     resultsClass            : [string] (Optional) Class name for results list element
#     resultItemClass         : [string] (Optional) Class name for result list item element
#     resultLinkClass         : [string] (Optional) Class name for result list link element
#     resultItemHoveredClass  : [string] (Optional) Class name for hovered
#     map                     : [object] (Optional) Endpoint mappings
#     inputCallback           : [function] (Optional) when something has been entered in the input element
#     selectCallback          : [function] (Optional) when an item has been selected
#     showResultsCallback     : [function] (Optional) when the results list has been displayed
#     removeResultsCallback   : [function] (Optional) when the results list has been removed
#
# Map:
#   Maps an endpoint key to the component's expected key
#     title : [string]
#     type  : [string]
#     uri   : [string]
#     data  : [array of strings] adds data attribute on the list item for each value
#
# Example:
#  args =
#    id: 'my_search'
#    uri: '/search'
#
#  new AutoComplete(args)
#
# Dependencies:
#   None

define [], ->

  class AutoComplete

    DEFAULTS =
      threshold: 3
      resultsClass: 'autocomplete__results'
      resultItemClass: 'autocomplete__result'
      resultLinkClass: 'autocomplete__result__link'
      resultItemHoveredClass: 'autocomplete__current'
      throttle: 200
      map:
        title: 'title',
        type: 'type',
        uri: 'uri'

    KEY =
      tab: 9,
      enter: 13,
      esc: 27,
      up: 38,
      down: 40

    constructor: (args) ->
      @_init args if args.id and args.uri

    _init: (args) ->
      @config = @_updateConfig args
      @el = document.getElementById @config.id
      @xhr = @_setupXHR()
      @_addEventHandlers()
      @results = @_buildResults()

    _updateConfig: (args) ->
      newConfig = {}
      newConfig[key] = value for own key, value of DEFAULTS
      newConfig[key] = value for own key, value of args
      newConfig

    _addEventHandlers: ->
      @_on(@el, 'input', (e) =>
        @_searchFor e.currentTarget.value
      )
      @_on(@el, 'keydown', (e) =>
        @_keypressHandler e
      )

    _setupXHR: ->
      xhr = new XMLHttpRequest()
      @_on(xhr, 'readystatechange', =>
        if @xhr.readyState == 4
          if @xhr.status == 200
            @_populateResults JSON.parse(@xhr.responseText), @currentSearch
      )
      xhr

    _buildResults: ->
      results = document.createElement 'UL'
      @_addClass results, @config.resultsClass
      @_addClass results, "#{@config.resultsClass}--#{@config.classModifier}" if @config.classModifier

      @_on(results, 'click', (e) =>
        @_resultsClick e
      , false)

      @_on(results, 'mouseover', (e) =>
        @_resultsMouseOver e.target
      , false)

      @_on(results, 'mouseout', (e) =>
        @_resultsMouseOut e.target
      , false)
      results

    # event handlers
    _keypressHandler: (e) ->
      switch e.keyCode
        when KEY.up
          if @results.displayed
            e.preventDefault()
            @_highlightUp()
        when KEY.down
          if @results.displayed
            e.preventDefault()
            @_highlightDown()
        when KEY.tab
          if @results.displayed
            e.preventDefault()
            if e.shiftKey
              @_highlightUp()
            else
              @_highlightDown()
        when KEY.enter
          if @results.highlighted
            e.preventDefault()
            @_handleEnter()
        when KEY.esc
          @_handleCancel()

    _resultsClick: (e) ->
      target = e.target

      # If the user clicked on the highlighted section of the result, check its parent
      target = target.parentNode if target.tagName == 'B'

      # if the real target was an anchor, prevent the event from bubbling so the text isn't selected
      if target.tagName == 'A'
        e.stopPropagation()
      else if target.tagName == 'LI'
        @results.highlighted = target
        @_selectCurrent()

    _resultsMouseOver: (target) ->
      until target.tagName is 'LI'
        target = target.parentNode

      @_clearHighlight()
      @results.highlighted = target
{'fraction_non_alphanumeric': 0.0975906053857056, 'fraction_numerical': 0.003441992306134845, 'mean_word_length': 2.1193558572781814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '1889107', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2755, 'n_words': 853}