lots of cleanup, moved pagination to requirejs module

v1.18.x
Baris Soner Usakli 11 years ago
parent aa73a10fc4
commit ef6fd35e85

@ -1,13 +1,10 @@
define(['composer'], function(composer) { define(['composer', 'forum/pagination'], function(composer, pagination) {
var Category = {}, var Category = {},
loadingMoreTopics = false; loadingMoreTopics = false;
Category.init = function() { Category.init = function() {
var cid = templates.get('category_id'), var cid = templates.get('category_id'),
categoryName = templates.get('category_name'), categoryName = templates.get('category_name'),
twitterEl = jQuery('#twitter-intent'),
facebookEl = jQuery('#facebook-share'),
googleEl = jQuery('#google-share'),
categoryUrl = encodeURIComponent(window.location.href), categoryUrl = encodeURIComponent(window.location.href),
twitterUrl = "https://twitter.com/intent/tweet?url=" + categoryUrl + "&text=" + encodeURIComponent(categoryName), twitterUrl = "https://twitter.com/intent/tweet?url=" + categoryUrl + "&text=" + encodeURIComponent(categoryName),
facebookUrl = "https://www.facebook.com/sharer/sharer.php?u=" + categoryUrl, facebookUrl = "https://www.facebook.com/sharer/sharer.php?u=" + categoryUrl,
@ -15,17 +12,17 @@ define(['composer'], function(composer) {
app.enterRoom('category_' + cid); app.enterRoom('category_' + cid);
$('#twitter-intent').on('click', function () {
twitterEl.on('click', function () {
window.open(twitterUrl, '_blank', 'width=550,height=420,scrollbars=no,status=no'); window.open(twitterUrl, '_blank', 'width=550,height=420,scrollbars=no,status=no');
return false; return false;
}); });
facebookEl.on('click', function () {
$('#facebook-share').on('click', function () {
window.open(facebookUrl, '_blank', 'width=626,height=436,scrollbars=no,status=no'); window.open(facebookUrl, '_blank', 'width=626,height=436,scrollbars=no,status=no');
return false; return false;
}); });
googleEl.on('click', function () {
$('#google-share').on('click', function () {
window.open(googleUrl, '_blank', 'width=500,height=570,scrollbars=no,status=no'); window.open(googleUrl, '_blank', 'width=500,height=570,scrollbars=no,status=no');
return false; return false;
}); });
@ -42,14 +39,36 @@ define(['composer'], function(composer) {
socket.emit('categories.getRecentReplies', cid, renderRecentReplies); socket.emit('categories.getRecentReplies', cid, renderRecentReplies);
$(window).off('scroll').on('scroll', function (ev) { enableInfiniteLoading();
var bottom = ($(document).height() - $(window).height()) * 0.9; };
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) { function loadPage(page, callback) {
Category.loadMoreTopics(cid); 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) { Category.onNewTopic = function(data) {
var html = templates.prepare(templates['category'].blocks['topics']).parse({ var html = templates.prepare(templates['category'].blocks['topics']).parse({
@ -116,7 +135,12 @@ define(['composer'], function(composer) {
jQuery('#category-no-topics').remove(); jQuery('#category-no-topics').remove();
html = $(translatedHTML); html = $(translatedHTML);
container.append(html);
if(config.usePagination) {
container.empty().append(html);
} else {
container.append(html);
}
$('#topics-container span.timeago').timeago(); $('#topics-container span.timeago').timeago();
app.makeNumbersHumanReadable(html.find('.human-readable-number')); app.makeNumbersHumanReadable(html.find('.human-readable-number'));

@ -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;
});

@ -1,4 +1,4 @@
define(['composer'], function(composer) { define(['composer', 'forum/pagination'], function(composer, pagination) {
var Topic = {}, var Topic = {},
infiniteLoaderActive = false, infiniteLoaderActive = false,
pagination; pagination;
@ -357,44 +357,12 @@ define(['composer'], function(composer) {
}); });
} else { } else {
$('.pagination-block').addClass('hide'); $('.pagination-block').addClass('hide');
updatePageLinks();
$('.pagination').on('click', '.previous', function() { pagination.init(currentPage, pageCount, loadPage);
loadPage(currentPage - 1);
});
$('.pagination').on('click', '.next', function() {
loadPage(currentPage + 1);
});
$('.pagination').on('click', '.page', function() {
loadPage($(this).attr('data-page'));
});
} }
} }
function updatePageLinks() { function loadPage(page, callback) {
$('.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;
}
if(page === 1) { if(page === 1) {
ajaxify.go('topic/' + tid ); ajaxify.go('topic/' + tid );
return; return;
@ -402,9 +370,8 @@ define(['composer'], function(composer) {
socket.emit('topics.loadPage', {tid: tid, page: page}, function(err, data) { socket.emit('topics.loadPage', {tid: tid, page: page}, function(err, data) {
if(err) { if(err) {
return app.alertError(err.message); return callback(err);
} }
currentPage = page;
if (data && data.posts && data.posts.length) { if (data && data.posts && data.posts.length) {
createPagePosts(data, function() { 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; var posts = data.posts;
socket.emit('topics.getPageCount', tid, function(err, newPageCount) { socket.emit('topics.getPageCount', tid, function(err, newPageCount) {
pageCount = newPageCount; pagination.recreatePaginationLinks(newPageCount);
recreatePaginationLinks();
if(currentPage === newPageCount) { if(pagination.currentPage === pagination.newPageCount) {
createNewPosts(data); createNewPosts(data);
} else if(data.posts && data.posts.length && parseInt(data.posts[0].uid, 10) === parseInt(app.uid, 10)) { } 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) { function createPagePosts(data, callback) {
@ -451,11 +405,9 @@ define(['composer'], function(composer) {
parseAndTranslatePosts(data.posts, function(translatedHTML) { parseAndTranslatePosts(data.posts, function(translatedHTML) {
var translated = $(translatedHTML); var translated = $(translatedHTML);
$('#post-container').fadeOut(function() { $('#post-container').fadeOut(200, function() {
$(this).empty(); $('#post-container').empty().append(translated).fadeIn('slow');
translated.appendTo($(this));
$(this).fadeIn('slow');
onNewPostsLoaded(data.posts); onNewPostsLoaded(data.posts);
@ -1068,7 +1020,8 @@ define(['composer'], function(composer) {
var progressBarContainer = $('.progress-container'); var progressBarContainer = $('.progress-container');
var tid = templates.get('topic_id'); var tid = templates.get('topic_id');
pagination.parentNode.style.display = 'block'; if(pagination.parentNode)
pagination.parentNode.style.display = 'block';
progressBarContainer.css('display', ''); progressBarContainer.css('display', '');
if (scrollTop < jQuery('.posts > .post-row:first-child').height() && Topic.postCount > 1) { if (scrollTop < jQuery('.posts > .post-row:first-child').height() && Topic.postCount > 1) {

@ -84,6 +84,17 @@
</li> </li>
<!-- END topics --> <!-- END topics -->
</ul> </ul>
<!-- IF usePagination -->
<div class="text-center">
<ul class="pagination">
<li class="previous pull-left"><a href="#">&larr; Older</a></li>
<!-- BEGIN pages -->
<li class="page" data-page="{pages.pageNumber}"><a href="#">{pages.pageNumber}</a></li>
<!-- END pages -->
<li class="next pull-right"><a href="#">Newer &rarr;</a></li>
</ul>
</div>
<!-- ENDIF usePagination -->
</div> </div>
<div class="col-md-3 col-xs-12 {show_sidebar} category-sidebar"> <div class="col-md-3 col-xs-12 {show_sidebar} category-sidebar">
<div class="panel panel-default"> <div class="panel panel-default">
@ -122,3 +133,5 @@
<input type="hidden" template-variable="category_id" value="{category_id}" /> <input type="hidden" template-variable="category_id" value="{category_id}" />
<input type="hidden" template-variable="category_name" value="{category_name}" /> <input type="hidden" template-variable="category_name" value="{category_name}" />
<input type="hidden" template-variable="currentPage" value="{currentPage}" />
<input type="hidden" template-variable="pageCount" value="{pageCount}" />

@ -52,7 +52,8 @@ var db = require('./database.js'),
} }
function getTopicIds(next) { 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) { 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], var tids = results[0],
active_users = results[1], active_users = results[1],
sidebars = results[2]; sidebars = results[2],
pages = results[3];
var category = { var category = {
'category_name': categoryData.name, 'category_name': categoryData.name,
@ -82,6 +88,8 @@ var db = require('./database.js'),
'category_id': category_id, 'category_id': category_id,
'active_users': [], 'active_users': [],
'topics': [], 'topics': [],
'pages': pages,
'pageCount': pages.length,
'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false, 'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false,
'sidebars': sidebars 'sidebars': sidebars
}; };
@ -137,6 +145,31 @@ var db = require('./database.js'),
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback); 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) { Categories.getAllCategories = function(current_user, callback) {
db.getListRange('categories:cid', 0, -1, function(err, cids) { db.getListRange('categories:cid', 0, -1, function(err, cids) {

@ -200,7 +200,7 @@ var path = require('path'),
return next(err); return next(err);
} }
// Add privilege data to template data data.currentPage = 1;
data.privileges = privileges; data.privileges = privileges;
if (data && parseInt(data.disabled, 10) === 0) { if (data && parseInt(data.disabled, 10) === 0) {

@ -1,4 +1,5 @@
var categories = require('../categories'), var categories = require('../categories'),
meta = require('./../meta'),
SocketCategories = {}; SocketCategories = {};
@ -15,8 +16,10 @@ SocketCategories.loadMore = function(socket, data, callback) {
return callback(new Error('invalid data')); return callback(new Error('invalid data'));
} }
var topicsPerPage = parseInt(meta.config.topicsPerPage, 10) || 20;
var start = data.after, var start = data.after,
end = start + 9; end = start + topicsPerPage - 1;
categories.getCategoryTopics(data.cid, start, end, socket.uid, function(err, topics) { categories.getCategoryTopics(data.cid, start, end, socket.uid, function(err, topics) {
callback(err, { 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; module.exports = SocketCategories;

@ -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) { Topics.getPageCount = function(tid, callback) {
db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) { db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {
if(err) { if(err) {
@ -782,16 +795,7 @@ var async = require('async'),
} }
function getPages(next) { function getPages(next) {
Topics.getPageCount(tid, function(err, pageCount) { Topics.getPages(tid, next);
if(err) {
return next(err);
}
var pages = [];
for(var i=1; i<=pageCount; ++i) {
pages.push({pageNumber: i});
}
next(null, pages);
});
} }
async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPages], function(err, results) { async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPages], function(err, results) {

Loading…
Cancel
Save