Skip to content

Commit

Permalink
Convert pivot facets to components
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jun 4, 2020
1 parent 9fd9d8d commit 04aa299
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
45 changes: 45 additions & 0 deletions app/components/blacklight/facet_item_pivot_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Blacklight
# Render facet items and any subtree
class FacetItemPivotComponent < ::ViewComponent::Base
with_collection_parameter :facet_item

def initialize(facet_item:, wrapping_element: 'li', suppress_link: false)
@facet_item = facet_item
@wrapping_element = wrapping_element
@suppress_link = suppress_link
end

def call
facet = Blacklight::FacetItemComponent.new(facet_item: @facet_item, wrapping_element: nil, suppress_link: @suppress_link)

content_tag @wrapping_element do
concat content_tag('span', render_component(facet), class: 'facet-values')

if @facet_item.items.present?
concat(content_tag('ul', class: 'pivot-facet list-unstyled') do
render_component(
self.class.with_collection(
@facet_item.items.map { |i| facet_item_presenter(i) }
)
)
end)
end
end
end

private

# This is a little convoluted in Blacklight 7 in order to maintain backwards-compat
# with overrides of deprecated helpers. In 8.x, we can just call Component#render_in
# and call it a day
def render_component(component)
@view_context.render(component)
end

def facet_item_presenter(facet_item)
Blacklight::FacetItemPresenter.new(facet_item, @facet_item.facet_config, @view_context, @facet_item.facet_field, @facet_item.search_state)
end
end
end
11 changes: 7 additions & 4 deletions app/helpers/blacklight/facets_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ def render_facet_limit(display_facet, options = {})
# to filter undesireable facet items so they don't appear in the UI
def render_facet_limit_list(paginator, facet_field, wrapping_element = :li)
facet_config ||= facet_configuration_for_field(facet_field)
component = facet_config.fetch(:item_component, Blacklight::FacetItemComponent)

collection = paginator.items.map do |item|
facet_item_presenter(facet_config, item, facet_field)
end

render(component.with_collection(collection, wrapping_element: wrapping_element))
render(facet_item_component_class(facet_config).with_collection(collection, wrapping_element: wrapping_element))
end
deprecation_deprecate :render_facet_limit_list

Expand Down Expand Up @@ -292,8 +291,12 @@ def facet_item_presenter(facet_config, facet_item, facet_field)
end

def facet_item_component(facet_config, facet_item, facet_field, **args)
component = facet_config.fetch(:item_component, Blacklight::FacetItemComponent)
component.new(facet_item: facet_item_presenter(facet_config, facet_item, facet_field), **args).with_view_context(self)
facet_item_component_class(facet_config).new(facet_item: facet_item_presenter(facet_config, facet_item, facet_field), **args).with_view_context(self)
end

def facet_item_component_class(facet_config)
default_component = facet_config.pivot ? Blacklight::FacetItemPivotComponent : Blacklight::FacetItemComponent
facet_config.fetch(:item_component, default_component)
end

# We can't use .deprecation_deprecate here, because the new components need to
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/blacklight/facet_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Blacklight
class FacetItemPresenter
attr_reader :facet_item, :facet_config, :view_context, :search_state, :facet_field

delegate :hits, to: :facet_item
delegate :hits, :items, to: :facet_item

def initialize(facet_item, facet_config, view_context, facet_field, search_state = view_context.search_state)
@facet_item = facet_item
Expand Down
21 changes: 3 additions & 18 deletions app/views/catalog/_facet_pivot.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
<ul class="pivot-facet list-unstyled">
<% display_facet.items.each do |item| -%>
<li>
<span class="facet-values">
<% if facet_in_params?(field_name, item) %>
<%= render_selected_facet_value(field_name, item) %>
<% else %>
<%= render_facet_value(field_name, item) %>
<% end -%>
</span>

<% unless item.items.blank? %>
<%= render 'facet_pivot', display_facet: item, field_name: field_name %>
<% end %>
</li>
<% end %>

</ul>
<%= render(Blacklight::FacetFieldListComponent.new(
facet_field: facet_field_presenter(facet_field.merge(item_component: Blacklight::FacetItemPivotComponent), display_facet),
layout: false)) %>

0 comments on commit 04aa299

Please sign in to comment.