<commit_before>@adverseEventsSubjectSearchReady = ->
  $("#subject-search").typeahead('destroy')
  $("#subject-search").each( ->
    $this = $(this)
    bloodhound = new Bloodhound(
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value')
      queryTokenizer: Bloodhound.tokenizers.whitespace
      remote:
        url: "#{root_url}projects/#{$this.data('project-slug')}/subjects/search?q=%QUERY"
        wildcard: '%QUERY'
    )
    $this.typeahead({ hint: true },
      display: 'value'
      source: bloodhound
      templates:
        suggestion: (item) -> return "<div><strong>#{item.subject_code}</strong></div>"
    )
  )

@subjectsAutocompleteReady = ->
  $('[data-object~="subjects-autocomplete"]').each( ->
    $this = $(this)
    $this.textcomplete(
      [
        {
          match: /(^|\s)has\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['adverse-events', 'comments', 'files']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1has:#{value}"
        },
        {
          match: /(^|\s)is\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['randomized']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1is:#{value}"
        },
        {
          match: /(^|\s)not\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['randomized']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1not:#{value}"
        },
        {
          match: /(^|\s)adverse-events\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['open', 'closed']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1adverse-events:#{value}"
        },
        {
          name: 'search'
          match: /(^|\s)([\w\-]+)$/
          search: (term, callback) ->
            # callback(cache[term], true)
            $.getJSON("#{root_url}projects/#{$this.data('project-id')}/subjects/autocomplete", { q: term })
              .done((resp) -> callback(resp))
              .fail(-> callback([]))
          replace: (value) ->
            return "$1#{value}"
          cache: true
        }
      ], { appendTo: 'body' }
    ).on('textComplete:select': (e, value, strategy) ->
      if strategy.name == 'search' and value not in ['adverse-events', 'has', 'is', 'not']
        $(this).closest('form').submit()
    )
  )

@subjectsReady = ->
  adverseEventsSubjectSearchReady()
  subjectsAutocompleteReady()
<commit_msg>Other autocomplete strategies now also submit form on selection<commit_after>@adverseEventsSubjectSearchReady = ->
  $("#subject-search").typeahead('destroy')
  $("#subject-search").each( ->
    $this = $(this)
    bloodhound = new Bloodhound(
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value')
      queryTokenizer: Bloodhound.tokenizers.whitespace
      remote:
        url: "#{root_url}projects/#{$this.data('project-slug')}/subjects/search?q=%QUERY"
        wildcard: '%QUERY'
    )
    $this.typeahead({ hint: true },
      display: 'value'
      source: bloodhound
      templates:
        suggestion: (item) -> return "<div><strong>#{item.subject_code}</strong></div>"
    )
  )

@subjectsAutocompleteReady = ->
  $('[data-object~="subjects-autocomplete"]').each( ->
    $this = $(this)
    $this.textcomplete(
      [
        {
          name: 'submit-on-click'
          match: /(^|\s)has\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['adverse-events', 'comments', 'files']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1has:#{value}"
        },
        {
          name: 'submit-on-click'
          match: /(^|\s)is\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['randomized']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1is:#{value}"
        },
        {
          name: 'submit-on-click'
          match: /(^|\s)not\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['randomized']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1not:#{value}"
        },
        {
          name: 'submit-on-click'
          match: /(^|\s)adverse-events\:([\w\-]*)$/
          search: (term, callback) ->
            words = ['open', 'closed']
            resp = $.map(words, (word) ->
              if word.indexOf(term) == 0
                word
              else
                null
            )
            callback(resp)
          replace: (value) -> return "$1adverse-events:#{value}"
        },
        {
          name: 'search'
          match: /(^|\s)([\w\-]+)$/
          search: (term, callback) ->
            # callback(cache[term], true)
            $.getJSON("#{root_url}projects/#{$this.data('project-id')}/subjects/autocomplete", { q: term })
              .done((resp) -> callback(resp))
              .fail(-> callback([]))
          replace: (value) ->
            return "$1#{value}"
          cache: true
        }
      ], { appendTo: 'body' }
    ).on('textComplete:select': (e, value, strategy) ->
      if strategy.name == 'search' and value not in ['adverse-events', 'has', 'is', 'not']
        $(this).closest('form').submit()
      else if strategy.name == 'submit-on-click'
        $(this).closest('form').submit()
    )
  )

@subjectsReady = ->
  adverseEventsSubjectSearchReady()
  subjectsAutocompleteReady()

{'fraction_non_alphanumeric': 0.1766346451812829, 'fraction_numerical': 0.0027889680818097306, 'mean_word_length': 1.5534018987341771, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, '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': '3614676', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1900, 'n_words': 349}