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
|
|
|
|
[
|
2015-03-22 23:10:12 -04:00
|
|
|
{ label: "Profile settings", url: profile_path },
|
|
|
|
{ label: "SSH Keys", url: profile_keys_path },
|
|
|
|
{ label: "Dashboard", url: root_path },
|
2013-10-20 01:05:20 -04:00
|
|
|
{ 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
|
|
|
|
|
|
|
|
[
|
2015-01-24 13:02:58 -05:00
|
|
|
{ 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) },
|
2015-03-13 11:22:03 -04:00
|
|
|
{ label: "#{prefix} - Members", url: namespace_project_project_members_path(@project.namespace, @project) },
|
2015-01-24 13:02:58 -05:00
|
|
|
{ label: "#{prefix} - Wiki", url: namespace_project_wikis_path(@project.namespace, @project) },
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
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)
|
2015-11-05 05:38:00 -05:00
|
|
|
GroupsFinder.new.execute(current_user).search(term).limit(limit).map do |group|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
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)}",
|
2015-01-24 13:02:58 -05:00
|
|
|
url: namespace_project_path(p.namespace, p)
|
2014-01-18 07:16:46 -05:00
|
|
|
}
|
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
|