Skip to content

Commit

Permalink
Merge pull request #2319 from projectblacklight/more-components
Browse files Browse the repository at this point in the history
More components
  • Loading branch information
jcoyne committed Sep 30, 2020
2 parents 8a65b7e + ce2f8e2 commit 49cec35
Show file tree
Hide file tree
Showing 57 changed files with 544 additions and 223 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Layout/LineLength:
Naming/HeredocDelimiterNaming:
Enabled: false

Naming/MethodParameterName:
AllowedNames:
- id
- q

Naming/PredicateName:
ForbiddenPrefixes:
- is_
Expand Down
16 changes: 16 additions & 0 deletions app/components/blacklight/document/bookmark_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%-
# the data-doc-id attribute is used by our JS that converts to a checkbox/label.
-%>
<%= form_tag(@bookmark_path,
method: bookmarked? ? :delete : :put,
class: "bookmark-toggle",
data: {
'doc-id' => @document.id,
present: t('blacklight.search.bookmarks.present'),
absent: t('blacklight.search.bookmarks.absent'),
inprogress: t('blacklight.search.bookmarks.inprogress')
}) do %>
<%= submit_tag(t(bookmarked? ? 'remove.button' : 'add.button', scope: 'blacklight.bookmarks'),
id: "bookmark_toggle_#{@document.id.to_s.parameterize}",
class: "bookmark-#{bookmarked? ? 'remove' : 'add'} btn btn-outline-secondary") %>
<% end %>
19 changes: 19 additions & 0 deletions app/components/blacklight/document/bookmark_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Blacklight
module Document
class BookmarkComponent < ::ViewComponent::Base
def initialize(document:, checked: nil, bookmark_path: nil)
@document = document
@checked = checked
@bookmark_path = bookmark_path
end

def bookmarked?
return @checked unless @checked.nil?

@view_context.bookmarked? @document
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<h1 class="modal-title"><%= title %></h1>

<%= @formats.each do |i18n_key, citation_method| %>
<h2><%= t(i18n_key) %></h2>
<%= @document.send(citation_method).html_safe %>
<% unless @formats.keys.last === i18n_key %><br/><br/><% end %>
<% end %>
</div>
24 changes: 24 additions & 0 deletions app/components/blacklight/document/citation_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Blacklight
module Document
class CitationComponent < ::ViewComponent::Base
DEFAULT_FORMATS = {
'blacklight.citation.mla': :export_as_mla_citation_txt,
'blacklight.citation.apa': :export_as_apa_citation_txt,
'blacklight.citation.chicago': :export_as_chicago_citation_txt
}.freeze

with_collection_parameter :document

def initialize(document:, formats: DEFAULT_FORMATS)
@document = document
@formats = formats.select { |_k, v| @document.respond_to?(v) }
end

def title
@view_context.document_heading(@document)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="card">
<div class="card-header">More Like This</div>
<div class="card-body">
<ul>
<%= @document.more_like_this.each do |document| %>
<li class="more_like_this_document">
<span class="mlt_title"><%= link_to_document document %></span>
</li>
<% end %>
</ul>
</div>
</div>
21 changes: 21 additions & 0 deletions app/components/blacklight/document/more_like_this_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Blacklight
module Document
class MoreLikeThisComponent < ::ViewComponent::Base
with_collection_parameter :document

def initialize(document:)
@document = document
end

def render?
@document.more_like_this.present?
end

def link_to_document(*args)
@view_context.link_to_document(*args)
end
end
end
end
25 changes: 25 additions & 0 deletions app/components/blacklight/response/facet_group_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<% # main container for facets/limits menu -%>
<%= content_tag :div, id: @id, class: 'facets sidenav facets-toggleable-md' do %>
<div class="facets-header">
<%= content_tag :h2, @title, class: 'facets-heading' if @title %>
<%= content_tag :button,
class:'navbar-toggler navbar-toggler-right',
type: 'button',
data: {
toggle: 'collapse',
target: "##{@panel_id}",
},
aria: {
controls: @panel_id,
expanded: 'false',
label: t('blacklight.search.facets.group.toggle'),
} do %>
<span class="navbar-toggler-icon"></span>
<% end %>
</div>

<%= content_tag :div, id: @panel_id, class: 'facets-collapse collapse' do %>
<%= @view_context.render_facet_partials @fields, response: @response %>
<% end %>
<% end %>
19 changes: 19 additions & 0 deletions app/components/blacklight/response/facet_group_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Blacklight
module Response
class FacetGroupComponent < ::ViewComponent::Base
def initialize(response:, fields: [], title: nil, id: nil)
@response = response
@fields = fields
@title = title
@id = id ? "facets-#{id}" : 'facets'
@panel_id = id ? "facet-panel-#{id}-collapse" : 'facet-panel-collapse'
end

def render?
@view_context.has_facet_values?(@fields, @response)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= content_tag :nav, class: 'pagination', role: 'region', **@html_attr do %>
<%= pagination %>
<% end %>
17 changes: 17 additions & 0 deletions app/components/blacklight/response/pagination_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Blacklight
module Response
class PaginationComponent < ::ViewComponent::Base
def initialize(response:, html: {}, **pagination_args)
@response = response
@html_attr = { aria: { label: t('views.pagination.aria.container_label') } }.merge(html)
@pagination_args = { outer_window: 2, theme: 'blacklight' }.merge(pagination_args)
end

