Add album_widget method.

This commit is contained in:
BoHung Chiu 2021-05-18 09:45:52 +08:00
parent 2377de1ac4
commit b1088b75b3
2 changed files with 57 additions and 1 deletions

View File

@ -118,6 +118,62 @@ class GalleriesController < ApplicationController
end
tmp
end
def album_widget
@album_setting = AlbumSetting.first
params = OrbitHelper.params
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags
album_tp = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags)
all_count = album_tp.count
page_data_count = OrbitHelper.widget_data_count
no_order_count = album_tp.where(:order.in=>[-1,nil]).count
with_order_count = all_count - no_order_count
with_order_total_pages = (with_order_count.to_f / page_data_count).ceil
albums_with_order = album_tp.asc(:order).where(:order.ne=>-1).and(:order.ne=>nil).to_a rescue []
page_no = (params[:page_no] || 1).to_i
if page_no < with_order_total_pages
albums_no_order = []
elsif page_no == with_order_total_pages
if albums_with_order.count == page_data_count
albums_no_order = []
else
albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[0...(page_data_count - albums_with_order.count)]
end
else
start_index = (page_data_count - (with_order_count % page_data_count) + page_data_count*(page_no - 1 - with_order_total_pages))
albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[start_index...(start_index+page_data_count)].to_a
end
albums = albums_with_order.concat(albums_no_order)
album_color_map = AlbumColor.where(:album_id.in=> albums.map{|v| v.id}).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
galleries = albums.collect.with_index do |a,i|
doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text
colors = album_color_map[a.id]
thumb_src = a.cover_path || "/assets/gallery/default.jpg"
cover_image = AlbumImage.find(a.cover) rescue a.album_images.first
image_description = a.description
image_short_description = a.name
{
"album-name" => a.name,
"album-description" => a.description,
"alt_title" => alt_text,
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
"src" => thumb_src.gsub("thumb_",""),
"thumb-src" => thumb_src,
"thumb-large-src" => thumb_src.gsub("thumb_","thumb_large_"),
"mobile-src" => thumb_src.gsub("thumb_","mobile_"),
"theater-src" => thumb_src.gsub("thumb_","theater_"),
"image_description" => image_description,
"image_short_description" => image_short_description,
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color),
"i" => i
}
end
{
"images" => galleries,
"extras" => {"widget-title"=>"Gallery","more_url" => OrbitHelper.widget_more_url}
}
end
def widget
@album_setting = AlbumSetting.first
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags

View File

@ -35,7 +35,7 @@ module Gallery
OrbitApp.registration "Gallery", :type => "ModuleApp" do
module_label "gallery.gallery"
base_url File.expand_path File.dirname(__FILE__)
widget_methods ["widget"]
widget_methods ["widget","album_widget"]
# widget_settings []
widget_settings [{"data_count"=>10}]
models_to_cache [:album,:album_image]