From b484a767681bd4562ee3f89974cc8fe026beb4d8 Mon Sep 17 00:00:00 2001 From: Bohung Date: Fri, 9 Apr 2021 15:59:01 +0800 Subject: [PATCH] Add tags filter. --- app/assets/javascripts/bulletin/wpex.min.js | 67 ++++++++++++++------- app/controllers/announcements_controller.rb | 67 +++++++++++++++++---- app/helpers/announcements_helper.rb | 2 +- 3 files changed, 100 insertions(+), 36 deletions(-) diff --git a/app/assets/javascripts/bulletin/wpex.min.js b/app/assets/javascripts/bulletin/wpex.min.js index a8eea40..e809ff1 100644 --- a/app/assets/javascripts/bulletin/wpex.min.js +++ b/app/assets/javascripts/bulletin/wpex.min.js @@ -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; diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 9a6f270..cd19cf4 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -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 + "
" + "" ) + filter_attr = (use_tag ? 'data-tags' : 'data-category') extra_html = '