Skip to content

Commit

Permalink
🎁 Add toggle for pdf viewer and download button
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kirkkwang committed Nov 16, 2023
1 parent 8d21dde commit 5a9c14a
Show file tree
Hide file tree
Showing 25 changed files with 298 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/hyku.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/forms/hyrax/etd_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/forms/hyrax/generic_work_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/forms/hyrax/image_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/forms/hyrax/paper_or_report_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions app/forms/hyrax/pdf_form_behavior.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/helpers/pdf_js_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions app/models/concerns/pdf_behavior.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/etd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions app/models/generic_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/paper_or_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 11 additions & 2 deletions app/presenters/hyku/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down
14 changes: 14 additions & 0 deletions app/views/hyrax/base/_download_pdf.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if can?(:download, file_set_id) && (Site.account.settings[:allow_downloads].nil? || Site.account.settings[:allow_downloads].to_i.nonzero?) %>
<div class="download-pdf-controls">
<% 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 %>
</div>
<% end %>
72 changes: 72 additions & 0 deletions app/views/hyrax/base/_form_files.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<div id="fileupload">
<!-- Redirect browsers with JavaScript disabled to the origin page -->
<noscript><input type="hidden" name="redirect" value="<%= main_app.root_path %>" /></noscript>
<!-- The table listing the files available for upload/download -->
<table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
<% 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 %>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="fileupload-buttonbar">
<div class="row">
<div class="col-xs-12">
<div class="fileinput-button" id="add-files">
<input id="addfiles" type="file" style="display:none;" name="files[]" multiple />
<button type="button" class="btn btn-success" onclick="document.getElementById('addfiles').click();">
<span class="glyphicon glyphicon-plus"></span>
<span><%= t(".add_files") %></span>
</button>
</div>
<div class="fileinput-button">
<input id="addfolder" type="file" style="display:none;" name="files[]" multiple directory webkitdirectory />
<button type="button" class="btn btn-success" onclick="document.getElementById('addfolder').click();">
<span class="glyphicon glyphicon-plus"></span>
<span><%= t(".add_folder") %></span>
</button>
</div>
<% 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 %>
<span class="glyphicon glyphicon-plus"></span>
<%= t('hyrax.upload.browse_everything.browse_files_button') %>
<% end %>
<% end %>
<button type="reset" id="file-upload-cancel-btn" class="btn btn-warning cancel hidden">
<span class="glyphicon glyphicon-ban-circle"></span>
<span><%= t('.cancel_upload') %></span>
</button>
<!-- The global file processing state -->
<span class="fileupload-process"></span>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<!-- The global progress state -->
<div class="fileupload-progress fade">
<!-- The global progress bar -->
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar progress-bar-success" style="width:0%;"></div>
</div>
<!-- The extended global progress state -->
<div class="progress-extended">&nbsp;</div>
</div>
</div>
</div>
</div>
<div class="dropzone">
<%= t('hyrax.base.form_files.dropzone') %>
</div>
</div>

<%= render 'hyrax/uploads/js_templates' %>
14 changes: 14 additions & 0 deletions app/views/hyrax/base/_representative_media.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
4 changes: 4 additions & 0 deletions app/views/hyrax/base/_show_pdf_download_button.erb
Original file line number Diff line number Diff line change
@@ -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 %>
4 changes: 4 additions & 0 deletions app/views/hyrax/base/_show_pdf_viewer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= f.input :show_pdf_viewer,
label: "Show PDF.js Viewer",
required: f.object.required?(:show_pdf_viewer),
as: :boolean %>
2 changes: 2 additions & 0 deletions app/views/hyrax/base/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
<% else %>
<div class="col-sm-3 text-center">
<%= 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' %>
</div>
<% end %>
<% end %>
<% if @presenter.viewer? %>
<div class="col-sm-3 text-center">
<%= 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' %>
</div>
Expand Down
58 changes: 58 additions & 0 deletions app/views/hyrax/file_sets/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button" id="dropdownMenu_<%= file_set.id %>" aria-haspopup="true" aria-expanded="false">
<span class="sr-only"><%= t('.press_to') %> </span>
<%= t('.header') %>
<span class="caret" aria-hidden="true"></span>
</button>

<ul role="menu" class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenu_<%= file_set.id %>">
<% if can?(:edit, file_set.id) %>
<li role="menuitem" tabindex="-1">
<%= link_to t('.edit'), edit_polymorphic_path([main_app, file_set]),
{ title: t('.edit_title', file_set: file_set) } %>
</li>

<li role="menuitem" tabindex="-1">
<%= link_to t('.versions'), edit_polymorphic_path([main_app, file_set], anchor: 'versioning_display'),
{ title: t('.versions_title') } %>
</li>
<% end %>
<% if can?(:destroy, file_set.id) %>
<li role="menuitem" tabindex="-1">
<%= link_to t('.delete'), polymorphic_path([main_app, file_set]),
method: :delete, title: t('.delete_title', file_set: file_set),
data: { confirm: t('.delete_confirm', file_set: file_set, application_name: application_name) } %>
</li>
<% end %>
<% if can?(:download, file_set.id) && (Site.account.settings[:allow_downloads].nil? || Site.account.settings[:allow_downloads].to_i.nonzero?) %>
<li role="menuitem" tabindex="-1">
<%= link_to t('.download'),
hyrax.download_path(file_set),
title: t('.download_title', file_set: file_set),
target: "_blank",
id: "file_download",
class: "download",
data: { label: file_set.id, work_id: @presenter.id, collection_ids: @presenter.member_of_collection_ids } %>
</li>
<% end %>

</ul>
</div>
<% end %>
<% end %>
Loading

0 comments on commit 5a9c14a

Please sign in to comment.