Skip to content

Commit

Permalink
Merge pull request #2176 from samvera/fix-pdf-js-for-valkyrie
Browse files Browse the repository at this point in the history
🐛 Fix PDF.js for Valkyrie
  • Loading branch information
jeremyf authored Apr 4, 2024
2 parents ca69d95 + e64904b commit e720484
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ GIT

GIT
remote: https://github.com/samvera/hyrax.git
revision: a5a0ae9e56df857a92fc53ae86216cbb007db47a
revision: 54bb7b22aaf44dcdda76e05fb88b65095b2e899f
branch: double_combo
specs:
hyrax (5.0.0)
hyrax (5.0.1)
active-fedora (~> 14.0)
almond-rails (~> 0.1)
awesome_nested_set (~> 3.1)
Expand Down Expand Up @@ -201,7 +201,7 @@ GIT
sass-rails (~> 6.0)
select2-rails (~> 3.5)
signet
sprockets (~> 3.7)
sprockets (= 3.7.2)
tinymce-rails (~> 5.10)
valkyrie (~> 3.1.1)
view_component (~> 2.74.1)
Expand Down
1 change: 0 additions & 1 deletion app/forms/generic_work_resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ class GenericWorkResourceForm < Hyrax::Forms::ResourceForm(GenericWorkResource)
include Hyrax::FormFields(:generic_work_resource)
include Hyrax::FormFields(:with_pdf_viewer)
include Hyrax::FormFields(:with_video_embed)
include PdfBehavior
include VideoEmbedBehavior::Validation
end
1 change: 0 additions & 1 deletion app/forms/image_resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ class ImageResourceForm < Hyrax::Forms::ResourceForm(ImageResource)
include Hyrax::FormFields(:image_resource)
include Hyrax::FormFields(:with_pdf_viewer)
include Hyrax::FormFields(:with_video_embed)
include PdfBehavior
include VideoEmbedBehavior::Validation
end
14 changes: 5 additions & 9 deletions app/helpers/pdf_js_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ def query_param
end

def render_show_pdf_behavior_checkbox?
# we are showing PDF.js based on the FlipFlop value
# instead of checking for the property value.
# TODO: Valkyrize PDF.js feature
false
# return unless Flipflop.default_pdf_viewer?
# return if params[:id].nil?
return unless Flipflop.default_pdf_viewer?
return if params[:id].nil?

# doc = SolrDocument.find params[:id]
doc = SolrDocument.find params[:id]

# presenter = @_controller.show_presenter.new(doc, current_ability)
# presenter.file_set_presenters.any?(&:pdf?)
presenter = @_controller.show_presenter.new(doc, current_ability)
presenter.file_set_presenters.any?(&:pdf?)
end
end
18 changes: 16 additions & 2 deletions app/listeners/hyrax_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def on_collection_metadata_updated(event)
# def on_collection_membership_update
# end

# def on_file_characterized
# end
def on_file_characterized(event)
pdf_viewer_and_download_button(event)
end

# def on_file_downloaded
# end
Expand Down Expand Up @@ -99,4 +100,17 @@ def on_collection_metadata_updated(event)

# def on_object_metadata_updated
# end

private

def pdf_viewer_and_download_button(event)
file_set = event[:file_set]
return unless file_set.original_file.pdf?

parent_work = Hyrax.custom_queries.find_parent_work(resource: file_set)
parent_work.show_pdf_viewer = '1'
parent_work.show_pdf_download_button = '1'

