Skip to content

Move facet components to Facets namespace #3614

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 2 additions & 20 deletions app/components/blacklight/facet_field_checkboxes_component.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
# frozen_string_literal: true

module Blacklight
class FacetFieldCheckboxesComponent < Blacklight::Component
def initialize(facet_field:, layout: nil)
@facet_field = facet_field
@layout = layout == false ? FacetFieldNoLayoutComponent : Blacklight::FacetFieldComponent
end

def render?
presenters.any?
end

def presenters
return [] unless @facet_field.paginator

return to_enum(:presenters) unless block_given?

@facet_field.paginator.items.each do |item|
yield Blacklight::FacetCheckboxItemPresenter.new(item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
end
end
end
class FacetFieldCheckboxesComponent < Facets::FieldComponent; end
FacetFieldCheckboxesComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetFieldCheckboxesComponent", "Blacklight::Facets::CheckboxesComponent", ActiveSupport::Deprecation.new)
end
19 changes: 2 additions & 17 deletions app/components/blacklight/facet_field_component.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
# frozen_string_literal: true

module Blacklight
class FacetFieldComponent < Blacklight::Component
renders_one :label
renders_one :body

# @param [Blacklight::FacetFieldPresenter] facet_field
def initialize(facet_field:)
@facet_field = facet_field
end

def html_id
"facet-#{@facet_field.key.parameterize}"
end

def header_html_id
"#{html_id}-header"
end
end
class FacetFieldComponent < Facets::FieldComponent; end
FacetFieldComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetFieldComponent", "Blacklight::Facets::FieldComponent", ActiveSupport::Deprecation.new)
end
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
# frozen_string_literal: true

module Blacklight
class FacetFieldInclusiveConstraintComponent < Blacklight::Component
with_collection_parameter :facet_field

def initialize(facet_field:, values: nil)
@facet_field = facet_field
@values = values
end

def values
@values ||= @facet_field.values.find { |v| v.is_a? Array }
@values || []
end

def render?
values.present?
end

def presenters
return to_enum(:presenters) unless block_given?

values.each do |item|
yield Blacklight::FacetGroupedItemPresenter.new(values, item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
end
end
end
class FacetFieldInclusiveConstraintComponent < Facets::InclusiveConstraintComponent; end
FacetFieldInclusiveConstraintComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetFieldInclusiveConstraintComponent",
"Blacklight::Facets::InclusiveConstraintComponent",
ActiveSupport::Deprecation.new)
end
34 changes: 2 additions & 32 deletions app/components/blacklight/facet_field_list_component.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
# frozen_string_literal: true

module Blacklight
class FacetFieldListComponent < Blacklight::Component
def initialize(facet_field:, layout: nil)
@facet_field = facet_field
@layout = layout == false ? FacetFieldNoLayoutComponent : Blacklight::FacetFieldComponent
end

def facet_items(wrapping_element: :li, **item_args)
facet_item_component_class.with_collection(facet_item_presenters, wrapping_element: wrapping_element, **item_args)
end

def facet_item_presenters
@facet_field.paginator.items.map do |item|
facet_item_presenter(item)
end
end

def render?
@facet_field.paginator&.items&.any?
end

def facet_item_presenter(facet_item)
facet_config.item_presenter.new(facet_item, facet_config, helpers, @facet_field.key)
end

def facet_item_component_class
facet_config.item_component
end

def facet_config
@facet_field.facet_field
end
end
class FacetFieldListComponent < Facets::ListComponent; end
FacetFieldListComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetFieldListComponent", "Blacklight::Facets::ListComponent", ActiveSupport::Deprecation.new)
end
12 changes: 2 additions & 10 deletions app/components/blacklight/facet_field_no_layout_component.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# frozen_string_literal: true

module Blacklight
class FacetFieldNoLayoutComponent < Blacklight::Component
renders_one :label
renders_one :body

def initialize(**); end

def call
body.to_s
end
end
class FacetFieldNoLayoutComponent < Facets::FieldComponent; end
FacetFieldNoLayoutComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetFieldNoLayoutComponent", "Blacklight::Facets::NoLayoutComponent", ActiveSupport::Deprecation.new)
end
76 changes: 2 additions & 74 deletions app/components/blacklight/facet_item_component.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,6 @@
# frozen_string_literal: true

module Blacklight
class FacetItemComponent < Blacklight::Component
attr_reader :label, :href, :hits

with_collection_parameter :facet_item

def initialize(facet_item:, wrapping_element: 'li', suppress_link: false)
@facet_item = facet_item
@label = facet_item.label
@hits = facet_item.hits
@href = facet_item.href
@selected = facet_item.selected?
@wrapping_element = wrapping_element
@suppress_link = suppress_link
end

def call
# if the downstream app has overridden the helper methods we'd usually call,
# use the helpers to preserve compatibility
content = if @selected
render_selected_facet_value
else
render_facet_value
end

return '' if content.blank?
return content unless @wrapping_element

content_tag @wrapping_element, content
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.
#
# @return [String]
# @private
def render_facet_value
tag.span(class: "facet-label") do
link_to_unless(@suppress_link, label, href, class: "facet-select", rel: "nofollow")
end + render_facet_count
end

##
# Standard display of a SELECTED facet value (e.g. without a link and with a remove button)
# @see #render_facet_value
#
# @private
def render_selected_facet_value
tag.span(class: "facet-label") do
tag.span(label, class: "selected") +
# remove link
link_to(href, class: "remove", rel: "nofollow") do
render(Blacklight::Icons::RemoveComponent.new(aria_hidden: true)) +
tag.span(helpers.t(:'blacklight.search.facets.selected.remove'), class: 'visually-hidden')
end
end + render_facet_count(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 [Hash] options
# @option options [Array<String>] an array of classes to add to count span.
# @return [String]
# @private
def render_facet_count(options = {})
return '' if hits.blank?

classes = (options[:classes] || []) << "facet-count"
tag.span(t('blacklight.search.facets.count', number: number_with_delimiter(hits)), class: classes)
end
end
class FacetItemComponent < Facets::ItemComponent; end
FacetItemComponent = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FacetItemComponent", "Blacklight::Facets::ItemComponent", ActiveSupport::Deprecation.new)
end
2 changes: 1 addition & 1 deletion app/components/blacklight/facet_item_pivot_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(facet_item:, wrapping_element: 'li', suppress_link: false, collap
end

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

id = "h-#{self.class.mint_id}" if @collapsing && has_items?

Expand Down
26 changes: 26 additions & 0 deletions app/components/blacklight/facets/checkboxes_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Blacklight
module Facets
class CheckboxesComponent < Blacklight::Component
def initialize(facet_field:, layout: nil)
@facet_field = facet_field
@layout = layout == false ? Blacklight::Facets::NoLayoutComponent : Blacklight::Facets::FieldComponent
end

def render?
presenters.any?
end

def presenters
return [] unless @facet_field.paginator

return to_enum(:presenters) unless block_given?

@facet_field.paginator.items.each do |item|
yield Blacklight::FacetCheckboxItemPresenter.new(item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
end
end
end
end
end
23 changes: 23 additions & 0 deletions app/components/blacklight/facets/field_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Blacklight
module Facets
class FieldComponent < Blacklight::Component
renders_one :label
renders_one :body

# @param [Blacklight::FacetFieldPresenter] facet_field
def initialize(facet_field:)
@facet_field = facet_field
end

def html_id
"facet-#{@facet_field.key.parameterize}"
end

def header_html_id
"#{html_id}-header"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="inclusive_or card card-body bg-light mb-3">
<h5><%= t('blacklight.advanced_search.any_of') %></h5>
<ul class="list-unstyled facet-values">
<%= render(Blacklight::FacetItemComponent.with_collection(presenters.to_a)) %>
<%= render Blacklight::Facets::ItemComponent.with_collection(presenters.to_a) %>
</ul>
</div>
31 changes: 31 additions & 0 deletions app/components/blacklight/facets/inclusive_constraint_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Blacklight
module Facets
class InclusiveConstraintComponent < Blacklight::Component
with_collection_parameter :facet_field

def initialize(facet_field:, values: nil)
@facet_field = facet_field
@values = values
end

def values
@values ||= @facet_field.values.find { |v| v.is_a? Array }
@values || []
end

def render?
values.present?
end

def presenters
return to_enum(:presenters) unless block_given?

values.each do |item|
yield Blacklight::FacetGroupedItemPresenter.new(values, item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
end
end
end
end
end
Loading