<commit_before>FormRenderer.Models.ResponseFieldRepeatingGroup = FormRenderer.Models.BaseFormComponent.extend
  group: true
  field_type: 'repeating_group'

  initialize: ->
    FormRenderer.Models.BaseFormComponent::initialize.apply @, arguments
    @entries = []

  validateComponent: ->
    for entry in @entries
      entry.formComponents.invoke('validateComponent')

  setExistingValue: (entryValues) ->
    if @isRequired()
      # If the field is required, ensure that there is at least one value present
      if !entryValues || entryValues.length == 0
        entryValues = [{}]

    else # Field is optional...
      # If entryValues is not set, then add an empty value.
      if !entryValues
        entryValues = [{}]
      # If entryValues is an empty array, the field is skipped.
      else if _.isArray(entryValues) && _.isEmpty(entryValues)
        @set('skipped', true)

    @entries = _.map entryValues, (value) =>
      new FormRenderer.Models.ResponseFieldRepeatingGroupEntry({ value }, @fr, @)

  addEntry: ->
    @entries.push(
      new FormRenderer.Models.ResponseFieldRepeatingGroupEntry({}, @fr, @)
    )

  removeEntry: (idx) ->
    @entries.splice(idx, 1)

    if @entries.length == 0
      @set('skipped', true)

  isSkipped: ->
    !!@get('skipped')

  getValue: ->
    if @isSkipped()
      []
    else
      _.invoke @entries, 'getValue'

  maxEntries: ->
    if @get('maxentries')
      parseInt(@get('maxentries'), 10) || Infinity
    else
      Infinity

  canAdd: ->
    @entries.length < @maxEntries()

FormRenderer.Models.ResponseFieldRepeatingGroupEntry = Backbone.Model.extend
  field_type: 'repeating_group_entry'

  initialize: (_attrs, @fr, @repeatingGroup) ->
    children = @repeatingGroup.get('children')
    unless children?
      children = []
    @initFormComponents children, @get('value') || {}

  reflectConditions: ->
    @view.reflectConditions()

  canRemove: ->
    @repeatingGroup.entries.length > 1

FormRenderer.Views.ResponseFieldRepeatingGroup = Backbone.View.extend
  className: 'fr_response_field fr_response_field_group'

  events:
    'click .js-remove-entry': 'removeEntry'
    'click .js-add-entry': 'addEntry'
    'click .js-skip': 'toggleSkip'

  initialize: (options) ->
    @_sharedInitialize(options)

    # Forward `shown` and `hidden` events to subviews
    @on 'shown', => view.trigger('shown') for view in @views
    @on 'hidden', => view.trigger('hidden') for view in @views

  toggleSkip: ->
    @model.set('skipped', !@model.isSkipped())

    # When clicking "answer this question", add an entry if there are none
    if !@model.isSkipped() && @model.entries.length == 0
      @addEntry()

    @render()

  addEntry: ->
    @model.addEntry()
    @render()
    _.last(@views).focus()

  removeEntry: (e) ->
    idx = @$el.find('.js-remove-entry').index(e.target)
    @model.removeEntry(idx)
    @render()

  render: ->
    @views = []
    $els = $()
<commit_msg>clicking delete entry on a repeating group now deletes the correct group<commit_after>FormRenderer.Models.ResponseFieldRepeatingGroup = FormRenderer.Models.BaseFormComponent.extend
  group: true
  field_type: 'repeating_group'

  initialize: ->
    FormRenderer.Models.BaseFormComponent::initialize.apply @, arguments
    @entries = []

  validateComponent: ->
    for entry in @entries
      entry.formComponents.invoke('validateComponent')

  setExistingValue: (entryValues) ->
    if @isRequired()
      # If the field is required, ensure that there is at least one value present
      if !entryValues || entryValues.length == 0
        entryValues = [{}]

    else # Field is optional...
      # If entryValues is not set, then add an empty value.
      if !entryValues
        entryValues = [{}]
      # If entryValues is an empty array, the field is skipped.
      else if _.isArray(entryValues) && _.isEmpty(entryValues)
        @set('skipped', true)

    @entries = _.map entryValues, (value) =>
      new FormRenderer.Models.ResponseFieldRepeatingGroupEntry({ value }, @fr, @)

  addEntry: ->
    @entries.push(
      new FormRenderer.Models.ResponseFieldRepeatingGroupEntry({}, @fr, @)
    )

  removeEntry: (idx) ->
    @entries.splice(idx, 1)

    if @entries.length == 0
      @set('skipped', true)

  isSkipped: ->
    !!@get('skipped')

  getValue: ->
    if @isSkipped()
      []
    else
      _.invoke @entries, 'getValue'

  maxEntries: ->
    if @get('maxentries')
      parseInt(@get('maxentries'), 10) || Infinity
    else
      Infinity

  canAdd: ->
    @entries.length < @maxEntries()

FormRenderer.Models.ResponseFieldRepeatingGroupEntry = Backbone.Model.extend
  field_type: 'repeating_group_entry'

  initialize: (_attrs, @fr, @repeatingGroup) ->
    children = @repeatingGroup.get('children')
    unless children?
      children = []
    @initFormComponents children, @get('value') || {}

  reflectConditions: ->
    @view.reflectConditions()

  canRemove: ->
    @repeatingGroup.entries.length > 1

FormRenderer.Views.ResponseFieldRepeatingGroup = Backbone.View.extend
  className: 'fr_response_field fr_response_field_group'

  events:
    'click .js-remove-entry': 'removeEntry'
    'click .js-add-entry': 'addEntry'
    'click .js-skip': 'toggleSkip'

  initialize: (options) ->
    @_sharedInitialize(options)

    # Forward `shown` and `hidden` events to subviews
    @on 'shown', => view.trigger('shown') for view in @views
    @on 'hidden', => view.trigger('hidden') for view in @views

  toggleSkip: ->
    @model.set('skipped', !@model.isSkipped())

    # When clicking "answer this question", add an entry if there are none
    if !@model.isSkipped() && @model.entries.length == 0
      @addEntry()

    @render()

  addEntry: ->
    @model.addEntry()
    @render()
    _.last(@views).focus()

  removeEntry: (e) ->
    idx = @$el.find('.js-remove-entry').index(e.target.closest('.js-remove-entry'))
    @model.removeEntry(idx)
    @render()

  render: ->
    @views = []
    $els = $()

{'fraction_non_alphanumeric': 0.13809284397519692, 'fraction_numerical': 0.0023462376403552876, 'mean_word_length': 3.208744710860367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '1549638', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1832, 'n_words': 427}