Add tags filter.

This commit is contained in:
BoHung Chiu 2021-04-09 15:59:01 +08:00
parent 1a4c305149
commit b484a76768
3 changed files with 100 additions and 36 deletions

View File

@ -2368,8 +2368,49 @@ var wpex = {};
var self = this;
if (undefined === $.fn.imagesLoaded || undefined === $.fn.isotope) { return }
var containers = $(window.wpexAnimsition.linkElement).parents(window.wpexAnimsition.container_block);
function get_filter_constraint(filterLink){
var filter_attr = window.wpexAnimsition.filter_attr;
var filter_constraint = filterLink.attr(filter_attr);
var filter_attrs = filter_attr.split(",");
if(filter_attrs.length != 1){
filter_constraint = [];
filter_attr = "";
filter_attrs.forEach(function(fa){
var tmp_filter_constraint = filterLink.attr(fa);
if( tmp_filter_constraint != undefined){
if(window.wpexAnimsition.use_attr_filter){
if(tmp_filter_constraint == "all"){
filter_constraint.push("["+fa+"]");
}else{
filter_constraint.push("["+fa+(fa[fa.length-1] == "s" ? '*' : '')+"=\""+tmp_filter_constraint+"\"]");
}
}else{
filter_constraint.push(tmp_filter_constraint);
}
}
})
if(window.wpexAnimsition.use_attr_filter){
filter_constraint = filter_constraint.join("],[");
}else{
filter_constraint = filter_constraint.join(",");
}
}else{
if(window.wpexAnimsition.use_attr_filter){
if(filter_constraint == "all"){
filter_constraint = "["+window.wpexAnimsition.filter_attr+"]";
}else{
filter_constraint = "["+window.wpexAnimsition.filter_attr+"=\""+filter_constraint+"\"]";
}
}
}
if(filterLink.data("count_limit") != undefined){
filter_constraint = filter_constraint+":nth-child(-n+"+filterLink.data("count_limit")+")";
}
console.log(filter_constraint);
return filter_constraint;
}
$(containers).each(function (i,v) {
var $container = $(this);
var $container = $(v);
$container.imagesLoaded(function () {
var activeItems;
var $filter = $container.parent().find(window.wpexAnimsition.filter_bar);
@ -2377,31 +2418,11 @@ var wpex = {};
var $filterLinks = $filter.find(window.wpexAnimsition.filter_option);
activeItems = '';
if($filterLinks.length != 0){
var filter_constraint = $filterLinks.eq(0).attr(window.wpexAnimsition.filter_attr);
if(filter_constraint == "all"){
filter_constraint = "["+window.wpexAnimsition.filter_attr+"]";
}else{
if(window.wpexAnimsition.use_attr_filter){
filter_constraint = "["+window.wpexAnimsition.filter_attr+"=\""+filter_constraint+"\"]";
}
}
if($filterLinks.eq(0).data("count_limit") != undefined){
filter_constraint = filter_constraint+":nth-child(-n+"+$filterLinks.eq(0).data("count_limit")+")";
}
var filter_constraint = get_filter_constraint($filterLinks.eq(0));
activeItems = filter_constraint;
}
$filterLinks.click(function () {
var filter_constraint = $(this).attr(window.wpexAnimsition.filter_attr);
if(filter_constraint == "all"){
filter_constraint = "["+window.wpexAnimsition.filter_attr+"]";
}else{
if(window.wpexAnimsition.use_attr_filter){
filter_constraint = "["+window.wpexAnimsition.filter_attr+"=\""+filter_constraint+"\"]";
}
}
if($(this).data("count_limit") != undefined){
filter_constraint = filter_constraint+":nth-child(-n+"+$(this).data("count_limit")+")";
}
var filter_constraint = get_filter_constraint($(this));
$grid.isotope({ filter: filter_constraint});
self.equalHeights();
return false;

View File

@ -133,23 +133,45 @@ class AnnouncementsController < ApplicationController
def pack_data(is_random=false)
cats = OrbitHelper.widget_categories || []
tags = OrbitHelper.widget_tags || []
subpart = OrbitHelper.get_current_widget
get_tabs_option
anns = []
use_tag = false
if @tab_option == 0
anns = get_anncs_for_pack_data(cats,is_random)
anns = get_anncs_for_pack_data(cats,tags,is_random)
else
cats.each do |cat|
anns = anns + get_anncs_for_pack_data([cat])
if cats.count != 1 || tags == ["all"]
cats.each do |cat|
anns = anns + get_anncs_for_pack_data([cat],tags)
end
else
tags.each do |tag|
anns = anns + get_anncs_for_pack_data(cats,[tag])
end
use_tag = true
end
end
mp = (anns[0]["img_src"] rescue "")
mpd = (anns[0]["img_description"] rescue "")
if @tab_option == 1
cats = ["all"] + cats
if use_tag
tags = ["all"] + tags
else
cats = ["all"] + cats
end
anns = anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
end
cats = cats.uniq
tags = tags.uniq
tags_translations = tags.map{|tag_id|
if tag_id == "all"
t = I18n.t(:all)
else
t = Tag.find(tag_id).name rescue ""
end
[tag_id,t]
}.to_h
cats_translations = cats.map{|cat_id|
if cat_id == "all"
t = I18n.t(:all)
@ -171,12 +193,29 @@ class AnnouncementsController < ApplicationController
if all_cats.count == 0
all_cats = ["all"]
end
all_tags = tags.dup
all_tags.delete "all"
if all_tags.count == 0
all_tags = ["all"]
end
max_all_count = [OrbitHelper.widget_data_count,anns.count].min
if @tab_option != 0
OrbitHelper.set_widget_title(OrbitHelper.widget_title +
"<div style=\"clear: both;\"></div>" +
"<ul class=\"nav_tabs_filter\">" +
cats.map.with_index{|cat,i|
(use_tag ? tags.map.with_index{|tag,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>all_cats,"tags"=>(tag == 'all' ? all_tags : [tag])}.to_param if read_more_url != ""
read_more_text = I18n.t("announcement.more")
if tag != "all"
begin
read_more_text = I18n.t("announcement.more_") + tags_translations[tag]
rescue
nil
end
end
"<li class=\"filter_tab#{i == 0 ? ' active' : ''}\" data-count_limit=\"#{max_all_count}\" data-read_more_text=\"#{read_more_text}\" data-read_more=\"#{read_more_url}\" data-tags=\"#{tag}\">#{tags_translations[tag]}</li>"
}.join("") : cats.map.with_index{|cat,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>(cat == 'all' ? all_cats : cat)}.to_param if read_more_url != ""
read_more_text = I18n.t("announcement.more")
@ -188,9 +227,10 @@ class AnnouncementsController < ApplicationController
end
end
"<li class=\"filter_tab#{i == 0 ? ' active' : ''}\" #{(cat == 'all' && @all_setting_option == 0) ? "data-count_limit=\"#{max_all_count}\"" : ''} data-read_more_text=\"#{read_more_text}\" data-read_more=\"#{read_more_url}\" data-category=\"#{cats_relations[cat]}\">#{cats_translations[cat]}</li>"
}.join("") +
}.join("")) +
"</ul>"
)
filter_attr = (use_tag ? 'data-tags' : 'data-category')
extra_html = '
<script>
if(typeof(wpexAnimsition) == "undefined"){
@ -206,14 +246,18 @@ class AnnouncementsController < ApplicationController
"parent_block": "[data-subpart-id=\"'+subpart.id.to_s+'\"]",
"filter_bar": ".nav_tabs_filter",
"filter_option": "li.filter_tab",
"filter_attr": "data-category",
"filter_attr": "'+filter_attr+'",
"filter_target_attr": "class",
"use_attr_filter": true,
"equal_height": false
};
}else{
var filter_attr = "'+filter_attr+'";
wpexAnimsition.parent_block = wpexAnimsition.parent_block + ", [data-subpart-id=\"'+subpart.id.to_s+'\"]";
wpexAnimsition.container_block = wpexAnimsition.container_block + ", [data-subpart-id=\"'+subpart.id.to_s+'\"] [data-list=\"announcements\"]:not(tbody)";
if( wpexAnimsition.filter_attr.indexOf(filter_attr) == -1 ){
wpexAnimsition.filter_attr += ("," + filter_attr);
}
}
var wpexLocalize = {
"lightboxType": "iLightbox",
@ -410,11 +454,10 @@ class AnnouncementsController < ApplicationController
end
end
end
def get_anncs_for_pack_data(cats,is_random = false)
tags = OrbitHelper.widget_tags || []
def get_anncs_for_pack_data(cats,tags,is_random = false)
subpart = OrbitHelper.get_current_widget
widget_data_count = OrbitHelper.widget_data_count
anns_cache = AnnsCache.where(parent_id: subpart.id.to_s + cats.to_s + widget_data_count.to_s,locale: I18n.locale.to_s)
anns_cache = AnnsCache.where(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s)
set_image_version_for_widget()
devide_flag = (!(defined? SiteFeed).nil?)
if anns_cache.count != 1 || is_random
@ -432,10 +475,10 @@ class AnnouncementsController < ApplicationController
now_anns = sorted_anns.to_a
top_anns = now_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v)}
not_top_anns = now_anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v)}
AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns})
AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns})
else
anns = sorted_anns.map{|v| data_to_human_type(v)}
AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: anns)
AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: anns)
end
else
if devide_flag

View File

@ -76,6 +76,7 @@ module AnnouncementsHelper
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"tag_ids" => (a.tag_ids.map{|id| id.to_s}.to_s.gsub('"',"'") rescue '[]'),
"postdate" => a.postdate,
"author" => author,
"link_to_show" => link_to_show,
@ -199,7 +200,6 @@ module AnnouncementsHelper
tags = page.tags
tags = params[:tags] if params[:tags].present?
categories = params['category']=='all' ? (page.categories || []) : (Array(params['category']) rescue (page.categories || []))
categories = ["all"] if params[:tags].present?
end
if !params["source"].present?
if @type == "show_widget"