def pagination
@view_context.paginate @response, **@pagination_args
end
end
end
end
6 changes: 6 additions & 0 deletions app/components/blacklight/response/sort_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= render(Blacklight::System::DropdownComponent.new(
param: @param,
choices: @choices,
id: @id,
search_state: @search_state,
selected: @selected)) %>
16 changes: 16 additions & 0 deletions app/components/blacklight/response/sort_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Blacklight
module Response
class SortComponent < ViewComponent::Base
def initialize(param: 'sort', choices: {}, search_state:, id: 'sort-dropdown', classes: [], selected: nil)
@param = param
@choices = choices
@search_state = search_state
@id = id
@classes = classes
@selected = selected
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="spell">
<h3 class="suggest">
<em>
<%= t('blacklight.did_you_mean', options: safe_join(@options.map { |word| link_to_query(word) }, " #{t('blacklight.or')} ")).html_safe %>
</em>
</h3>
</div>
20 changes: 20 additions & 0 deletions app/components/blacklight/response/spellcheck_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Blacklight
module Response
class SpellcheckComponent < ViewComponent::Base
def initialize(response:, options: nil)
@response = response
@options = options || @response&.spelling&.words
end

def link_to_query(query)
@view_context.link_to_query(query)
end

def render?
@options.any? && @view_context.should_show_spellcheck_suggestions?(@response)
end
end
end
end
29 changes: 29 additions & 0 deletions app/components/blacklight/search_bar_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_tag @url, method: @method, class: @classes.join(' '), role: 'search', 'aria-label' => t('blacklight.search.form.submit') do %>
<%= render_hash_as_hidden_fields(@params) %>
<% if @search_fields.length > 1 %>
<label for="search_field" class="sr-only"><%= t('blacklight.search.form.search_field.label') %></label>
<% end %>
<div class="input-group">
<% if @search_fields.length > 1 %>
<%= select_tag(:search_field,
options_for_select(@search_fields, h(@search_field)),
title: t('blacklight.search.form.search_field.title'),
id: "#{@prefix}search_field",
class: "custom-select search-field") %>
<% elsif @search_fields.length == 1 %>
<%= hidden_field_tag :search_field, search_fields.first.last %>
<% end %>

<label for="<%= @prefix %>q" class="sr-only"><%= t('blacklight.search.form.search.label') %></label>
<%= text_field_tag :q, @q, placeholder: t('blacklight.search.form.search.placeholder'), class: "search-q q form-control rounded-#{@search_fields.length > 1 ? '0' : 'left'}", id: "#{@prefix}q", autocomplete: @autocomplete_path.present? ? "off" : "", autofocus: @autofocus, data: { autocomplete_enabled: @autocomplete_path.present?, autocomplete_path: @autocomplete_path } %>
<%= content %>

<span class="input-group-append">
<button type="submit" class="btn btn-primary search-btn" id="<%= @prefix %>search">
<span class="submit-search-text"><%= t('blacklight.search.form.submit') %></span>
<%= blacklight_icon :search, aria_hidden: true %>
</button>
</span>
</div>
<% end %>
45 changes: 45 additions & 0 deletions app/components/blacklight/search_bar_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Blacklight
class SearchBarComponent < ::ViewComponent::Base
# rubocop:disable Metrics/ParameterLists
def initialize(url:, params:, classes: [], presenter: nil, prefix: '', method: 'GET', q: nil, search_field: nil, search_fields: [], autocomplete_path: nil, autofocus: nil)
@url = url
@q = q || params[:q]
@search_field = search_field || params[:search_field]
@params = params.except(:q, :search_field, :utf8, :page)
@prefix = prefix
@classes = classes
@presenter = presenter
@method = method
@autocomplete_path = autocomplete_path
@autofocus = autofocus
@search_fields = search_fields
end
# rubocop:enable Metrics/ParameterLists

def autocomplete_path
return nil unless presenter.autocomplete_enabled?

@autocomplete_path
end

def autofocus
if @autofocus.nil?
presenter.autofocus?
else
@autofocus
end
end

private

def presenter
@presenter ||= blacklight_config.index.search_bar_presenter_class.new(controller, blacklight_config)
end

def blacklight_config
@view_context.blacklight_config
end
end
end
10 changes: 10 additions & 0 deletions app/components/blacklight/search_context_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class='pagination-search-widgets'>

<div class="page-links">
<%= link_to_previous_document @search_context[:prev] %> |

<%= item_page_entry_info %> |

<%= link_to_next_document @search_context[:next] %>
</div>
</div>
30 changes: 30 additions & 0 deletions app/components/blacklight/search_context_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Blacklight
class SearchContextComponent < ::ViewComponent::Base
with_collection_parameter :search_context

def initialize(search_context:, search_session:)
@search_context = search_context
@search_session = search_session
end

def render?
@search_context.present? && (@search_context[:prev] || @search_context[:next])
end

def item_page_entry_info
Deprecation.silence(Blacklight::CatalogHelperBehavior) do
@view_context.item_page_entry_info
end
end

def link_to_previous_document(*args)
@view_context.link_to_previous_document(*args)
end

def link_to_next_document(*args)
@view_context.link_to_next_document(*args)
end
end
end
12 changes: 12 additions & 0 deletions app/components/blacklight/system/dropdown_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= content_tag :div, id: @id, class: @classes.join(' ') do %>
<button type="button" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<%= t(:button_label_html, default: :label_html, scope: "blacklight.search.#{@param}", @interpolation => label_for_value(@selected)) %> <span class="caret"></span>
</button>

<div class="dropdown-menu" role="menu">
<%- @choices.each do |option| %>
<% text, value = option_text_and_value(option) %>
<%= link_to(text, url_for(@search_state.params_for_search(@param => value)), class: 'dropdown-item', role: 'menuitem') %>
<%- end -%>
</div>
<% end %>
Loading

0 comments on commit 49cec35

Please sign in to comment.