]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Call paging heading helpers from partial
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def printable_name(object, version: false)
3     id = if object.id.is_a?(Array)
4            object.id[0]
5          else
6            object.id
7          end
8     name = t "printable_name.with_id", :id => id.to_s
9     name = t "printable_name.with_version", :id => name, :version => object.version.to_s if version
10
11     # don't look at object tags if redacted, so as to avoid giving
12     # away redacted version tag information.
13     unless object.redacted?
14       available_locales = Locale.list(name_locales(object))
15
16       locale = available_locales.preferred(preferred_languages, :default => nil)
17
18       if object.tags.include? "name:#{locale}"
19         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name:#{locale}"].to_s), :id => tag.bdi(name)
20       elsif object.tags.include? "name"
21         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["name"].to_s), :id => tag.bdi(name)
22       elsif object.tags.include? "ref"
23         name = t "printable_name.with_name_html", :name => tag.bdi(object.tags["ref"].to_s), :id => tag.bdi(name)
24       end
25     end
26
27     name
28   end
29
30   def link_class(type, object)
31     classes = [type]
32
33     if object.redacted?
34       classes << "deleted"
35     else
36       classes += icon_tags(object).flatten.map { |t| h(t) }
37       classes << "deleted" unless object.visible?
38     end
39
40     classes.join(" ")
41   end
42
43   def link_title(object)
44     if object.redacted?
45       ""
46     else
47       h(icon_tags(object).map { |k, v| "#{k}=#{v}" }.to_sentence)
48     end
49   end
50
51   def link_follow(object)
52     "nofollow" if object.tags.empty?
53   end
54
55   def type_and_paginated_count(type, pages)
56     if pages.page_count == 1
57       t "browse.changeset.#{type}",
58         :count => pages.item_count
59     else
60       t "browse.changeset.#{type}_paginated",
61         :x => pages.current_page.first_item,
62         :y => pages.current_page.last_item,
63         :count => pages.item_count
64     end
65   end
66
67   def sidebar_classic_pagination(pages, page_param)
68     max_width_for_default_padding = 35
69
70     width = 0
71     pagination_items(pages, {}).each do |body|
72       width += 2 # padding width
73       width += body.length
74     end
75     link_classes = ["page-link", { "px-1" => width > max_width_for_default_padding }]
76
77     tag.ul :class => "pagination pagination-sm mb-1 ms-auto" do
78       pagination_items(pages, {}).each do |body, n|
79         linked = !(n.is_a? String)
80         link = if linked
81                  link_to body, url_for(page_param => n), :class => link_classes
82                else
83                  tag.span body, :class => link_classes
84                end
85         concat tag.li link, :class => ["page-item", { n => !linked }]
86       end
87     end
88   end
89
90   private
91
92   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
93
94   def icon_tags(object)
95     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
96   end
97
98   def name_locales(object)
99     object.tags.keys.map { |k| Regexp.last_match(1) if k =~ /^name:(.*)$/ }.flatten
100   end
101 end