]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Improve the diary comments view
[rails.git] / app / helpers / application_helper.rb
1 module ApplicationHelper
2   require 'rexml/document'
3
4   def sanitize(text)
5     Sanitize.clean(text, Sanitize::Config::OSM).html_safe
6   end
7
8   def htmlize(text)
9     return linkify(sanitize(simple_format(text)))
10   end
11
12   def linkify(text)
13     if text.html_safe?
14       Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow")).html_safe
15     else
16       Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow"))
17     end
18   end
19
20   def html_escape_unicode(text)
21     chars = ActiveSupport::Multibyte::Unicode.u_unpack(text).map do |c|
22       c < 127 ? c.chr : "&##{c.to_s};"
23     end
24
25     return chars.join("")
26   end
27
28   def rss_link_to(*args)
29     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
30   end
31
32   def atom_link_to(*args)
33     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
34   end
35
36   def javascript_strings
37     js = ""
38
39     js << "<script type='text/javascript'>\n"
40     js << "i18n_strings = new Array();\n"
41     js << javascript_strings_for_key("javascripts")
42     js << "</script>\n"
43
44     return raw(js)
45   end
46
47   def style_rules
48     css = ""
49
50     css << ".hidden { display: none }";
51     css << ".hide_unless_logged_in { display: none }" unless @user;
52     css << ".hide_if_logged_in { display: none }" if @user;
53     css << ".hide_if_user_#{@user.id} { display: none }" if @user;
54     css << ".show_if_user_#{@user.id} { display: inline }" if @user;
55     css << ".hide_unless_administrator { display: none }" unless @user and @user.administrator?;
56
57     return content_tag(:style, css, :type => "text/css")
58   end
59
60   def if_logged_in(tag = :div, &block)
61     content_tag(tag, capture(&block), :class => "hide_unless_logged_in")
62   end
63
64   def if_not_logged_in(tag = :div, &block)
65     content_tag(tag, capture(&block), :class => "hide_if_logged_in")
66   end
67
68   def if_user(user, tag = :div, &block)
69     if user
70       content_tag(tag, capture(&block), :class => "hidden show_if_user_#{user.id}")
71     end
72   end
73
74   def unless_user(user, tag = :div, &block)
75     if user
76       content_tag(tag, capture(&block), :class => "hide_if_user_#{user.id}")
77     else
78       content_tag(tag, capture(&block))
79     end
80   end
81
82   def if_administrator(tag = :div, &block)
83     content_tag(tag, capture(&block), :class => "hide_unless_administrator")
84   end
85
86   def describe_location(lat, lon, zoom = nil, language = nil)
87     zoom = zoom || 14
88     language = language || request.user_preferred_languages.join(',')
89     url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
90
91     begin
92       response = OSM::Timer.timeout(4) do
93         REXML::Document.new(Net::HTTP.get(URI.parse(url)))
94       end
95     rescue Exception
96       response = nil
97     end
98
99     if response and result = response.get_text("reversegeocode/result")
100       result.to_s
101     else
102       "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
103     end
104   end
105
106   def user_image(user, options = {})
107     options[:class] ||= "user_image"
108
109     image_tag user.image.url(:large), options
110   end
111
112   def user_thumbnail(user, options = {})
113     options[:class] ||= "user_thumbnail"
114
115     image_tag user.image.url(:small), options
116   end
117
118   def preferred_editor
119     if params[:editor]
120       params[:editor]
121     elsif @user and @user.preferred_editor
122       @user.preferred_editor
123     else
124       DEFAULT_EDITOR
125     end
126   end
127
128 private
129
130   def javascript_strings_for_key(key)
131     js = ""
132     value = I18n.t(key, :locale => "en")
133
134     if value.is_a?(String)
135       js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
136     else
137       value.each_key do |k|
138         js << javascript_strings_for_key("#{key}.#{k}")
139       end
140     end
141
142     return js
143   end
144 end