From 161b934827e21b0c8fc1db17fd8b729f87bd00b4 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 5 Feb 2015 17:20:56 -0500 Subject: [PATCH] reply count filter atleast/atmost --- public/language/en_GB/search.json | 5 ++++- public/src/client/search.js | 16 ++++++++++++---- public/src/modules/search.js | 6 ++++++ src/controllers/search.js | 2 ++ src/search.js | 25 ++++++++++++++++--------- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/public/language/en_GB/search.json b/public/language/en_GB/search.json index 7ae707185c..c68ead290b 100644 --- a/public/language/en_GB/search.json +++ b/public/language/en_GB/search.json @@ -5,5 +5,8 @@ "by": "By", "posted-by": "Posted by", "in-categories": "In Categories", - "search-child-categories": "Search child categories" + "search-child-categories": "Search child categories", + "reply-count": "Reply Count", + "at-least": "At least", + "at-most": "At most" } diff --git a/public/src/client/search.js b/public/src/client/search.js index d6b0566e56..e832e6f6c6 100644 --- a/public/src/client/search.js +++ b/public/src/client/search.js @@ -16,7 +16,7 @@ define('forum/search', ['search'], function(searchModule) { } if (params && params.by) { - $('.by-container #posted-by-user').val(params.by); + $('#posted-by-user').val(params.by); } if (params && params['categories[]']) { @@ -27,8 +27,14 @@ define('forum/search', ['search'], function(searchModule) { $('#search-children').prop('checked', true); } + if (params && params.replies) { + $('#reply-count').val(params.replies); + $('#reply-count-filter').val(params.repliesFilter); + } + searchIn.on('change', function() { - $('.by-container').toggleClass('hide', searchIn.val() !== 'posts'); + $('.post-search-item').toggleClass('hide', searchIn.val() !== 'posts'); + }); highlightMatches(searchQuery); @@ -42,7 +48,9 @@ define('forum/search', ['search'], function(searchModule) { in: $(this).find('#search-in').val(), by: $(this).find('#posted-by-user').val(), categories: $(this).find('#posted-in-categories').val(), - searchChildren: $(this).find('#search-children').is(':checked') + searchChildren: $(this).find('#search-children').is(':checked'), + replies: $(this).find('#reply-count').val(), + repliesFilter: $(this).find('#reply-count-filter').val() }, function() { input.val(''); }); @@ -71,7 +79,7 @@ define('forum/search', ['search'], function(searchModule) { function enableAutoComplete() { - var input = $('.by-container #posted-by-user'); + var input = $('#posted-by-user'); input.autocomplete({ delay: 100, source: function(request, response) { diff --git a/public/src/modules/search.js b/public/src/modules/search.js index e8cb85b31f..b8f4eb6607 100644 --- a/public/src/modules/search.js +++ b/public/src/modules/search.js @@ -34,6 +34,12 @@ define('search', ['navigator'], function(nav) { query.searchChildren = data.searchChildren; } } + + if (parseInt(data.replies, 10) > 0) { + query.replies = data.replies; + query.repliesFilter = data.repliesFilter || 'atleast'; + } + ajaxify.go('search/' + term + '?' + decodeURIComponent($.param(query))); callback(); } else { diff --git a/src/controllers/search.js b/src/controllers/search.js index f974cb3e94..cbf3f3fd40 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -43,6 +43,8 @@ searchController.search = function(req, res, next) { postedBy: req.query.by, categories: req.query.categories, searchChildren: req.query.searchChildren, + replies: req.query.replies, + repliesFilter: req.query.repliesFilter, uid: uid }, function(err, results) { if (err) { diff --git a/src/search.js b/src/search.js index 800aea308c..04147118f2 100644 --- a/src/search.js +++ b/src/search.js @@ -50,7 +50,6 @@ search.search = function(data, callback) { function searchInPosts(query, data, callback) { data.uid = data.uid || 0; - var postedBy = data.postedBy || ''; async.parallel({ pids: function(next) { searchQuery('post', query, next); @@ -86,20 +85,28 @@ function searchInPosts(query, data, callback) { posts.getPostSummaryByPids(pids, data.uid, {stripTags: true, parse: false}, next); }, function(posts, next) { - var searchCategories = results.searchCategories; - if (postedBy || searchCategories.length) { - posts = posts.filter(function(post) { - return post && - (postedBy ? post.user.username === postedBy : true) && - (searchCategories.length ? searchCategories.indexOf(post.category.cid) !== -1 : true); - }); - } + posts = filterPosts(data, results.searchCategories, posts); next(null, posts); } ], callback); }); } +function filterPosts(data, searchCategories, posts) { + var postedBy = data.postedBy; + var isAtLeast = data.repliesFilter === 'atleast'; + data.replies = parseInt(data.replies, 10); + if (postedBy || searchCategories.length || data.replies) { + posts = posts.filter(function(post) { + return post && + (postedBy ? post.user.username === postedBy : true) && + (searchCategories.length ? searchCategories.indexOf(post.category.cid) !== -1 : true) && + (data.replies ? (isAtLeast ? post.topic.postcount >= data.replies : post.topic.postcount <= data.replies) : true); + }); + } + return posts; +} + function getSearchCategories(data, callback) { if (!Array.isArray(data.categories) || !data.categories.length || data.categories.indexOf('all') !== -1) { return callback(null, []);