diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index fb82d0800..33f13d25c 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -8,24 +8,47 @@ App.Map = App.Map.initializeMap map initializeMap: (element) -> - latitude = $(element).data('marker-latitude') - longitude = $(element).data('marker-longitude') - zoom = $(element).data('map-zoom') - mapTilesProvider = $(element).data('map-tiles-provider') - mapAttributionSelector = $(element).data('map-tiles-attribution-selector') - latitudeInputSelector = $(element).data('latitude-input-selector') - longitudeInputSelector = $(element).data('longitude-input-selector') - zoomInputSelector = $(element).data('zoom-input-selector') - removeMarkerSelector = $(element).data('marker-remove-selector') - attribution = $(mapAttributionSelector) - editable = $(element).data('marker-editable') - marker_icon = L.divIcon( + + mapCenterLatitude = $(element).data('map-center-latitude') + mapCenterLongitude = $(element).data('map-center-longitude') + markerLatitude = $(element).data('marker-latitude') + markerLongitude = $(element).data('marker-longitude') + zoom = $(element).data('map-zoom') + mapTilesProvider = $(element).data('map-tiles-provider') + mapAttributionSelector = $(element).data('map-tiles-attribution-selector') + latitudeInputSelector = $(element).data('latitude-input-selector') + longitudeInputSelector = $(element).data('longitude-input-selector') + zoomInputSelector = $(element).data('zoom-input-selector') + removeMarkerSelector = $(element).data('marker-remove-selector') + attribution = $(mapAttributionSelector) + editable = $(element).data('marker-editable') + marker = null; + markerIcon = L.divIcon( iconSize: null html: '
') - placeMarker = (e) -> - marker.setLatLng(e.latlng) + createMarker = (latitude, longitude) -> + markerLatLng = new (L.LatLng)(latitude, longitude) + marker = L.marker(markerLatLng, { icon: markerIcon, draggable: editable }) + if editable + marker.on 'dragend', updateFormfields marker.addTo(map) + return marker + + removeMarker = (e) -> + e.preventDefault() + if marker + map.removeLayer(marker) + marker = null; + clearFormfields() + return + + moveOrPlaceMarker = (e) -> + if marker + marker.setLatLng(e.latlng) + else + marker = createMarker(e.latlng.lat, e.latlng.lng) + updateFormfields() return @@ -41,20 +64,14 @@ App.Map = $(zoomInputSelector).val '' return - removeMarker = (e) -> - e.preventDefault() - map.removeLayer(marker) - clearFormfields() - return - - latLng = new (L.LatLng)(latitude, longitude) - map = L.map(element.id).setView(latLng, zoom) - marker = L.marker(latLng, { icon: marker_icon, draggable: editable }) + mapCenterLatLng = new (L.LatLng)(mapCenterLatitude, mapCenterLongitude) + map = L.map(element.id).setView(mapCenterLatLng, zoom) L.tileLayer(mapTilesProvider, attribution: attribution.html()).addTo map - marker.addTo(map) + + if markerLatitude && markerLongitude + marker = createMarker(markerLatitude, markerLongitude) if editable $(removeMarkerSelector).on 'click', removeMarker - marker.on 'dragend', updateFormfields map.on 'zoomend', updateFormfields - map.on 'click', placeMarker \ No newline at end of file + map.on 'click', moveOrPlaceMarker \ No newline at end of file diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb index 805d5504c..1df7a861c 100644 --- a/app/helpers/map_locations_helper.rb +++ b/app/helpers/map_locations_helper.rb @@ -1,5 +1,9 @@ module MapLocationsHelper + def map_location_available?(map_location) + map_location.present? && map_location.filled? + end + def map_location_latitude(map_location) map_location.present? && map_location.latitude.present? ? map_location.latitude : Setting["map.latitude"] end @@ -30,12 +34,14 @@ module MapLocationsHelper class: "map", data:{ map: "", + map_center_latitude: map_location_latitude(map_location), + map_center_longitude: map_location_longitude(map_location), map_zoom: map_location_zoom(map_location), map_tiles_attribution_selector: map_location_attribution_id(map_location), map_tiles_provider: "//{s}.tile.osm.org/{z}/{x}/{y}.png", marker_editable: editable, - marker_latitude: map_location_latitude(map_location), - marker_longitude: map_location_longitude(map_location), + marker_latitude: map_location.latitude, + marker_longitude: map_location.longitude, marker_remove_selector: "##{map_location_remove_marker_link_id(map_location)}", latitude_input_selector: "##{map_location_input_id(parent_class, 'latitude')}", longitude_input_selector: "##{map_location_input_id(parent_class, 'longitude')}", @@ -43,6 +49,7 @@ module MapLocationsHelper } map += map_attributtion(map_location) map += map_location_remove_marker(map_location, remove_marker_label) if editable + map end def map_attributtion(map_location, klass = nil) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index af8feac1d..e48bc2c57 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -27,8 +27,8 @@ class Budget has_many :valuators, through: :valuator_assignments has_many :comments, as: :commentable has_many :milestones - has_one :map_location - accepts_nested_attributes_for :map_location + has_one :map_location, dependent: :destroy + accepts_nested_attributes_for :map_location, allow_destroy: true validates :title, presence: true validates :author, presence: true diff --git a/app/models/map_location.rb b/app/models/map_location.rb index 61cb10893..8db4cda9c 100644 --- a/app/models/map_location.rb +++ b/app/models/map_location.rb @@ -3,4 +3,8 @@ class MapLocation < ActiveRecord::Base belongs_to :proposal belongs_to :investment + def filled? + latitude.present? && longitude.present? && zoom.present? + end + end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 7ad7a863b..b8d919e97 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -25,8 +25,8 @@ class Proposal < ActiveRecord::Base belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :geozone - has_one :map_location - accepts_nested_attributes_for :map_location + has_one :map_location, dependent: :destroy + accepts_nested_attributes_for :map_location, allow_destroy: true has_many :comments, as: :commentable has_many :proposal_notifications diff --git a/app/views/admin/settings/_map_form.html.erb b/app/views/admin/settings/_map_form.html.erb index 256bcd478..beac8e87b 100644 --- a/app/views/admin/settings/_map_form.html.erb +++ b/app/views/admin/settings/_map_form.html.erb @@ -2,6 +2,8 @@
" + data-map-center-longitude="<%= Setting["map.longitude"] %>" data-map-zoom="<%= Setting["map.zoom"] %>" data-map-tiles-attribution-selector="#admin-map-attribution" data-map-tiles-provider="//{s}.tile.osm.org/{z}/{x}/{y}.png" diff --git a/app/views/budgets/investments/_investment_show.html.erb b/app/views/budgets/investments/_investment_show.html.erb index e257de0bb..eab4de525 100644 --- a/app/views/budgets/investments/_investment_show.html.erb +++ b/app/views/budgets/investments/_investment_show.html.erb @@ -45,6 +45,10 @@ <%= safe_html_with_links investment.description.html_safe %> + <% if feature?(:map) && map_location_available?(@investment.map_location) %> + <%= render_map(@investment.map_location, "budget_investment", false, nil) %> + <% end %> + <% if investment.external_url.present? %>