Module: Blacklight::BlacklightMapsHelperBehavior

Included in:
BlacklightMapsHelper
Defined in:
app/helpers/blacklight/blacklight_maps_helper_behavior.rb

Instance Method Summary collapse

Instance Method Details

#blacklight_map_tag(id, tag_options = {}, &block) ⇒ Object

Parameters:

  • id (String)

    the html element id

  • tag_options (Hash) (defaults to: {})

    options to put on the tag



7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 7

def blacklight_map_tag(id, tag_options = {}, &block)
  maps_config = blacklight_config.view.maps
  default_data = {
    maxzoom: maps_config.maxzoom,
    tileurl: maps_config.tileurl,
    mapattribution: maps_config.mapattribution
  }
  options = { id: id, data: default_data }.deep_merge(tag_options)
  block_given? ? (:div, options, &block) : tag.div(**options)
end

create a link to a bbox spatial search

Parameters:

  • bbox (Array)


26
27
28
29
30
31
32
33
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 26

def link_to_bbox_search(bbox)
  bbox_coords = bbox.map(&:to_s)
  bbox_search_coords = "[#{bbox_coords[1]},#{bbox_coords[0]} TO #{bbox_coords[3]},#{bbox_coords[2]}]"
  link_to(t('blacklight.maps.interactions.bbox_search'),
          search_catalog_path(spatial_search_type: 'bbox',
                              coordinates: bbox_search_coords,
                              view: default_document_index_view_type))
end

create a link to a location name facet value

Parameters:

  • field_value (String)

    Solr field value

  • field (String)

    Solr field name

  • display_value (String) (defaults to: nil)

    value to display instead of field_value



39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 39

def link_to_placename_field(field_value, field, display_value = nil)
  new_params = if params[:f] && params[:f][field]&.include?(field_value)
                 search_state.params
               else
                 search_state.add_facet_params(field, field_value)
               end
  new_params[:view] = default_document_index_view_type
  new_params.except!(:id, :spatial_search_type, :coordinates, :controller, :action)
  link_to(display_value.presence || field_value, search_catalog_path(new_params))
end

create a link to a spatial search for a set of point coordinates

Parameters:

  • point_coords (Array)


52
53
54
55
56
57
58
59
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 52

def link_to_point_search(point_coords)
  new_params = params.except(:controller, :action, :view, :id, :spatial_search_type, :coordinates)
  new_params[:spatial_search_type] = 'point'
  new_params[:coordinates] = "#{point_coords[1]},#{point_coords[0]}"
  new_params[:view] = default_document_index_view_type
  new_params.permit!
  link_to(t('blacklight.maps.interactions.point_search'), search_catalog_path(new_params))
end

#placename_value(geojson_hash) ⇒ Object

return the placename value to be used as a link

Parameters:

  • geojson_hash (Hash)


20
21
22
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 20

def placename_value(geojson_hash)
  geojson_hash[:properties][blacklight_config.view.maps.placename_property.to_sym]
end

#render_index_mapviewObject

render the map for #index and #map views



68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 68

def render_index_mapview
  maps_config = blacklight_config.view.maps
  map_facet_field = if maps_config.facet_mode == 'coordinates'
                      maps_config.coordinates_facet_field
                    else
                      maps_config.geojson_field
                    end
  map_facet_values = @response.aggregations[map_facet_field]&.items || []
  render partial: 'catalog/index_mapview',
         locals: { geojson_features: serialize_geojson(map_facet_values) }
end

#render_placename_heading(geojson_hash) ⇒ Object

render the location name for the Leaflet popup

Parameters:

  • geojson_hash (Hash)


63
64
65
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 63

def render_placename_heading(geojson_hash)
  geojson_hash[:properties][blacklight_config.view.maps.placename_property.to_sym]
end

determine the type of spatial search to use based on coordinates (bbox or point)

Parameters:

  • coords (Array)


82
83
84
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 82

def render_spatial_search_link(coords)
  coords.length == 4 ? link_to_bbox_search(coords) : link_to_point_search(coords)
end

#serialize_geojson(documents, options = {}) ⇒ Object

pass the document or facet values to BlacklightMaps::GeojsonExport

Parameters:

  • documents (Array || SolrDocument)


88
89
90
91
92
93
94
# File 'app/helpers/blacklight/blacklight_maps_helper_behavior.rb', line 88

def serialize_geojson(documents, options = {})
  export = BlacklightMaps::GeojsonExport.new(controller,
                                             action_name.to_sym,
                                             documents,
                                             options)
  export.to_geojson
end