]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Update to rails 7.2.2.1
[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       available_locales = Locale.list(name_locales(object))
51
52       locale = available_locales.preferred(preferred_languages, :default => nil)
53
54       if object.tags.include? "name:#{locale}"
55         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name:#{locale}"].to_s), :id => tag.bdi(name)
56       elsif object.tags.include? "name"
57         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name"].to_s), :id => tag.bdi(name)
58       elsif object.tags.include? "ref"
59         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["ref"].to_s), :id => tag.bdi(name)
60       end
61     end
62
63     name
64   end
65
66   def printable_element_version(object)
67     t "printable_name.version", :version => object.version
68   end
69
70   def element_strikethrough(object, &)
71     if object.redacted? || !object.visible?
72       tag.s(&)
73     else
74       yield
75     end
76   end
77
78   def link_follow(object)
79     "nofollow" if object.tags.empty?
80   end
81
82   def type_and_paginated_count(type, pages, selected_page = pages.current_page)
83     if pages.page_count == 1
84       t ".#{type.pluralize}",
85         :count => pages.item_count
86     else
87       t ".#{type.pluralize}_paginated",
88         :x => selected_page.first_item,
89         :y => selected_page.last_item,
90         :count => pages.item_count
91     end
92   end
93
94   def sidebar_classic_pagination(pages, page_param)
95     max_width_for_default_padding = 35
96
97     width = 0
98     pagination_items(pages, {}).each do |(body)|
99       width += 2 # padding width
100       width += body.length
101     end
102     link_classes = ["page-link", { "px-1" => width > max_width_for_default_padding }]
103
104     tag.ul :class => "pagination pagination-sm mb-2" do
105       pagination_items(pages, {}).each do |body, page_or_class|
106         linked = !(page_or_class.is_a? String)
107         link = if linked
108                  link_to body, url_for(page_param => page_or_class.number), :class => link_classes, **yield(page_or_class)
109                else
110                  tag.span body, :class => link_classes
111                end
112         concat tag.li link, :class => ["page-item", { page_or_class => !linked }]
113       end
114     end
115   end
116
117   private
118
119   def name_locales(object)
120     object.tags.keys.map { |k| Regexp.last_match(1) if k =~ /^name:(.*)$/ }.flatten
121   end
122 end