annotate .svn/pristine/35/353b4ea784cce222db202d9a47753a1bd3685b81.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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