proper qs, pagination on search results

v1.18.x
barisusakli 10 years ago
parent 161b934827
commit 5c0bcd7e5d

@ -6,6 +6,7 @@ var searchController = {},
plugins = require('../plugins'), plugins = require('../plugins'),
search = require('../search'), search = require('../search'),
categories = require('../categories'), categories = require('../categories'),
pagination = require('../pagination'),
helpers = require('./helpers'); helpers = require('./helpers');
@ -50,6 +51,13 @@ searchController.search = function(req, res, next) {
if (err) { if (err) {
return next(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.breadcrumbs = breadcrumbs;
results.categories = categories; results.categories = categories;

@ -1,9 +1,10 @@
'use strict'; '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) { if (pageCount <= 1) {
data.pagination = { data.pagination = {
prev: {page: 1, active: currentPage > 1}, prev: {page: 1, active: currentPage > 1},
@ -35,8 +36,11 @@ pagination.create = function(currentPage, pageCount, data) {
return a - b; return a - b;
}); });
queryObj = queryObj || {};
var pages = pagesToShow.map(function(page) { 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 = { data.pagination = {

@ -99,7 +99,7 @@ function filterPosts(data, searchCategories, posts) {
if (postedBy || searchCategories.length || data.replies) { if (postedBy || searchCategories.length || data.replies) {
posts = posts.filter(function(post) { posts = posts.filter(function(post) {
return 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) && (searchCategories.length ? searchCategories.indexOf(post.category.cid) !== -1 : true) &&
(data.replies ? (isAtLeast ? post.topic.postcount >= data.replies : post.topic.postcount <= data.replies) : true); (data.replies ? (isAtLeast ? post.topic.postcount >= data.replies : post.topic.postcount <= data.replies) : true);
}); });

Loading…
Cancel
Save