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 @@ + +