diff --git a/public/src/app.js b/public/src/app.js index 6d724f3f49..f77c860b02 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -71,12 +71,14 @@ app.cacheBuster = null; } }); - require(['taskbar', 'helpers'], function(taskbar, helpers) { + require(['taskbar', 'helpers', 'forum/pagination'], function(taskbar, helpers, pagination) { taskbar.init(); // templates.js helpers helpers.register(); + pagination.init(); + $(window).trigger('action:app.load'); }); }); diff --git a/public/src/client/category.js b/public/src/client/category.js index 7a295c1b28..cfb7de08d1 100644 --- a/public/src/client/category.js +++ b/public/src/client/category.js @@ -2,7 +2,6 @@ /* global define, config, templates, app, utils, ajaxify, socket */ define('forum/category', [ - 'forum/pagination', 'forum/infinitescroll', 'share', 'navigator', @@ -10,8 +9,7 @@ define('forum/category', [ 'sort', 'components', 'translator' - -], function(pagination, infinitescroll, share, navigator, categoryTools, sort, components, translator) { +], function(infinitescroll, share, navigator, categoryTools, sort, components, translator) { var Category = {}; $(window).on('action:ajaxify.start', function(ev, data) { @@ -112,7 +110,7 @@ define('forum/category', [ if (config.usePagination) { var page = Math.ceil((parseInt(bookmarkIndex, 10) + 1) / config.topicsPerPage); - if (parseInt(page, 10) !== pagination.currentPage) { + if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) { pagination.loadPage(page, function() { Category.scrollToTopic(bookmarkIndex, clickedIndex, 400); }); @@ -176,7 +174,6 @@ define('forum/category', [ infinitescroll.init($('[component="category"]'), Category.loadMoreTopics); } else { navigator.hide(); - pagination.init(ajaxify.data.currentPage, ajaxify.data.pageCount); } } diff --git a/public/src/client/pagination.js b/public/src/client/pagination.js index a94967c7c5..534b4e27f2 100644 --- a/public/src/client/pagination.js +++ b/public/src/client/pagination.js @@ -4,14 +4,8 @@ define('forum/pagination', function() { var pagination = {}; - pagination.currentPage = 0; - pagination.pageCount = 0; - - pagination.init = function(currentPage, pageCount) { - pagination.currentPage = parseInt(currentPage, 10); - pagination.pageCount = parseInt(pageCount, 10); - - $('.pagination').on('click', '.select-page', function(e) { + pagination.init = function() { + $('body').on('click', '.pagination .select-page', function(e) { e.preventDefault(); bootbox.prompt('Enter page number:', function(pageNum) { pagination.loadPage(pageNum); @@ -22,10 +16,14 @@ define('forum/pagination', function() { pagination.loadPage = function(page, callback) { callback = callback || function() {}; page = parseInt(page, 10); - if (!utils.isNumber(page) || page < 1 || page > pagination.pageCount) { + if (!utils.isNumber(page) || page < 1 || page > ajaxify.data.pagination.pageCount) { return; } - var url = window.location.pathname.slice(1).split('/').slice(0, 3).join('/') + '?page=' + page; + + var query = utils.params(); + query.page = page; + + var url = window.location.pathname + '?' + $.param(query); ajaxify.go(url, callback); }; diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 89b6e30a1d..116dc62a1b 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -4,7 +4,6 @@ /* globals define, app, socket, config, ajaxify, RELATIVE_PATH, utils */ define('forum/topic', [ - 'forum/pagination', 'forum/infinitescroll', 'forum/topic/threadTools', 'forum/topic/postTools', @@ -14,7 +13,7 @@ define('forum/topic', [ 'navigator', 'sort', 'components' -], function(pagination, infinitescroll, threadTools, postTools, events, browsing, posts, navigator, sort, components) { +], function(infinitescroll, threadTools, postTools, events, browsing, posts, navigator, sort, components) { var Topic = {}, currentUrl = ''; @@ -147,7 +146,7 @@ define('forum/topic', [ if (components.get('post/anchor', postIndex).length) { return navigator.scrollToPostIndex(postIndex, true); } - } else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && ajaxify.data.postcount > 10) { + } else if (bookmark && (!config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1)) && ajaxify.data.postcount > 10) { app.alert({ alert_id: 'bookmark', message: '[[topic:bookmark_instructions]]', @@ -218,12 +217,9 @@ define('forum/topic', [ infinitescroll.init($('[component="topic"]'), posts.loadMorePosts); } else { navigator.hide(); - - pagination.init(parseInt(ajaxify.data.currentPage, 10), parseInt(ajaxify.data.pageCount, 10)); } } - function updateTopicTitle() { if ($(window).scrollTop() > 50) { components.get('navbar/title').find('span').text(ajaxify.data.title).show(); diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js index fd5f310b59..222c07f98b 100644 --- a/public/src/client/topic/posts.js +++ b/public/src/client/topic/posts.js @@ -51,15 +51,15 @@ define('forum/topic/posts', [ var posts = data.posts; - pagination.pageCount = Math.max(1, Math.ceil((posts[0].topic.postcount - 1) / config.postsPerPage)); + ajaxify.data.pagination.pageCount = Math.max(1, Math.ceil((posts[0].topic.postcount - 1) / config.postsPerPage)); var direction = config.topicPostSort === 'oldest_to_newest' || config.topicPostSort === 'most_votes' ? 1 : -1; - var isPostVisible = (pagination.currentPage === pagination.pageCount && direction === 1) || (pagination.currentPage === 1 && direction === -1); + var isPostVisible = (ajaxify.data.pagination.currentPage === ajaxify.data.pagination.pageCount && direction === 1) || (ajaxify.data.pagination.currentPage === 1 && direction === -1); if (isPostVisible) { createNewPosts(data, components.get('post').not('[data-index=0]'), direction, scrollToPost); } else if (parseInt(posts[0].uid, 10) === parseInt(app.user.uid, 10)) { - pagination.loadPage(pagination.pageCount, scrollToPost); + pagination.loadPage(ajaxify.data.pagination.pageCount, scrollToPost); } } diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 251cc78182..c9275348f3 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -162,7 +162,7 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com if (config.usePagination) { var page = Math.max(1, Math.ceil(postIndex / config.postsPerPage)); - if (parseInt(page, 10) !== pagination.currentPage) { + if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) { pagination.loadPage(page, function() { navigator.scrollToPostIndex(postIndex, highlight, duration); }); diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 220779c524..b72921c5c8 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -81,7 +81,7 @@ categoriesController.list = function(req, res, next) { categoriesController.get = function(req, res, callback) { var cid = req.params.category_id, - page = parseInt(req.query.page, 10) || 1, + currentPage = parseInt(req.query.page, 10) || 1, pageCount = 1, userPrivileges; @@ -127,7 +127,7 @@ categoriesController.get = function(req, res, callback) { return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : '')); } - if (settings.usePagination && (page < 1 || page > pageCount)) { + if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) { return callback(); } @@ -135,7 +135,7 @@ categoriesController.get = function(req, res, callback) { topicIndex = Math.max(topicIndex - (settings.topicsPerPage - 1), 0); } else if (!req.query.page) { var index = Math.max(parseInt((topicIndex || 0), 10), 0); - page = Math.ceil((index + 1) / settings.topicsPerPage); + currentPage = Math.ceil((index + 1) / settings.topicsPerPage); topicIndex = 0; } @@ -149,7 +149,7 @@ categoriesController.get = function(req, res, callback) { set = 'cid:' + cid + ':tids:posts'; } - var start = (page - 1) * settings.topicsPerPage + topicIndex, + var start = (currentPage - 1) * settings.topicsPerPage + topicIndex, stop = start + settings.topicsPerPage - 1; next(null, { @@ -249,12 +249,10 @@ categoriesController.get = function(req, res, callback) { return callback(err); } - data.currentPage = page; - data.pageCount = pageCount; data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data.rssFeedUrl = nconf.get('relative_path') + '/category/' + data.cid + '.rss'; data.title = data.name; - data.pagination = pagination.create(data.currentPage, data.pageCount); + data.pagination = pagination.create(currentPage, pageCount); data.pagination.rel.forEach(function(rel) { rel.href = nconf.get('url') + '/category/' + data.slug + rel.href; res.locals.linkTags.push(rel); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index abbb245f28..7f386c5af8 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -18,6 +18,8 @@ var topicsController = {}, topicsController.get = function(req, res, callback) { var tid = req.params.topic_id, sort = req.query.sort, + currentPage = parseInt(req.query.page, 10) || 1, + pageCount = 1, userPrivileges; if ((req.params.post_index && !utils.isNumber(req.params.post_index)) || !utils.isNumber(tid)) { @@ -56,14 +58,13 @@ topicsController.get = function(req, res, callback) { var settings = results.settings; var postCount = parseInt(results.topic.postcount, 10); - var pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage)); - var page = parseInt(req.query.page, 10) || 1; + pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage)); if (utils.isNumber(req.params.post_index) && (req.params.post_index < 1 || req.params.post_index > postCount)) { return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : '')); } - if (settings.usePagination && (page < 1 || page > pageCount)) { + if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) { return callback(); } @@ -105,10 +106,10 @@ topicsController.get = function(req, res, callback) { index = Math.max(0, req.params.post_index - 1) || 0; } - page = Math.max(1, Math.ceil(index / settings.postsPerPage)); + currentPage = Math.max(1, Math.ceil(index / settings.postsPerPage)); } - var start = (page - 1) * settings.postsPerPage + postIndex, + var start = (currentPage - 1) * settings.postsPerPage + postIndex, stop = start + settings.postsPerPage - 1; topics.getTopicWithPosts(tid, set, req.uid, start, stop, reverse, function (err, topicData) { @@ -120,9 +121,6 @@ topicsController.get = function(req, res, callback) { return next(err); } - topicData.pageCount = pageCount; - topicData.currentPage = page; - topics.modifyByPrivilege(topicData.posts, results.privileges); plugins.fireHook('filter:controllers.topic.get', topicData, next); @@ -261,7 +259,7 @@ topicsController.get = function(req, res, callback) { data['downvote:disabled'] = parseInt(meta.config['downvote:disabled'], 10) === 1; data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.rss'; - data.pagination = pagination.create(data.currentPage, data.pageCount); + data.pagination = pagination.create(currentPage, pageCount); data.pagination.rel.forEach(function(rel) { rel.href = nconf.get('url') + '/topic/' + data.slug + rel.href; res.locals.linkTags.push(rel); diff --git a/src/pagination.js b/src/pagination.js index 58813c391b..15b8aa6eb3 100644 --- a/src/pagination.js +++ b/src/pagination.js @@ -10,7 +10,9 @@ pagination.create = function(currentPage, pageCount, queryObj) { prev: {page: 1, active: currentPage > 1}, next: {page: 1, active: currentPage < pageCount}, rel: [], - pages: [] + pages: [], + currentPage: 1, + pageCount: 1 }; } pageCount = parseInt(pageCount, 10); @@ -44,7 +46,7 @@ pagination.create = function(currentPage, pageCount, queryObj) { } } - var data = {rel: [], pages: pages}; + var data = {rel: [], pages: pages, currentPage: currentPage, pageCount: pageCount}; queryObj.page = previous; data.prev = {page: previous, active: currentPage > 1, qs: qs.stringify(queryObj)}; queryObj.page = next; diff --git a/src/user/search.js b/src/user/search.js index 60f125096a..ae91ba1615 100644 --- a/src/user/search.js +++ b/src/user/search.js @@ -45,7 +45,6 @@ module.exports = function(User) { var pagination = User.paginate(page, uids); uids = pagination.data; searchResult.pagination = pagination.pagination; - searchResult.pageCount = pagination.pageCount; } User.getUsers(uids, uid, next);