<commit_before># Image upload control

class @ImageUpload
  uploadMethod: null

  constructor: (@container)->
    @setupNestedAttributes()
    @setupSwtiching()

  setupNestedAttributes: ->
    @container.nestedAttributes
      bindAddTo: $(".actions .add")
      collectionName: 'images'
      collectIdAttributes: false
      $clone: @container.children('.field')

    @container.find('input').focus (e)=>
      inputs = @container.find("input:enabled.upload[type=#{@uploadMethod}]").not(e.target).filter (index)->
        @value.length == 0

      if inputs.length == 0
        @container.nestedAttributes("add")

  setupSwtiching: ->
    @updateView()
    @setCurrentUploadMethod()

    @container.find('.actions a.switch').click (e)=>
      e.preventDefault()

      # toggle disable
      @container.find('.field input.upload').prop 'disabled', (i,v)=>
        !v

      @updateView()
      @setCurrentUploadMethod()

  setCurrentUploadMethod: ->
    if @container.find("input:enabled.upload[type=file]").length
      @uploadMethod = 'file'
    else
      @uploadMethod = 'url'


  updateView: ->
    # visibility
    @container.find('.field input').each (index, input)=>
      input = $(input)
      if input.prop('disabled')
        input.addClass('hide')
      else
        input.removeClass('hide')

$ ->
  new ImageUpload($('.file-section'))
<commit_msg>Fix file input not adding new field after change bug.<commit_after># Image upload control

class @ImageUpload
  uploadMethod: null

  constructor: (@container)->
    @setupNestedAttributes()
    @setupSwtiching()

  setupNestedAttributes: ->
    @container.nestedAttributes
      bindAddTo: $(".actions .add")
      collectionName: 'images'
      collectIdAttributes: false
      $clone: @container.children('.field')

    @container.find('input[type=url]').focus (e)=>
      @addInput(e.target)

    @container.find('input[type=file]').change (e)=>
      @addInput(e.target)

  addInput: (target)->
      empty_file_inputs = @container.find("input:enabled.upload[type=#{@uploadMethod}]").not(target).filter (index)->
        @value.length == 0

      if empty_file_inputs.length == 0
        @container.nestedAttributes("add")

  setupSwtiching: ->
    @updateView()
    @setCurrentUploadMethod()

    @container.find('.actions a.switch').click (e)=>
      e.preventDefault()

      # toggle disable
      @container.find('.field input.upload').prop 'disabled', (i,v)=>
        !v

      @updateView()
      @setCurrentUploadMethod()

  setCurrentUploadMethod: ->
    if @container.find("input:enabled.upload[type=file]").length
      @uploadMethod = 'file'
    else
      @uploadMethod = 'url'


  updateView: ->
    # visibility
    @container.find('.field input').each (index, input)=>
      input = $(input)
      if input.prop('disabled')
        input.addClass('hide')
      else
        input.removeClass('hide')

$ ->
  new ImageUpload($('.file-section'))

{'fraction_non_alphanumeric': 0.15631399317406144, 'fraction_numerical': 0.0013651877133105802, 'mean_word_length': 3.1107994389901825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '6876609', 'n_tokens_mistral': 882, 'n_tokens_neox': 857, 'n_words': 161}