Skip to content

Commit

Permalink
🎁 Allow admins to upload multiple banners
Browse files Browse the repository at this point in the history
This commit allows the admin to upload multiple banners to the homepage
through the dashboard.  The homepage will randomly select a banner and
then cycle through the uploaded banners.

Ref:
  - #657
  • Loading branch information
kirkkwang committed Aug 4, 2024
1 parent 6bb9460 commit 5bf1e5f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 19 deletions.
10 changes: 9 additions & 1 deletion app/assets/stylesheets/themes/dc_repository.scss
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,17 @@ body.dc_repository {
}

.background-container {
height: 90%;
height: 100%;
z-index: 0;
align-self: flex-end;
background-size: cover;
background-position: center;
opacity: 0;
transition: opacity 2s ease-in-out;
}

.background-container.active {
opacity: 1;
}

.circle-container {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def set_site
end

def update_params
params.permit(:remove_banner_image,
params.permit(:remove_banner_images,
:remove_logo_image,
:remove_directory_image,
:remove_default_collection_image,
Expand All @@ -37,7 +37,7 @@ def site_theme_params

REMOVE_TEXT_MAPS = {
"remove_logo_image" => "logo_image_text",
"remove_banner_image" => "banner_image_text",
"remove_banner_images" => "banner_image_text",
"remove_directory_image" => "directory_image_text",
"remove_default_collection_image" => "default_collection_image_text",
"remove_default_work_image" => "default_work_image_text"
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/hyrax_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def institution_name_full
Site.institution_name_full || super
end

def banner_image
Site.instance.banner_image? ? Site.instance.banner_image.url : super
def banner_images
Site.instance.banner_images.any? ? Site.instance.banner_images.map(&:url) : [banner_image]
end

def logo_image
Expand Down
26 changes: 19 additions & 7 deletions app/views/hyrax/admin/appearances/_banner_image_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<%= simple_form_for @form, url: admin_appearance_path do |f| %>
<div class="panel-body">
<% require_image = @form.banner_image? ? false : true %>
<%# Upload Banner Image %>
<%= f.input :banner_image, as: :file, wrapper: :vertical_file_input, required: require_image, hint: t('hyrax.admin.appearances.show.forms.banner_image.hint') %>
<% require_image = @form.banner_images.any? ? false : true %>
<%# Upload Banner Images %>
<%= f.input :banner_images,
as: :file,
wrapper: :vertical_file_input,
required: require_image,
hint: t('hyrax.admin.appearances.show.forms.banner_image.hint'),
input_html: { multiple: true, accept: 'image/*' } %>
<%= f.input :banner_image_text, required: true, as: :text, label: 'Banner image alt text' %>
<%= image_tag @form.banner_image.url, class: "img-responsive" if @form.banner_image? %>
<% if @form.banner_images.any? %>
<% @form.banner_images.each do |banner_image| %>
<div class="panel-footer">
<%= image_tag banner_image.url, class: "img-responsive" %> <br>
</div>
<% end %>
<% end %>
</div>
<div class="panel-footer">
<%= f.submit class: 'btn btn-primary pull-right' %>
</div>
<% end %>
<% if @form.banner_image? %>
<% if @form.banner_images.any? %>
<div class="panel-footer">
<%= simple_form_for @form.site, url: main_app.site_path(@form.site) do |f| %>
<%= f.submit 'Remove banner image', class: 'btn btn-danger', name: :remove_banner_image %>
<%= f.submit 'Remove all banner images', class: 'btn btn-danger', name: :remove_banner_images %>
<% end %>
</div>
<% end %>
<% end %>
23 changes: 21 additions & 2 deletions app/views/themes/dc_repository/layouts/homepage.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<% content_for(:navbar) do %>
<div class="image-masthead d-flex">
<!-- dc-repository homepage -->
<div class="background-container" title="<%= block_for(name: 'banner_image_text') %>" style="background-image: url('<%= banner_image %>')"></div>
<% banner_images.shuffle.each_with_index do |image, index| %>
<div id="background-container-<%= index %>" class="background-container <%= index == 0 ? 'active' : '' %>" style="background-image: url('<%= image %>');" title="<%= block_for(name: 'banner_image_text') %>"></div>
<% end %>
<% # OVERRIDE: Hyrax v3.4.1 - remove background-container-gradient %>
<% # OVERRIDE: Hyrax v3.4.1 - remove site-title-container %>
<% # OVERRIDE: Hyrax v3.4.1 - add divs and classes for custom styles %>
Expand Down Expand Up @@ -45,3 +46,21 @@
<% end %>
<%= render template: 'layouts/hyrax' %>
<% if banner_images.size > 1 %>
<script>
document.addEventListener("DOMContentLoaded", function() {
var containers = document.querySelectorAll('.background-container');
var currentIndex = 0;
var changeInterval = 7500;
function changeBackgroundImage() {
containers[currentIndex].classList.remove('active');
currentIndex = (currentIndex + 1) % containers.length;
containers[currentIndex].classList.add('active');
}
setInterval(changeBackgroundImage, changeInterval);
});
</script>
<% end %>
6 changes: 3 additions & 3 deletions spec/helpers/hyrax_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

RSpec.describe HyraxHelper, type: :helper do
describe "#banner_image" do
describe "#banner_images" do
context "with uploaded banner image" do
before do
f = fixture_file_upload('/images/nypl-hydra-of-lerna.jpg', 'image/jpg')
Site.instance.update(banner_image: f)
Site.instance.update(banner_images: [f])
end

it "returns the uploaded banner image" do
expect(helper.banner_image).to eq(Site.instance.banner_image.url)
expect(helper.banner_images.first).to eq(Site.instance.banner_images.first.url)
end
end

Expand Down
6 changes: 4 additions & 2 deletions spec/views/hyrax/admin/appearances/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
# logo tab
assert_select "input#admin_appearance_logo_image[name=?]", "admin_appearance[logo_image]"
# banner image tab
assert_select "input#admin_appearance_banner_image[name=?]", "admin_appearance[banner_image]"
assert_select "input#admin_appearance_banner_image[type=?]", "file"
assert_select "input#admin_appearance_banner_images[name=?]", "admin_appearance[banner_images][]"
assert_select "input#admin_appearance_banner_images[type=?]", "file"
assert_select "input#admin_appearance_banner_images[multiple=?]", "multiple"
assert_select "input#admin_appearance_banner_images[accept=?]", "image/*"
# directory image
assert_select "input#admin_appearance_directory_image[name=?]", "admin_appearance[directory_image]"
# default collection image
Expand Down

0 comments on commit 5bf1e5f

Please sign in to comment.