Class: Avo::Index::ResourceMapComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/avo/index/resource_map_component.rb

Overview

Render a map view for a list of resources, where each resource is expected to have an attribute/attribute set representing its location.

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Constructor Details

#initialize(resources: nil, resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, pagy: nil, query: nil) ⇒ ResourceMapComponent

Returns a new instance of ResourceMapComponent.



10
11
12
13
14
15
16
17
18
19
# File 'app/components/avo/index/resource_map_component.rb', line 10

def initialize(resources: nil, resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, pagy: nil, query: nil)
  super
  @resources = resources
  @resource = resource
  @reflection = reflection
  @parent_record = parent_record
  @parent_resource = parent_resource
  @pagy = pagy
  @query = query
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



8
9
10
# File 'app/components/avo/index/resource_map_component.rb', line 8

def resources
  @resources
end

Instance Method Details

#grid_layout_classesObject



21
22
23
24
25
26
27
28
29
# File 'app/components/avo/index/resource_map_component.rb', line 21

def grid_layout_classes
  return unless render_table?

  if table_positioned_horizontally
    "grid-flow-row sm:grid-flow-col grid-rows-1 auto-cols-fr"
  elsif table_positioned_vertically
    "grid-flow-row grid-cols-1"
  end
end

#map_component_order_classObject



39
40
41
42
43
44
45
# File 'app/components/avo/index/resource_map_component.rb', line 39

def map_component_order_class
  if render_table? && table_positioned_at_the_start
    "order-last"
  else
    "order-first"
  end
end

#map_view_table_layoutObject



59
60
61
# File 'app/components/avo/index/resource_map_component.rb', line 59

def map_view_table_layout
  map_options.dig(:table, :layout)
end

#render_table?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/components/avo/index/resource_map_component.rb', line 85

def render_table?
  map_options.dig(:table, :visible)
end

#resource_location_markersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/components/avo/index/resource_map_component.rb', line 63

def resource_location_markers
  # If we have no proc and no default location method, don't try to create markers
  return [] unless resource_mappable?

  records_markers = resources
    .map do |resource|
      Avo::ExecutionContext.new(target: marker_proc, record: resource.record).handle
    end
    .compact
    .filter do |coordinates|
      coordinates[:latitude].present? && coordinates[:longitude].present?
    end

  return records_markers if map_options[:extra_markers].nil?

  records_markers + Avo::ExecutionContext.new(target: map_options[:extra_markers]).handle
end

#resource_mapkick_optionsObject



81
82
83
# File 'app/components/avo/index/resource_map_component.rb', line 81

def resource_mapkick_options
  map_options[:mapkick_options] || {}
end

#table_component_order_classObject



47
48
49
50
51
52
53
# File 'app/components/avo/index/resource_map_component.rb', line 47

def table_component_order_class
  if table_positioned_at_the_start
    "order-first"
  else
    "order-last"
  end
end

#table_positioned_at_the_startObject



55
56
57
# File 'app/components/avo/index/resource_map_component.rb', line 55

def table_positioned_at_the_start
  %i[left top].include?(map_view_table_layout)
end

#table_positioned_horizontallyObject



31
32
33
# File 'app/components/avo/index/resource_map_component.rb', line 31

def table_positioned_horizontally
  %i[left right].include?(map_view_table_layout)
end

#table_positioned_verticallyObject



35
36
37
# File 'app/components/avo/index/resource_map_component.rb', line 35

def table_positioned_vertically
  %i[bottom top].include?(map_view_table_layout)
end