Sha256: 01e15806487e4ccbae4106a00a1ee353e69afaea1785fd16f73eac87566318fa

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

Spree.ready ($) ->
  ($ '#bstate select').change ->
    updateCounty 'b'

  ($ '#sstate select').change ->
    updateCounty 's'

  updateCounty = (region) ->
    stateId = getStateId(region)
    if stateId?
      $.get '/api/counties', {state_id: stateId}, (data) ->
        Spree.Checkout[stateId] =
          counties: data.counties
        fillCounties(Spree.Checkout[stateId], region)

  getStateId = (region) ->
      $('#' + region + 'state select').val()

  fillCounties = (data, region) ->
    countiesRequired = true
    counties = data.counties

    countyPara = ($ '#' + region + 'county')
    countySelect = countyPara.find('select')
    countyInput = countyPara.find('input')
    countySpanRequired = countyPara.find('county-required')
    if counties.length > 0
      selected = parseInt countySelect.val()
      countySelect.html ''
      countyWithBlank = [{ name: '', id: ''}].concat(counties)
      $.each countyWithBlank, (idx, county) ->
        opt = ($ document.createElement('option')).attr('value', county.id).html(county.name)
        opt.prop 'selected', true if selected is county.id
        countySelect.append opt

      countySelect.prop('disabled', false).show()
      countyInput.hide().prop 'disabled', true
      countyPara.show()
      countySpanRequired.show()
      countySelect.addClass('required') if countiesRequired
      countySelect.removeClass('hidden')
      countyInput.removeClass('required')
    else
      countySelect.hide().prop 'disabled', true
      countyInput.show()
      if countiesRequired
        countySpanRequired.show()
        countyInput.addClass('required')
      else
        countyInput.val ''
        countySpanRequired.hide()
        countyInput.removeClass('required')
      countyPara.toggle(!!countiesRequired)
      countyInput.prop('disabled', !countiesRequired)
      countyInput.removeClass('hidden')
      countySelect.removeClass('required')

  updateCounty 'b'

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_counties-3.0.1 app/assets/javascripts/spree/frontend/spree_counties.js.coffee
spree_counties-3.0.0 app/assets/javascripts/spree/frontend/spree_counties.js.coffee
spree_counties-2.3.6 app/assets/javascripts/spree/frontend/spree_counties.js.coffee
spree_counties-2.3.5 app/assets/javascripts/spree/frontend/spree_counties.js.coffee
spree_counties-1.0.0 app/assets/javascripts/spree/frontend/spree_counties.js.coffee