To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / redmine_tags / lib / redmine_tags / patches / projects_helper_patch.rb @ 746:2ced57750157
History | View | Annotate | Download (3.48 KB)
| 1 |
module RedmineTags |
|---|---|
| 2 |
module Patches |
| 3 |
module ProjectsHelperPatch |
| 4 |
|
| 5 |
def self.included(base) # :nodoc: |
| 6 |
base.send(:include, InstanceMethods) |
| 7 |
base.class_eval do
|
| 8 |
unloadable |
| 9 |
end
|
| 10 |
end
|
| 11 |
|
| 12 |
module InstanceMethods |
| 13 |
# Renders a tree of projects as a nested set of unordered lists
|
| 14 |
# The given collection may be a subset of the whole project tree
|
| 15 |
# (eg. some intermediate nodes are private and can not be seen)
|
| 16 |
def render_project_hierarchy_with_filtering(projects,custom_fields,question) |
| 17 |
s = [] |
| 18 |
if projects.any?
|
| 19 |
tokens = RedmineProjectFiltering.calculate_tokens(question, custom_fields)
|
| 20 |
debugger |
| 21 |
|
| 22 |
|
| 23 |
ancestors = [] |
| 24 |
original_project = @project
|
| 25 |
projects.each do |project|
|
| 26 |
# set the project environment to please macros.
|
| 27 |
@project = project
|
| 28 |
if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
|
| 29 |
s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>"
|
| 30 |
else
|
| 31 |
ancestors.pop |
| 32 |
s << "</li>"
|
| 33 |
while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
|
| 34 |
ancestors.pop |
| 35 |
s << "</ul></li>"
|
| 36 |
end
|
| 37 |
end
|
| 38 |
classes = (ancestors.empty? ? 'root' : 'child') |
| 39 |
s << "<li class='#{classes}'><div class='#{classes}'>" +
|
| 40 |
link_to( highlight_tokens(project.name, tokens), |
| 41 |
{:controller => 'projects', :action => 'show', :id => project},
|
| 42 |
:class => "project #{User.current.member_of?(project) ? 'my-project' : nil}" |
| 43 |
) |
| 44 |
s << "<ul class='filter_fields'>"
|
| 45 |
|
| 46 |
# CustomField.usable_for_project_filtering.each do |field|
|
| 47 |
# value_model = project.custom_value_for(field.id)
|
| 48 |
# value = value_model.present? ? value_model.value : nil
|
| 49 |
# s << "<li><b>#{field.name.humanize}:</b> #{highlight_tokens(value, tokens)}</li>" if value.present?
|
| 50 |
# end
|
| 51 |
|
| 52 |
s << "</ul>"
|
| 53 |
s << "<div class='clear'></div>"
|
| 54 |
unless project.description.blank?
|
| 55 |
s << "<div class='wiki description'>"
|
| 56 |
s << "<b>#{ t(:field_description) }:</b>"
|
| 57 |
s << highlight_tokens(textilizable(project.short_description, :project => project), tokens)
|
| 58 |
s << "\n</div>"
|
| 59 |
end
|
| 60 |
s << "</div>"
|
| 61 |
ancestors << project |
| 62 |
end
|
| 63 |
ancestors.size.times{ s << "</li></ul>" }
|
| 64 |
@project = original_project
|
| 65 |
end
|
| 66 |
s.join "\n"
|
| 67 |
end
|
| 68 |
|
| 69 |
private |
| 70 |
|
| 71 |
# copied from search_helper. This one doesn't escape html or limit the text length
|
| 72 |
def highlight_tokens(text, tokens) |
| 73 |
return text unless text && tokens && !tokens.empty? |
| 74 |
re_tokens = tokens.collect {|t| Regexp.escape(t)}
|
| 75 |
regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE |
| 76 |
result = ''
|
| 77 |
text.split(regexp).each_with_index do |words, i|
|
| 78 |
words = words.mb_chars |
| 79 |
if i.even?
|
| 80 |
result << words |
| 81 |
else
|
| 82 |
t = (tokens.index(words.downcase) || 0) % 4 |
| 83 |
result << content_tag('span', words, :class => "highlight token-#{t}") |
| 84 |
end
|
| 85 |
end
|
| 86 |
result |
| 87 |
end
|
| 88 |
|
| 89 |
end
|
| 90 |
end
|
| 91 |
end
|
| 92 |
end
|
| 93 |
|