Sha256: ebfbef57fe678932a0db62287e842fbc964afdaf4f5a08a91d8cb2b6ca1d64b7

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

loadSubregions = ($countrySelect, clearPostalCode = false) ->
  $.each $countrySelect, (index, countrySelector) ->
    $particularCountrySelect = $(countrySelector)
    countryCode = $particularCountrySelect.val()
    uuid = $particularCountrySelect.data('effective-address-country')

    $nestedFields = $particularCountrySelect.closest('.nested-fields')
    $form = $particularCountrySelect.closest('form')

    $container = if $nestedFields.length == 0 then $form else $nestedFields

    # clear postal_code values on country change if it is turned on
    $container.find("input[data-effective-address-postal-code='#{uuid}']").val('') if clearPostalCode

    # load state options
    url = "/addresses/subregions/#{countryCode}"
    $stateSelect = $container.find("select[data-effective-address-state='#{uuid}']:first")

    if countryCode.length == 0
      $stateSelect.prop('disabled', true).addClass('disabled').parent('.form-group').addClass('disabled').find('label').addClass('disabled')
      $stateSelect.html('<option value="">Please choose a country first</option>')
    else
      $stateSelect.removeAttr('disabled').parent('.form-group').removeClass('disabled').find('.disabled').removeClass('disabled')
      $stateSelect.find('option').first().text('loading...')
      $stateSelect.load url, ->
        stateSelectAvailable = $(@).find('option:last').val().length > 0
        if stateSelectAvailable
          $(@).prop('required', true)
          $(@).removeAttr('disabled')
        else
          $(@).removeAttr('required')
          $(@).prop('disabled', true)

$(document).on 'change', 'select[data-effective-address-country]', -> loadSubregions($(@), true)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
effective_addresses-1.4.1 app/assets/javascripts/effective_addresses/address_fields.js.coffee
effective_addresses-1.4.0 app/assets/javascripts/effective_addresses/address_fields.js.coffee
effective_addresses-1.3.9 app/assets/javascripts/effective_addresses/address_fields.js.coffee
effective_addresses-1.3.8 app/assets/javascripts/effective_addresses/address_fields.js.coffee