From 97bfc3f6012027077077ea4a6b5f2f709c76158e Mon Sep 17 00:00:00 2001 From: hellekin Date: Fri, 22 Jan 2021 10:35:07 +0100 Subject: Add markers and popups - Replace MakiMarkers with ExtraMarkers and ForkAwesome - Add HTML popup binding --- app/helpers/leaflet_helper.rb | 19 +++++++++++++++---- app/helpers/sections_helper.rb | 15 ++------------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'app/helpers') diff --git a/app/helpers/leaflet_helper.rb b/app/helpers/leaflet_helper.rb index 6ca03f3..67820d3 100644 --- a/app/helpers/leaflet_helper.rb +++ b/app/helpers/leaflet_helper.rb @@ -1,11 +1,22 @@ module LeafletHelper - def marker_for(resource, options = {}) - coords = [resource.latitude, resource.longitude] + def json_marker_for(resource, section) + # Style according to requested section + marker = resource.to_geojson + marker['style'] = { + classes: "cat#{section.category_id} sec#{section.id}", + color: section.category.color + } + marker['icon'] = { + name: 'fa-neuter' || section.icon_name || 'fa-circle', + shape: 'penta' # TODO: change shape according to marker state + } + # Render HTML popup + marker['popup'] = popup_for(resource) - "L.marker(#{coords}, #{options}).bindPopup(%s)" % popup_for(resource) + marker.to_json end def popup_for(resource) - render partial: 'resource/popup', locals: { resource: resource } + render partial: 'resources/popup', locals: { resource: resource }, formats: [:html] end end diff --git a/app/helpers/sections_helper.rb b/app/helpers/sections_helper.rb index 8180fee..20f602b 100644 --- a/app/helpers/sections_helper.rb +++ b/app/helpers/sections_helper.rb @@ -1,19 +1,8 @@ module SectionsHelper # Render a section as a GeoJSON FeatureCollection def geojson_feature_collection(section) - out = [] - class_name = { baseVal: "cat#{section.category_id} sec#{section.id}" } - style = { fill: section.category.color } + features = section.resources.map { |res| json_marker_for(res, section) }.join(',') - section.resources.each do |marker| - # Add styling - marker = marker.to_geojson - marker['className'] = class_name - marker['style'] = style - - out << marker.to_json - end - - raw("{ \"type\": \"FeatureCollection\", \"features\": [ #{out.join(',')} ] }") + raw("{ \"type\": \"FeatureCollection\", \"features\": [ #{features} ] }") end end -- cgit v1.2.3