diff --git a/app/assets/javascripts/maps.js b/app/assets/javascripts/maps.js index c4757a5f..f6670b72 100644 --- a/app/assets/javascripts/maps.js +++ b/app/assets/javascripts/maps.js @@ -1,8 +1,11 @@ var map; -function initMap(x, y, image){ +function initMap(x, y, image, draggable, callback){ image = typeof image !== 'undefined' ? image : currentLocationImage; + // Draggable defaults to false + draggable = draggable || false; + //init map var mapOptions = { zoom: 15, @@ -21,9 +24,12 @@ function initMap(x, y, image){ var currentLocation = new google.maps.Marker({ position: myLatLng, + draggable: draggable, map: map, icon: image }); + + currentLocation.addListener("dragend", callback); } @@ -272,6 +278,10 @@ $(function(){ window.Maps = { reloadMap: function(map) { initMap(map.dataset.latitude, map.dataset.longitude, showMarkerImage); + }, + + reloadDraggable: function(map, callback) { + initMap(map.dataset.latitude, map.dataset.longitude, showMarkerImage, true, callback); } } }); diff --git a/app/assets/javascripts/views/restrooms/new.coffee b/app/assets/javascripts/views/restrooms/new.coffee index ec33b093..69c1267d 100644 --- a/app/assets/javascripts/views/restrooms/new.coffee +++ b/app/assets/javascripts/views/restrooms/new.coffee @@ -29,34 +29,49 @@ class Refuge.Restrooms.NewRestroomForm console.log result[0] @_getNewForm(coords).then (data, textStatus) => console.log data - $('.form-container').html(data).hide().fadeIn() - @_requestNearbyRestrooms(coords) - @_updateMap(coords) + @_updateForm(coords, data, textStatus) _bindPreviewButton: => @_previewButton.click (event) => - # Show map - @_map.classList.remove("hidden") - form = @_form[0] - name = form.elements.restroom_name.value street = form.elements.restroom_street.value city = form.elements.restroom_city.value state = form.elements.restroom_state.value country = form.elements.restroom_country.value - address = "#{name}, #{street}, #{city}, #{state}, #{country}" + address = "#{street}, #{city}, #{state}, #{country}" # Obtain coordinates @_geocoder.geocodeSearchString(address).then (coords) => @_updateMap(coords) + _rebind: => + @_map = $("#mapArea").get(0) + @_previewButton = $(".preview-btn") + @_guessButton = $(".guess-btn") + + @_bindEvents() + + # Rebind form + @_form = $('form.simple_form') + + _updateMap: (coords) => + # Show map + @_map.classList.remove("hidden") + @_map.dataset.latitude = coords.lat @_map.dataset.longitude = coords.long - Maps.reloadMap(@_map) + Maps.reloadDraggable(@_map, @_onDrag) + # Callback for map marker 'dragend' event + _onDrag: (event) => + coords = + lat: event.latLng.lat(), + long: event.latLng.lng() + @_getNewForm(coords).then (data, textStatus) => + @_updateForm(coords, data, textStatus) _getNewForm: (coords) => $.ajax @@ -67,9 +82,12 @@ class Refuge.Restrooms.NewRestroomForm restroom: latitude: coords.lat longitude: coords.long - success: (data, textStatus) -> - # $('.new-restrooms-form-container').html(data) + _updateForm: (coords, data, textStatus) => + $('.form-container').html(data).hide().fadeIn() + @_rebind() + @_requestNearbyRestrooms(coords) + @_updateMap(coords) _requestNearbyRestrooms: (coords) -> $.ajax diff --git a/app/assets/stylesheets/components/common.scss b/app/assets/stylesheets/components/common.scss index 2aef84b5..d2c536e6 100644 --- a/app/assets/stylesheets/components/common.scss +++ b/app/assets/stylesheets/components/common.scss @@ -26,6 +26,10 @@ input { border-radius: 0; } +.form-control{ + color: black; +} + h1 { padding: 5px; text-align: center; diff --git a/app/models/restroom.rb b/app/models/restroom.rb index 446dd6e6..e26d6ca9 100644 --- a/app/models/restroom.rb +++ b/app/models/restroom.rb @@ -28,7 +28,7 @@ class Restroom < ApplicationRecord obj.street = geo.address.split(',').first obj.city = geo.city obj.state = geo.state - obj.country = geo.country + obj.country = geo.country_code end end