To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / helpers / search_helper.rb @ 912:5e80956cc792
History | View | Annotate | Download (2.73 KB)
| 1 | 909:cbb26bc654de | Chris | # encoding: utf-8
|
|---|---|---|---|
| 2 | #
|
||
| 3 | 507:0c939c159af4 | Chris | # Redmine - project management software
|
| 4 | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
||
| 5 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 11 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 16 | 0:513646585e45 | Chris | # 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 | |||
| 20 | module SearchHelper |
||
| 21 | def highlight_tokens(text, tokens) |
||
| 22 | return text unless text && tokens && !tokens.empty? |
||
| 23 | re_tokens = tokens.collect {|t| Regexp.escape(t)}
|
||
| 24 | 909:cbb26bc654de | Chris | regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE |
| 25 | 0:513646585e45 | Chris | result = ''
|
| 26 | text.split(regexp).each_with_index do |words, i|
|
||
| 27 | if result.length > 1200 |
||
| 28 | # maximum length of the preview reached
|
||
| 29 | result << '...'
|
||
| 30 | break
|
||
| 31 | end
|
||
| 32 | words = words.mb_chars |
||
| 33 | if i.even?
|
||
| 34 | result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words) |
||
| 35 | else
|
||
| 36 | t = (tokens.index(words.downcase) || 0) % 4 |
||
| 37 | result << content_tag('span', h(words), :class => "highlight token-#{t}") |
||
| 38 | end
|
||
| 39 | end
|
||
| 40 | result |
||
| 41 | end
|
||
| 42 | 909:cbb26bc654de | Chris | |
| 43 | 0:513646585e45 | Chris | def type_label(t) |
| 44 | 37:94944d00e43c | chris | l("label_#{t.singularize}_plural", :default => t.to_s.humanize) |
| 45 | 0:513646585e45 | Chris | end
|
| 46 | 909:cbb26bc654de | Chris | |
| 47 | 0:513646585e45 | Chris | def project_select_tag |
| 48 | options = [[l(:label_project_all), 'all']] |
||
| 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? |
||
| 51 | options << [@project.name, ''] unless @project.nil? |
||
| 52 | 909:cbb26bc654de | Chris | label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") + |
| 53 | 0:513646585e45 | Chris | select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1 |
| 54 | end
|
||
| 55 | 909:cbb26bc654de | Chris | |
| 56 | 0:513646585e45 | Chris | def render_results_by_type(results_by_type) |
| 57 | links = [] |
||
| 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|
|
||
| 60 | c = results_by_type[t] |
||
| 61 | next if c == 0 |
||
| 62 | text = "#{type_label(t)} (#{c})"
|
||
| 63 | 909:cbb26bc654de | Chris | links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only], |
| 64 | :all_words => params[:all_words], :scope => params[:scope], t => 1) |
||
| 65 | 0:513646585e45 | Chris | end
|
| 66 | ('<ul>' + links.map {|link| content_tag('li', link)}.join(' ') + '</ul>') unless links.empty? |
||
| 67 | end
|
||
| 68 | end |