Allow to render map without marker on new forms.

This commit is contained in:
Senén Rodero Rodríguez
2017-08-08 10:47:35 +02:00
parent dd7afd3593
commit 130e4533ac
9 changed files with 79 additions and 32 deletions

View File

@@ -8,8 +8,11 @@ App.Map =
App.Map.initializeMap map
initializeMap: (element) ->
latitude = $(element).data('marker-latitude')
longitude = $(element).data('marker-longitude')
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')
@@ -19,13 +22,33 @@ App.Map =
removeMarkerSelector = $(element).data('marker-remove-selector')
attribution = $(mapAttributionSelector)
editable = $(element).data('marker-editable')
marker_icon = L.divIcon(
marker = null;
markerIcon = L.divIcon(
iconSize: null
html: '<div class="map-marker"></div>')
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
map.on 'click', moveOrPlaceMarker

View File

@@ -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)

View File

@@ -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

View File

@@ -3,4 +3,8 @@ class MapLocation < ActiveRecord::Base
belongs_to :proposal
belongs_to :investment
def filled?
latitude.present? && longitude.present? && zoom.present?
end
end

View File

@@ -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

View File

@@ -2,6 +2,8 @@
<div class="small-12 column">
<div id="admin-map" class="map"
data-map
data-map-center-latitude="<%= Setting["map.latitude"] %>"
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"

View File

@@ -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? %>
<div class="document-link">
<%= text_with_links investment.external_url %>

View File

@@ -68,6 +68,10 @@
<%= safe_html_with_links @proposal.description %>
<% if feature?(:map) && map_location_available?(@proposal.map_location) %>
<%= render_map(@proposal.map_location, "proposal", false, nil) %>
<% end %>
<% if @proposal.external_url.present? %>
<div class="document-link">
<p>

View File

@@ -0,0 +1,9 @@
require 'rails_helper'
describe MapLocation do
context "#filled?" do
end
end