mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
closes #2581
This commit is contained in:
@@ -233,10 +233,6 @@ define('forum/category', [
|
||||
|
||||
topic.hide().fadeIn('slow');
|
||||
|
||||
socket.emit('categories.getPageCount', ajaxify.variables.get('category_id'), function(err, newPageCount) {
|
||||
pagination.recreatePaginationLinks(newPageCount);
|
||||
});
|
||||
|
||||
topic.find('span.timeago').timeago();
|
||||
app.createUserTooltips();
|
||||
updateTopicCount();
|
||||
|
||||
@@ -11,69 +11,13 @@ define('forum/pagination', function() {
|
||||
pagination.currentPage = parseInt(currentPage, 10);
|
||||
pagination.pageCount = parseInt(pageCount, 10);
|
||||
|
||||
pagination.recreatePaginationLinks(pageCount);
|
||||
|
||||
$('.pagination')
|
||||
.on('click', '.previous', function() {
|
||||
return pagination.loadPage(pagination.currentPage - 1);
|
||||
}).on('click', '.next', function() {
|
||||
return pagination.loadPage(pagination.currentPage + 1);
|
||||
}).on('click', '.select_page', function(e) {
|
||||
e.preventDefault();
|
||||
bootbox.prompt('Enter page number:', function(pageNum) {
|
||||
pagination.loadPage(pageNum);
|
||||
});
|
||||
$('.pagination').on('click', '.select_page', function(e) {
|
||||
e.preventDefault();
|
||||
bootbox.prompt('Enter page number:', function(pageNum) {
|
||||
pagination.loadPage(pageNum);
|
||||
});
|
||||
};
|
||||
|
||||
pagination.recreatePaginationLinks = function(newPageCount) {
|
||||
pagination.pageCount = parseInt(newPageCount, 10);
|
||||
|
||||
var pagesToShow = determinePagesToShow();
|
||||
|
||||
var html = '';
|
||||
for(var i=0; i<pagesToShow.length; ++i) {
|
||||
if(i > 0) {
|
||||
if (pagesToShow[i] - 1 !== pagesToShow[i-1]) {
|
||||
html += '<li><a class="select_page" href="#">|</a></li>';
|
||||
}
|
||||
}
|
||||
html += '<li class="page" data-page="' + pagesToShow[i] + '"><a href="#">' + pagesToShow[i] + '</a></li>';
|
||||
}
|
||||
|
||||
$('.pagination li.page').remove();
|
||||
$('.pagination li .select_page').parent().remove();
|
||||
$(html).insertAfter($('.pagination li.previous'));
|
||||
|
||||
updatePageLinks();
|
||||
};
|
||||
|
||||
function determinePagesToShow() {
|
||||
var pagesToShow = [1];
|
||||
if(pagination.pageCount !== 1) {
|
||||
pagesToShow.push(pagination.pageCount);
|
||||
}
|
||||
|
||||
var previous = pagination.currentPage - 1;
|
||||
var next = pagination.currentPage + 1;
|
||||
|
||||
if(previous > 1 && pagesToShow.indexOf(previous) === -1) {
|
||||
pagesToShow.push(previous);
|
||||
}
|
||||
|
||||
if(next < pagination.pageCount && pagesToShow.indexOf(next) === -1) {
|
||||
pagesToShow.push(next);
|
||||
}
|
||||
|
||||
if(pagesToShow.indexOf(pagination.currentPage) === -1) {
|
||||
pagesToShow.push(pagination.currentPage);
|
||||
}
|
||||
|
||||
pagesToShow.sort(function(a, b) {
|
||||
return parseInt(a, 10) - parseInt(b, 10);
|
||||
});
|
||||
return pagesToShow;
|
||||
}
|
||||
};
|
||||
|
||||
pagination.loadPage = function(page, callback) {
|
||||
page = parseInt(page, 10);
|
||||
@@ -89,20 +33,5 @@ define('forum/pagination', function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
function updatePageLinks() {
|
||||
$('.pagination').toggleClass('hide', pagination.pageCount === 0 || pagination.pageCount === 1);
|
||||
|
||||
$('.pagination .next').toggleClass('disabled', pagination.currentPage === pagination.pageCount);
|
||||
$('.pagination .previous').toggleClass('disabled', pagination.currentPage === 1);
|
||||
|
||||
$('.pagination .page').removeClass('active');
|
||||
$('.pagination .page[data-page="' + pagination.currentPage + '"]').addClass('active');
|
||||
$('.pagination .page').each(function(index, element) {
|
||||
var li = $(this);
|
||||
var page = li.attr('data-page');
|
||||
li.find('a').attr('href', window.location.pathname + '?page=' + page);
|
||||
});
|
||||
}
|
||||
|
||||
return pagination;
|
||||
});
|
||||
|
||||
@@ -9,6 +9,7 @@ var categoriesController = {},
|
||||
topics = require('../topics'),
|
||||
meta = require('../meta'),
|
||||
plugins = require('../plugins'),
|
||||
pagination = require('../pagination'),
|
||||
helpers = require('./helpers'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
@@ -255,16 +256,11 @@ categoriesController.get = function(req, res, next) {
|
||||
data.currentPage = page;
|
||||
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
|
||||
|
||||
if (!res.locals.isAPI) {
|
||||
// Paginator for noscript
|
||||
data.pages = [];
|
||||
for(var x=1;x<=data.pageCount;x++) {
|
||||
data.pages.push({
|
||||
page: x,
|
||||
active: x === parseInt(page, 10)
|
||||
});
|
||||
}
|
||||
}
|
||||
pagination.create(data.currentPage, data.pageCount, data);
|
||||
|
||||
data.pagination.rel.forEach(function(rel) {
|
||||
res.locals.linkTags.push(rel);
|
||||
});
|
||||
|
||||
res.render('category', data);
|
||||
});
|
||||
|
||||
@@ -13,6 +13,7 @@ var topicsController = {},
|
||||
privileges = require('../privileges'),
|
||||
plugins = require('../plugins'),
|
||||
helpers = require('./helpers'),
|
||||
pagination = require('../pagination'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
topicsController.get = function(req, res, next) {
|
||||
@@ -257,16 +258,11 @@ topicsController.get = function(req, res, next) {
|
||||
|
||||
topics.increaseViewCount(tid);
|
||||
|
||||
if (!res.locals.isAPI) {
|
||||
// Paginator for noscript
|
||||
data.pages = [];
|
||||
for(var x=1; x<=data.pageCount; x++) {
|
||||
data.pages.push({
|
||||
page: x,
|
||||
active: x === parseInt(page, 10)
|
||||
});
|
||||
}
|
||||
}
|
||||
pagination.create(data.currentPage, data.pageCount, data);
|
||||
|
||||
data.pagination.rel.forEach(function(rel) {
|
||||
res.locals.linkTags.push(rel);
|
||||
});
|
||||
|
||||
res.render('topic', data);
|
||||
});
|
||||
|
||||
66
src/pagination.js
Normal file
66
src/pagination.js
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
var pagination = {};
|
||||
|
||||
pagination.create = function(currentPage, pageCount, data) {
|
||||
|
||||
if (pageCount <= 1) {
|
||||
data.pagination = {
|
||||
prev: {page: 1, active: currentPage > 1},
|
||||
next: {page: 1, active: currentPage < pageCount},
|
||||
rel: [],
|
||||
pages: []
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
var pagesToShow = [1];
|
||||
if (pageCount !== 1) {
|
||||
pagesToShow.push(pageCount);
|
||||
}
|
||||
|
||||
currentPage = parseInt(currentPage, 10) || 1;
|
||||
var previous = Math.max(1, currentPage - 1);
|
||||
var next = Math.min(pageCount, currentPage + 1);
|
||||
|
||||
var startPage = currentPage - 2;
|
||||
for(var i=0; i<5; ++i) {
|
||||
var p = startPage + i;
|
||||
if (p >= 1 && p <= pageCount && pagesToShow.indexOf(p) === -1) {
|
||||
pagesToShow.push(startPage + i);
|
||||
}
|
||||
}
|
||||
|
||||
pagesToShow.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
var pages = pagesToShow.map(function(page) {
|
||||
return {page: page, active: page === currentPage};
|
||||
});
|
||||
|
||||
data.pagination = {
|
||||
prev: {page: previous, active: currentPage > 1},
|
||||
next: {page: next, active: currentPage < pageCount},
|
||||
rel: [],
|
||||
pages: pages
|
||||
};
|
||||
|
||||
if (currentPage < pageCount) {
|
||||
data.pagination.rel.push({
|
||||
rel: 'next',
|
||||
href: '?page=' + next
|
||||
});
|
||||
}
|
||||
|
||||
if (currentPage > 1) {
|
||||
data.pagination.rel.push({
|
||||
rel: 'prev',
|
||||
href: '?page=' + previous
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports = pagination;
|
||||
Reference in New Issue
Block a user