2013-10-20 01:05:20 -04:00
|
|
|
module SearchHelper
|
2014-01-18 07:16:46 -05:00
|
|
|
def search_autocomplete_opts(term)
|
2013-10-20 01:05:20 -04:00
|
|
|
return unless current_user
|
2014-01-18 07:16:46 -05:00
|
|
|
|
|
|
|
resources_results = [
|
|
|
|
groups_autocomplete(term),
|
2014-02-13 15:45:51 -05:00
|
|
|
projects_autocomplete(term)
|
2014-01-18 07:16:46 -05:00
|
|
|
].flatten
|
|
|
|
|
|
|
|
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
|
|
|
|
generic_results.select! { |result| result[:label] =~ Regexp.new(term, "i") }
|
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
[
|
2014-01-18 07:16:46 -05:00
|
|
|
resources_results,
|
|
|
|
generic_results
|
2013-12-09 09:26:43 -05:00
|
|
|
].flatten.uniq do |item|
|
|
|
|
item[:label]
|
2014-01-18 07:16:46 -05:00
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Autocomplete results for various settings pages
|
|
|
|
def default_autocomplete
|
|
|
|
[
|
|
|
|
{ label: "My Profile settings", url: profile_path },
|
|
|
|
{ label: "My SSH Keys", url: profile_keys_path },
|
|
|
|
{ label: "My Dashboard", url: root_path },
|
|
|
|
{ label: "Admin Section", url: admin_root_path },
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for internal help pages
|
|
|
|
def help_autocomplete
|
|
|
|
[
|
2014-05-28 09:47:03 -04:00
|
|
|
{ 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") },
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current project, if it's defined
|
|
|
|
def project_autocomplete
|
|
|
|
if @project && @project.repository.exists? && @project.repository.root_ref
|
2014-01-18 09:06:20 -05:00
|
|
|
prefix = search_result_sanitize(@project.name_with_namespace)
|
2013-10-20 01:05:20 -04:00
|
|
|
ref = @ref || @project.repository.root_ref
|
|
|
|
|
|
|
|
[
|
|
|
|
{ label: "#{prefix} - Files", url: project_tree_path(@project, ref) },
|
|
|
|
{ label: "#{prefix} - Commits", url: project_commits_path(@project, ref) },
|
|
|
|
{ label: "#{prefix} - Network", url: project_network_path(@project, ref) },
|
|
|
|
{ label: "#{prefix} - Graph", url: project_graph_path(@project, ref) },
|
|
|
|
{ label: "#{prefix} - Issues", url: project_issues_path(@project) },
|
|
|
|
{ label: "#{prefix} - Merge Requests", url: project_merge_requests_path(@project) },
|
|
|
|
{ label: "#{prefix} - Milestones", url: project_milestones_path(@project) },
|
|
|
|
{ label: "#{prefix} - Snippets", url: project_snippets_path(@project) },
|
|
|
|
{ label: "#{prefix} - Team", url: project_team_index_path(@project) },
|
|
|
|
{ label: "#{prefix} - Wiki", url: project_wikis_path(@project) },
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's groups
|
2014-01-18 07:16:46 -05:00
|
|
|
def groups_autocomplete(term, limit = 5)
|
|
|
|
current_user.authorized_groups.search(term).limit(limit).map do |group|
|
|
|
|
{
|
2014-01-18 09:06:20 -05:00
|
|
|
label: "group: #{search_result_sanitize(group.name)}",
|
2014-01-18 07:16:46 -05:00
|
|
|
url: group_path(group)
|
|
|
|
}
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's projects
|
2014-01-18 07:16:46 -05:00
|
|
|
def projects_autocomplete(term, limit = 5)
|
2014-09-25 12:41:49 -04:00
|
|
|
ProjectsFinder.new.execute(current_user).search_by_title(term).
|
|
|
|
sorted_by_stars.non_archived.limit(limit).map do |p|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
2014-01-18 09:06:20 -05:00
|
|
|
label: "project: #{search_result_sanitize(p.name_with_namespace)}",
|
2014-01-18 07:16:46 -05:00
|
|
|
url: project_path(p)
|
|
|
|
}
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2014-01-18 09:06:20 -05:00
|
|
|
def search_result_sanitize(str)
|
2014-01-18 07:16:46 -05:00
|
|
|
Sanitize.clean(str)
|
|
|
|
end
|
2014-08-27 02:57:50 -04:00
|
|
|
|
|
|
|
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)
|
2014-08-27 04:55:42 -04:00
|
|
|
search_path(options)
|
2014-08-27 02:57:50 -04:00
|
|
|
end
|
2014-08-27 05:37:31 -04:00
|
|
|
|
|
|
|
# Sanitize html generated after parsing markdown from issue description or comment
|
|
|
|
def search_md_sanitize(html)
|
2014-08-28 04:06:19 -04:00
|
|
|
sanitize(html, tags: %w(a p ol ul li pre code))
|
2014-08-27 05:37:31 -04:00
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|