debian-mirror-gitlab/app/helpers/search_helper.rb

170 lines
5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module SearchHelper
2019-12-26 22:10:19 +05:30
SEARCH_PERMITTED_PARAMS = [:search, :scope, :project_id, :group_id, :repository_ref, :snippets].freeze
2016-06-02 11:05:42 +05:30
def search_entries_info(collection, scope, term)
2019-02-15 15:39:39 +05:30
return if collection.to_a.empty?
2016-06-02 11:05:42 +05:30
from = collection.offset_value + 1
2019-02-15 15:39:39 +05:30
to = collection.offset_value + collection.to_a.size
2016-06-02 11:05:42 +05:30
count = collection.total_count
2020-01-01 13:55:28 +05:30
term_element = "<span>&nbsp;<code>#{h(term)}</code>&nbsp;</span>".html_safe
2016-06-02 11:05:42 +05:30
2019-12-04 20:38:33 +05:30
search_entries_info_template(collection) % {
from: from,
to: to,
count: count,
2019-12-21 20:55:43 +05:30
scope: search_entries_scope_label(scope, count),
2020-01-01 13:55:28 +05:30
term_element: term_element
2019-12-04 20:38:33 +05:30
}
end
2019-12-21 20:55:43 +05:30
def search_entries_scope_label(scope, count)
2019-12-04 20:38:33 +05:30
case scope
2019-12-21 20:55:43 +05:30
when 'blobs'
ns_('SearchResults|code result', 'SearchResults|code results', count)
2019-12-04 20:38:33 +05:30
when 'commits'
ns_('SearchResults|commit', 'SearchResults|commits', count)
when 'issues'
ns_('SearchResults|issue', 'SearchResults|issues', count)
when 'merge_requests'
ns_('SearchResults|merge request', 'SearchResults|merge requests', count)
when 'milestones'
ns_('SearchResults|milestone', 'SearchResults|milestones', count)
when 'notes'
ns_('SearchResults|comment', 'SearchResults|comments', count)
when 'projects'
ns_('SearchResults|project', 'SearchResults|projects', count)
when 'snippet_titles'
ns_('SearchResults|snippet', 'SearchResults|snippets', count)
when 'users'
ns_('SearchResults|user', 'SearchResults|users', count)
2019-12-21 20:55:43 +05:30
when 'wiki_blobs'
ns_('SearchResults|wiki result', 'SearchResults|wiki results', count)
2019-12-04 20:38:33 +05:30
else
raise "Unrecognized search scope '#{scope}'"
end
end
def search_entries_info_template(collection)
if collection.total_pages > 1
2020-01-01 13:55:28 +05:30
s_("SearchResults|Showing %{from} - %{to} of %{count} %{scope} for%{term_element}").html_safe
2019-12-04 20:38:33 +05:30
else
2020-01-01 13:55:28 +05:30
s_("SearchResults|Showing %{count} %{scope} for%{term_element}").html_safe
2019-12-04 20:38:33 +05:30
end
2016-06-02 11:05:42 +05:30
end
2019-12-21 20:55:43 +05:30
def search_entries_empty_message(scope, term)
(s_("SearchResults|We couldn't find any %{scope} matching %{term}") % {
scope: search_entries_scope_label(scope, 0),
term: "<code>#{h(term)}</code>"
}).html_safe
end
2019-12-26 22:10:19 +05:30
# Overriden in EE
def search_blob_title(project, path)
path
2016-09-29 09:46:39 +05:30
end
2019-07-07 11:18:12 +05:30
def search_service
@search_service ||= ::SearchService.new(current_user, params)
end
2014-09-02 18:07:02 +05:30
private
def search_result_sanitize(str)
Sanitize.clean(str)
end
2015-04-26 12:48:37 +05:30
2019-10-12 21:52:04 +05:30
def search_filter_link(scope, label, data: {}, search: {})
search_params = params
.merge(search)
.merge({ scope: scope })
2019-12-26 22:10:19 +05:30
.permit(SEARCH_PERMITTED_PARAMS)
2019-10-12 21:52:04 +05:30
if @scope == scope
li_class = 'active'
count = @search_results.formatted_count(scope)
else
badge_class = 'js-search-count hidden'
badge_data = { url: search_count_path(search_params) }
end
2015-04-26 12:48:37 +05:30
2019-10-12 21:52:04 +05:30
content_tag :li, class: li_class, data: data do
link_to search_path(search_params) do
concat label
concat ' '
concat content_tag(:span, count, class: ['badge badge-pill', badge_class], data: badge_data)
end
end
2015-04-26 12:48:37 +05:30
end
2020-05-24 23:13:21 +05:30
def search_filter_input_options(type, placeholder = _('Search or filter results...'))
2018-03-17 18:26:18 +05:30
opts =
{
id: "filtered-search-#{type}",
2020-05-24 23:13:21 +05:30
placeholder: placeholder,
2018-03-17 18:26:18 +05:30
data: {
'username-params' => UserSerializer.new.represent(@users)
},
autocomplete: 'off'
2017-09-10 17:25:29 +05:30
}
2019-09-30 21:07:59 +05:30
opts[:data]['runner-tags-endpoint'] = tag_list_admin_runners_path
2017-09-10 17:25:29 +05:30
if @project.present?
opts[:data]['project-id'] = @project.id
2019-09-04 21:01:54 +05:30
opts[:data]['labels-endpoint'] = project_labels_path(@project)
opts[:data]['milestones-endpoint'] = project_milestones_path(@project)
2019-12-26 22:10:19 +05:30
opts[:data]['releases-endpoint'] = project_releases_path(@project)
2019-02-15 15:39:39 +05:30
elsif @group.present?
2018-03-17 18:26:18 +05:30
opts[:data]['group-id'] = @group.id
2019-09-04 21:01:54 +05:30
opts[:data]['labels-endpoint'] = group_labels_path(@group)
opts[:data]['milestones-endpoint'] = group_milestones_path(@group)
2019-02-15 15:39:39 +05:30
else
2019-09-04 21:01:54 +05:30
opts[:data]['labels-endpoint'] = dashboard_labels_path
opts[:data]['milestones-endpoint'] = dashboard_milestones_path
2017-09-10 17:25:29 +05:30
end
opts
end
2019-02-15 15:39:39 +05:30
def search_history_storage_prefix
if @project.present?
@project.full_path
elsif @group.present?
@group.full_path
else
'dashboard'
end
end
2016-11-03 12:29:30 +05:30
# 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
2015-04-26 12:48:37 +05:30
sanitize(html, tags: %w(a p ol ul li pre code))
end
2018-03-17 18:26:18 +05:30
2019-12-26 22:10:19 +05:30
def show_user_search_tab?
2019-07-07 11:18:12 +05:30
return false if Feature.disabled?(:users_search, default_enabled: true)
if @project
project_search_tabs?(:members)
else
can?(current_user, :read_users_list)
end
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
SearchHelper.prepend_if_ee('EE::SearchHelper')