From 3534e7b799ca70ea164a84ba726d39d43cbedfc4 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 12 Oct 2014 19:52:18 -0500 Subject: [PATCH] More sensible tag filtering behavior. --- static/main.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/static/main.js b/static/main.js index ba18e24..159d738 100644 --- a/static/main.js +++ b/static/main.js @@ -5,22 +5,23 @@ function fix_tag_links() { } function load_tag_filters() { - var filter_posts = function(filter) { - var num_selected = $(".tag-selected").length; - if (num_selected == 0 || num_selected == $(".tag").length) - $("#post-list li").show(); - else { - $("#post-list li").hide(); - $("#post-list li").each(function() { - var tags = $(this).data("tags").split("|"); - for (var t in tags) { - if ($.inArray(tags[t], filter) != -1) { - $(this).show(); - return; - } + var filter_posts = function() { + var filter = []; + $(".tag-selected").each(function() { + filter.push($(this).data("tag")) + }); + $("#post-list li").show(); + if ($(".tag-selected").length == 0) + return; + $("#post-list li").each(function() { + var tags = $(this).data("tags").split("|"); + for (var i in filter) { + if ($.inArray(filter[i], tags) == -1) { + $(this).hide(); + return; } - }); - } + } + }); } if (window.location.hash) { @@ -29,13 +30,12 @@ function load_tag_filters() { if ($.inArray($(this).data("tag"), tags) != -1) $(this).toggleClass("tag-selected"); }); - filter_posts(tags); + filter_posts(); } $(".tag").click(function() { - $(this).toggleClass("tag-selected"); - var tags = []; + $(this).toggleClass("tag-selected"); $(".tag-selected").each(function() { tags.push($(this).data("tag")) }); @@ -43,7 +43,7 @@ function load_tag_filters() { window.location.hash = encodeURIComponent(tags.join("|")); else history.pushState("", "", window.location.pathname); - filter_posts(tags); + filter_posts(); }); }