From 5a9c14a448aef75f6c0d7c9b887b45e855b28eec Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Wed, 15 Nov 2023 19:08:17 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20Add=20toggle=20for=20pdf=20viewe?= =?UTF-8?q?r=20and=20download=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will add two properties for all work types to allow for the pdf viewer and download button to be toggled on and off on the work show page. These two options will exist on the forms for each work type on the files tab. This will also hide the default download button on PDF.js so the users can only download from the newly introduced download button. --- app/assets/stylesheets/hyku.scss | 4 ++ app/forms/hyrax/etd_form.rb | 1 + app/forms/hyrax/generic_work_form.rb | 1 + app/forms/hyrax/image_form.rb | 1 + app/forms/hyrax/paper_or_report_form.rb | 1 + app/forms/hyrax/pdf_form_behavior.rb | 21 ++++++ app/helpers/pdf_js_helper.rb | 10 +++ app/models/concerns/pdf_behavior.rb | 40 +++++++++++ app/models/etd.rb | 1 + app/models/generic_work.rb | 2 + app/models/image.rb | 1 + app/models/paper_or_report.rb | 1 + app/models/solr_document.rb | 8 +++ app/presenters/hyku/work_show_presenter.rb | 13 +++- app/views/hyrax/base/_download_pdf.html.erb | 14 ++++ app/views/hyrax/base/_form_files.html.erb | 72 +++++++++++++++++++ .../hyrax/base/_representative_media.html.erb | 14 ++++ .../hyrax/base/_show_pdf_download_button.erb | 4 ++ .../hyrax/base/_show_pdf_viewer.html.erb | 4 ++ app/views/hyrax/base/show.html.erb | 2 + app/views/hyrax/file_sets/_actions.html.erb | 58 +++++++++++++++ .../file_sets/media_display/_pdf.html.erb | 24 +++++++ .../cultural_show/hyrax/base/show.html.erb | 1 + .../scholarly_show/hyrax/base/show.html.erb | 1 + public/pdf.js/web/viewer.html | 2 +- 25 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 app/forms/hyrax/pdf_form_behavior.rb create mode 100644 app/models/concerns/pdf_behavior.rb create mode 100644 app/views/hyrax/base/_download_pdf.html.erb create mode 100644 app/views/hyrax/base/_form_files.html.erb create mode 100644 app/views/hyrax/base/_representative_media.html.erb create mode 100644 app/views/hyrax/base/_show_pdf_download_button.erb create mode 100644 app/views/hyrax/base/_show_pdf_viewer.html.erb create mode 100644 app/views/hyrax/file_sets/_actions.html.erb create mode 100644 app/views/hyrax/file_sets/media_display/_pdf.html.erb diff --git a/app/assets/stylesheets/hyku.scss b/app/assets/stylesheets/hyku.scss index 797b7270..f0d1d97c 100644 --- a/app/assets/stylesheets/hyku.scss +++ b/app/assets/stylesheets/hyku.scss @@ -428,6 +428,10 @@ body.public-facing { } } +#download-pdf-button { + margin: 10px 0 10px 0; +} + // FEATURED COLLECTIONS @media (max-width: $screen-sm-min) { .admin-show-page diff --git a/app/forms/hyrax/etd_form.rb b/app/forms/hyrax/etd_form.rb index 193d87bf..7f83c65a 100644 --- a/app/forms/hyrax/etd_form.rb +++ b/app/forms/hyrax/etd_form.rb @@ -6,6 +6,7 @@ module Hyrax # Generated form for Etd class EtdForm < Hyrax::Forms::WorkForm self.model_class = ::Etd + include PdfFormBehavior self.required_fields += %i[ year abstract diff --git a/app/forms/hyrax/generic_work_form.rb b/app/forms/hyrax/generic_work_form.rb index e98afbbc..f7398c80 100644 --- a/app/forms/hyrax/generic_work_form.rb +++ b/app/forms/hyrax/generic_work_form.rb @@ -7,6 +7,7 @@ class GenericWorkForm < Hyrax::Forms::WorkForm include Hyrax::FormTerms self.model_class = ::GenericWork include HydraEditor::Form::Permissions + include PdfFormBehavior self.terms += %i[ resource_type format diff --git a/app/forms/hyrax/image_form.rb b/app/forms/hyrax/image_form.rb index 3d3e09b5..765d7fad 100644 --- a/app/forms/hyrax/image_form.rb +++ b/app/forms/hyrax/image_form.rb @@ -6,6 +6,7 @@ module Hyrax class ImageForm < Hyrax::Forms::WorkForm include Hyrax::FormTerms self.model_class = ::Image + include PdfFormBehavior self.terms += %i[resource_type extent] end end diff --git a/app/forms/hyrax/paper_or_report_form.rb b/app/forms/hyrax/paper_or_report_form.rb index ae352447..f5fd7d6a 100644 --- a/app/forms/hyrax/paper_or_report_form.rb +++ b/app/forms/hyrax/paper_or_report_form.rb @@ -6,6 +6,7 @@ module Hyrax # Generated form for PaperOrReport class PaperOrReportForm < Hyrax::Forms::WorkForm self.model_class = ::PaperOrReport + include PdfFormBehavior self.required_fields += %i[ institution diff --git a/app/forms/hyrax/pdf_form_behavior.rb b/app/forms/hyrax/pdf_form_behavior.rb new file mode 100644 index 00000000..1ed049e1 --- /dev/null +++ b/app/forms/hyrax/pdf_form_behavior.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Hyrax + module PdfFormBehavior + extend ActiveSupport::Concern + + included do + class_attribute :hidden_terms + + self.terms += %i[show_pdf_viewer show_pdf_download_button] + self.hidden_terms = %i[show_pdf_viewer show_pdf_download_button] + # Not sure why this is needed but the form was not working without it + # it was getting a Unpermitted parameter error for these terms + permitted_params << %i[show_pdf_viewer show_pdf_download_button] + end + + def hidden?(key) + hidden_terms.include? key.to_sym + end + end +end diff --git a/app/helpers/pdf_js_helper.rb b/app/helpers/pdf_js_helper.rb index 72365d32..863ff87a 100644 --- a/app/helpers/pdf_js_helper.rb +++ b/app/helpers/pdf_js_helper.rb @@ -19,4 +19,14 @@ def query_param "search=#{params[:q]}&phrase=true" end + + def render_show_pdf_behavior_checkbox? + return unless Flipflop.default_pdf_viewer? + return if params[:id].nil? + + doc = SolrDocument.find params[:id] + + presenter = @_controller.show_presenter.new(doc, current_ability) + presenter.file_set_presenters.any?(&:pdf?) + end end diff --git a/app/models/concerns/pdf_behavior.rb b/app/models/concerns/pdf_behavior.rb new file mode 100644 index 00000000..ca2ede58 --- /dev/null +++ b/app/models/concerns/pdf_behavior.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module RDF + class CustomShowPdfViewerTerm < Vocabulary('http://id.loc.gov/vocabulary/identifiers/') + property 'show_pdf_viewer' + end + + class CustomShowPdfDownloadButtonTerm < Vocabulary('http://id.loc.gov/vocabulary/identifiers/') + property 'show_pdf_download_button' + end +end + +module PdfBehavior + extend ActiveSupport::Concern + + included do + property :show_pdf_viewer, predicate: RDF::CustomShowPdfViewerTerm.show_pdf_viewer, multiple: false do |index| + index.as :stored_searchable + end + + property :show_pdf_download_button, + predicate: RDF::CustomShowPdfDownloadButtonTerm.show_pdf_download_button, + multiple: false do |index| + index.as :stored_searchable + end + + after_initialize :set_default_show_pdf_viewer, :set_default_show_pdf_download_button + end + + private + + # This is here so that the checkbox is checked by default + def set_default_show_pdf_viewer + self.show_pdf_viewer ||= '1' + end + + def set_default_show_pdf_download_button + self.show_pdf_download_button ||= '1' + end +end diff --git a/app/models/etd.rb b/app/models/etd.rb index ad2cc477..e3d4d86c 100644 --- a/app/models/etd.rb +++ b/app/models/etd.rb @@ -7,6 +7,7 @@ class Etd < ActiveFedora::Base include IiifPrint.model_configuration( pdf_split_child_model: GenericWork ) + include PdfBehavior self.indexer = EtdIndexer # Change this to restrict which works can be added as a child. diff --git a/app/models/generic_work.rb b/app/models/generic_work.rb index 3d2e2c86..38162a3c 100644 --- a/app/models/generic_work.rb +++ b/app/models/generic_work.rb @@ -5,6 +5,8 @@ class GenericWork < ActiveFedora::Base include IiifPrint.model_configuration( pdf_split_child_model: self ) + include PdfBehavior + # this line needs to be before the validations & properties in order for them to be indexed correctly self.indexer = GenericWorkIndexer # Change this to restrict which works can be added as a child. diff --git a/app/models/image.rb b/app/models/image.rb index e0218ec9..dea60c55 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -7,6 +7,7 @@ class Image < ActiveFedora::Base include IiifPrint.model_configuration( pdf_split_child_model: self ) + include PdfBehavior property :extent, predicate: ::RDF::Vocab::DC.extent, multiple: true do |index| index.as :stored_searchable diff --git a/app/models/paper_or_report.rb b/app/models/paper_or_report.rb index f40448a4..3a8d8cfb 100644 --- a/app/models/paper_or_report.rb +++ b/app/models/paper_or_report.rb @@ -7,6 +7,7 @@ class PaperOrReport < ActiveFedora::Base include IiifPrint.model_configuration( pdf_split_child_model: GenericWork ) + include PdfBehavior self.indexer = PaperOrReportIndexer # Change this to restrict which works can be added as a child. diff --git a/app/models/solr_document.rb b/app/models/solr_document.rb index 4bd61d86..71c62327 100644 --- a/app/models/solr_document.rb +++ b/app/models/solr_document.rb @@ -79,4 +79,12 @@ class SolrDocument title: 'title_tesim', type: 'types_tesim' ) + + def show_pdf_viewer + self['show_pdf_viewer_tesim'] + end + + def show_pdf_download_button + self['show_pdf_download_button_tesim'] + end end diff --git a/app/presenters/hyku/work_show_presenter.rb b/app/presenters/hyku/work_show_presenter.rb index e77bee5d..3a3f3e49 100644 --- a/app/presenters/hyku/work_show_presenter.rb +++ b/app/presenters/hyku/work_show_presenter.rb @@ -2,6 +2,7 @@ # OVERRIDE here to add featured collection methods and to delegate collection presenters to the member presenter factory # OVERRIDE: Hyrax 3.4.0 to add Hyrax IIIF AV +# OVERRIDE: Hyrax 3.5.0 to add checks for PDF.js viewer module Hyku class WorkShowPresenter < Hyrax::WorkShowPresenter @@ -10,7 +11,7 @@ class WorkShowPresenter < Hyrax::WorkShowPresenter include Hyrax::IiifAv::DisplaysIiifAv Hyrax::MemberPresenterFactory.file_presenter_class = Hyrax::IiifAv::IiifFileSetPresenter - delegate :title_or_label, :extent, to: :solr_document + delegate :title_or_label, :extent, :show_pdf_viewer, :show_pdf_download_button, to: :solr_document # OVERRIDE Hyrax v2.9.0 here to make featured collections work delegate :collection_presenters, to: :member_presenter_factory @@ -72,9 +73,17 @@ def video_embed_viewer? def pdf_viewer? return unless Flipflop.default_pdf_viewer? + return unless show_pdf_viewer return unless file_set_presenters.any?(&:pdf?) - true + show_pdf_viewer.first.to_i.positive? + end + + def show_pdf_download_button? + return unless file_set_presenters.any?(&:pdf?) + return unless show_pdf_download_button + + show_pdf_download_button.first.to_i.positive? end def viewer? diff --git a/app/views/hyrax/base/_download_pdf.html.erb b/app/views/hyrax/base/_download_pdf.html.erb new file mode 100644 index 00000000..b794520c --- /dev/null +++ b/app/views/hyrax/base/_download_pdf.html.erb @@ -0,0 +1,14 @@ +<% if can?(:download, file_set_id) && (Site.account.settings[:allow_downloads].nil? || Site.account.settings[:allow_downloads].to_i.nonzero?) %> +
+ <% if @presenter.representative_presenter.present? && presenter.file_set_presenters.any?(&:pdf?) %> + <%= button_tag type: 'button', + id: "download-pdf-button", + data: { label: @presenter.representative_presenter.id, + path: hyrax.download_path(presenter.representative_presenter) }, + class: "btn btn-success btn-block download-pdf-button center-block", + onclick: "window.open(this.dataset.path, '_blank');" do %> + Download PDF + <% end %> + <% end %> +
+<% end %> diff --git a/app/views/hyrax/base/_form_files.html.erb b/app/views/hyrax/base/_form_files.html.erb new file mode 100644 index 00000000..08f7f829 --- /dev/null +++ b/app/views/hyrax/base/_form_files.html.erb @@ -0,0 +1,72 @@ +<%# OVERRIDE HYRAX 3.5.0 to add show_pdf_viewer to files tab %> + +<%# OVERRIDE begin %> +<% if render_show_pdf_behavior_checkbox? %> + <%= render partial: 'show_pdf_viewer', locals: { f: f } %> + <%= render partial: 'show_pdf_download_button', locals: { f: f } %> +<% end %> +<%# OVERRIDE end %> +
+ + + + + <% if Hyrax.config.browse_everything? %> + <%= t('hyrax.base.form_files.local_upload_browse_everything_html', contact_href: link_to(t("hyrax.upload.alert.contact_href_text"), hyrax.contact_form_index_path)) %> + <% else %> + <%= t('hyrax.base.form_files.local_upload_html') %> + <% end %> + +
+
+
+
+ + +
+
+ + +
+ <% if Hyrax.config.browse_everything? %> + <%= button_tag(type: 'button', class: 'btn btn-success', id: "browse-btn", + 'data-toggle' => 'browse-everything', 'data-route' => browse_everything_engine.root_path, + 'data-target' => "#{f.object.persisted? ? "#edit_#{f.object.model.model_name.param_key}_#{f.object.model.id}" : "#new_#{f.object.model.model_name.param_key}"}" ) do %> + + <%= t('hyrax.upload.browse_everything.browse_files_button') %> + <% end %> + <% end %> + + + +
+
+
+
+ +
+ +
+
+
+ +
 
+
+
+
+
+
+ <%= t('hyrax.base.form_files.dropzone') %> +
+
+ +<%= render 'hyrax/uploads/js_templates' %> diff --git a/app/views/hyrax/base/_representative_media.html.erb b/app/views/hyrax/base/_representative_media.html.erb new file mode 100644 index 00000000..b5500a56 --- /dev/null +++ b/app/views/hyrax/base/_representative_media.html.erb @@ -0,0 +1,14 @@ +<%# OVERRIDE: Hyrax 3.5.0 to add support for PDF.js %> + +<% if presenter.representative_id.present? && presenter.representative_presenter.present? %> + <% if defined?(viewer) && viewer && presenter.iiif_viewer?%> + <%= iiif_viewer_display presenter %> + <% elsif presenter.pdf_viewer? %> + <%= render 'pdf_js', file_set_presenter: pdf_file_set_presenter(@presenter) %> + <% else %> + <%= render media_display_partial(presenter.representative_presenter), file_set: presenter.representative_presenter %> + <% end %> +<% else %> + <% alt = block_for(name: 'default_work_image_text') || 'Default work thumbnail' %> + <%= image_tag default_work_image, class: "canonical-image", alt: alt %> +<% end %> diff --git a/app/views/hyrax/base/_show_pdf_download_button.erb b/app/views/hyrax/base/_show_pdf_download_button.erb new file mode 100644 index 00000000..aa326db7 --- /dev/null +++ b/app/views/hyrax/base/_show_pdf_download_button.erb @@ -0,0 +1,4 @@ +<%= f.input :show_pdf_download_button, + label: "Show Download PDF button", + required: f.object.required?(:show_pdf_download_button), + as: :boolean %> diff --git a/app/views/hyrax/base/_show_pdf_viewer.html.erb b/app/views/hyrax/base/_show_pdf_viewer.html.erb new file mode 100644 index 00000000..a2478201 --- /dev/null +++ b/app/views/hyrax/base/_show_pdf_viewer.html.erb @@ -0,0 +1,4 @@ +<%= f.input :show_pdf_viewer, + label: "Show PDF.js Viewer", + required: f.object.required?(:show_pdf_viewer), + as: :boolean %> diff --git a/app/views/hyrax/base/show.html.erb b/app/views/hyrax/base/show.html.erb index be2451b2..3dd2e6ce 100644 --- a/app/views/hyrax/base/show.html.erb +++ b/app/views/hyrax/base/show.html.erb @@ -29,6 +29,7 @@ <% else %>
<%= render 'representative_media', presenter: @presenter, viewer: false %> + <%= render('download_pdf', presenter: @presenter, file_set_id: @presenter.file_set_presenters.first.id) if @presenter.show_pdf_download_button? %> <%= render 'citations', presenter: @presenter %> <%= render 'social_media' %>
@@ -36,6 +37,7 @@ <% end %> <% if @presenter.viewer? %>
+ <%= render('download_pdf', presenter: @presenter, file_set_id: @presenter.file_set_presenters.first.id) if @presenter.show_pdf_download_button? %> <%= render 'citations', presenter: @presenter %> <%= render 'social_media' %>
diff --git a/app/views/hyrax/file_sets/_actions.html.erb b/app/views/hyrax/file_sets/_actions.html.erb new file mode 100644 index 00000000..ea6941a0 --- /dev/null +++ b/app/views/hyrax/file_sets/_actions.html.erb @@ -0,0 +1,58 @@ +<%# Overridden from Hyrax 3.5.0 - To add extra download restrictions %> +<% if (can?(:download, file_set.id) || can?(:destroy, file_set.id) || can?(:edit, file_set.id)) && !workflow_restriction?(@parent) %> + <% if can?(:download, file_set.id) && !(can?(:edit, file_set.id) || can?(:destroy, file_set.id)) %> + <% if (Site.account.settings[:allow_downloads].nil? || Site.account.settings[:allow_downloads].to_i.nonzero?) && + (@presenter.show_pdf_download_button? && file_set.pdf? || !file_set.pdf?) %> + <%= link_to t('.download'), + hyrax.download_path(file_set), + class: 'btn btn-default btn-sm', + title: t('.download_title', file_set: file_set), + target: "_blank", + id: "file_download", + data: { label: file_set.id, work_id: @presenter.id, collection_ids: @presenter.member_of_collection_ids } %> + <% end %> + <% else %> +
+ + + +
+ <% end %> +<% end %> diff --git a/app/views/hyrax/file_sets/media_display/_pdf.html.erb b/app/views/hyrax/file_sets/media_display/_pdf.html.erb new file mode 100644 index 00000000..a0dd111c --- /dev/null +++ b/app/views/hyrax/file_sets/media_display/_pdf.html.erb @@ -0,0 +1,24 @@ +<%# OVERRIDE Hyrax 3.5.0 to only show pdf download link on the file set show page %> +<% if display_media_download_link?(file_set: SolrDocument.find(file_set.id)) %> +
+

<%= t('hyrax.file_set.show.downloadable_content.heading') %>

+ <%= image_tag thumbnail_url(file_set), + class: "representative-media", + alt: "", + role: "presentation" %> + <% if controller_name == 'file_sets' %> + <%= 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 } %> + <% end %> +
+<% else %> +
+ <%= image_tag thumbnail_url(file_set), + class: "representative-media", + alt: "", + role: "presentation" %> +
+<% end %> diff --git a/app/views/themes/cultural_show/hyrax/base/show.html.erb b/app/views/themes/cultural_show/hyrax/base/show.html.erb index b7ff694a..f7d67079 100644 --- a/app/views/themes/cultural_show/hyrax/base/show.html.erb +++ b/app/views/themes/cultural_show/hyrax/base/show.html.erb @@ -39,6 +39,7 @@
<%= render 'relationships', presenter: @presenter %>
+ <%= render('download_pdf', presenter: @presenter, file_set_id: @presenter.file_set_presenters.first.id) if @presenter.show_pdf_download_button? %> <%= render 'citations', presenter: @presenter %>
diff --git a/app/views/themes/scholarly_show/hyrax/base/show.html.erb b/app/views/themes/scholarly_show/hyrax/base/show.html.erb index 5b20640d..a1820f41 100644 --- a/app/views/themes/scholarly_show/hyrax/base/show.html.erb +++ b/app/views/themes/scholarly_show/hyrax/base/show.html.erb @@ -53,6 +53,7 @@ <%= render 'metadata', presenter: @presenter %>
+ <%= render('download_pdf', presenter: @presenter, file_set_id: @presenter.file_set_presenters.first.id) if @presenter.show_pdf_download_button? %> <%= render 'citations', presenter: @presenter %>
diff --git a/public/pdf.js/web/viewer.html b/public/pdf.js/web/viewer.html index 45eec228..14ab4a98 100644 --- a/public/pdf.js/web/viewer.html +++ b/public/pdf.js/web/viewer.html @@ -273,7 +273,7 @@ Print -