pools: fix pool gallery page layout.

Fix the /pools/gallery page layout being broken by 8841de68a.

This required refactoring the PostGalleryComponent to take a set of
PostPreviewComponents instead of a set of Posts.

The upshot is that it's technically possible to have adjustable
thumbnail sizes on the pool gallery page now (although this is not yet
exposed in the UI).
This commit is contained in:
evazion
2021-12-05 01:45:31 -06:00
parent 8841de68ac
commit 2e6f480d07
10 changed files with 58 additions and 54 deletions

View File

@@ -1,31 +1,27 @@
# A component that displays a gallery of post thumbnails. # A component that displays a gallery of post thumbnails.
# #
# There are three types of galleries: # There are two types of galleries:
# #
# * Paginated galleries, as seen on the post index page and pool show page. # * Grid galleries, where posts are arranged in a fixed grid.
# * Unpaginated galleries, as seen on wiki pages, artist pages, and user profiles. # * Inline galleries, where posts are arranged on a single scrollable row, as
# * Inline galleries that fit on a single row, as seen in parent/child post sets. # seen in parent/child post sets.
# #
class PostGalleryComponent < ApplicationComponent class PostGalleryComponent < ApplicationComponent
attr_reader :posts, :current_user, :inline, :size, :options attr_reader :inline, :size, :options
delegate :post_preview, :numbered_paginator, to: :helpers # The list of posts in the gallery.
renders_many :posts, PostPreviewComponent
# A gallery can optionally have a footer that displays between the posts and the paginator. # An optional footer that displays beneath the posts. Usually used for the paginator.
renders_one :footer renders_one :footer
# @param posts [Array<Post>, ActiveRecord::Relation<Post>] The set of posts to display
# @param current_user [User] The current user.
# @param inline [Boolean] If true, the gallery is rendered as a single row with a # @param inline [Boolean] If true, the gallery is rendered as a single row with a
# horizontal scrollbar. If false, the gallery is rendered as a grid of thumbnails. # horizontal scrollbar. If false, the gallery is rendered as a grid of thumbnails.
# @param size [String] The size of thumbnails in the gallery. Can be "150", # @param size [String] The size of thumbnails in the gallery. Can be "150",
# "180", "225", "225w", "270", "270w", or "360". # "180", "225", "225w", "270", "270w", or "360".
# @param options [Hash] A set of options given to the thumbnail in `post_preview`. # @param options [Hash] A set of options given to the PostPreviewComponent.
def initialize(posts:, current_user:, inline: false, size: PostPreviewComponent::DEFAULT_SIZE, **options) def initialize(inline: false, size: PostPreviewComponent::DEFAULT_SIZE, **options)
super super
@posts = posts
@posts = @posts.includes(:media_asset) if posts.is_a?(ActiveRecord::Relation)
@current_user = current_user
@inline = inline @inline = inline
@options = options @options = options
@@ -39,26 +35,8 @@ class PostGalleryComponent < ApplicationComponent
def gallery_type def gallery_type
if inline if inline
:inline :inline
elsif has_paginator?
:paginated
else else
:unpaginated :grid
end end
end end
def has_paginator?
posts.respond_to?(:total_count)
end
def total_count
if has_paginator?
posts.total_count
else
posts.length
end
end
def empty?
total_count == 0
end
end end

View File

@@ -1,17 +1,13 @@
<div class="post-gallery post-gallery-<%= gallery_type %> post-gallery-<%= size %>"> <div class="post-gallery post-gallery-<%= gallery_type %> post-gallery-<%= size %>">
<% if empty? %> <% if posts.empty? %>
<p>No posts found.</p> <p>No posts found.</p>
<% else %> <% else %>
<div class="posts-container grid gap-2"> <div class="posts-container grid gap-2">
<% posts.each do |post| -%> <% posts.each do |post| -%>
<% %><%= post_preview(post, **options) -%> <% %><%= post -%>
<% end -%> <% end -%>
</div> </div>
<% end %> <% end %>
<%= footer %> <%= footer %>
<% if has_paginator? %>
<%= numbered_paginator(posts) %>
<% end %>
</div> </div>

View File

