-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert document show/index views to support and use view components
- Loading branch information
Showing
23 changed files
with
474 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<%= content_tag @component, | ||
id: @id, | ||
data: { | ||
'document-id': @document.id.to_s.parameterize, | ||
'document-counter': @counter, | ||
}, | ||
itemscope: true, | ||
itemtype: @document.itemtype, | ||
class: classes.flatten.join(' ') do %> | ||
<%= header %> | ||
<% if body.present? %> | ||
<%= body %> | ||
<% else %> | ||
<div class="document-main-section"> | ||
<header class="documentHeader row"> | ||
<%= content_tag @title_component, class: 'index_title document-title-heading' do %> | ||
<%= before_title %><%= title %><%= after_title %> | ||
<% end %> | ||
<%= actions %> | ||
</header> | ||
|
||
<%= content %> | ||
<%= metadata %> | ||
</div> | ||
|
||
<%= thumbnail %> | ||
<% end %> | ||
<%= footer %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
class DocumentComponent < ::ViewComponent::Base | ||
# Available content areas; some have defaults provided by | ||
# the accessors below. | ||
with_content_areas :header, :body, :footer, | ||
:before_title, :title, :after_title, | ||
:actions, :metadata, :thumbnail, | ||
:partials | ||
with_collection_parameter :document | ||
|
||
# rubocop:disable Metrics/ParameterLists | ||
# @param document [Blacklight::Document] | ||
# @param presenter [Blacklight::DocumentPresenter] | ||
# @param id [String] HTML id for the root element | ||
# @param classes [Array, String] additional HTML classes for the root element | ||
# @param component [Symbol, String] HTML tag type to use for the root element | ||
# @param title_component [Symbol, String] HTML tag type to use for the title element | ||
# @param metadata_component [Blacklight::DocumentMetadataComponent] | ||
# @param counter [Number, nil] | ||
# @param show [Boolean] are we showing only a single document (vs a list of search results); used for backwards-compatibility | ||
def initialize(document: nil, presenter: nil, id: nil, classes: [], component: :article, title_component: :h4, metadata_component: Blacklight::DocumentMetadataComponent, counter: nil, show: false) | ||
if presenter.nil? && document.nil? | ||
raise ArgumentError, 'missing keyword: :document or :presenter' | ||
end | ||
|
||
@document = document || presenter&.document | ||
@presenter = presenter | ||
|
||
@component = component | ||
@title_component = title_component | ||
@id = id || ('document' if show) | ||
@classes = classes | ||
|
||
@metadata_component = metadata_component | ||
|
||
@counter = counter | ||
|
||
@show = show | ||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
|
||
# HTML classes to apply to the root element | ||
def classes | ||
[ | ||
@classes, | ||
@view_context.render_document_class(@document), | ||
'document', | ||
("document-position-#{@counter}" if @counter) | ||
].compact.flatten | ||
end | ||
|
||
# Content for the document title area; should be an inline element | ||
def title | ||
@title || begin | ||
if show? | ||
content_tag('span', presenter.heading, itemprop: "name") | ||
else | ||
@view_context.link_to_document @document, counter: @counter, itemprop: 'name' | ||
end | ||
end | ||
end | ||
|
||
# Content for the document actions area | ||
def actions | ||
return if @show | ||
|
||
@actions || begin | ||
@view_context.render_index_doc_actions @document, wrapping_class: "index-document-functions col-sm-3 col-lg-2" | ||
end | ||
end | ||
|
||
# Content for the document thumbnail area | ||
def thumbnail | ||
return if @show | ||
|
||
@thumbnail || begin | ||
return unless presenter.thumbnail.exists? | ||
|
||
content_tag :div, class: "document-thumbnail" do | ||
presenter.thumbnail.thumbnail_tag({ alt: '' }, 'aria-hidden': true, tabindex: -1, counter: @counter) | ||
end | ||
end | ||
end | ||
|
||
# Content for the document metadata area | ||
def metadata | ||
@metadata || @view_context.render(@metadata_component.new(fields: presenter.field_presenters, show: show?)) | ||
end | ||
|
||
# Content that goes before the document title (e.g. the counter) | ||
def before_title | ||
@before_title || counter | ||
end | ||
|
||
private | ||
|
||
def counter | ||
return unless @counter | ||
|
||
content_tag :span, class: 'document-counter' do | ||
t('blacklight.search.documents.counter', counter: @counter) | ||
end | ||
end | ||
|
||
def presenter | ||
@presenter ||= begin | ||
if show? | ||
@view_context.presenter(@document) | ||
else | ||
@view_context.index_presenter(@document) | ||
end | ||
end | ||
end | ||
|
||
def show? | ||
@show | ||
end | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
app/components/blacklight/document_metadata_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<dl class="document-metadata dl-invert row"> | ||
<% @fields.each do |field| -%> | ||
<%= @view_context.render(field_component(field).new(field: field, show: @show)) %> | ||
<% end -%> | ||
</dl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
class DocumentMetadataComponent < ::ViewComponent::Base | ||
with_collection_parameter :fields | ||
|
||
# @param fields [Enumerable<Blacklight::FieldPresenter>] Document field presenters | ||
def initialize(fields:, show: false) | ||
@fields = fields | ||
@show = show | ||
end | ||
|
||
def render? | ||
@fields.any? | ||
end | ||
|
||
def field_component(field) | ||
field.try(:component) || Blacklight::MetadataFieldComponent | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%= render(@layout.new(field: @field)) do |component| %> | ||
<% component.with(:label) do %> | ||
<%= label %> | ||
<% end %> | ||
<% component.with(:value) do %> | ||
<%= @field.render %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
class MetadataFieldComponent < ::ViewComponent::Base | ||
with_collection_parameter :field | ||
|
||
# @param field [Blacklight::FieldPresenter] | ||
# @param layout [Blacklight::MetadataFieldLayoutComponent] alternate layout component to use | ||
# @param show [Boolean] are we showing only a single document (vs a list of search results); used for backwards-compatibility | ||
def initialize(field:, layout: nil, show: false) | ||
@field = field | ||
@layout = layout || Blacklight::MetadataFieldLayoutComponent | ||
@show = show | ||
end | ||
|
||
# @private | ||
def label | ||
Deprecation.silence(Blacklight::BlacklightHelperBehavior) do | ||
if @show | ||
@view_context.render_document_show_field_label @field.document, label: @field.label, field: @field.key | ||
else | ||
@view_context.render_index_field_label @field.document, label: @field.label, field: @field.key | ||
end | ||
end | ||
end | ||
|
||
def render? | ||
@field.render_field? | ||
end | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
app/components/blacklight/metadata_field_layout_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<dt class="blacklight-<%= @key %> <%= @label_class %>"><%= label %></dt> | ||
<dd class="blacklight-<%= @key %> <%= @value_class %>"><%= value %></dd> |
16 changes: 16 additions & 0 deletions
16
app/components/blacklight/metadata_field_layout_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
class MetadataFieldLayoutComponent < ::ViewComponent::Base | ||
with_collection_parameter :field | ||
with_content_areas :label, :value | ||
|
||
# @param field [Blacklight::FieldPresenter] | ||
def initialize(field:, label_class: 'col-md-3', value_class: 'col-md-9') | ||
@field = field | ||
@key = @field.key.parameterize | ||
@label_class = label_class | ||
@value_class = value_class | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<% # container for a single doc -%> | ||
<article class="document <%= render_document_class document %> document-position-<%= document_counter%> " data-document-counter="<%= document_counter %>" itemscope itemtype="<%= document.itemtype %>"> | ||
<%= render_document_partials document, | ||
blacklight_config.view_config(document_index_view_type).partials, | ||
document_counter: document_counter %> | ||
</article> | ||
<%= render (blacklight_config.view_config(document_index_view_type).document_component || Blacklight::DocumentComponent).new(document: document, counter: document_counter_with_offset(document_counter)) do |component| %> | ||
<% component.with(blacklight_config.view_config(document_index_view_type).document_component.blank? && blacklight_config.view_config(document_index_view_type).partials.any? ? :body : :partials) do %> | ||
<%= render_document_partials document, blacklight_config.view_config(document_index_view_type).partials, component: component, document_counter: document_counter %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1 @@ | ||
<% doc_presenter = index_presenter(document) %> | ||
<%# default partial to display solr document fields in catalog index view -%> | ||
<dl class="document-metadata dl-invert row"> | ||
|
||
<% doc_presenter.fields_to_render.each do |field_name, field, field_presenter| -%> | ||
<dt class="blacklight-<%= field_name.parameterize %> col-md-3"><%= render_index_field_label document, label: field_presenter.label, field: field_name %></dt> | ||
<dd class="blacklight-<%= field_name.parameterize %> col-md-9"><%= field_presenter.render %></dd> | ||
<% end -%> | ||
|
||
</dl> | ||
<%= render(Blacklight::DocumentMetadataComponent.new(fields: index_presenter(document).field_presenters)) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<% doc_presenter = show_presenter(document) %> | ||
<%# default partial to display solr document fields in catalog show view -%> | ||
<dl class="row dl-invert document-metadata"> | ||
<% doc_presenter.fields_to_render.each do |field_name, field, field_presenter| -%> | ||
<dt class="blacklight-<%= field_name.parameterize %> col-md-3"><%= render_document_show_field_label document, label: field_presenter.label, field: field_name %></dt> | ||
<dd class="blacklight-<%= field_name.parameterize %> col-md-9"><%= field_presenter.render %></dd> | ||
<% end -%> | ||
</dl> | ||
<%= render( | ||
Blacklight::DocumentMetadataComponent.new( | ||
fields: show_presenter(document).field_presenters, | ||
show: true | ||
) | ||
) %> |
Oops, something went wrong.