<commit_before>React = require 'react'
ReactDOM = require 'react-dom'
DragReorderable = require 'drag-reorderable'
{days, months} = require './data/time'
{countries, statesUSA} = require './data/location'

DropdownDialog = React.createClass

  getDefaultProps: ->
    selects: []
    initialSelect: {}
    related: []
    onCancel: ->
    onSave: ->

  getInitialState: ->
    editSelect: JSON.parse JSON.stringify @props.initialSelect
    optionsKeys: {}
    conditionalSelects: []
    deletedValues: []

  componentDidMount: ->
    if @props.initialSelect.condition?
      conditionalSelect = @props.selects.filter (select) => select.id is @props.initialSelect.condition
      conditionalSelects = []
      while conditionalSelect[0]?
        conditionalSelects.push conditionalSelect[0]
        conditionalSelect = @props.selects.filter (select) => select.id is conditionalSelect[0].condition
      conditionalSelects.reverse()
      @setState conditionalSelects: conditionalSelects

  handleOptionsKeys: (select, value) ->
    optionsKeys = @state.optionsKeys
    if not select.condition?
      optionsKeys[select.id] = value
    else if value is null or value is ''
      optionsKeys[select.id] = null
    else
      optionsKeys[select.id] = "#{optionsKeys[select.condition]};#{value}"
    @setState optionsKeys: optionsKeys

  getOptionsKey: (select) ->
    if not select.condition? then '*' else @state.optionsKeys[select.condition]

  getOptionsByProp: (prop) ->
    optionsByProp = @state.editSelect.options[@getOptionsKey(@state.editSelect)]?.map (option) => option[prop]
    optionsByProp or []

  editSelect: ->
    select = @state.editSelect
    select.title = @refs.title.value
    select.required = @refs.required.checked
    select.allowCreate = @refs.allowCreate.checked
    @setState editSelect: select

  onChangeConditionalAnswer: (select, e) ->
    @handleOptionsKeys(select, e.target.value)
    for select in @props.related
      @handleOptionsKeys(select, null)

  addOption: (optionLabel) ->
    if not optionLabel
      return window.alert('Please provide an option')

    if @getOptionsByProp("label")?.indexOf(optionLabel) isnt -1
      return window.alert('Options must be unique within each dropdown')

    select = @state.editSelect

    if select.condition? and not @state.optionsKeys[select.condition]
      return window.alert('Please select an answer to the related conditional dropdown(s) to associate the new option to')

    optionsKey = @getOptionsKey(select)

    if select.options[optionsKey]?
      select.options[optionsKey].push {value: "#{Math.random().toString(16).split('.')[1]}", label: "#{optionLabel}"}
    else
      select.options["#{optionsKey}"] = [{value: "#{Math.random().toString(16).split('.')[1]}", label: "#{optionLabel}"}]

    @setState editSelect: select

  onClickAddOption: ->
    @addOption(@refs.optionInput.value)
    @refs.optionInput.value = ''

  onReorder: (newOptions) ->
    select = @state.editSelect
    select.options[@getOptionsKey(select)] = newOptions
    @setState editSelect: select

  onClickDeleteOption: (value) ->
    select = @state.editSelect
    newOptions = select.options[@getOptionsKey(select)].filter (option) =>
      option.value isnt value
    select.options[@getOptionsKey(select)] = newOptions
    @setState editSelect: select

    if @props.related.length
      deletedValues = @state.deletedValues
      deletedValues.push value
      @setState deletedValues: deletedValues

  handlePreset: ->
    console.log 'handlePreset ran'
    preset = @refs.preset.value
    select = @state.editSelect
    optionsKey = @getOptionsKey(select)

    switch preset
      when "days" then select.options["#{optionsKey}"] = days
      when "months" then select.options["#{optionsKey}"] = months
      when "countries" then select.options["#{optionsKey}"] = countries
      when "statesUSA" then select.options["#{optionsKey}"] = statesUSA
      else return

    @setState editSelect: select
    @refs.preset.value = ""

  save: (e) ->
    if not @state.editSelect.title
      return window.alert('Dropdowns must have a Title.')
<commit_msg>fix issue with dep selects<commit_after>React = require 'react'
ReactDOM = require 'react-dom'
DragReorderable = require 'drag-reorderable'
{days, months} = require './data/time'
{countries, statesUSA} = require './data/location'

