mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
proper qs, pagination on search results
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
var qs = require('querystring');
|
||||
|
||||
var pagination = {};
|
||||
|
||||
pagination.create = function(currentPage, pageCount, data) {
|
||||
|
||||
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 = {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user