From ef6fd35e85ce20d679aef9692d711f12ffe4fc56 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Fri, 24 Jan 2014 22:26:11 -0500 Subject: [PATCH] lots of cleanup, moved pagination to requirejs module --- public/src/forum/category.js | 54 ++++++++++++++++------- public/src/forum/pagination.js | 79 ++++++++++++++++++++++++++++++++++ public/src/forum/topic.js | 73 ++++++------------------------- public/templates/category.tpl | 15 ++++++- src/categories.js | 39 +++++++++++++++-- src/routes/api.js | 2 +- src/socket.io/categories.js | 25 ++++++++++- src/topics.js | 24 ++++++----- 8 files changed, 220 insertions(+), 91 deletions(-) create mode 100644 public/src/forum/pagination.js diff --git a/public/src/forum/category.js b/public/src/forum/category.js index a2f7443768..d995aaf8d9 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -1,13 +1,10 @@ -define(['composer'], function(composer) { +define(['composer', 'forum/pagination'], function(composer, pagination) { var Category = {}, loadingMoreTopics = false; Category.init = function() { var cid = templates.get('category_id'), categoryName = templates.get('category_name'), - twitterEl = jQuery('#twitter-intent'), - facebookEl = jQuery('#facebook-share'), - googleEl = jQuery('#google-share'), categoryUrl = encodeURIComponent(window.location.href), twitterUrl = "https://twitter.com/intent/tweet?url=" + categoryUrl + "&text=" + encodeURIComponent(categoryName), facebookUrl = "https://www.facebook.com/sharer/sharer.php?u=" + categoryUrl, @@ -15,17 +12,17 @@ define(['composer'], function(composer) { app.enterRoom('category_' + cid); - - - twitterEl.on('click', function () { + $('#twitter-intent').on('click', function () { window.open(twitterUrl, '_blank', 'width=550,height=420,scrollbars=no,status=no'); return false; }); - facebookEl.on('click', function () { + + $('#facebook-share').on('click', function () { window.open(facebookUrl, '_blank', 'width=626,height=436,scrollbars=no,status=no'); return false; }); - googleEl.on('click', function () { + + $('#google-share').on('click', function () { window.open(googleUrl, '_blank', 'width=500,height=570,scrollbars=no,status=no'); return false; }); @@ -42,14 +39,36 @@ define(['composer'], function(composer) { socket.emit('categories.getRecentReplies', cid, renderRecentReplies); - $(window).off('scroll').on('scroll', function (ev) { - var bottom = ($(document).height() - $(window).height()) * 0.9; + enableInfiniteLoading(); + }; + + function enableInfiniteLoading() { + if(!config.usePagination) { + $(window).off('scroll').on('scroll', function (ev) { + var bottom = ($(document).height() - $(window).height()) * 0.9; + + if ($(window).scrollTop() > bottom && !loadingMoreTopics) { + Category.loadMoreTopics(cid); + } + }); + } else { + pagination.init(templates.get('currentPage'), templates.get('pageCount'), loadPage); + } + } - if ($(window).scrollTop() > bottom && !loadingMoreTopics) { - Category.loadMoreTopics(cid); + function loadPage(page, callback) { + socket.emit('categories.loadPage', {cid: templates.get('category_id'), page: page}, function(err, data) { + if(err) { + return callback(err); } + + if (data && data.topics && data.topics.length) { + Category.onTopicsLoaded(data.topics); + } + + callback(null); }); - }; + } Category.onNewTopic = function(data) { var html = templates.prepare(templates['category'].blocks['topics']).parse({ @@ -116,7 +135,12 @@ define(['composer'], function(composer) { jQuery('#category-no-topics').remove(); html = $(translatedHTML); - container.append(html); + + if(config.usePagination) { + container.empty().append(html); + } else { + container.append(html); + } $('#topics-container span.timeago').timeago(); app.makeNumbersHumanReadable(html.find('.human-readable-number')); diff --git a/public/src/forum/pagination.js b/public/src/forum/pagination.js new file mode 100644 index 0000000000..4ebc89e313 --- /dev/null +++ b/public/src/forum/pagination.js @@ -0,0 +1,79 @@ + + +define(function() { + var pagination = {}; + + pagination.currentPage = 0; + pagination.pageCount = 0; + pagination.loadFunction = null; + + pagination.init = function(currentPage, pageCount, loadFunction) { + pagination.currentPage = parseInt(currentPage, 10); + pagination.pageCount = parseInt(pageCount, 10); + pagination.loadFunction = loadFunction; + + updatePageLinks(); + + $('.pagination').on('click', '.previous', function() { + pagination.loadPage(pagination.currentPage - 1); + }); + + $('.pagination').on('click', '.next', function() { + pagination.loadPage(pagination.currentPage + 1); + }); + + $('.pagination').on('click', '.page', function() { + pagination.loadPage($(this).attr('data-page')); + }); + } + + pagination.recreatePaginationLinks = function(newPageCount) { + pagination.pageCount = parseInt(newPageCount); + + var pages = []; + for(var i=1; i<=pagination.page; ++i) { + pages.push({pageNumber: i}); + } + + var html = templates.prepare(templates['topic'].blocks['pages']).parse({pages:pages}); + html = $(html); + $('.pagination li.page').remove(); + html.insertAfter($('.pagination li.previous')); + updatePageLinks(); + } + + pagination.loadPage = function(page, callback) { + page = parseInt(page, 10); + if(page < 1 || page > pagination.pageCount) { + return; + } + + pagination.loadFunction(page, function(err) { + if(err) { + return app.alertError(err.message); + } + + pagination.currentPage = parseInt(page, 10); + updatePageLinks(); + }); + } + + + function updatePageLinks() { + $('.pagination .next').removeClass('disabled'); + $('.pagination .previous').removeClass('disabled'); + + if(pagination.currentPage === 1) { + $('.pagination .previous').addClass('disabled'); + } + + if(pagination.currentPage === pagination.pageCount) { + $('.pagination .next').addClass('disabled'); + } + + $('.pagination .page').removeClass('active'); + $('.pagination .page[data-page="' + pagination.currentPage + '"]').addClass('active'); + } + + return pagination; +}); \ No newline at end of file diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index d17b136c63..9f18bd247e 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -1,4 +1,4 @@ -define(['composer'], function(composer) { +define(['composer', 'forum/pagination'], function(composer, pagination) { var Topic = {}, infiniteLoaderActive = false, pagination; @@ -357,44 +357,12 @@ define(['composer'], function(composer) { }); } else { $('.pagination-block').addClass('hide'); - updatePageLinks(); - $('.pagination').on('click', '.previous', function() { - loadPage(currentPage - 1); - }); - - $('.pagination').on('click', '.next', function() { - loadPage(currentPage + 1); - }); - - $('.pagination').on('click', '.page', function() { - loadPage($(this).attr('data-page')); - }); + pagination.init(currentPage, pageCount, loadPage); } } - function updatePageLinks() { - $('.pagination .next').removeClass('disabled'); - $('.pagination .previous').removeClass('disabled'); - - if(currentPage === 1) { - $('.pagination .previous').addClass('disabled'); - } - - if(currentPage === pageCount) { - $('.pagination .next').addClass('disabled'); - } - - $('.pagination .page').removeClass('active'); - $('.pagination .page[data-page="' + currentPage + '"]').addClass('active'); - } - - function loadPage(page) { - page = parseInt(page, 10); - if(page < 1 || page > pageCount) { - return; - } - + function loadPage(page, callback) { if(page === 1) { ajaxify.go('topic/' + tid ); return; @@ -402,9 +370,8 @@ define(['composer'], function(composer) { socket.emit('topics.loadPage', {tid: tid, page: page}, function(err, data) { if(err) { - return app.alertError(err.message); + return callback(err); } - currentPage = page; if (data && data.posts && data.posts.length) { createPagePosts(data, function() { @@ -412,7 +379,7 @@ define(['composer'], function(composer) { }); } - updatePageLinks(); + callback(null); }); } @@ -420,27 +387,14 @@ define(['composer'], function(composer) { var posts = data.posts; socket.emit('topics.getPageCount', tid, function(err, newPageCount) { - pageCount = newPageCount; - recreatePaginationLinks(); - if(currentPage === newPageCount) { + pagination.recreatePaginationLinks(newPageCount); + + if(pagination.currentPage === pagination.newPageCount) { createNewPosts(data); } else if(data.posts && data.posts.length && parseInt(data.posts[0].uid, 10) === parseInt(app.uid, 10)) { - loadPage(pageCount); + pagination.loadPage(pagination.pageCount); } }); - - } - - function recreatePaginationLinks() { - var pages = []; - for(var i=1; i<=pageCount; ++i) { - pages.push({pageNumber: i}); - } - var html = templates.prepare(templates['topic'].blocks['pages']).parse({pages:pages}); - html = $(html); - $('.pagination li.page').remove(); - html.insertAfter($('.pagination li.previous')); - updatePageLinks(); } function createPagePosts(data, callback) { @@ -451,11 +405,9 @@ define(['composer'], function(composer) { parseAndTranslatePosts(data.posts, function(translatedHTML) { var translated = $(translatedHTML); - $('#post-container').fadeOut(function() { + $('#post-container').fadeOut(200, function() { - $(this).empty(); - translated.appendTo($(this)); - $(this).fadeIn('slow'); + $('#post-container').empty().append(translated).fadeIn('slow'); onNewPostsLoaded(data.posts); @@ -1068,7 +1020,8 @@ define(['composer'], function(composer) { var progressBarContainer = $('.progress-container'); var tid = templates.get('topic_id'); - pagination.parentNode.style.display = 'block'; + if(pagination.parentNode) + pagination.parentNode.style.display = 'block'; progressBarContainer.css('display', ''); if (scrollTop < jQuery('.posts > .post-row:first-child').height() && Topic.postCount > 1) { diff --git a/public/templates/category.tpl b/public/templates/category.tpl index 8ba94b3788..6bbd2a5ae7 100644 --- a/public/templates/category.tpl +++ b/public/templates/category.tpl @@ -84,6 +84,17 @@ + +
+ +
+
@@ -121,4 +132,6 @@
- \ No newline at end of file + + + \ No newline at end of file diff --git a/src/categories.js b/src/categories.js index 06394ee47f..0a742c00e7 100644 --- a/src/categories.js +++ b/src/categories.js @@ -52,7 +52,8 @@ var db = require('./database.js'), } function getTopicIds(next) { - Categories.getTopicIds(category_id, 0, 19, next); + var topicsPerPage = meta.config.topicsPerPage || 20; + Categories.getTopicIds(category_id, 0, topicsPerPage - 1, next); } function getActiveUsers(next) { @@ -65,10 +66,15 @@ var db = require('./database.js'), }); } - async.parallel([getTopicIds, getActiveUsers, getSidebars], function(err, results) { + function getPages(next) { + Categories.getPages(category_id, next); + } + + async.parallel([getTopicIds, getActiveUsers, getSidebars, getPages], function(err, results) { var tids = results[0], active_users = results[1], - sidebars = results[2]; + sidebars = results[2], + pages = results[3]; var category = { 'category_name': categoryData.name, @@ -82,6 +88,8 @@ var db = require('./database.js'), 'category_id': category_id, 'active_users': [], 'topics': [], + 'pages': pages, + 'pageCount': pages.length, 'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false, 'sidebars': sidebars }; @@ -137,6 +145,31 @@ var db = require('./database.js'), db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback); }; + Categories.getPages = function(cid, callback) { + Categories.getPageCount(cid, function(err, pageCount) { + if(err) { + return callback(err); + } + var pages = []; + for(var i=1; i<=pageCount; ++i) { + pages.push({pageNumber: i}); + } + callback(null, pages); + }); + }; + + Categories.getPageCount = function(cid, callback) { + db.sortedSetCard('categories:' + cid + ':tid', function(err, topicCount) { + if(err) { + return callback(err); + } + + var topicsPerPage = parseInt(meta.config.topicsPerPage, 10); + topicsPerPage = topicsPerPage ? topicsPerPage : 20; + + callback(null, Math.ceil(parseInt(topicCount, 10) / topicsPerPage)); + }); + }; Categories.getAllCategories = function(current_user, callback) { db.getListRange('categories:cid', 0, -1, function(err, cids) { diff --git a/src/routes/api.js b/src/routes/api.js index ca5c63fd6f..171dc5147d 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -200,7 +200,7 @@ var path = require('path'), return next(err); } - // Add privilege data to template data + data.currentPage = 1; data.privileges = privileges; if (data && parseInt(data.disabled, 10) === 0) { diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 545635750d..b664114cdc 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -1,4 +1,5 @@ var categories = require('../categories'), + meta = require('./../meta'), SocketCategories = {}; @@ -15,8 +16,10 @@ SocketCategories.loadMore = function(socket, data, callback) { return callback(new Error('invalid data')); } + var topicsPerPage = parseInt(meta.config.topicsPerPage, 10) || 20; + var start = data.after, - end = start + 9; + end = start + topicsPerPage - 1; categories.getCategoryTopics(data.cid, start, end, socket.uid, function(err, topics) { callback(err, { @@ -25,4 +28,24 @@ SocketCategories.loadMore = function(socket, data, callback) { }); }; +SocketCategories.loadPage = function(socket, data, callback) { + if(!data) { + return callback(new Error('invalid data')); + } + + var topicsPerPage = parseInt(meta.config.topicsPerPage, 10) || 20; + + var start = (data.page - 1) * topicsPerPage, + end = start + topicsPerPage - 1; + + console.log('start to end' ,start, end); + + categories.getCategoryTopics(data.cid, start, end, socket.uid, function(err, topics) { + console.log('getting topics', topics.length); + callback(err, { + topics: topics + }); + }); +} + module.exports = SocketCategories; \ No newline at end of file diff --git a/src/topics.js b/src/topics.js index b64e4fee1a..439f751d36 100644 --- a/src/topics.js +++ b/src/topics.js @@ -392,6 +392,19 @@ var async = require('async'), }); } + Topics.getPages = function(tid, callback) { + Topics.getPageCount(tid, function(err, pageCount) { + if(err) { + return callback(err); + } + var pages = []; + for(var i=1; i<=pageCount; ++i) { + pages.push({pageNumber: i}); + } + callback(null, pages); + }); + } + Topics.getPageCount = function(tid, callback) { db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) { if(err) { @@ -782,16 +795,7 @@ var async = require('async'), } function getPages(next) { - Topics.getPageCount(tid, function(err, pageCount) { - if(err) { - return next(err); - } - var pages = []; - for(var i=1; i<=pageCount; ++i) { - pages.push({pageNumber: i}); - } - next(null, pages); - }); + Topics.getPages(tid, next); } async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPages], function(err, results) {