@@ -1,7 +1,9 @@
@import "../../javascript/src/styles/base/000_vars.scss"; @import "../../javascript/src/styles/base/000_vars.scss";
article.post-preview { article.post-preview {
display: inline-block; display: inline-flex;
flex-direction: column;
place-items: center;
position: relative; position: relative;
overflow: hidden; overflow: hidden;
@@ -28,6 +30,7 @@ article.post-preview {
.desc { .desc {
font-size: var(--text-sm); font-size: var(--text-sm);
margin-bottom: 0; margin-bottom: 0;
text-align: center;
} }
} }

View File

@@ -3,13 +3,22 @@ module ComponentsHelper
render PostPreviewComponent.new(post: post, **options) render PostPreviewComponent.new(post: post, **options)
end end
def post_previews_html(posts, **options) # Render a set of posts as thumbnail gallery.
#
# @param posts [ActiveRecord::Relation<Post>, Array<Post>] A set of posts.
# @param options [Hash] A hash of options for the PostGalleryComponent and PostPreviewComponent.
def render_post_gallery(posts, **options, &block)
posts = posts.includes(:media_asset) if posts.is_a?(ActiveRecord::Relation) posts = posts.includes(:media_asset) if posts.is_a?(ActiveRecord::Relation)
render PostPreviewComponent.with_collection(posts, **options)
end
def render_post_gallery(posts, current_user: CurrentUser.user, **options, &block) render(PostGalleryComponent.new(**options)) do |gallery|
render PostGalleryComponent.new(posts: posts, current_user: current_user, **options), &block posts.each do |post|
gallery.post(post: post, size: gallery.size, **options)
end
if block_given?
yield(gallery, posts)
end
end
end end
def render_comment(comment, current_user:, **options) def render_comment(comment, current_user:, **options)

View File

@@ -10,6 +10,10 @@
<%= render "explore/posts/nav_links", path: :curated_explore_posts_path, date: @date, scale: @scale %> <%= render "explore/posts/nav_links", path: :curated_explore_posts_path, date: @date, scale: @scale %>
<%= render "posts/partials/common/inline_blacklist" %> <%= render "posts/partials/common/inline_blacklist" %>
<%= render_post_gallery(@posts) %> <%= render_post_gallery(@posts) do |gallery| %>
<% gallery.footer do %>
<%= numbered_paginator(@posts) %>
<% end %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -10,6 +10,10 @@
<%= render "explore/posts/nav_links", path: :popular_explore_posts_path, date: @date, scale: @scale %> <%= render "explore/posts/nav_links", path: :popular_explore_posts_path, date: @date, scale: @scale %>
<%= render "posts/partials/common/inline_blacklist" %> <%= render "posts/partials/common/inline_blacklist" %>
<%= render_post_gallery(@posts) %> <%= render_post_gallery(@posts) do |gallery| %>
<% gallery.footer do %>
<%= numbered_paginator(@posts) %>
<% end %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -15,6 +15,10 @@
<%= render "posts/partials/common/inline_blacklist" %> <%= render "posts/partials/common/inline_blacklist" %>
<%= render_post_gallery(@posts, tags: "favgroup:#{@favorite_group.id}", show_deleted: true) %> <%= render_post_gallery(@posts, tags: "favgroup:#{@favorite_group.id}", show_deleted: true) do |gallery| %>
<% gallery.footer do %>
<%= numbered_paginator(@posts) %>
<% end %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -9,12 +9,12 @@
<%= render "posts/partials/common/inline_blacklist" %> <%= render "posts/partials/common/inline_blacklist" %>
<section> <%= render(PostGalleryComponent.new) do |gallery| %>
<% @pools.each do |pool| %> <% @pools.each do |pool| %>
<%= post_preview(pool.cover_post, link_target: pool, pool: pool, show_deleted: true) %> <% gallery.post(post: pool.cover_post, link_target: pool, pool: pool, show_deleted: true) %>
<% end %> <% end %>
<% end %>
<%= numbered_paginator(@pools) %> <%= numbered_paginator(@pools) %>
</section>
</div> </div>
</div> </div>

View File

@@ -20,6 +20,10 @@
<%= render "posts/partials/common/inline_blacklist" %> <%= render "posts/partials/common/inline_blacklist" %>
<%= render_post_gallery(@posts, tags: "pool:#{@pool.id}", show_deleted: @pool.is_series?) %> <%= render_post_gallery(@posts, tags: "pool:#{@pool.id}", show_deleted: @pool.is_series?) do |gallery| %>
<% gallery.footer do %>
<%= numbered_paginator(@posts) %>
<% end %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -2,7 +2,7 @@
<% if post_set.shown_posts.empty? %> <% if post_set.shown_posts.empty? %>
<%= render "post_sets/blank" %> <%= render "post_sets/blank" %>
<% else %> <% else %>
<%= render_post_gallery(post_set.posts, show_deleted: post_set.show_deleted?, tags: post_set.tag_string, show_votes: post_set.show_votes?, size: params[:size]) do |gallery| %> <%= render_post_gallery(post_set.posts, show_deleted: post_set.show_deleted?, tags: post_set.tag_string, show_votes: post_set.show_votes?, size: params[:size]) do |gallery, posts| %>
<% gallery.footer do %> <% gallery.footer do %>
<% if post_set.hidden_posts.present? %> <% if post_set.hidden_posts.present? %>
<div class="fineprint hidden-posts-notice"> <div class="fineprint hidden-posts-notice">
@@ -32,6 +32,8 @@
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
<%= numbered_paginator(posts) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>