##// END OF EJS Templates
Fixed #208: Issue list does not scroll up when you click next...
Jean-Philippe Lang -
r1100:0beafecd3bd4
parent child
Show More
@@ -1,444 +1,450
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module ApplicationHelper
19 19 include Redmine::WikiFormatting::Macros::Definitions
20 20
21 21 def current_role
22 22 @current_role ||= User.current.role_for_project(@project)
23 23 end
24 24
25 25 # Return true if user is authorized for controller/action, otherwise false
26 26 def authorize_for(controller, action)
27 27 User.current.allowed_to?({:controller => controller, :action => action}, @project)
28 28 end
29 29
30 30 # Display a link if user is authorized
31 31 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
32 32 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
33 33 end
34 34
35 35 def link_to_signin
36 36 link_to l(:label_login), { :controller => 'account', :action => 'login' }, :class => 'signin'
37 37 end
38 38
39 39 def link_to_signout
40 40 link_to l(:label_logout), { :controller => 'account', :action => 'logout' }, :class => 'logout'
41 41 end
42 42
43 43 # Display a link to user's account page
44 44 def link_to_user(user)
45 45 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
46 46 end
47 47
48 48 def link_to_issue(issue)
49 49 link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue
50 50 end
51 51
52 52 def toggle_link(name, id, options={})
53 53 onclick = "Element.toggle('#{id}'); "
54 54 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
55 55 onclick << "return false;"
56 56 link_to(name, "#", :onclick => onclick)
57 57 end
58 58
59 59 def show_and_goto_link(name, id, options={})
60 60 onclick = "Element.show('#{id}'); "
61 61 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
62 62 onclick << "location.href='##{id}-anchor'; "
63 63 onclick << "return false;"
64 64 link_to(name, "#", options.merge(:onclick => onclick))
65 65 end
66 66
67 67 def image_to_function(name, function, html_options = {})
68 68 html_options.symbolize_keys!
69 69 tag(:input, html_options.merge({
70 70 :type => "image", :src => image_path(name),
71 71 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
72 72 }))
73 73 end
74 74
75 75 def prompt_to_remote(name, text, param, url, html_options = {})
76 76 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
77 77 link_to name, {}, html_options
78 78 end
79 79
80 80 def format_date(date)
81 81 return nil unless date
82 82 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
83 83 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
84 84 date.strftime(@date_format)
85 85 end
86 86
87 87 def format_time(time, include_date = true)
88 88 return nil unless time
89 89 time = time.to_time if time.is_a?(String)
90 90 zone = User.current.time_zone
91 91 if time.utc?
92 92 local = zone ? zone.adjust(time) : time.getlocal
93 93 else
94 94 local = zone ? zone.adjust(time.getutc) : time
95 95 end
96 96 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
97 97 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
98 98 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
99 99 end
100 100
101 101 def authoring(created, author)
102 102 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
103 103 l(:label_added_time_by, author || 'Anonymous', time_tag)
104 104 end
105 105
106 106 def day_name(day)
107 107 l(:general_day_names).split(',')[day-1]
108 108 end
109 109
110 110 def month_name(month)
111 111 l(:actionview_datehelper_select_month_names).split(',')[month-1]
112 112 end
113 113
114 114 def pagination_links_full(paginator, count=nil, options={})
115 115 page_param = options.delete(:page_param) || :page
116 116 url_param = params.dup
117 117 # don't reuse params if filters are present
118 118 url_param.clear if url_param.has_key?(:set_filter)
119 119
120 120 html = ''
121 121 html << link_to_remote(('&#171; ' + l(:label_previous)),
122 {:update => "content", :url => url_param.merge(page_param => paginator.current.previous)},
122 {:update => 'content',
123 :url => url_param.merge(page_param => paginator.current.previous),
124 :complete => 'window.scrollTo(0,0)'},
123 125 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
124 126
125 127 html << (pagination_links_each(paginator, options) do |n|
126 128 link_to_remote(n.to_s,
127 {:url => {:params => url_param.merge(page_param => n)}, :update => 'content'},
129 {:url => {:params => url_param.merge(page_param => n)},
130 :update => 'content',
131 :complete => 'window.scrollTo(0,0)'},
128 132 {:href => url_for(:params => url_param.merge(page_param => n))})
129 133 end || '')
130 134
131 135 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
132 {:update => "content", :url => url_param.merge(page_param => paginator.current.next)},
136 {:update => 'content',
137 :url => url_param.merge(page_param => paginator.current.next),
138 :complete => 'window.scrollTo(0,0)'},
133 139 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
134 140
135 141 unless count.nil?
136 142 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
137 143 end
138 144
139 145 html
140 146 end
141 147
142 148 def per_page_links(selected=nil)
143 149 url_param = params.dup
144 150 url_param.clear if url_param.has_key?(:set_filter)
145 151
146 152 links = Setting.per_page_options_array.collect do |n|
147 153 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
148 154 {:href => url_for(url_param.merge(:per_page => n))})
149 155 end
150 156 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
151 157 end
152 158
153 159 def html_title(*args)
154 160 if args.empty?
155 161 title = []
156 162 title << @project.name if @project
157 163 title += @html_title if @html_title
158 164 title << Setting.app_title
159 165 title.compact.join(' - ')
160 166 else
161 167 @html_title ||= []
162 168 @html_title += args
163 169 end
164 170 end
165 171
166 172 def accesskey(s)
167 173 Redmine::AccessKeys.key_for s
168 174 end
169 175
170 176 # Formats text according to system settings.
171 177 # 2 ways to call this method:
172 178 # * with a String: textilizable(text, options)
173 179 # * with an object and one of its attribute: textilizable(issue, :description, options)
174 180 def textilizable(*args)
175 181 options = args.last.is_a?(Hash) ? args.pop : {}
176 182 case args.size
177 183 when 1
178 184 obj = nil
179 185 text = args.shift || ''
180 186 when 2
181 187 obj = args.shift
182 188 text = obj.send(args.shift)
183 189 else
184 190 raise ArgumentError, 'invalid arguments to textilizable'
185 191 end
186 192
187 193 # when using an image link, try to use an attachment, if possible
188 194 attachments = options[:attachments]
189 195 if attachments
190 196 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
191 197 style = $1
192 198 filename = $6
193 199 rf = Regexp.new(filename, Regexp::IGNORECASE)
194 200 # search for the picture in attachments
195 201 if found = attachments.detect { |att| att.filename =~ rf }
196 202 image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id
197 203 "!#{style}#{image_url}!"
198 204 else
199 205 "!#{style}#{filename}!"
200 206 end
201 207 end
202 208 end
203 209
204 210 text = (Setting.text_formatting == 'textile') ?
205 211 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
206 212 simple_format(auto_link(h(text)))
207 213
208 214 # different methods for formatting wiki links
209 215 case options[:wiki_links]
210 216 when :local
211 217 # used for local links to html files
212 218 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
213 219 when :anchor
214 220 # used for single-file wiki export
215 221 format_wiki_link = Proc.new {|project, title| "##{title}" }
216 222 else
217 223 format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title }
218 224 end
219 225
220 226 project = options[:project] || @project
221 227
222 228 # Wiki links
223 229 #
224 230 # Examples:
225 231 # [[mypage]]
226 232 # [[mypage|mytext]]
227 233 # wiki links can refer other project wikis, using project name or identifier:
228 234 # [[project:]] -> wiki starting page
229 235 # [[project:|mytext]]
230 236 # [[project:mypage]]
231 237 # [[project:mypage|mytext]]
232 238 text = text.gsub(/(!)?(\[\[([^\]\|]+)(\|([^\]\|]+))?\]\])/) do |m|
233 239 link_project = project
234 240 esc, all, page, title = $1, $2, $3, $5
235 241 if esc.nil?
236 242 if page =~ /^([^\:]+)\:(.*)$/
237 243 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
238 244 page = $2
239 245 title ||= $1 if page.blank?
240 246 end
241 247
242 248 if link_project && link_project.wiki
243 249 # check if page exists
244 250 wiki_page = link_project.wiki.find_page(page)
245 251 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
246 252 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
247 253 else
248 254 # project or wiki doesn't exist
249 255 title || page
250 256 end
251 257 else
252 258 all
253 259 end
254 260 end
255 261
256 262 # Redmine links
257 263 #
258 264 # Examples:
259 265 # Issues:
260 266 # #52 -> Link to issue #52
261 267 # Changesets:
262 268 # r52 -> Link to revision 52
263 269 # Documents:
264 270 # document#17 -> Link to document with id 17
265 271 # document:Greetings -> Link to the document with title "Greetings"
266 272 # document:"Some document" -> Link to the document with title "Some document"
267 273 # Versions:
268 274 # version#3 -> Link to version with id 3
269 275 # version:1.0.0 -> Link to version named "1.0.0"
270 276 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
271 277 # Attachments:
272 278 # attachment:file.zip -> Link to the attachment of the current object named file.zip
273 279 text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version)?((#|r)(\d+)|(:)([^"][^\s<>]+|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m|
274 280 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
275 281 link = nil
276 282 if esc.nil?
277 283 if prefix.nil? && sep == 'r'
278 284 if project && (changeset = project.changesets.find_by_revision(oid))
279 285 link = link_to("r#{oid}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => oid}, :class => 'changeset',
280 286 :title => truncate(changeset.comments, 100))
281 287 end
282 288 elsif sep == '#'
283 289 oid = oid.to_i
284 290 case prefix
285 291 when nil
286 292 if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
287 293 link = link_to("##{oid}", {:controller => 'issues', :action => 'show', :id => oid}, :class => 'issue',
288 294 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
289 295 link = content_tag('del', link) if issue.closed?
290 296 end
291 297 when 'document'
292 298 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
293 299 link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document'
294 300 end
295 301 when 'version'
296 302 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
297 303 link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version'
298 304 end
299 305 end
300 306 elsif sep == ':'
301 307 # removes the double quotes if any
302 308 name = oid.gsub(%r{^"(.*)"$}, "\\1")
303 309 case prefix
304 310 when 'document'
305 311 if project && document = project.documents.find_by_title(name)
306 312 link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document'
307 313 end
308 314 when 'version'
309 315 if project && version = project.versions.find_by_name(name)
310 316 link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version'
311 317 end
312 318 when 'attachment'
313 319 if attachments && attachment = attachments.detect {|a| a.filename == name }
314 320 link = link_to h(attachment.filename), {:controller => 'attachments', :action => 'download', :id => attachment}, :class => 'attachment'
315 321 end
316 322 end
317 323 end
318 324 end
319 325 leading + (link || "#{prefix}#{sep}#{oid}")
320 326 end
321 327
322 328 text
323 329 end
324 330
325 331 # Same as Rails' simple_format helper without using paragraphs
326 332 def simple_format_without_paragraph(text)
327 333 text.to_s.
328 334 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
329 335 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
330 336 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
331 337 end
332 338
333 339 def error_messages_for(object_name, options = {})
334 340 options = options.symbolize_keys
335 341 object = instance_variable_get("@#{object_name}")
336 342 if object && !object.errors.empty?
337 343 # build full_messages here with controller current language
338 344 full_messages = []
339 345 object.errors.each do |attr, msg|
340 346 next if msg.nil?
341 347 msg = msg.first if msg.is_a? Array
342 348 if attr == "base"
343 349 full_messages << l(msg)
344 350 else
345 351 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
346 352 end
347 353 end
348 354 # retrieve custom values error messages
349 355 if object.errors[:custom_values]
350 356 object.custom_values.each do |v|
351 357 v.errors.each do |attr, msg|
352 358 next if msg.nil?
353 359 msg = msg.first if msg.is_a? Array
354 360 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
355 361 end
356 362 end
357 363 end
358 364 content_tag("div",
359 365 content_tag(
360 366 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
361 367 ) +
362 368 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
363 369 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
364 370 )
365 371 else
366 372 ""
367 373 end
368 374 end
369 375
370 376 def lang_options_for_select(blank=true)
371 377 (blank ? [["(auto)", ""]] : []) +
372 378 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
373 379 end
374 380
375 381 def label_tag_for(name, option_tags = nil, options = {})
376 382 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
377 383 content_tag("label", label_text)
378 384 end
379 385
380 386 def labelled_tabular_form_for(name, object, options, &proc)
381 387 options[:html] ||= {}
382 388 options[:html].store :class, "tabular"
383 389 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
384 390 end
385 391
386 392 def check_all_links(form_name)
387 393 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
388 394 " | " +
389 395 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
390 396 end
391 397
392 398 def progress_bar(pcts, options={})
393 399 pcts = [pcts, pcts] unless pcts.is_a?(Array)
394 400 pcts[1] = pcts[1] - pcts[0]
395 401 pcts << (100 - pcts[1] - pcts[0])
396 402 width = options[:width] || '100px;'
397 403 legend = options[:legend] || ''
398 404 content_tag('table',
399 405 content_tag('tr',
400 406 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
401 407 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
402 408 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
403 409 ), :class => 'progress', :style => "width: #{width};") +
404 410 content_tag('p', legend, :class => 'pourcent')
405 411 end
406 412
407 413 def context_menu_link(name, url, options={})
408 414 options[:class] ||= ''
409 415 if options.delete(:selected)
410 416 options[:class] << ' icon-checked disabled'
411 417 options[:disabled] = true
412 418 end
413 419 if options.delete(:disabled)
414 420 options.delete(:method)
415 421 options.delete(:confirm)
416 422 options.delete(:onclick)
417 423 options[:class] << ' disabled'
418 424 url = '#'
419 425 end
420 426 link_to name, url, options
421 427 end
422 428
423 429 def calendar_for(field_id)
424 430 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
425 431 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
426 432 end
427 433
428 434 def wikitoolbar_for(field_id)
429 435 return '' unless Setting.text_formatting == 'textile'
430 436 javascript_include_tag('jstoolbar/jstoolbar') +
431 437 javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") +
432 438 javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();")
433 439 end
434 440
435 441 def content_for(name, content = nil, &block)
436 442 @has_content ||= {}
437 443 @has_content[name] = true
438 444 super(name, content, &block)
439 445 end
440 446
441 447 def has_content?(name)
442 448 (@has_content && @has_content[name]) || false
443 449 end
444 450 end
General Comments 0
You need to be logged in to leave comments. Login now