Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

search_helper.rb 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2012 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. module SearchHelper
  20. def highlight_tokens(text, tokens)
  21. return text unless text && tokens && !tokens.empty?
  22. re_tokens = tokens.collect {|t| Regexp.escape(t)}
  23. regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
  24. result = ''
  25. text.split(regexp).each_with_index do |words, i|
  26. if result.length > 1200
  27. # maximum length of the preview reached
  28. result << '...'
  29. break
  30. end
  31. words = words.mb_chars
  32. if i.even?
  33. result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
  34. else
  35. t = (tokens.index(words.downcase) || 0) % 4
  36. result << content_tag('span', h(words), :class => "highlight token-#{t}")
  37. end
  38. end
  39. result.html_safe
  40. end
  41. def type_label(t)
  42. l("label_#{t.singularize}_plural", :default => t.to_s.humanize)
  43. end
  44. def project_select_tag
  45. options = [[l(:label_project_all), 'all']]
  46. options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty?
  47. options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
  48. options << [@project.name, ''] unless @project.nil?
  49. label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") +
  50. select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
  51. end
  52. def render_results_by_type(results_by_type)
  53. links = []
  54. # Sorts types by results count
  55. results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t|
  56. c = results_by_type[t]
  57. next if c == 0
  58. text = "#{type_label(t)} (#{c})"
  59. links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only],
  60. :all_words => params[:all_words], :scope => params[:scope], t => 1)
  61. end
  62. ('<ul>'.html_safe +
  63. links.map {|link| content_tag('li', link)}.join(' ').html_safe +
  64. '</ul>'.html_safe) unless links.empty?
  65. end
  66. end