From 1cabf885a2435c39badf8c6a001c869b35e0bb20 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 26 Jun 2014 22:11:16 -0400 Subject: [PATCH] closes #1730 postCount != lastPostIndex unfortunately, that will need some rethinking --- public/language/en_GB/global.json | 1 + public/src/modules/navigator.js | 37 ++++++++++++++++++++++++++----- src/controllers/categories.js | 10 ++++++++- src/middleware/middleware.js | 20 +++++++++++++++++ src/routes/index.js | 3 +++ 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/public/language/en_GB/global.json b/public/language/en_GB/global.json index b26ad47c8a..dab7fd1df6 100644 --- a/public/language/en_GB/global.json +++ b/public/language/en_GB/global.json @@ -24,6 +24,7 @@ "pagination": "Pagination", "pagination.out_of": "%1 out of %2", + "pagination.enter_index": "Enter index", "header.admin": "Admin", "header.recent": "Recent", diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 21a4a982f1..f41602131a 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -1,7 +1,7 @@ 'use strict'; -/* globals app, define, ajaxify */ +/* globals app, define, ajaxify, utils, translator */ define('navigator', function() { @@ -17,22 +17,49 @@ define('navigator', function() { $(window).on('scroll', navigator.update); - $('.pagination-block a').off('click').on('click', function() { - return false; + $('.pagination-block .dropdown-menu').off('click').on('click', function(e) { + e.stopPropagation(); }); - $('.pagination-block i:first').off('click').on('click', function() { + $('.pagination-block .pageup').off('click').on('click', function() { navigator.scrollToTop(); }); - $('.pagination-block i:last').off('click').on('click', function() { + $('.pagination-block .pagedown').off('click').on('click', function() { navigator.scrollToBottom(); }); + $('.pagination-block .pagetop').off('click').on('click', function() { + ajaxify.go(generateUrl()); + }); + + $('.pagination-block .pagebottom').off('click').on('click', function() { + ajaxify.go(generateUrl(count)); + }); + + $('.pagination-block input').on('keydown', function(e) { + if (e.which === 13) { + var input = $(this); + if (!utils.isNumber(input.val())) { + input.val(''); + return; + } + var url = generateUrl(input.val()); + input.val(''); + $('.pagination-block .dropdown-toggle').trigger('click'); + ajaxify.go(url); + } + }); + navigator.setCount(count); navigator.update(); }; + function generateUrl(index) { + var parts = window.location.pathname.split('/'); + return parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : ''); + } + navigator.setCount = function(value) { count = parseInt(value, 10); navigator.updateTextAndProgressBar(); diff --git a/src/controllers/categories.js b/src/controllers/categories.js index b066d4c8e4..705ff09612 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -90,7 +90,15 @@ categoriesController.get = function(req, res, next) { return next(err); } - var start = (page - 1) * settings.topicsPerPage, + var topicIndex = 0; + if (!settings.usePagination) { + topicIndex = Math.max((req.params.topic_index || 1) - (settings.topicsPerPage - 1), 0); + } else if (!req.query.page) { + var index = Math.max(parseInt((req.params.topic_index || 0), 10), 0); + page = Math.ceil((index + 1) / settings.topicsPerPage); + } + + var start = (page - 1) * settings.topicsPerPage + topicIndex, end = start + settings.topicsPerPage - 1; categories.getCategoryById(cid, start, end, uid, function (err, categoryData) { diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 1ad8b0c060..bb8313236d 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -99,6 +99,26 @@ middleware.checkPostIndex = function(req, res, next) { }); }; +middleware.checkTopicIndex = function(req, res, next) { + categories.getCategoryField(req.params.category_id, 'topic_count', function(err, topicCount) { + if (err) { + return next(err); + } + var topicIndex = parseInt(req.params.topic_index, 10); + topicCount = parseInt(topicCount, 10) + 1; + var url = ''; + console.log(topicIndex, topicCount); + if (topicIndex > topicCount) { + url = '/category/' + req.params.category_id + '/' + req.params.slug + '/' + topicCount; + return res.locals.isAPI ? res.json(302, url) : res.redirect(url); + } else if (topicIndex < 1) { + url = '/category/' + req.params.category_id + '/' + req.params.slug; + return res.locals.isAPI ? res.json(302, url) : res.redirect(url); + } + next(); + }); +}; + middleware.prepareAPI = function(req, res, next) { res.locals.isAPI = true; next(); diff --git a/src/routes/index.js b/src/routes/index.js index 184a658e81..9166d340c1 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -79,6 +79,9 @@ function categoryRoutes(app, middleware, controllers) { app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal); + app.get('/category/:category_id/:slug/:topic_index', middleware.buildHeader, middleware.checkTopicIndex, controllers.categories.get); + app.get('/api/category/:category_id/:slug/:topic_index', middleware.checkTopicIndex, controllers.categories.get); + app.get('/category/:category_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.categories.get); app.get('/api/category/:category_id/:slug?', controllers.categories.get); }