Files
nairobi/app/components/shared/map_location_component.rb
Matheus Miranda de13e789dd Add polygon geographies to Budgets' map
Note that in the budgets wizard test we now create district with no
associated geozone, so the text "all city" will appear in the districts
table too, meaning we can't use `within "section", text: "All city" do`
anymore since it would result in an ambiguous match.

Co-Authored-By: Julian Herrero <microweb10@gmail.com>
Co-Authored-By: Javi Martín <javim@elretirao.net>
2023-05-31 16:56:15 +02:00

81 lines
2.1 KiB
Ruby

class Shared::MapLocationComponent < ApplicationComponent
attr_reader :investments_coordinates, :form, :geozones_data
def initialize(map_location, investments_coordinates: nil, form: nil, geozones_data: nil)
@map_location = map_location
@investments_coordinates = investments_coordinates
@form = form
@geozones_data = geozones_data
end
def map_location
@map_location ||= MapLocation.new
end
private
def editable?
form.present?
end
def latitude
map_location.latitude.presence || Setting["map.latitude"]
end
def longitude
map_location.longitude.presence || Setting["map.longitude"]
end
def zoom
map_location.zoom.presence || Setting["map.zoom"]
end
def remove_marker_label
t("proposals.form.map_remove_marker")
end
def remove_marker_id
"remove-marker-#{dom_id(map_location)}"
end
def remove_marker
button_tag remove_marker_label,
id: remove_marker_id,
class: "map-location-remove-marker",
type: "button"
end
def data
{
map: "",
map_center_latitude: latitude,
map_center_longitude: longitude,
map_zoom: zoom,
map_tiles_provider: Rails.application.secrets.map_tiles_provider,
map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution,
marker_editable: editable?,
marker_remove_selector: "##{remove_marker_id}",
marker_investments_coordinates: investments_coordinates,
marker_latitude: map_location.latitude.presence,
marker_longitude: map_location.longitude.presence,
geozones: geozones_data
}.merge(input_selectors)
end
def input_selectors
if form
{
latitude_input_selector: "##{input_id(:latitude)}",
longitude_input_selector: "##{input_id(:longitude)}",
zoom_input_selector: "##{input_id(:zoom)}"
}
else
{}
end
end
def input_id(attribute)
form.hidden_field(attribute).match(/ id="([^"]+)"/)[1]
end
end