-
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.
allow show tools to be moved to the header, closes #2316
- Loading branch information
Showing
10 changed files
with
222 additions
and
16 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
7 changes: 7 additions & 0 deletions
7
app/components/blacklight/document/page_header_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,7 @@ | ||
<%= render applied_params_component %> | ||
<div class="<%= header_container_classes %>"> | ||
<div class="<%= pagination_container_classes %>"> | ||
<%= render pagination_component %> | ||
</div> | ||
<%= render_header_tools %> | ||
</div> |
87 changes: 87 additions & 0 deletions
87
app/components/blacklight/document/page_header_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,87 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
module Document | ||
# Render the start over and prev/next displays | ||
class PageHeaderComponent < Blacklight::Component | ||
attr_reader :document, :blacklight_config, :search_context, :search_session | ||
|
||
delegate :blacklight_config, to: :helpers | ||
|
||
def initialize(document:, search_context:, search_session:) | ||
super | ||
@search_context = search_context | ||
@search_session = search_session | ||
@document = document | ||
end | ||
|
||
def render? | ||
search_context.present? || search_session.present? || has_header_tools? | ||
end | ||
|
||
def applied_params_component | ||
return unless blacklight_config.track_search_session.applied_params_component | ||
|
||
blacklight_config.track_search_session.applied_params_component.new | ||
end | ||
|
||
def pagination_component | ||
return unless blacklight_config.track_search_session.item_pagination_component | ||
|
||
blacklight_config.track_search_session.item_pagination_component.new(search_context: search_context, search_session: search_session, current_document: document) | ||
end | ||
|
||
def has_header_tools? | ||
header_actions.any? || show_header_tools_component | ||
end | ||
|
||
def pagination_container_classes | ||
has_header_tools? ? 'col-12 col-md-6 ms-auto' : '' | ||
end | ||
|
||
def header_container_classes | ||
has_header_tools? ? 'row pagination-search-widgets pb-2' : 'pagination-search-widgets' | ||
end | ||
|
||
def header_actions | ||
actions = helpers.filter_partials(blacklight_config.view_config(:show).header_actions, { document: document }) | ||
actions.map { |_k, v| v } | ||
end | ||
|
||
def show_header_tools_component | ||
blacklight_config.view_config(:show).show_header_tools_component | ||
end | ||
|
||
def default_action_component_render | ||
render Blacklight::Document::ActionsComponent.new(document: document, | ||
tag: action_componet_tag, | ||
classes: classes, | ||
link_classes: link_classes, | ||
actions: header_actions, | ||
url_opts: Blacklight::Parameters.sanitize(params.to_unsafe_h)) | ||
end | ||
|
||
def action_componet_tag | ||
'div' | ||
end | ||
|
||
def classes | ||
'd-inline-flex header-tools align-items-center col-12 col-md-6 ms-auto justify-content-md-end' | ||
end | ||
|
||
def link_classes | ||
'btn btn-outline-primary ms-2' | ||
end | ||
|
||
def render_header_tools | ||
return unless has_header_tools? | ||
|
||
show_header_tools_component&.tap do |show_tools_component_class| | ||
return render show_tools_component_class.new(document: document) | ||
end | ||
|
||
default_action_component_render | ||
end | ||
end | ||
end | ||
end |
11 changes: 4 additions & 7 deletions
11
app/components/blacklight/search_context/server_item_pagination_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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
<div class='pagination-search-widgets'> | ||
<div class="search-context page-links"> | ||
<%= link_to_previous_document %> | | ||
|
||
<div class="page-links"> | ||
<%= link_to_previous_document %> | | ||
<%= item_page_entry_info %> | | ||
|
||
<%= item_page_entry_info %> | | ||
|
||
<%= link_to_next_document %> | ||
</div> | ||
<%= link_to_next_document %> | ||
</div> |
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
92 changes: 92 additions & 0 deletions
92
spec/components/blacklight/document/page_header_component_spec.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,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Blacklight::Document::PageHeaderComponent, type: :component do | ||
subject(:component) { described_class.new(document: document, search_context: search_context, search_session: current_search_session) } | ||
|
||
let(:show_header_tools_component) { Class.new(Blacklight::Document::ShowToolsComponent) } | ||
|
||
let(:view_context) { controller.view_context } | ||
let(:render) do | ||
component.render_in(view_context) | ||
end | ||
|
||
let(:rendered) do | ||
Capybara::Node::Simple.new(render) | ||
end | ||
|
||
let(:document) { SolrDocument.new(id: 'x', title_tsim: 'Title') } | ||
|
||
let(:blacklight_config) do | ||
CatalogController.blacklight_config.deep_copy | ||
end | ||
|
||
# rubocop:disable RSpec/SubjectStub | ||
before do | ||
# Every call to view_context returns a different object. This ensures it stays stable. | ||
allow(controller).to receive_messages(blacklight_config: blacklight_config) | ||
allow(controller).to receive(:current_search_session).and_return(double(id: document.id)) | ||
controller.class.helper_method :current_search_session | ||
allow(controller).to receive_messages(controller_name: 'catalog', link_to_previous_document: '', link_to_next_document: '') | ||
allow(view_context).to receive_messages(search_context: search_context, search_session: current_search_session, current_search_session: current_search_session) | ||
allow(component).to receive(:render).and_call_original | ||
allow(component).to receive(:render).with(an_instance_of(show_header_tools_component)).and_return('tool component content') | ||
replace_hash = { 'application/_start_over.html.erb' => 'Start Over' } | ||
if Rails.version.to_f >= 7.1 | ||
controller.prepend_view_path(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash)) | ||
else | ||
view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash)) | ||
end | ||
end | ||
# rubocop:enable RSpec/SubjectStub | ||
|
||
context "all variables are empty" do | ||
let(:search_context) { nil } | ||
let(:current_search_session) { {} } | ||
|
||
it 'does not render' do | ||
expect(rendered.native.inner_html).to be_blank | ||
end | ||
|
||
context 'has header tools' do | ||
before do | ||
blacklight_config.show.show_header_tools_component = show_header_tools_component | ||
end | ||
|
||
it 'renders the tools' do | ||
expect(rendered).to have_text 'tool component content' | ||
expect(rendered).to have_css '.row' | ||
end | ||
end | ||
end | ||
|
||
context "has pagination" do | ||
let(:search_context) { { next: next_doc, prev: prev_doc } } | ||
let(:prev_doc) { SolrDocument.new(id: '777') } | ||
let(:next_doc) { SolrDocument.new(id: '888') } | ||
let(:current_search_session) { { query_params: { q: 'abc' }, 'id' => '123', 'document_id' => document.id } } | ||
|
||
it 'renders pagination' do | ||
expect(rendered).to have_text 'Previous' | ||
expect(rendered).to have_text 'Next' | ||
expect(rendered).to have_text 'Start Over' | ||
expect(rendered).to have_text 'Back to Search' | ||
end | ||
|
||
context 'has header tools' do | ||
before do | ||
blacklight_config.show.show_header_tools_component = show_header_tools_component | ||
end | ||
|
||
it 'renders the tools and pagination' do | ||
expect(rendered).to have_text 'Previous' | ||
expect(rendered).to have_text 'Next' | ||
expect(rendered).to have_text 'Start Over' | ||
expect(rendered).to have_text 'Back to Search' | ||
expect(rendered).to have_text 'tool component content' | ||
expect(rendered).to have_css '.row' | ||
end | ||
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