]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Bump eslint from 8.56.0 to 8.57.0
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def element_single_current_link(type, object, url)
3     link_to url, { :class => element_class(type, object), :title => element_title(object), :rel => (link_follow(object) if type == "node") } do
4       element_strikethrough object do
5         printable_element_name object
6       end
7     end
8   end
9
10   def element_list_item(type, object, &block)
11     tag.li :class => element_class(type, object), :title => element_title(object) do
12       element_strikethrough object, &block
13     end
14   end
15
16   def printable_element_name(object)
17     id = if object.id.is_a?(Array)
18            object.id[0]
19          else
20            object.id
21          end
22     name = id.to_s
23
24     # don't look at object tags if redacted, so as to avoid giving
25     # away redacted version tag information.
26     unless object.redacted?
27       available_locales = Locale.list(name_locales(object))
28
29       locale = available_locales.preferred(preferred_languages, :default => nil)
30
31       if object.tags.include? "name:#{locale}"
32         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name:#{locale}"].to_s), :id => tag.bdi(name)
33       elsif object.tags.include? "name"
34         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name"].to_s), :id => tag.bdi(name)
35       elsif object.tags.include? "ref"
36         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["ref"].to_s), :id => tag.bdi(name)
37       end
38     end
39
40     name
41   end
42
43   def printable_element_version(object)
44     t "printable_name.version", :version => object.version
45   end
46
47   def element_strikethrough(object, &block)
48     if object.redacted? || !object.visible?
49       tag.s(&block)
50     else
51       yield
52     end
53   end
54
55   def element_class(type, object)
56     classes = [type]
57     classes += icon_tags(object).flatten.map { |t| h(t) } unless object.redacted?
58     classes.join(" ")
59   end
60
61   def element_title(object)
62     if object.redacted?
63       ""
64     else
65       h(icon_tags(object).map { |k, v| "#{k}=#{v}" }.to_sentence)
66     end
67   end
68
69   def link_follow(object)
70     "nofollow" if object.tags.empty?
71   end
72
73   def type_and_paginated_count(type, pages)
74     if pages.page_count == 1
75       t "browse.changeset.#{type}",
76         :count => pages.item_count
77     else
78       t "browse.changeset.#{type}_paginated",
79         :x => pages.current_page.first_item,
80         :y => pages.current_page.last_item,
81         :count => pages.item_count
82     end
83   end
84
85   private
86
87   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
88
89   def icon_tags(object)
90     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
91   end
92
93   def name_locales(object)
94     object.tags.keys.map { |k| Regexp.last_match(1) if k =~ /^name:(.*)$/ }.flatten
95   end
96 end