SHARE
TWEET

Untitled

a guest Oct 16th, 2019 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # autocomplete with here
  2. $ ->
  3.   APP_ID_HERE = '<%= Rails.application.secrets.here_app_id_frontend %>'
  4.   APP_CODE_HERE = '<%= Rails.application.secrets.here_app_code_frontend %>'
  5.  
  6.   addressAC = (query, callback) ->
  7.     $.getJSON 'https://autocomplete.geocoder.api.here.com/6.2/suggest.json?query=' + query.term + '&country=USA' + '&app_id=' + APP_ID_HERE + '&app_code=' + APP_CODE_HERE, (data) ->
  8.       addresses = data.suggestions
  9.       addresses = addresses.map((addr) ->
  10.         result = []
  11.         if addr.address.houseNumber != undefined and addr.address.street != undefined
  12.           result.push addr.address.houseNumber + ' ' + addr.address.street
  13.         else if addr.address.houseNumber == undefined and addr.address.street != undefined
  14.           result.push addr.address.street
  15.         if addr.address.city != undefined
  16.           result.push addr.address.city
  17.         if addr.address.state != undefined
  18.           result.push addr.address.state
  19.         if addr.address.postalCode != undefined
  20.           result.push addr.address.postalCode
  21.         if addr.countryCode != undefined
  22.           result.push addr.countryCode
  23.         {
  24.           title: result.join(', ')
  25.           value: result.join(', ')
  26.           id: addr.locationId
  27.         }
  28.       )
  29.       callback addresses
  30.     return
  31.  
  32.   $('#search_query').autocomplete
  33.     source: addressAC
  34.     minLength: 2
  35.     select: (event, ui) ->
  36.       $('#search_query').prop 'verified', 'yes'
  37.       $('#search_query').prop 'address', ui.item.value
  38.       console.log 'Selected: ' + ui.item.value + ' with LocationId ' + ui.item.id
  39.       return
  40.   return
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.   showError = (message) ->
  48.     $('.input_error').html(message).show()
  49.  
  50.   hideError = ->
  51.     $('.input_error').hide()
  52.  
  53.   $('#search_form_submit').click (e) ->
  54.     e.preventDefault()
  55.     hideError()
  56.  
  57.     if $('#search_query').prop('verified') != 'yes'
  58.       showError('Please use the suggested address in drop down')
  59.       return
  60.  
  61.     $('#search_form').submit()
  62.     return
  63.  
  64.   input = document.getElementById('search_query')
  65.   if input != null
  66.     $(input).on 'keyup keypress', (e) ->
  67.       if ($(input).val() == $("#search_query").prop('address'))
  68.         $(input).prop('verified', 'yes')
  69.       else
  70.         $(input).prop('verified', false)
  71.       keyCode = e.keyCode or e.which
  72.       if e.keyCode == 8 || e.keyCode == 46
  73.         $(input).prop('verified', false)
  74.       else if keyCode == 13
  75.         e.preventDefault()
  76.         return false
  77.       return
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top