BigW Consortium Gitlab

search_helper.rb 4.36 KB
Newer Older
1
module SearchHelper
2
  def search_autocomplete_opts(term)
3
    return unless current_user
4 5 6

    resources_results = [
      groups_autocomplete(term),
7
      projects_autocomplete(term)
8 9 10 11 12
    ].flatten

    generic_results = project_autocomplete + default_autocomplete + help_autocomplete
    generic_results.select! { |result| result[:label] =~ Regexp.new(term, "i") }

13
    [
14 15
      resources_results,
      generic_results
16 17
    ].flatten.uniq do |item|
      item[:label]
18
    end
19 20 21 22 23 24 25
  end

  private

  # Autocomplete results for various settings pages
  def default_autocomplete
    [
26 27 28
      { label: "Profile settings", url: profile_path },
      { label: "SSH Keys",         url: profile_keys_path },
      { label: "Dashboard",        url: root_path },
29 30 31 32 33 34 35
      { label: "Admin Section",       url: admin_root_path },
    ]
  end

  # Autocomplete results for internal help pages
  def help_autocomplete
    [
36 37 38 39 40 41 42 43 44
      { label: "help: API Help",           url: help_page_path("api", "README") },
      { label: "help: Markdown Help",      url: help_page_path("markdown", "markdown") },
      { label: "help: Permissions Help",   url: help_page_path("permissions", "permissions") },
      { label: "help: Public Access Help", url: help_page_path("public_access", "public_access") },
      { label: "help: Rake Tasks Help",    url: help_page_path("raketasks", "README") },
      { label: "help: SSH Keys Help",      url: help_page_path("ssh", "README") },
      { label: "help: System Hooks Help",  url: help_page_path("system_hooks", "system_hooks") },
      { label: "help: Web Hooks Help",     url: help_page_path("web_hooks", "web_hooks") },
      { label: "help: Workflow Help",      url: help_page_path("workflow", "README") },
45 46 47 48 49 50
    ]
  end

  # Autocomplete results for the current project, if it's defined
  def project_autocomplete
    if @project && @project.repository.exists? && @project.repository.root_ref
51
      prefix = search_result_sanitize(@project.name_with_namespace)
52 53 54
      ref    = @ref || @project.repository.root_ref

      [
Vinnie Okada committed
55 56 57 58 59 60 61 62
        { label: "#{prefix} - Files",          url: namespace_project_tree_path(@project.namespace, @project, ref) },
        { label: "#{prefix} - Commits",        url: namespace_project_commits_path(@project.namespace, @project, ref) },
        { label: "#{prefix} - Network",        url: namespace_project_network_path(@project.namespace, @project, ref) },
        { label: "#{prefix} - Graph",          url: namespace_project_graph_path(@project.namespace, @project, ref) },
        { label: "#{prefix} - Issues",         url: namespace_project_issues_path(@project.namespace, @project) },
        { label: "#{prefix} - Merge Requests", url: namespace_project_merge_requests_path(@project.namespace, @project) },
        { label: "#{prefix} - Milestones",     url: namespace_project_milestones_path(@project.namespace, @project) },
        { label: "#{prefix} - Snippets",       url: namespace_project_snippets_path(@project.namespace, @project) },
63
        { label: "#{prefix} - Members",        url: namespace_project_project_members_path(@project.namespace, @project) },
Vinnie Okada committed
64
        { label: "#{prefix} - Wiki",           url: namespace_project_wikis_path(@project.namespace, @project) },
65 66 67 68 69 70 71
      ]
    else
      []
    end
  end

  # Autocomplete results for the current user's groups
72 73 74
  def groups_autocomplete(term, limit = 5)
    current_user.authorized_groups.search(term).limit(limit).map do |group|
      {
75
        label: "group: #{search_result_sanitize(group.name)}",
76 77
        url: group_path(group)
      }
78 79 80 81
    end
  end

  # Autocomplete results for the current user's projects
82
  def projects_autocomplete(term, limit = 5)
83 84
    ProjectsFinder.new.execute(current_user).search_by_title(term).
      sorted_by_stars.non_archived.limit(limit).map do |p|
85
      {
86
        label: "project: #{search_result_sanitize(p.name_with_namespace)}",
Vinnie Okada committed
87
        url: namespace_project_path(p.namespace, p)
88
      }
89 90
    end
  end
91

92
  def search_result_sanitize(str)
93 94
    Sanitize.clean(str)
  end
95 96 97 98 99 100 101 102 103 104

  def search_filter_path(options={})
    exist_opts = {
      search: params[:search],
      project_id: params[:project_id],
      group_id: params[:group_id],
      scope: params[:scope]
    }

    options = exist_opts.merge(options)
105
    search_path(options)
106
  end
107 108 109

  # Sanitize html generated after parsing markdown from issue description or comment
  def search_md_sanitize(html)
110
    sanitize(html, tags: %w(a p ol ul li pre code))
111
  end
112
end