From 5c0bcd7e5d87a192bd7c09b46e29327315f0a10f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 5 Feb 2015 18:02:30 -0500 Subject: [PATCH] proper qs, pagination on search results --- src/controllers/search.js | 8 ++++++++ src/pagination.js | 10 +++++++--- src/search.js | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/controllers/search.js b/src/controllers/search.js index cbf3f3fd40..d21977b69d 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -6,6 +6,7 @@ var searchController = {}, plugins = require('../plugins'), search = require('../search'), categories = require('../categories'), + pagination = require('../pagination'), helpers = require('./helpers'); @@ -50,6 +51,13 @@ searchController.search = function(req, res, next) { if (err) { return next(err); } + var currentPage = Math.max(1, parseInt(req.query.page, 10)) || 1; + var pageCount = Math.max(1, Math.ceil(results.matchCount / 10)); + var searchIn = req.query.in || 'posts'; + var start = Math.max(0, (currentPage - 1)) * 10; + results[searchIn] = results[searchIn].slice(start, start + 10); + + pagination.create(currentPage, pageCount, results, req.query); results.breadcrumbs = breadcrumbs; results.categories = categories; diff --git a/src/pagination.js b/src/pagination.js index 3fa2e31bf7..9fadfceab4 100644 --- a/src/pagination.js +++ b/src/pagination.js @@ -1,9 +1,10 @@ 'use strict'; -var pagination = {}; +var qs = require('querystring'); -pagination.create = function(currentPage, pageCount, data) { +var pagination = {}; +pagination.create = function(currentPage, pageCount, data, queryObj) { if (pageCount <= 1) { data.pagination = { prev: {page: 1, active: currentPage > 1}, @@ -35,8 +36,11 @@ pagination.create = function(currentPage, pageCount, data) { return a - b; }); + queryObj = queryObj || {}; + var pages = pagesToShow.map(function(page) { - return {page: page, active: page === currentPage}; + queryObj.page = page; + return {page: page, active: page === currentPage, qs: qs.stringify(queryObj)}; }); data.pagination = { diff --git a/src/search.js b/src/search.js index 04147118f2..3360f09273 100644 --- a/src/search.js +++ b/src/search.js @@ -99,7 +99,7 @@ function filterPosts(data, searchCategories, posts) { if (postedBy || searchCategories.length || data.replies) { posts = posts.filter(function(post) { return post && - (postedBy ? post.user.username === postedBy : true) && + (postedBy ? (post.user && 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); });