<commit_before>$.taggable = (options = {}) ->
  new Taggable(options)

class Taggable
  constructor: (options) ->
    defaultOptions =
      key: 13
      inputFieldName: '#taggable'
      hiddenFieldName: '#tag_list'
      tagName: '.tag'

    @options = $.extend defaultOptions, options
    @inputField = $(@options.inputFieldName)

    @add()

  add: =>
    @inputField
      .on('keypress', @tagging)

  tagging: (ev) =>
    if ev.keyCode is @options.key
      ev.preventDefault()
      value = ev.target.value
      if value isnt ''
        if $.isEmptyObject $(@options.hiddenFieldName).val()
          tags = []
        else
          tags = $(@options.hiddenFieldName).val().split(',')
        tags.push value
        $(@options.hiddenFieldName).val(tags.join())

        li = "<li data-tag-name='#{value}' class='tag'>#{value}<a href='#' class='remove'>x</a></li>"
        $('.tag_list').append li
        @inputField.val('')

      $('.remove')
        .on('click', @remove)

  remove: (ev) =>
    ev.preventDefault()
    value = $(ev.target).parent().data 'tag-name'

    tags = $(@options.hiddenFieldName).val()
    tags = tags.split(',')
    tags = $.map tags, (n, i) ->
      if n isnt value
        n
      else
        null
    $(@options.hiddenFieldName).val(tags.join())

    $(ev.target).parent().remove()

$ ->
  $.taggable
    inputFieldName: '.taggable'<commit_msg>Generate tags with find by candidates from given json<commit_after>$.taggable = (options = {}) ->
  new Taggable(options)

class Taggable
  constructor: (options) ->
    defaultOptions =
      key: 13
      inputFieldName: '#taggable'
      hiddenFieldName: '#tag_list'
      tagName: '.tag'

    @options = $.extend defaultOptions, options
    @inputField = $(@options.inputFieldName)

    @add()

  add: =>
    @inputField
      .on('keyup', @findTags)
      .on('keypress', @createTags)

  findTags: (ev) =>
    if ev.target.value is ''
      @inputField.parent().find('#suggestion').empty()
    else if ev.keyCode isnt @options.key
      setTimeout =>
        $.ajax
          type: 'GET'
          url: '/tags'
          dataType: 'json'
          data:
            q: ev.target.value
          success: (data) =>
            suggestionField = @inputField.parent().find('#suggestion')
            if suggestionField.length == 0
              ul = document.createElement 'ul'
              $(ul).attr 'id', 'suggestion'
              @inputField.parent().append ul
            result = data.result
            suggestionField.empty()
            for str in result
              suggestionField.append "<li class=\"res\" data-suggestion-name=\"#{str}\">#{str}</li>"
            suggestionField.find('li').on('click', @createTag)
          error: () =>
            @inputField.parent().find('#suggestion').empty()

      , 300

  createTags: (ev) =>
    if ev.keyCode is @options.key
      ev.preventDefault()
      value = ev.target.value
      if value isnt ''
        if $.isEmptyObject $(@options.hiddenFieldName).val()
          tags = []
        else
          tags = $(@options.hiddenFieldName).val().split(',')
        tags.push value
        $(@options.hiddenFieldName).val(tags.join())

        li = "<li data-tag-name='#{value}' class='tag'><a href='#' class='add-tag-link'>#{value}</a><a href='#' class='remove'>x</a></li>"
        $('.tag_list').append li
        @inputField.val('')

      $('.remove')
        .on('click', @remove)

  createTag: (ev) =>
    li = $(ev.target)
    value = li.data('suggestion-name')
    if $.isEmptyObject $(@options.hiddenFieldName).val()
      tags = []
    else
      tags = $(@options.hiddenFieldName).val().split(',')
    tags.push value
    $(@options.hiddenFieldName).val(tags.join())

    li = "<li data-tag-name='#{value}' class='tag'><a href='#' class='add-tag-link'>#{value}</a><a href='#' class='remove'>x</a></li>"
    $('.tag_list').append li
    @inputField.val('')

    @inputField.parent().find('#suggestion').empty()

    $('.remove')
      .on('click', @remove)

  remove: (ev) =>
    ev.preventDefault()
    value = $(ev.target).parent().data 'tag-name'

    tags = $(@options.hiddenFieldName).val()
    tags = tags.split(',')
    tags = $.map tags, (n, i) ->
      if n isnt value
        n
      else
        null
    $(@options.hiddenFieldName).val(tags.join())

    $(ev.target).parent().remove()

$ ->
  $.taggable
    inputFieldName: '.taggable'
{'fraction_non_alphanumeric': 0.18357159065123668, 'fraction_numerical': 0.0018152938506920807, 'mean_word_length': 2.344461305007587, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 40, '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': '5320055', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1385, 'n_words': 273}