You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_helper.rb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2016 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. if i.even?
  32. result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
  33. else
  34. t = (tokens.index(words.downcase) || 0) % 4
  35. result << content_tag('span', h(words), :class => "highlight token-#{t}")
  36. end
  37. end
  38. result.html_safe
  39. end
  40. def type_label(t)
  41. l("label_#{t.singularize}_plural", :default => t.to_s.humanize)
  42. end
  43. def project_select_tag
  44. options = [[l(:label_project_all), 'all']]
  45. options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty?
  46. options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
  47. options << [@project.name, ''] unless @project.nil?
  48. label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") +
  49. select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
  50. end
  51. def render_results_by_type(results_by_type)
  52. links = []
  53. # Sorts types by results count
  54. results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t|
  55. c = results_by_type[t]
  56. next if c == 0
  57. text = "#{type_label(t)} (#{c})"
  58. links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only],
  59. :all_words => params[:all_words], :scope => params[:scope], t => 1)
  60. end
  61. ('<ul>'.html_safe +
  62. links.map {|link| content_tag('li', link)}.join(' ').html_safe +
  63. '</ul>'.html_safe) unless links.empty?
  64. end
  65. end