Module: Backend::MapsHelper

Defined in:
app/helpers/backend/maps_helper.rb

Instance Method Summary collapse

Instance Method Details

#collection_map(data, options = {}, &_block) ⇒ Object

Build a map with a given list of object


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/backend/maps_helper.rb', line 145

def collection_map(data, options = {}, &_block)
  return nil unless data.any?
  backgrounds = options.delete(:backgrounds) || []
  options = {
    controls: {
      zoom: true,
      scale: true,
      fullscreen: true,
      layer_selector: true
    }
  }.deep_merge(options)
  if options.delete(:main)
    options[:box] ||= {}
    options[:box][:height] = '100%'
  end
  visualization(options) do |v|
    backgrounds.each do |b|
      v.background(b)
    end
    v.serie :main, data
    if block_given?
      yield v
    else
      layer_options = options[:layer_options] || {}
      layer_options[:fill_color] = options[:color] if options[:color]
      v.simple options[:id] || :items, :main, layer_options
    end
  end
end

#importer_form(imports = []) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/backend/maps_helper.rb', line 57

def importer_form(imports = [])
  form_tag({ controller: '/backend/map_editors', action: :upload }, method: :post, multipart: true, remote: true, authenticity_token: true, data: { importer_form: 'true' }) do
    (:div, nil, id: 'alert', class: 'row alert-danger') +
      (:div, class: 'row') do
        imports.collect.with_index do |k, i|
          (:div, class: 'choice-padding') do
            radio_button_tag(:importer_format, k, (i == 0 ? true : false)) + label_tag("importer_format_#{k}".to_sym, k)
          end
        end.join.html_safe
      end + (:div, class: 'row') do
              file_field_tag(:import_file) + (:span, (:i), class: 'spinner-loading', data: { importer_spinner: 'true' })
            end
  end
end

#main_resources_map(options = {}, &block) ⇒ Object

Use model_map to 'resources' map from current controller


99
100
101
# File 'app/helpers/backend/maps_helper.rb', line 99

def main_resources_map(options = {}, &block)
  resources_map(options.deep_merge(main: true), &block)
end

#map(resources, options = {}, html_options = {}, &_block) ⇒ Object

Raw map for given resource


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/backend/maps_helper.rb', line 22

def map(resources, options = {}, html_options = {}, &_block)
  resources = [resources] unless resources.respond_to?(:each)

  global = nil
  options[:geometries] = resources.collect do |resource|
    hash = (block_given? ? yield(resource) : { name: resource.name, shape: resource.shape })
    hash[:url] ||= url_for(controller: "/backend/#{resource.class.name.tableize}", action: :show, id: resource.id)
    if hash[:shape]
      global = (global ? global.merge(hash[:shape]) : Charta.new_geometry(hash[:shape]))
      hash[:shape] = Charta.new_geometry(hash[:shape]).transform(:WGS84).to_json_object
    end
    hash
  end

  # Box
  options[:box] ||= {}
  options[:box][:height] ||= 480

  # View box
  if global
    options[:view] ||= {}
    options[:view][:bounding_box] = global.bounding_box.to_a
  end

  (:div, nil, html_options.merge(data: { map: options.jsonize_keys.to_json }))
end

#mini_map(resources, options = {}, html_options = {}, &block) ⇒ Object


49
50
51
52
53
54
55
# File 'app/helpers/backend/maps_helper.rb', line 49

def mini_map(resources, options = {}, html_options = {}, &block)
  options[:box] ||= {}
  options[:box] = { width: 300, height: 300 }.merge(options[:box])
  html_options[:class] ||= ''
  html_options[:class] << ' picture mini-map'
  map(resources, options, html_options, &block)
end

#model_map(records, options = {}, &block) ⇒ Object

A module map displays an ActiveRecord::Relation as map. Model must have a shape method and a name method. Methods are configurable through options (:shape_method and :label_method) the area unit could be given with options :area_unit


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/backend/maps_helper.rb', line 112

def model_map(records, options = {}, &block)
  return nil unless records.any?
  klass = records.first.class
  controller = klass.model_name.plural
  label_method = options.delete(:label_method) || :name
  shape_method = options.delete(:shape_method) || :shape
  popup = options.delete(:popup)
  options[:id] ||= klass.model_name.human
  data = records.map do |record|
    if popup.respond_to?(:call)
      feature = popup.call(record)
    elsif popup.is_a?(String)
      feature = render popup, object: record, resource: record
    else
      area_unit = options[:area_unit] || :hectare
      content = []
      # content << { label: klass.human_attribute_name(label_method), value: record.send(label_method) }
      content << { label: Nomen::Indicator.find(:net_surface_area).human_name,
                   value: record.net_surface_area.in(area_unit).round(3).l }
      content << (:div, class: 'btn-group') do
        link_to(:show.tl, { controller: controller, action: :show, id: record.id }, class: 'btn btn-default') +
          link_to(:edit.tl, { controller: controller, action: :edit, id: record.id }, class: 'btn btn-default')
      end
      feature = { popup: { content: content, header: true } }
    end
    feature[:name] ||= record.send(label_method)
    feature[:shape] ||= record.send(shape_method)
    feature
  end
  collection_map(data, options, &block)
end

#resources_map(options = {}, &block) ⇒ Object

Use model_map to 'resources' map from current controller


104
105
106
# File 'app/helpers/backend/maps_helper.rb', line 104

def resources_map(options = {}, &block)
  model_map(resource_model.where.not(id: nil), options, &block)
end

#shape_field_tag(name, value = nil, options = {}) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/backend/maps_helper.rb', line 72

def shape_field_tag(name, value = nil, options = {})
  geometry = Charta.new_geometry(value)
  box ||= {}
  options[:box] ||= {}
  options[:data][:map_editor] ||= {}
  if options[:data][:map_editor].key? :customClass
    box[:width] = options[:box][:width]
    box[:height] = options[:box][:height]
  else
    box[:width] = options[:box][:width] || 360
    box[:height] = options[:box][:height] || 240
  end
  # FIXME: map_editors options cannot be in data/map_editors because it's pleonastic
  options[:data][:map_editor][:controls] ||= {}
  options[:data][:map_editor][:controls][:importers] ||= { formats: [:gml, :kml, :geojson] }

  if options[:data][:map_editor][:controls].key? :importers
    options.deep_merge!(data: { map_editor: { controls: { importers: { content: importer_form(options[:data][:map_editor][:controls][:importers][:formats]) } } } })
  end

  options[:data][:map_editor][:back] ||= MapBackground.availables.collect(&:to_json_object)

  options.deep_merge!(data: { map_editor: { edit: geometry.to_json_object } }) unless value.nil?
  text_field_tag(name, value, options.deep_merge(data: { map_editor: { box: box.jsonize_keys } }))
end