From 88edb05ceb3092b4df5c0726f4e8a8d62fcc14e2 Mon Sep 17 00:00:00 2001 From: Anders Thomsen Date: Mon, 19 Nov 2018 22:02:41 +0100 Subject: [PATCH 1/2] show ascendant parent projects when filtering projects in the jump box --- app/controllers/projects_controller.rb | 2 +- app/helpers/application_helper.rb | 45 +++++++++++++++++++++++--- app/views/projects/autocomplete.js.erb | 2 +- public/stylesheets/application.css | 5 +++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 820aa6c81..25314d831 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -66,7 +66,7 @@ class ProjectsController < ApplicationController respond_to do |format| format.js { if params[:q].present? - @projects = Project.visible.like(params[:q]).to_a + @projects = Project.visible.to_a else @projects = User.current.projects.to_a end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 864a9d094..52c0df1c2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -406,13 +406,20 @@ module ApplicationHelper end end - def render_projects_for_jump_box(projects, selected=nil) + def render_projects_for_jump_box(projects, q, selected=nil) jump = params[:jump].presence || current_menu_item + options = {} + if !q.blank? + options[:q] = q.to_s + end s = ''.html_safe - project_tree(projects) do |project, level| + project_tree(projects, options) do |project, level, css| padding = level * 16 text = content_tag('span', project.name, :style => "padding-left:#{padding}px;") - s << link_to(text, project_path(project, :jump => jump), :title => project.name, :class => (project == selected ? 'selected' : nil)) + css_class = [] + css_class << 'selected' if project == selected + css_class << css if css + s << link_to(text, project_path(project, :jump => jump), :title => project.name, :class => css_class.any? ? css_class.join(' ') : nil) end s end @@ -431,7 +438,7 @@ module ApplicationHelper all = link_to(l(:label_project_all), projects_path(:jump => current_menu_item), :class => (@project.nil? && controller.class.main_menu ? 'selected' : nil)) content = content_tag('div', content_tag('div', q, :class => 'quick-search') + - content_tag('div', render_projects_for_jump_box(projects, @project), :class => 'drdn-items projects selection') + + content_tag('div', render_projects_for_jump_box(projects, nil, @project), :class => 'drdn-items projects selection') + content_tag('div', all, :class => 'drdn-items all-projects selection'), :class => 'drdn-content' ) @@ -465,7 +472,35 @@ module ApplicationHelper # # Wrapper for Project#project_tree def project_tree(projects, options={}, &block) - Project.project_tree(projects, options, &block) + if options[:q].blank? + Project.project_tree(projects, options, &block) + else + project_tree_filtered(projects, options, options[:q], &block) + end + end + + # Like project_tree, but filters the tree to projects (and ascendants) containing a string + def project_tree_filtered(projects, options={}, q, &block) + q = q.downcase + match_enum = projects.sort_by(&:lft).select do |project| + project.identifier.include?(q) || project.name.downcase.include?(q) + end.each + + begin + next_match = match_enum.next + Project.project_tree(projects, options) do |project, level| + # make sure next match is not behind the current node + while next_match.lft < project.lft + next_match = match_enum.next + end + + if next_match.is_or_is_descendant_of?(project) + yield project, level, next_match == project ? "match" : "matches-child" + end + end + rescue StopIteration + # no more matches + end end def principals_check_box_tags(name, principals) diff --git a/app/views/projects/autocomplete.js.erb b/app/views/projects/autocomplete.js.erb index 03f7fa640..cbfa9c470 100644 --- a/app/views/projects/autocomplete.js.erb +++ b/app/views/projects/autocomplete.js.erb @@ -1,7 +1,7 @@ <% s = '' if @projects.any? - s = render_projects_for_jump_box(@projects) + s = render_projects_for_jump_box(@projects, params[:q]) elsif params[:q].present? s = content_tag('span', l(:label_no_data)) end diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 19fb90f58..62c94770e 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -200,6 +200,11 @@ div + .drdn-items {border-top:1px solid #ccc;} .drdn-items.selection>*.selected:before { content:"\2713 "; } + +.drdn-items.selection>*.matches-child { + font-weight: lighter; +} + .drdn-items.selection:empty { border: none; } -- 2.19.2