2018-08-18 11:19:57 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-20 05:05:20 +00:00
|
|
|
module SearchHelper
|
2014-01-18 12:16:46 +00:00
|
|
|
def search_autocomplete_opts(term)
|
2013-10-20 05:05:20 +00:00
|
|
|
return unless current_user
|
2014-01-18 12:16:46 +00:00
|
|
|
|
|
|
|
resources_results = [
|
|
|
|
groups_autocomplete(term),
|
2014-02-13 20:45:51 +00:00
|
|
|
projects_autocomplete(term)
|
2014-01-18 12:16:46 +00:00
|
|
|
].flatten
|
|
|
|
|
2016-09-07 13:16:22 +00:00
|
|
|
search_pattern = Regexp.new(Regexp.escape(term), "i")
|
|
|
|
|
2014-01-18 12:16:46 +00:00
|
|
|
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
|
2017-09-03 05:49:19 +00:00
|
|
|
generic_results.concat(default_autocomplete_admin) if current_user.admin?
|
2016-09-07 13:16:22 +00:00
|
|
|
generic_results.select! { |result| result[:label] =~ search_pattern }
|
2014-01-18 12:16:46 +00:00
|
|
|
|
2013-10-20 05:05:20 +00:00
|
|
|
[
|
2014-01-18 12:16:46 +00:00
|
|
|
resources_results,
|
|
|
|
generic_results
|
2013-12-09 14:26:43 +00:00
|
|
|
].flatten.uniq do |item|
|
|
|
|
item[:label]
|
2014-01-18 12:16:46 +00:00
|
|
|
end
|
2013-10-20 05:05:20 +00:00
|
|
|
end
|
|
|
|
|
2016-04-15 16:00:53 +00:00
|
|
|
def search_entries_info(collection, scope, term)
|
2016-04-26 08:02:28 +00:00
|
|
|
return unless collection.count > 0
|
2016-04-15 13:13:21 +00:00
|
|
|
|
2016-04-26 08:02:28 +00:00
|
|
|
from = collection.offset_value + 1
|
2018-06-04 11:41:37 +00:00
|
|
|
to = collection.offset_value + collection.count
|
2016-04-26 08:02:28 +00:00
|
|
|
count = collection.total_count
|
|
|
|
|
|
|
|
"Showing #{from} - #{to} of #{count} #{scope.humanize(capitalize: false)} for \"#{term}\""
|
2016-04-15 13:13:21 +00:00
|
|
|
end
|
|
|
|
|
2018-06-04 11:41:37 +00:00
|
|
|
def find_project_for_result_blob(result)
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2016-09-14 10:29:23 +00:00
|
|
|
def parse_search_result(result)
|
2018-06-04 11:41:37 +00:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_blob_title(project, filename)
|
|
|
|
filename
|
2016-09-14 10:29:23 +00:00
|
|
|
end
|
|
|
|
|
2013-10-20 05:05:20 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# Autocomplete results for various settings pages
|
|
|
|
def default_autocomplete
|
|
|
|
[
|
2017-01-04 22:05:53 +00:00
|
|
|
{ category: "Settings", label: "User settings", url: profile_path },
|
2016-03-24 20:19:43 +00:00
|
|
|
{ category: "Settings", label: "SSH Keys", url: profile_keys_path },
|
2017-09-03 05:49:19 +00:00
|
|
|
{ category: "Settings", label: "Dashboard", url: root_path }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for settings pages, for admins
|
|
|
|
def default_autocomplete_admin
|
|
|
|
[
|
|
|
|
{ category: "Settings", label: "Admin Section", url: admin_root_path }
|
2013-10-20 05:05:20 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for internal help pages
|
|
|
|
def help_autocomplete
|
|
|
|
[
|
2016-05-16 22:06:26 +00:00
|
|
|
{ category: "Help", label: "API Help", url: help_page_path("api/README") },
|
2016-08-25 14:19:14 +00:00
|
|
|
{ category: "Help", label: "Markdown Help", url: help_page_path("user/markdown") },
|
2016-07-14 10:04:03 +00:00
|
|
|
{ category: "Help", label: "Permissions Help", url: help_page_path("user/permissions") },
|
2016-05-16 22:06:26 +00:00
|
|
|
{ category: "Help", label: "Public Access Help", url: help_page_path("public_access/public_access") },
|
|
|
|
{ category: "Help", label: "Rake Tasks Help", url: help_page_path("raketasks/README") },
|
|
|
|
{ category: "Help", label: "SSH Keys Help", url: help_page_path("ssh/README") },
|
|
|
|
{ category: "Help", label: "System Hooks Help", url: help_page_path("system_hooks/system_hooks") },
|
2017-02-03 18:26:48 +00:00
|
|
|
{ category: "Help", label: "Webhooks Help", url: help_page_path("user/project/integrations/webhooks") },
|
2017-05-03 11:22:03 +00:00
|
|
|
{ category: "Help", label: "Workflow Help", url: help_page_path("workflow/README") }
|
2013-10-20 05:05:20 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current project, if it's defined
|
|
|
|
def project_autocomplete
|
|
|
|
if @project && @project.repository.exists? && @project.repository.root_ref
|
2016-05-11 02:58:06 +00:00
|
|
|
ref = @ref || @project.repository.root_ref
|
2013-10-20 05:05:20 +00:00
|
|
|
|
|
|
|
[
|
2018-08-07 09:59:01 +00:00
|
|
|
{ category: "In this project", label: "Files", url: project_tree_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: "Commits", url: project_commits_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: "Network", url: project_network_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: "Graph", url: project_graph_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: "Issues", url: project_issues_path(@project) },
|
|
|
|
{ category: "In this project", label: "Merge Requests", url: project_merge_requests_path(@project) },
|
|
|
|
{ category: "In this project", label: "Milestones", url: project_milestones_path(@project) },
|
|
|
|
{ category: "In this project", label: "Snippets", url: project_snippets_path(@project) },
|
|
|
|
{ category: "In this project", label: "Members", url: project_project_members_path(@project) },
|
|
|
|
{ category: "In this project", label: "Wiki", url: project_wikis_path(@project) }
|
2013-10-20 05:05:20 +00:00
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's groups
|
2014-01-18 12:16:46 +00:00
|
|
|
def groups_autocomplete(term, limit = 5)
|
2017-08-15 10:27:37 +00:00
|
|
|
current_user.authorized_groups.order_id_desc.search(term).limit(limit).map do |group|
|
2014-01-18 12:16:46 +00:00
|
|
|
{
|
2016-03-24 20:19:43 +00:00
|
|
|
category: "Groups",
|
2016-03-08 07:56:43 +00:00
|
|
|
id: group.id,
|
2017-01-26 19:31:32 +00:00
|
|
|
label: "#{search_result_sanitize(group.full_name)}",
|
2018-07-17 05:48:39 +00:00
|
|
|
url: group_path(group),
|
|
|
|
avatar_url: group.avatar_url || ''
|
2014-01-18 12:16:46 +00:00
|
|
|
}
|
2013-10-20 05:05:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's projects
|
2014-01-18 12:16:46 +00:00
|
|
|
def projects_autocomplete(term, limit = 5)
|
2017-08-15 10:27:37 +00:00
|
|
|
current_user.authorized_projects.order_id_desc.search_by_title(term)
|
2017-06-21 13:48:12 +00:00
|
|
|
.sorted_by_stars.non_archived.limit(limit).map do |p|
|
2014-01-18 12:16:46 +00:00
|
|
|
{
|
2016-03-24 20:19:43 +00:00
|
|
|
category: "Projects",
|
2016-03-08 07:56:43 +00:00
|
|
|
id: p.id,
|
|
|
|
value: "#{search_result_sanitize(p.name)}",
|
2018-03-05 14:15:26 +00:00
|
|
|
label: "#{search_result_sanitize(p.full_name)}",
|
2018-07-17 05:48:39 +00:00
|
|
|
url: project_path(p),
|
|
|
|
avatar_url: p.avatar_url || ''
|
2014-01-18 12:16:46 +00:00
|
|
|
}
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
end
|
2014-01-18 12:16:46 +00:00
|
|
|
|
2014-01-18 14:06:20 +00:00
|
|
|
def search_result_sanitize(str)
|
2014-01-18 12:16:46 +00:00
|
|
|
Sanitize.clean(str)
|
|
|
|
end
|
2014-08-27 06:57:50 +00:00
|
|
|
|
2016-08-06 02:03:01 +00:00
|
|
|
def search_filter_path(options = {})
|
2014-08-27 06:57:50 +00:00
|
|
|
exist_opts = {
|
|
|
|
search: params[:search],
|
|
|
|
project_id: params[:project_id],
|
|
|
|
group_id: params[:group_id],
|
2016-07-22 16:42:24 +00:00
|
|
|
scope: params[:scope],
|
|
|
|
repository_ref: params[:repository_ref]
|
2014-08-27 06:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options = exist_opts.merge(options)
|
2014-08-27 08:55:42 +00:00
|
|
|
search_path(options)
|
2014-08-27 06:57:50 +00:00
|
|
|
end
|
2014-08-27 09:37:31 +00:00
|
|
|
|
2017-06-15 14:01:12 +00:00
|
|
|
def search_filter_input_options(type)
|
2017-08-28 21:56:49 +00:00
|
|
|
opts =
|
|
|
|
{
|
|
|
|
id: "filtered-search-#{type}",
|
|
|
|
placeholder: 'Search or filter results...',
|
|
|
|
data: {
|
2017-09-19 18:22:43 +00:00
|
|
|
'username-params' => UserSerializer.new.represent(@users)
|
2017-11-20 07:49:13 +00:00
|
|
|
},
|
|
|
|
autocomplete: 'off'
|
2017-06-15 14:01:12 +00:00
|
|
|
}
|
2017-08-02 09:27:24 +00:00
|
|
|
|
|
|
|
if @project.present?
|
|
|
|
opts[:data]['project-id'] = @project.id
|
|
|
|
opts[:data]['base-endpoint'] = project_path(@project)
|
|
|
|
else
|
|
|
|
# Group context
|
2017-08-28 21:56:49 +00:00
|
|
|
opts[:data]['group-id'] = @group.id
|
2017-08-02 09:27:24 +00:00
|
|
|
opts[:data]['base-endpoint'] = group_canonical_path(@group)
|
|
|
|
end
|
|
|
|
|
|
|
|
opts
|
2017-06-15 14:01:12 +00:00
|
|
|
end
|
|
|
|
|
2016-10-06 21:55:27 +00:00
|
|
|
# Sanitize a HTML field for search display. Most tags are stripped out and the
|
|
|
|
# maximum length is set to 200 characters.
|
|
|
|
def search_md_sanitize(object, field)
|
|
|
|
html = markdown_field(object, field)
|
|
|
|
html = Truncato.truncate(
|
|
|
|
html,
|
|
|
|
count_tags: false,
|
|
|
|
count_tail: false,
|
|
|
|
max_length: 200
|
|
|
|
)
|
|
|
|
|
|
|
|
# Truncato's filtered_tags and filtered_attributes are not quite the same
|
2014-08-28 08:06:19 +00:00
|
|
|
sanitize(html, tags: %w(a p ol ul li pre code))
|
2014-08-27 09:37:31 +00:00
|
|
|
end
|
2018-01-23 11:03:15 +00:00
|
|
|
|
|
|
|
def limited_count(count, limit = 1000)
|
|
|
|
count > limit ? "#{limit}+" : count
|
|
|
|
end
|
2013-10-20 05:05:20 +00:00
|
|
|
end
|