annotate .svn/pristine/f3/f36627f1ceebd181333d428d96386397035bb171.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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