DropdownDialog = React.createClass

  getDefaultProps: ->
    selects: []
    initialSelect: {}
    related: []
    onCancel: ->
    onSave: ->

  getInitialState: ->
    editSelect: JSON.parse JSON.stringify @props.initialSelect
    optionsKeys: {}
    conditionalSelects: []
    deletedValues: []

  componentDidMount: ->
    if @props.initialSelect.condition?
      conditionalSelect = @props.selects.filter (select) => select.id is @props.initialSelect.condition
      conditionalSelects = []
      while conditionalSelect[0]?
        conditionalSelects.push conditionalSelect[0]
        conditionalSelect = @props.selects.filter (select) => select.id is conditionalSelect[0].condition
      conditionalSelects.reverse()
      @setState conditionalSelects: conditionalSelects

  handleOptionsKeys: (select, value) ->
    optionsKeys = @state.optionsKeys
    if not select.condition?
      optionsKeys[select.id] = value
    else if value is null or value is ''
      optionsKeys[select.id] = null
    else
      optionsKeys[select.id] = "#{optionsKeys[select.condition]};#{value}"
    @setState optionsKeys: optionsKeys

  getOptionsKey: (select) ->
    if not select.condition? then '*' else @state.optionsKeys[select.condition]

  getOptionsByProp: (prop) ->
    optionsByProp = @state.editSelect.options[@getOptionsKey(@state.editSelect)]?.map (option) => option[prop]
    optionsByProp or []

  editSelect: ->
    select = @state.editSelect
    select.title = @refs.title.value
    select.required = @refs.required.checked
    select.allowCreate = @refs.allowCreate.checked
    @setState editSelect: select

  onChangeConditionalAnswer: (select, e) ->
    @handleOptionsKeys(select, e.target.value)
    for select in @props.related
      @handleOptionsKeys(select, null)

  addOption: (optionLabel) ->
    if not optionLabel
      return window.alert('Please provide an option')

    if @getOptionsByProp("label")?.indexOf(optionLabel) isnt -1
      return window.alert('Options must be unique within each dropdown')

    select = @state.editSelect

    if select.condition? and not @state.optionsKeys[select.condition]
      return window.alert('Please select an answer to the related conditional dropdown(s) to associate the new option to')

    optionsKey = @getOptionsKey(select)

    if select.options[optionsKey]?
      select.options[optionsKey].push {value: "#{Math.random().toString(16).split('.')[1]}", label: "#{optionLabel}"}
    else
      select.options["#{optionsKey}"] = [{value: "#{Math.random().toString(16).split('.')[1]}", label: "#{optionLabel}"}]

    @setState editSelect: select

  onClickAddOption: ->
    @addOption(@refs.optionInput.value)
    @refs.optionInput.value = ''

  onReorder: (newOptions) ->
    select = @state.editSelect
    select.options[@getOptionsKey(select)] = newOptions
    @setState editSelect: select

  onClickDeleteOption: (value) ->
    select = @state.editSelect
    newOptions = select.options[@getOptionsKey(select)].filter (option) =>
      option.value isnt value
    select.options[@getOptionsKey(select)] = newOptions
    @setState editSelect: select

    if @props.related.length
      deletedValues = @state.deletedValues
      deletedValues.push value
      @setState deletedValues: deletedValues

  handlePreset: ->
    select = @state.editSelect

    if select.condition? and not @state.optionsKeys[select.condition]
      return window.alert('Please select an answer to the related conditional dropdown(s) to associate the new option to')

    optionsKey = @getOptionsKey(select)

    preset = @refs.preset.value

    switch preset
      when "days" then select.options["#{optionsKey}"] = days
      when "months" then select.options["#{optionsKey}"] = months
      when "countries" then select.options["#{optionsKey}"] = countries
      when "statesUSA" then select.options["#{optionsKey}"] = statesUSA
      else return

    @setState editSelect: select
    @refs.preset.value = ""

  save: (e) ->
    if not @state.editSelect.title
      return window.alert('Dropdowns must have a Title.')

{'fraction_non_alphanumeric': 0.12339667458432305, 'fraction_numerical': 0.0023752969121140144, 'mean_word_length': 3.738885762521103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '6074365', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2408, 'n_words': 562}