mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 00:45:47 +01:00
lots of cleanup, moved pagination to requirejs module
This commit is contained in:
@@ -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,6 +39,11 @@ define(['composer'], function(composer) {
|
||||
|
||||
socket.emit('categories.getRecentReplies', cid, renderRecentReplies);
|
||||
|
||||
enableInfiniteLoading();
|
||||
};
|
||||
|
||||
function enableInfiniteLoading() {
|
||||
if(!config.usePagination) {
|
||||
$(window).off('scroll').on('scroll', function (ev) {
|
||||
var bottom = ($(document).height() - $(window).height()) * 0.9;
|
||||
|
||||
@@ -49,7 +51,24 @@ define(['composer'], function(composer) {
|
||||
Category.loadMoreTopics(cid);
|
||||
}
|
||||
});
|
||||
};
|
||||
} else {
|
||||
pagination.init(templates.get('currentPage'), templates.get('pageCount'), loadPage);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if(config.usePagination) {
|
||||
container.empty().append(html);
|
||||
} else {
|
||||
container.append(html);
|
||||
}
|
||||
|
||||
$('#topics-container span.timeago').timeago();
|
||||
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||
|
||||
79
public/src/forum/pagination.js
Normal file
79
public/src/forum/pagination.js
Normal file
@@ -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 = {},
|
||||
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,6 +1020,7 @@ define(['composer'], function(composer) {
|
||||
var progressBarContainer = $('.progress-container');
|
||||
var tid = templates.get('topic_id');
|
||||
|
||||
if(pagination.parentNode)
|
||||
pagination.parentNode.style.display = 'block';
|
||||
progressBarContainer.css('display', '');
|
||||
|
||||
|
||||
@@ -84,6 +84,17 @@
|
||||
</li>
|
||||
<!-- END topics -->
|
||||
</ul>
|
||||
<!-- IF usePagination -->
|
||||
<div class="text-center">
|
||||
<ul class="pagination">
|
||||
<li class="previous pull-left"><a href="#">← 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 →</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<!-- ENDIF usePagination -->
|
||||
</div>
|
||||
<div class="col-md-3 col-xs-12 {show_sidebar} category-sidebar">
|
||||
<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_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) {
|
||||
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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user