Hyrax.persister.save(resource: parent_work)
end
end
7 changes: 3 additions & 4 deletions app/models/concerns/pdf_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ class CustomShowPdfDownloadButtonTerm < Vocabulary('http://id.loc.gov/vocabulary

module PdfBehavior
extend ActiveSupport::Concern
# TODO: Valkyrize PDF.js feature

included do
property :show_pdf_viewer, predicate: RDF::CustomShowPdfViewerTerm.show_pdf_viewer, multiple: false do |index|
# index.as :stored_searchable
index.as :stored_searchable
end

# rubocop:disable Metrics/LineLength
property :show_pdf_download_button, predicate: RDF::CustomShowPdfDownloadButtonTerm.show_pdf_download_button, multiple: false do |index|
# index.as :stored_searchable
index.as :stored_searchable
end
# rubocop:enable Metrics/LineLength

# after_initialize :set_default_show_pdf_viewer, :set_default_show_pdf_download_button
after_initialize :set_default_show_pdf_viewer, :set_default_show_pdf_download_button
end

private
Expand Down
17 changes: 10 additions & 7 deletions app/presenters/hyku/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,19 @@ def user_can_feature_collection?
# End Featured Collections Methods

def show_pdf_viewer?
Flipflop.default_pdf_viewer? && file_set_presenters.any?(&:pdf?)
# TODO: Valkyrize PDF.js feature
# return unless Flipflop.default_pdf_viewer?
# return unless show_pdf_viewer
# return unless file_set_presenters.any?(&:pdf?)
return unless Flipflop.default_pdf_viewer?
return unless show_pdf_viewer
return unless file_set_presenters.any?(&:pdf?)

# show_pdf_viewer.first.to_i.positive?
show_for_pdf?(show_pdf_viewer)
end

def show_pdf_download_button?
return unless Hyrax.config.display_media_download_link?
return unless file_set_presenters.any?(&:pdf?)
return unless show_pdf_download_button

show_pdf_download_button.first.to_i.positive?
show_for_pdf?(show_pdf_download_button)
end

def viewer?
Expand Down Expand Up @@ -128,5 +126,10 @@ def extract_from_identifier(rgx)
def extract_video_embed_presence
solr_document[:video_embed_tesim]&.first&.present?
end

def show_for_pdf?(field)
# With Valkyrie, we store the field as a boolean while AF stores it as an Array
valkyrie_presenter? ? field : field.first.to_i.positive?
end
end
end
23 changes: 23 additions & 0 deletions app/views/hyrax/file_sets/media_display/_pdf.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if display_media_download_link?(file_set: file_set) %>
<div>
<h2 class="sr-only"><%= t('hyrax.file_set.show.downloadable_content.heading') %></h2>
<%= image_tag thumbnail_url(file_set),
class: "representative-media",
alt: "",
role: "presentation" %>
<% if controller_name == 'file_sets' && @presenter.solr_document.load_parent_docs.any?(&:show_pdf_download_button) %>
<%= link_to t('hyrax.file_set.show.downloadable_content.pdf_link'),
hyrax.download_path(file_set),
target: :_blank,
id: "file_download",
data: { label: file_set.id, work_id: @presenter.id, collection_ids: @presenter.member_of_collection_ids } %>
<% end %>
</div>
<% else %>
<div>
<%= image_tag thumbnail_url(file_set),
class: "representative-media",
alt: "",
role: "presentation" %>
</div>
<% end %>
10 changes: 10 additions & 0 deletions config/metadata/with_pdf_viewer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ attributes:
- "show_pdf_viewer_bsi"
# For historic reasons, this is the predicate
predicate: http://id.loc.gov/vocabulary/identifiers/show_pdf_viewer
form:
display: true
primary: false
required: false
multiple: false
show_pdf_download_button:
# https://dry-rb.org/gems/dry-types/1.7/built-in-types/
type: bool
Expand All @@ -15,3 +20,8 @@ attributes:
- "show_pdf_download_button_bsi"
# For historic reasons, this is the predicate
predicate: http://id.loc.gov/vocabulary/identifiers/show_pdf_download_button
form:
display: true
primary: false
required: false
multiple: false
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# First find the Hyrax factories; then find the local factories (which extend/modify Hyrax
# factories).
FactoryBot.definition_file_paths = [
Hyrax::Engine.root.join("spec/factories/").to_s,
Hyrax::Engine.root.join("lib/hyrax/specs/shared_specs/factories").to_s,
File.expand_path("../factories", __FILE__)
]
FactoryBot.find_definitions
Expand Down

0 comments on commit e720484

Please sign in to comment.