Advertisement
Guest User

Untitled

a guest
May 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. getCookie = (name) ->
  2.   value = '; ' + document.cookie
  3.   parts = value.split('; ' + name + '=')
  4.   if parts.length == 2
  5.     parts.pop().split(';').shift()
  6.  
  7. rounded_value = (value) ->
  8.   Math.round(value * 10000)/10000
  9.  
  10. getUserPosition = ->
  11.   return if getCookie('selected_shop')
  12.   if navigator.geolocation
  13.     navigator.geolocation.getCurrentPosition (position) ->
  14.       coords    = position.coords
  15.       latitude  = rounded_value(getCookie('latitude'))
  16.       longitude = rounded_value(getCookie('longitude'))
  17.  
  18.       return if latitude == rounded_value(coords.latitude) && longitude == rounded_value(coords.longitude)
  19.  
  20.       $.ajax
  21.         type: 'GET'
  22.         url:  '/'
  23.         data: {position: position.coords }
  24.       document.cookie = "latitude="  + escape(coords.latitude)
  25.       document.cookie = "longitude=" + escape(coords.longitude)
  26.  
  27. selectShop = ->
  28.   $('.js-shops-select').on 'change', ->
  29.     shop_id = $(this).val()
  30.     $.ajax
  31.       type: 'GET'
  32.       url:  '/'
  33.       data: { selected_shop: { shop_id: shop_id} }
  34.       document.cookie = "selected_shop=" + escape(shop_id)
  35.  
  36. $(document).on 'turbolinks:load', ->
  37.   getUserPosition()
  38.   selectShop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement