Class: Blacklight::DocumentComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/blacklight/document_component.rb

Overview

Note:

when subclassing this component, if you override the initializer, you must explicitly specify the counter variable ‘document_counter` even if you don’t use it. Otherwise view_component will not provide the count value when calling the component.

A component for rendering a single document

Examples:

class MyDocumentComponent < Blacklight::DocumentComponent
  def initialize(document_counter: nil, **kwargs)
    super
    ... custom code ...
  end
end

See Also:

Constant Summary collapse

COLLECTION_INDEX_OFFSET =

ViewComponent 3 changes iteration counters to begin at 0 rather than 1

ViewComponent::VERSION::MAJOR < 3 ? 0 : 1

Constants inherited from Component

Component::EXCLUDE_VARIABLES

Instance Method Summary collapse

Methods inherited from Component

compiler, config, #inspect

Constructor Details

#initialize(document: nil, presenter: nil, partials: nil, id: nil, classes: [], component: :article, title_component: nil, counter: nil, document_counter: nil, counter_offset: 0, show: false, **args) ⇒ DocumentComponent

rubocop:disable Metrics/ParameterLists

Parameters:

  • document (Blacklight::DocumentPresenter) (defaults to: nil)
  • presenter (Blacklight::DocumentPresenter) (defaults to: nil)

    alias for document

  • partials (Array, nil) (defaults to: nil)

    view partial names that should be used to provide content for the ‘partials` slot

  • id (String) (defaults to: nil)

    HTML id for the root element

  • classes (Array, String) (defaults to: [])

    additional HTML classes for the root element

  • component (Symbol, String) (defaults to: :article)

    HTML tag type to use for the root element

  • title_component (Symbol, String) (defaults to: nil)

    HTML tag type to use for the title element

  • counter (Number, nil) (defaults to: nil)

    a pre-computed counter for the position of this document in a search result set

  • document_counter (Number, nil) (defaults to: nil)

    provided by ViewComponent collection iteration

  • counter_offset (Number) (defaults to: 0)

    the offset of the start of the collection counter parameter for the component to the overall result set

  • show (Boolean) (defaults to: false)

    are we showing only a single document (vs a list of search results); used for backwards-compatibility



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/blacklight/document_component.rb', line 94

def initialize(document: nil, presenter: nil, partials: nil,
               id: nil, classes: [], component: :article, title_component: nil,
               counter: nil, document_counter: nil, counter_offset: 0,
               show: false, **args)
  Blacklight.deprecation.warn('the `presenter` argument to DocumentComponent#initialize is deprecated; pass the `presenter` in as document instead') if presenter

  @presenter = presenter || document || args[self.class.collection_parameter]
  @document = @presenter.document
  @view_partials = partials || []

  @component = component
  @title_component = title_component
  @id = id || ('document' if show)
  @classes = classes

  @counter = counter
  @document_counter = document_counter || args.fetch(self.class.collection_counter_parameter, nil)
  @counter ||= @document_counter + COLLECTION_INDEX_OFFSET + counter_offset if @document_counter.present?

  @show = show
end

Instance Method Details

#before_renderObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/components/blacklight/document_component.rb', line 127

def before_render
  set_slot(:title, nil) unless title
  set_slot(:thumbnail, nil) unless thumbnail || show?
  set_slot(:metadata, nil, fields: presenter.field_presenters) unless 
  set_slot(:embed, nil) unless embed
  if view_partials.present?
    view_partials.each do |view_partial|
      with_partial(view_partial) do
        helpers.render_document_partial @document, view_partial, component: self, document_counter: @counter
      end
    end
  else
    set_slot(:partials, nil)
  end
end

#classesObject

HTML classes to apply to the root element



118
119
120
121
122
123
124
125
# File 'app/components/blacklight/document_component.rb', line 118

def classes
  [
    @classes,
    helpers.render_document_class(@document),
    'document',
    ("document-position-#{@counter}" if @counter)
  ].compact.flatten
end