]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Merge pull request #5295 from tomhughes/danger-branches
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def element_single_current_link(type, object)
3     link_to object, { :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, &)
48     if object.redacted? || !object.visible?
49       tag.s(&)
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, selected_page = pages.current_page)
74     if pages.page_count == 1
75       t ".#{type.pluralize}",
76         :count => pages.item_count
77     else
78       t ".#{type.pluralize}_paginated",
79         :x => selected_page.first_item,
80         :y => selected_page.last_item,
81         :count => pages.item_count
82     end
83   end
84
85   def sidebar_classic_pagination(pages, page_param)
86     max_width_for_default_padding = 35
87
88     width = 0
89     pagination_items(pages, {}).each do |(body)|
90       width += 2 # padding width
91       width += body.length
92     end
93     link_classes = ["page-link", { "px-1" => width > max_width_for_default_padding }]
94
95     tag.ul :class => "pagination pagination-sm mb-2" do
96       pagination_items(pages, {}).each do |body, page_or_class|
97         linked = !(page_or_class.is_a? String)
98         link = if linked
99                  link_to body, url_for(page_param => page_or_class.number), :class => link_classes, **yield(page_or_class)
100                else
101                  tag.span body, :class => link_classes
102                end
103         concat tag.li link, :class => ["page-item", { page_or_class => !linked }]
104       end
105     end
106   end
107
108   private
109
110   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural office railway shop tourism waterway].freeze
111
112   def icon_tags(object)
113     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
114   end
115
116   def name_locales(object)
117     object.tags.keys.map { |k| Regexp.last_match(1) if k =~ /^name:(.*)$/ }.flatten
118   end
119 end