From 874a951dd27f5cc9060c1a3df9b6929918b9e994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Mon, 7 Aug 2017 11:59:25 +0200 Subject: [PATCH] Add map locations helper. Allow to configure map marker edition on map instantiation at views. --- app/assets/javascripts/map.js.coffee | 24 ++++++------ app/helpers/map_locations_helper.rb | 19 +++++++++ app/views/budgets/investments/_form.html.erb | 5 ++- app/views/map_locations/_form_fields.html.erb | 39 ++++++++++++------- app/views/proposals/_form.html.erb | 1 + config/locales/en/budgets.yml | 3 ++ config/locales/en/general.yml | 1 + config/locales/es/budgets.yml | 3 ++ config/locales/es/general.yml | 1 + 9 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 app/helpers/map_locations_helper.rb diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index 8aca94bc7..fb82d0800 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -8,16 +8,17 @@ App.Map = App.Map.initializeMap map initializeMap: (element) -> - latitude = $(element).data('latitude') - longitude = $(element).data('longitude') - zoom = $(element).data('zoom') - mapTilesProvider = $(element).data('tiles-provider') - mapAttributionSelector = $(element).data('tiles-attribution-selector') + 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('remove-marker-selector') + removeMarkerSelector = $(element).data('marker-remove-selector') attribution = $(mapAttributionSelector) + editable = $(element).data('marker-editable') marker_icon = L.divIcon( iconSize: null html: '
') @@ -48,11 +49,12 @@ App.Map = latLng = new (L.LatLng)(latitude, longitude) map = L.map(element.id).setView(latLng, zoom) - marker = L.marker(latLng, { icon: marker_icon, draggable: 'true' }) + marker = L.marker(latLng, { icon: marker_icon, draggable: editable }) L.tileLayer(mapTilesProvider, attribution: attribution.html()).addTo map marker.addTo(map) - $(removeMarkerSelector).on 'click', removeMarker - marker.on 'dragend', updateFormfields - map.on 'zoomend', updateFormfields - map.on 'click', placeMarker \ No newline at end of file + if editable + $(removeMarkerSelector).on 'click', removeMarker + marker.on 'dragend', updateFormfields + map.on 'zoomend', updateFormfields + map.on 'click', placeMarker \ No newline at end of file diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb new file mode 100644 index 000000000..08d16391f --- /dev/null +++ b/app/helpers/map_locations_helper.rb @@ -0,0 +1,19 @@ +module MapLocationsHelper + + def map_location_latitude(map_location) + map_location.present? && map_location.latitude.present? ? map_location.latitude : Setting["map.latitude"] + end + + def map_location_longitude(map_location) + map_location.present? && map_location.longitude.present? ? map_location.longitude : Setting["map.longitude"] + end + + def map_location_zoom(map_location) + map_location.present? && map_location.zoom.present? ? map_location.zoom : Setting["map.zoom"] + end + + def map_location_input_id(prefix, attribute) + "#{prefix}_map_location_attributes_#{attribute}" + end + +end \ No newline at end of file diff --git a/app/views/budgets/investments/_form.html.erb b/app/views/budgets/investments/_form.html.erb index 789d0f8a9..8a19c188a 100644 --- a/app/views/budgets/investments/_form.html.erb +++ b/app/views/budgets/investments/_form.html.erb @@ -35,8 +35,9 @@ <%= render 'map_locations/form_fields', form: f, map_location: @investment.map_location || MapLocation.new, - label: t("proposals.form.map_location"), - help: t("proposals.form.map_location_instructions"), + label: t("budgets.investments.form.map_location"), + help: t("budgets.investments.form.map_location_instructions"), + remove_marker_label: t("budgets.investments.form.map_remove_marker"), parent_class: "budget_investment" %> diff --git a/app/views/map_locations/_form_fields.html.erb b/app/views/map_locations/_form_fields.html.erb index 7c4a1785e..4fcefa2fa 100644 --- a/app/views/map_locations/_form_fields.html.erb +++ b/app/views/map_locations/_form_fields.html.erb @@ -1,25 +1,34 @@ <%= form.label :map_location, label %>

<%= help %>

-
" - data-longitude="<%= map_location.present? && map_location.longitude.present? ? map_location.longitude : Setting["map.longitude"] %>" - data-zoom="<%= map_location.present? && map_location.zoom.present? ? map_location.zoom : Setting["map.zoom"] %>" - data-tiles-attribution-selector="#map-location-attribution" - data-tiles-provider="//{s}.tile.osm.org/{z}/{x}/{y}.png" - data-latitude-input-selector="#<%= parent_class %>_map_location_attributes_latitude" - data-longitude-input-selector="#<%= parent_class %>_map_location_attributes_longitude" - data-zoom-input-selector="#<%= parent_class %>_map_location_attributes_zoom" - data-remove-marker-selector=".location-map-remove-marker-button"> + +
© OpenStreetMap contributors
-Remove marker +<%= remove_marker_label %> -<%= form.fields_for :map_location, map_location do |map_location_fields| %> - <%= map_location_fields.hidden_field :latitude, value: map_location.latitude, id: "#{parent_class}_map_location_attributes_latitude" %> - <%= map_location_fields.hidden_field :longitude, value: map_location.longitude, id: "#{parent_class}_map_location_attributes_longitude" %> - <%= map_location_fields.hidden_field :zoom, value: map_location.zoom, id: "#{parent_class}_map_location_attributes_zoom" %> +<%= form.fields_for :map_location, map_location do |m_l_fields| %> + <%= m_l_fields.hidden_field :latitude, + value: map_location.latitude, + id: map_location_input_id(parent_class, 'latitude') %> + <%= m_l_fields.hidden_field :longitude, + value: map_location.longitude, + id: map_location_input_id(parent_class, 'latitude') %> + <%= m_l_fields.hidden_field :zoom, + value: map_location.zoom, + id: map_location_input_id(parent_class, 'latitude') %> <% end %> \ No newline at end of file diff --git a/app/views/proposals/_form.html.erb b/app/views/proposals/_form.html.erb index 72feba9c3..2390b62f6 100644 --- a/app/views/proposals/_form.html.erb +++ b/app/views/proposals/_form.html.erb @@ -67,6 +67,7 @@ map_location: @proposal.map_location || MapLocation.new, label: t("proposals.form.map_location"), help: t("proposals.form.map_location_instructions"), + remove_marker_label: t("proposals.form.map_remove_marker"), parent_class: "proposal" %>
diff --git a/config/locales/en/budgets.yml b/config/locales/en/budgets.yml index c1879df16..1f29370bb 100644 --- a/config/locales/en/budgets.yml +++ b/config/locales/en/budgets.yml @@ -55,6 +55,9 @@ en: tags_instructions: "Tag this proposal. You can choose from proposed categories or add your own" tags_label: Tags tags_placeholder: "Enter the tags you would like to use, separated by commas (',')" + map_location: "Map location" + map_location_instructions: "Navigate the map to the location and place the marker." + map_remove_marker: "Remove map marker" index: title: Participatory budgeting unfeasible: Unfeasible investment projects diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index c118db367..290e1a278 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -335,6 +335,7 @@ en: tags_placeholder: "Enter the tags you would like to use, separated by commas (',')" map_location: "Map location" map_location_instructions: "Navigate the map to the location and place the marker." + map_remove_marker: "Remove map marker" index: featured_proposals: Featured filter_topic: diff --git a/config/locales/es/budgets.yml b/config/locales/es/budgets.yml index 78b81fbd3..5c76c8abe 100644 --- a/config/locales/es/budgets.yml +++ b/config/locales/es/budgets.yml @@ -55,6 +55,9 @@ es: tags_label: Temas tag_category_label: "Categorías" tags_placeholder: "Escribe las etiquetas que desees separadas por una coma (',')" + map_location: "Ubicación en el mapa" + map_location_instructions: "Navega por el mapa hasta la ubicación y coloca el marcador." + map_remove_marker: "Eliminar el marcador" index: title: Presupuestos participativos unfeasible: Propuestas de inversión no viables diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 46664e2ae..722c0dabb 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -335,6 +335,7 @@ es: tags_placeholder: "Escribe las etiquetas que desees separadas por una coma (',')" map_location: "Ubicación en el mapa" map_location_instructions: "Navega por el mapa hasta la ubicación y coloca el marcador." + map_remove_marker: "Eliminar el marcador" index: featured_proposals: Destacadas filter_topic: