Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert dashboard facets to use Dropdown facet components instead of overriding upstream #5785

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 5 additions & 62 deletions app/assets/stylesheets/hyrax/dashboard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,14 @@ body.dashboard {
margin-bottom: 20px;
padding: $card-spacer-y $card-spacer-x;

.btn-group > .facet-values {
display: none;
}
.btn-group.show > .facet-values.show {
display: table;
}

.facet-values {
width: 20em; // TODO: make this responsive.
.dropdown-item {
display: flex;
align-items: center;
justify-content: space-between;
}

.facet-label {
padding: 3px 20px;
font-weight: 400;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

.facet-count {
padding: 3px 20px;
font-weight: 400;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
padding-right: 20px;
}
}

Expand Down Expand Up @@ -176,44 +157,6 @@ body.dashboard {
}
}

.dropdown-item {
display: list-item;

> .facet-values {
display: table;
table-layout: fixed;

> .facet-label {
display: table-cell;
hyphens: auto;
text-indent: -0.938rem;
}

> .facet-count {
display: table-cell;
text-align: right;
}
}
}

ul#visibility_ssi-dropdown-options {
table-layout: fixed;

> li {
display: table-row;

> .facet-label {
display: table-cell;
hyphens: auto;
}

> .facet-count {
display: table-cell;
text-align: right;
}
}
}

.card.tabs {
background-color: transparent;

Expand Down
21 changes: 21 additions & 0 deletions app/components/hyrax/dropdown_facet_field_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<fieldset class="btn-group">
<legend class="sr-only"><%= @facet_field.label %></legend>
<button class="btn btn-secondary dropdown-toggle" type="button" id="<%= @facet_field.key.parameterize %>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" aria-controls="<%= @facet_field.key.parameterize + '-dropdown-options' %>">
<%= @facet_field.label %>
</button>
<ul class="dropdown-menu" id="<%= @facet_field.key.parameterize + '-dropdown-options' %>">
<% facet_item_presenters.each do |item| -%>
<li class="dropdown-item">
<%= render Blacklight::FacetItemComponent.new(facet_item: item, wrapping_element: nil) %>
</li>
<% end %>

<% if @facet_field.modal_path %>
<li class="dropdown-item more_facets">
<%= link_to t("more_#{@facet_field.key}_html", scope: 'blacklight.search.facets', default: :more_html, field_name: @facet_field.label),
@facet_field.modal_path,
data: { blacklight_modal: 'trigger' } %>
</li>
<% end %>
</ul>
</fieldset>
30 changes: 30 additions & 0 deletions app/components/hyrax/dropdown_facet_field_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true
module Hyrax
class DropdownFacetFieldComponent < Blacklight::FacetFieldListComponent
def facet_item_presenters
return to_enum(:facet_item_presenters) unless block_given?

@facet_field.paginator.items.each do |item|
skipped = facet_config.skip_item&.call(item)

yield facet_item_presenter(item) unless skipped

item.items&.each do |subitem|
yield facet_item_presenter(subitem.dup.tap { |i| i.fq = nil if skipped })
end
end
end

def facet_item_presenter(facet_item)
config = facet_config(facet_item.field)

(config.item_presenter || Blacklight::FacetItemPresenter).new(facet_item, config, helpers, facet_item.field)
end

def facet_config(key = nil)
return @facet_field.facet_field if key.nil?

helpers.blacklight_config.facet_fields[key]
end
end
end
1 change: 1 addition & 0 deletions app/controllers/hyrax/my/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def self.configure_facets
config.add_facet_field Hyrax.config.collection_type_index_field,
helper_method: :collection_type_label, limit: 5,
pivot: ['has_model_ssim', Hyrax.config.collection_type_index_field],
skip_item: ->(item) { item.value == "Collection" },
label: I18n.t('hyrax.dashboard.my.heading.collection_type')
# This causes AdminSets to also be shown with the Collection Type label
config.add_facet_field 'has_model_ssim',
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/hyrax/my/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.configure_facets
configure_blacklight do |config|
config.search_builder_class = Hyrax::My::WorksSearchBuilder
config.add_facet_field "admin_set_sim", limit: 5
config.add_facet_field "member_of_collections_ssim", limit: 5
config.add_facet_field "member_of_collections_ssim", limit: 1
end
end
configure_facets
Expand Down
140 changes: 0 additions & 140 deletions app/helpers/hyrax/facets_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,145 +6,5 @@ module FacetsHelper
# This module is used to ensure Hyrax facet views that rely on deprecated Blacklight helper methods are still functional

include Blacklight::Facet

##
# Renders the list of values
# removes any elements where render_facet_item returns a nil value. This enables an application
# to filter undesireable facet items so they don't appear in the UI
def render_facet_limit_list(paginator, facet_field, wrapping_element = :li)
safe_join(paginator.items.map { |item| render_facet_item(facet_field, item) }.compact.map { |item| content_tag(wrapping_element, item) })
end

##
# Renders a single facet item
def render_facet_item(facet_field, item)
if facet_in_params?(facet_field, item.value)
render_selected_facet_value(facet_field, item)
else
render_facet_value(facet_field, item)
end
end

##
# Standard display of a facet value in a list. Used in both _facets sidebar
# partial and catalog/facet expanded list. Will output facet value name as
# a link to add that to your restrictions, with count in parens.
#
# @param [Blacklight::Solr::Response::Facets::FacetField] facet_field
# @param [Blacklight::Solr::Response::Facets::FacetItem] item
# @param [Hash] options
# @option options [Boolean] :suppress_link display the facet, but don't link to it
# @return [String]
def render_facet_value(facet_field, item, options = {})
path = path_for_facet(facet_field, item)
content_tag(:span, class: "facet-label") do
link_to_unless(options[:suppress_link], facet_display_value(facet_field, item), path, class: "facet_select")
end + render_facet_count(item.hits)
end

##
# Where should this facet link to?
# @param [Blacklight::Solr::Response::Facets::FacetField] facet_field
# @param [String] item
# @return [String]
def path_for_facet(facet_field, item)
facet_config = facet_configuration_for_field(facet_field)
if facet_config.url_method
send(facet_config.url_method, facet_field, item)
else
search_action_path(search_state.add_facet_params_and_redirect(facet_field, item))
end
end

##
# Standard display of a SELECTED facet value (e.g. without a link and with a remove button)
# @see #render_facet_value
# @param [Blacklight::Solr::Response::Facets::FacetField] facet_field
# @param [String] item
def render_selected_facet_value(facet_field, item)
remove_href = search_action_path(search_state.remove_facet_params(facet_field, item))
content_tag(:span, class: "facet-label") do
content_tag(:span, facet_display_value(facet_field, item), class: "selected") +
# remove link
link_to(remove_href, class: "remove") do
content_tag(:i, '', class: "fa fa-times") +
content_tag(:span, '[remove]', class: 'sr-only')
end
end + render_facet_count(item.hits, classes: ["selected"])
end

##
# Renders a count value for facet limits. Can be over-ridden locally
# to change style. And can be called by plugins to get consistent display.
#
# @param [Integer] num number of facet results
# @param [Hash] options
# @option options [Array<String>] an array of classes to add to count span.
# @return [String]
def render_facet_count(num, options = {})
classes = (options[:classes] || []) << "facet-count"
content_tag("span", t('blacklight.search.facets.count', number: number_with_delimiter(num)), class: classes)
end

##
# Check if the query parameters have the given facet field with the
# given value.
#
# @param [Object] field
# @param [Object] item facet value
# @return [Boolean]
def facet_in_params?(field, item)
value = facet_value_for_facet_item(item)

(facet_params(field) || []).include? value
end

##
# Get the values of the facet set in the blacklight query string
def facet_params(field)
config = facet_configuration_for_field(field)

params[:f][config.key] if params[:f]
end

##
# Get the displayable version of a facet's value
#
# @param [Object] field
# @param [String] item value
# @return [String]
# rubocop:disable Metrics/MethodLength
def facet_display_value(field, item)
facet_config = facet_configuration_for_field(field)

value = if item.respond_to? :label
item.label
else
facet_value_for_facet_item(item)
end

if facet_config.helper_method
send facet_config.helper_method, value
elsif facet_config.query && facet_config.query[value]
facet_config.query[value][:label]
elsif facet_config.date
localization_options = facet_config.date == true ? {} : facet_config.date

l(value.to_datetime, localization_options)
else
value
end
end
# rubocop:enable Metrics/MethodLength

private

def facet_value_for_facet_item(item)
if item.respond_to? :value
item.value
else
item
end
end
end
end
7 changes: 0 additions & 7 deletions app/views/hyrax/my/_facet_layout.html.erb

This file was deleted.

15 changes: 0 additions & 15 deletions app/views/hyrax/my/_facet_limit.html.erb

This file was deleted.

26 changes: 0 additions & 26 deletions app/views/hyrax/my/_facet_pivot.html.erb

This file was deleted.

12 changes: 7 additions & 5 deletions app/views/hyrax/my/collections/_facets.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<% # main container for facets/limits menu -%>
<% if has_facet_values?(facet_field_names, @response) %>
<div class="list-filters" role="group">
<span class="list-filters-label"><%= t("hyrax.dashboard.my.facet_label.collections") %></span>
<%= render_facet_partials(facet_field_names, response: @response) %>
</div>
<% facets = capture do %>
<%= render Blacklight::FacetComponent.with_collection(blacklight_config.facet_fields_in_group(nil), response: @response, component: Hyrax::DropdownFacetFieldComponent)%>
<% end %>

<div class="list-filters" role="group">
<span class="list-filters-label"><%= t("hyrax.dashboard.my.facet_label.collections") %></span>
<%= facets %>
</div>
8 changes: 6 additions & 2 deletions app/views/hyrax/my/works/_facets.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% # main container for facets/limits menu -%>
<% if has_facet_values?(facet_field_names, @response) %>
<% facets = capture do %>
<%= render Blacklight::FacetComponent.with_collection(blacklight_config.facet_fields_in_group(nil), response: @response, component: Hyrax::DropdownFacetFieldComponent)%>
<% end %>

<% if facets.present? %>
<div class="list-filters" role="group">
<span class="list-filters-label"><%= t("hyrax.dashboard.my.facet_label.works") %></span>
<%= render_facet_partials(facet_field_names, response: @response) %>
<%= facets %>
</div>
<% end %>
4 changes: 2 additions & 2 deletions hyrax.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SUMMARY
spec.add_dependency 'active-fedora', '~> 13.1', '>= 13.1.2'
spec.add_dependency 'almond-rails', '~> 0.1'
spec.add_dependency 'awesome_nested_set', '~> 3.1'
spec.add_dependency 'blacklight', '~> 7.25'
spec.add_dependency 'blacklight', '~> 7.29'
spec.add_dependency 'blacklight-gallery', '~> 4.0'
spec.add_dependency 'breadcrumbs_on_rails', '~> 3.0'
spec.add_dependency 'browse-everything', '>= 0.16', '< 2.0'
Expand Down Expand Up @@ -90,7 +90,7 @@ SUMMARY
spec.add_development_dependency "capybara", '~> 3.29'
spec.add_development_dependency 'capybara-screenshot', '~> 1.0'
spec.add_development_dependency 'database_cleaner', '~> 1.3'
spec.add_development_dependency 'engine_cart', '~> 2.2'
spec.add_development_dependency 'engine_cart', '~> 2.5'
spec.add_development_dependency "equivalent-xml", '~> 0.5'
spec.add_development_dependency "factory_bot", '~> 4.4'
spec.add_development_dependency 'fcrepo_wrapper', '~> 0.5', '>= 0.5.1'
Expand Down
Loading