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