]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Merge remote-tracking branch 'upstream/pull/6087'
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def element_icon(type, object)
3     selected_icon_data = { :filename => "#{type}.svg", :priority => 1 }
4
5     unless object.redacted?
6       target_tags = object.tags.find_all { |k, _v| BROWSE_ICONS.key? k }.sort
7       title = target_tags.map { |k, v| "#{k}=#{v}" }.to_sentence unless target_tags.empty?
8
9       target_tags.each do |k, v|
10         icon_data = BROWSE_ICONS[k][v] || BROWSE_ICONS[k][:*]
11         selected_icon_data = icon_data if icon_data && icon_data[:priority] > selected_icon_data[:priority]
12       end
13     end
14
15     image_tag "browse/#{selected_icon_data[:filename]}",
16               :size => 20,
17               :class => ["align-bottom object-fit-none browse-icon", { "browse-icon-invertible" => selected_icon_data[:invert] }],
18               :title => title
19   end
20
21   def element_single_current_link(type, object)
22     link_to object, { :rel => (link_follow(object) if type == "node") } do
23       element_strikethrough object do
24         printable_element_name object
25       end
26     end
27   end
28
29   def element_list_item(type, object, &)
30     tag.li(tag.div(element_icon(type, object) + tag.div(:class => "align-self-center", &), :class => "d-flex gap-1"))
31   end
32
33   def element_list_item_with_strikethrough(type, object, &)
34     element_list_item type, object do
35       element_strikethrough object, &
36     end
37   end
38
39   def printable_element_name(object)
40     id = if object.id.is_a?(Array)
41            object.id[0]
42          else
43            object.id
44          end
45     name = id.to_s
46
47     # don't look at object tags if redacted, so as to avoid giving
48     # away redacted version tag information.
49     unless object.redacted?
50       feature_name = feature_name(object.tags)
51       name = t "printable_name.with_name_html", :name => tag.bdi(feature_name), :id => tag.bdi(id.to_s) if feature_name.present?
52     end
53
54     name
55   end
56
57   def printable_element_version(object)
58     t "printable_name.version", :version => object.version
59   end
60
61   def element_strikethrough(object, &)
62     if object.redacted? || !object.visible?
63       tag.s(&)
64     else
65       yield
66     end
67   end
68
69   def link_follow(object)
70     "nofollow" if object.tags.empty?
71   end
72
73   def sidebar_classic_pagination(pages, page_param)
74     max_width_for_default_padding = 35
75
76     width = 0
77     pagination_items(pages, {}).each do |(body)|
78       width += 2 # padding width
79       width += body.length
80     end
81     link_classes = ["page-link", { "px-1" => width > max_width_for_default_padding }]
82
83     tag.ul :class => "pagination pagination-sm mb-2" do
84       pagination_items(pages, {}).each do |body, page_or_class|
85         linked = !(page_or_class.is_a? String)
86         link = if linked
87                  link_to body, url_for(page_param => page_or_class.number), :class => link_classes, **yield(page_or_class)
88                else
89                  tag.span body, :class => link_classes
90                end
91         concat tag.li link, :class => ["page-item", { page_or_class => !linked }]
92       end
93     end
94   end
95
96   private
97
98   def feature_name(tags)
99     locale_keys = preferred_languages.expand.map { |locale| "name:#{locale}" }
100
101     (locale_keys + %w[name ref addr:housename]).each do |key|
102       return tags[key] if tags[key]
103     end
104     # TODO: Localize format to country of address
105     return "#{tags['addr:housenumber']} #{tags['addr:street']}" if tags["addr:housenumber"] && tags["addr:street"]
106
107     nil
108   end
109 end