Skip to content

Node.js based forum software built for the modern web

closes #2581

barisusakli98ff75b9c36e
diff --git a/public/src/client/category.js b/public/src/client/category.js
index 58d7074528..b9ab5bc800 100644
--- a/public/src/client/category.js
+++ b/public/src/client/category.js
@@ -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();
diff --git a/public/src/client/pagination.js b/public/src/client/pagination.js
index 5aab6ada6b..3d306e9f65 100644
--- a/public/src/client/pagination.js
+++ b/public/src/client/pagination.js
@@ -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;
});
diff --git a/src/controllers/categories.js b/src/controllers/categories.js
index 06b2b957fa..5083a7d71a 100644
--- a/src/controllers/categories.js
+++ b/src/controllers/categories.js
@@ -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);
});
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 791baa0c79..d3d6e571e6 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -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);
});
diff --git a/src/pagination.js b/src/pagination.js
new file mode 100644
index 0000000000..3fa2e31bf7
--- /dev/null
+++ b/src/pagination.js
@@ -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;
\ No newline at end of file