<commit_before>{div, input, span, a, i, button, ul, li, select, option } = React.DOM
window.Dropdown = class Dropdown extends React.Component
  constructor: (props, context) ->
    super(props, context)
    @state = {
      title: props.title,
      name: props.name,
      placeholder: props.placeholder,
      data: props.data,
      enabled: props.enabled,
      blankCheckbox: props.blankCheckbox || false
      value: props.value
      form: props.form
      apiBaseUrl: props.apiBaseUrl
    }
    @setBlank = @setBlank.bind(@)

  render: ->
    disabled_class = if @state.enabled then '' else 'disabled'
    div(
      { className: "shipments-dropdown #{disabled_class}" }
      span({ className: 'bold' }, @state.title)
      @renderDropdown()
      @renderCheckbox() if @state.blankCheckbox
    )

  renderDropdown: ->
    name = if @state.form then "#{@state.form}[#{@state.name}]" else @state.name
    div(
      {}
      select(
        {
          id: "#{@state.name}_dropdown",
          className: 'dropdown-menu',
          name: name
        }
        option(
          { defaultValue: @state.value }
          @state.value
        )
      )
    )

  renderCheckbox: ->
    div(
      { className: 'blank-checkbox' }
      input(
        { type: 'checkbox', onClick: @setBlank }
      )
      span({}, 'Set blank')
    )

  setBlank: ->
    @setState({value: ' '})

  componentWillUpdate: (nextProps, nextState)->
    @refs.textInput.value = nextState.value

  componentDidMount: ->
    data = []
    if @state.name in ['trading_partner', 'country_of_origin']
      data = @state.data.map (value) ->
        id: value[0]
        text: value[1]
    else if @state.name == 'taxon_name'
      @select2TaxonConcept()
      return
    else
      data = @state.data.map (value) ->
        id: value
        text: value
    $("##{@state.name}_dropdown").select2({
      placeholder: @state.placeholder,
      data: data
    })

  select2TaxonConcept: ->
    $("#taxon_name_dropdown").select2({
      minimumInputLength: 3
      quietMillis: 500,
      ajax:
        url: "#{@state.apiBaseUrl}/api/v1/auto_complete_taxon_concepts.json"
        dataType: 'json'
        data: (term, page) =>
          taxon_concept_query: term.term # search term
          #visibility: 'trade_internal' #includes status
          include_synonyms: true
          per_page: 10
          page: page
        processResults: (data, page) => # parse the results into the format expected by Select2.
          more = (page * 10) < data.meta.total
          formatted_taxon_concepts = data.auto_complete_taxon_concepts.map (tc) =>
            nameStatusFormatted = unless tc.name_status == 'A'
              ' [' + tc.name_status + ']'
            else
              ''
            id: tc.full_name
            text: tc.full_name + nameStatusFormatted
          results: formatted_taxon_concepts
          more: more
    })
<commit_msg>Fix set blank checkbox for dropdown<commit_after>{div, input, span, a, i, button, ul, li, select, option } = React.DOM
window.Dropdown = class Dropdown extends React.Component
  constructor: (props, context) ->
    super(props, context)
    @state = {
      title: props.title,
      name: props.name,
      placeholder: props.placeholder,
      data: props.data,
      enabled: props.enabled,
      blankCheckbox: props.blankCheckbox || false
      value: props.value
      form: props.form
      apiBaseUrl: props.apiBaseUrl
      isBlank: false
    }
    @setBlank = @setBlank.bind(@)

  render: ->
    disabled_class = if @state.enabled then '' else 'disabled'
    div(
      { className: "shipments-dropdown #{disabled_class}" }
      span({ className: 'bold' }, @state.title)
      @renderDropdown()
      @renderCheckbox() if @state.blankCheckbox
    )

  renderDropdown: ->
    name = if @state.form then "#{@state.form}[#{@state.name}]" else @state.name
    div(
      {}
      select(
        {
          id: "#{@state.name}_dropdown",
          className: 'dropdown-menu',
          name: name
        }
        option(
          { defaultValue: @state.value }
          @state.value
        )
      )
    )

  renderCheckbox: ->
    div(
      { className: 'blank-checkbox' }
      input(
        { type: 'checkbox', onClick: @setBlank }
      )
      span({}, 'Set blank')
    )

  setBlank: ->
    @setState({isBlank: !@state.isBlank})

  componentDidMount: ->
    data = []
    if @state.name in ['trading_partner', 'country_of_origin']
      data = @state.data.map (value) ->
        id: value[0]
        text: value[1]
    else if @state.name == 'taxon_name'
      @select2TaxonConcept()
      return
    else
      data = @state.data.map (value) ->
        id: value
        text: value
    $("##{@state.name}_dropdown").select2({
      placeholder: @state.placeholder,
      data: data
    })

  componentDidUpdate: ->
    dropdown = "##{@state.name}_dropdown"
    if @state.isBlank
      $(dropdown).html('').select2({data: [{id: '', text: ''}]})
    else
      data = @state.data.map (value) ->
        id: value
        text: value
      $(dropdown).html('').select2({data: data}).val(@state.value).trigger('change')

  select2TaxonConcept: ->
    $("#taxon_name_dropdown").select2({
      minimumInputLength: 3
      quietMillis: 500,
      ajax:
        url: "#{@state.apiBaseUrl}/api/v1/auto_complete_taxon_concepts.json"
        dataType: 'json'
        data: (term, page) =>
          taxon_concept_query: term.term # search term
          #visibility: 'trade_internal' #includes status
          include_synonyms: true
          per_page: 10
          page: page
        processResults: (data, page) => # parse the results into the format expected by Select2.
          more = (page * 10) < data.meta.total
          formatted_taxon_concepts = data.auto_complete_taxon_concepts.map (tc) =>
            nameStatusFormatted = unless tc.name_status == 'A'
              ' [' + tc.name_status + ']'
            else
              ''
            id: tc.full_name
            text: tc.full_name + nameStatusFormatted
          results: formatted_taxon_concepts
          more: more
    })

{'fraction_non_alphanumeric': 0.13178926765617355, 'fraction_numerical': 0.005545587995433045, 'mean_word_length': 2.1526992287917737, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '5623394', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1894, 'n_words': 413}