From ff2bad635c77ea09794a386c1892559f73ef86d0 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 17 May 2014 17:55:51 -0400 Subject: [PATCH 1/2] infinite scroll refactor --- public/src/app.js | 18 ---- public/src/forum/account/favourites.js | 55 +++++------- public/src/forum/account/posts.js | 48 ++++------- public/src/forum/account/topics.js | 44 ++++------ public/src/forum/category.js | 94 +++++++------------- public/src/forum/infinitescroll.js | 79 +++++++++++++++++ public/src/forum/popular.js | 28 +++--- public/src/forum/recent.js | 47 ++++------ public/src/forum/topic.js | 115 +++++++++---------------- public/src/forum/unread.js | 23 ++--- public/src/translator.js | 8 +- 11 files changed, 237 insertions(+), 322 deletions(-) create mode 100644 public/src/forum/infinitescroll.js diff --git a/public/src/app.js b/public/src/app.js index 9ae50d4d87..f0638e8a6a 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -352,24 +352,6 @@ var socket, }); }; - var previousScrollTop = 0; - - app.enableInfiniteLoading = function(callback) { - $(window).on('scroll', function() { - - var top = $(window).height() * 0.1; - var bottom = ($(document).height() - $(window).height()) * 0.9; - var currentScrollTop = $(window).scrollTop(); - - if(currentScrollTop < top && currentScrollTop < previousScrollTop) { - callback(-1); - } else if (currentScrollTop > bottom && currentScrollTop > previousScrollTop) { - callback(1); - } - previousScrollTop = currentScrollTop; - }); - }; - var titleObj = { active: false, interval: undefined, diff --git a/public/src/forum/account/favourites.js b/public/src/forum/account/favourites.js index 487d0558ad..5b36c58831 100644 --- a/public/src/forum/account/favourites.js +++ b/public/src/forum/account/favourites.js @@ -1,51 +1,40 @@ -define(['forum/account/header'], function(header) { - var Favourites = {}, - loadingMore = false; +'use strict'; + +/* globals define, app, utils */ + +define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) { + var Favourites = {}; Favourites.init = function() { header.init(); $('.user-favourite-posts img').addClass('img-responsive'); - app.enableInfiniteLoading(function() { - if(!loadingMore) { - loadMore(); - } - }); + infinitescroll.init(loadMore); }; - function loadMore() { - loadingMore = true; - socket.emit('posts.loadMoreFavourites', { - after: $('.user-favourite-posts').attr('data-nextstart') - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } + function loadMore(direction) { + if (direction < 0) { + return; + } + infinitescroll.loadMore('posts.loadMoreFavourites', { + after: $('.user-favourite-posts').attr('data-nextstart') + }, function(data) { if (data.posts && data.posts.length) { - onTopicsLoaded(data.posts); + onPostsLoaded(data.posts); $('.user-favourite-posts').attr('data-nextstart', data.nextStart); } - - loadingMore = false; }); } - function onTopicsLoaded(posts) { - ajaxify.loadTemplate('account/favourites', function(favouritesTemplate) { - var html = templates.parse(templates.getBlock(favouritesTemplate, 'posts'), {posts: posts}); - - translator.translate(html, function(translatedHTML) { - $('#category-no-topics').remove(); - - html = $(translatedHTML); - html.find('img').addClass('img-responsive'); - $('.user-favourite-posts').append(html); - html.find('span.timeago').timeago(); - app.createUserTooltips(); - utils.makeNumbersHumanReadable(html.find('.human-readable-number')); - }); + function onPostsLoaded(posts) { + infinitescroll.parseAndTranslate('account/favourites', 'posts', {posts: posts}, function(html) { + $('.user-favourite-posts').append(html); + html.find('img').addClass('img-responsive'); + html.find('span.timeago').timeago(); + app.createUserTooltips(); + utils.makeNumbersHumanReadable(html.find('.human-readable-number')); }); } diff --git a/public/src/forum/account/posts.js b/public/src/forum/account/posts.js index f3a9a0b0a8..8a72f8b9cf 100644 --- a/public/src/forum/account/posts.js +++ b/public/src/forum/account/posts.js @@ -1,55 +1,41 @@ 'use strict'; -/* globals define, app, socket, ajaxify, templates, translator, utils */ +/* globals define, app, socket, utils */ -define(['forum/account/header'], function(header) { - var AccountPosts = {}, - loadingMore = false; +define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) { + var AccountPosts = {}; AccountPosts.init = function() { header.init(); $('.user-favourite-posts img').addClass('img-responsive'); - app.enableInfiniteLoading(function() { - if(!loadingMore) { - loadMore(); - } - }); + infinitescroll.init(loadMore); }; - function loadMore() { - loadingMore = true; - socket.emit('posts.loadMoreUserPosts', { + function loadMore(direction) { + if (direction < 0) { + return; + } + + infinitescroll.loadMore('posts.loadMoreUserPosts', { uid: $('.account-username-box').attr('data-uid'), after: $('.user-favourite-posts').attr('data-nextstart') - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } - + }, function(data) { if (data.posts && data.posts.length) { onPostsLoaded(data.posts); $('.user-favourite-posts').attr('data-nextstart', data.nextStart); } - - loadingMore = false; }); } function onPostsLoaded(posts) { - ajaxify.loadTemplate('account/posts', function(accountposts) { - var html = templates.parse(templates.getBlock(accountposts, 'posts'), {posts: posts}); - - translator.translate(html, function(translatedHTML) { - - html = $(translatedHTML); - html.find('img').addClass('img-responsive'); - $('.user-favourite-posts').append(html); - html.find('span.timeago').timeago(); - app.createUserTooltips(); - utils.makeNumbersHumanReadable(html.find('.human-readable-number')); - }); + infinitescroll.parseAndTranslate('account/posts', 'posts', {posts: posts}, function(html) { + $('.user-favourite-posts').append(html); + html.find('img').addClass('img-responsive'); + html.find('span.timeago').timeago(); + app.createUserTooltips(); + utils.makeNumbersHumanReadable(html.find('.human-readable-number')); }); } diff --git a/public/src/forum/account/topics.js b/public/src/forum/account/topics.js index 6416412c64..e9adcb3acc 100644 --- a/public/src/forum/account/topics.js +++ b/public/src/forum/account/topics.js @@ -1,51 +1,39 @@ 'use strict'; -/* globals define, app, socket, ajaxify, templates, translator, utils */ +/* globals define, app, socket, utils */ -define(['forum/account/header'], function(header) { - var AccountTopics = {}, - loadingMore = false; +define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) { + var AccountTopics = {}; AccountTopics.init = function() { header.init(); - app.enableInfiniteLoading(function() { - if(!loadingMore) { - loadMore(); - } - }); + infinitescroll.init(loadMore); }; - function loadMore() { - loadingMore = true; - socket.emit('topics.loadMoreFromSet', { + function loadMore(direction) { + if (direction < 0) { + return; + } + + infinitescroll.loadMore('topics.loadMoreFromSet', { set: 'uid:' + $('.account-username-box').attr('data-uid') + ':topics', after: $('.user-topics').attr('data-nextstart') - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } + }, function(data) { if (data.topics && data.topics.length) { onTopicsLoaded(data.topics); $('.user-topics').attr('data-nextstart', data.nextStart); } - - loadingMore = false; }); } function onTopicsLoaded(topics) { - ajaxify.loadTemplate('account/topics', function(accounttopics) { - var html = templates.parse(templates.getBlock(accounttopics, 'topics'), {topics: topics}); - - translator.translate(html, function(translatedHTML) { - html = $(translatedHTML); - $('#topics-container').append(html); - html.find('span.timeago').timeago(); - app.createUserTooltips(); - utils.makeNumbersHumanReadable(html.find('.human-readable-number')); - }); + infinitescroll.parseAndTranslate('account/topics', 'topics', {topics: topics}, function(html) { + $('#topics-container').append(html); + html.find('span.timeago').timeago(); + app.createUserTooltips(); + utils.makeNumbersHumanReadable(html.find('.human-readable-number')); }); } diff --git a/public/src/forum/category.js b/public/src/forum/category.js index 5b3358659a..76ebaea7f5 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -1,10 +1,8 @@ "use strict"; /* global define, config, templates, app, utils, ajaxify, socket, translator */ -define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryTools'], function(composer, pagination, share, navigator, categoryTools) { - var Category = {}, - loadingMoreTopics = false; - +define(['composer', 'forum/pagination', 'forum/infinitescroll', 'share', 'navigator', 'forum/categoryTools'], function(composer, pagination, infinitescroll, share, navigator, categoryTools) { + var Category = {}; $(window).on('action:ajaxify.start', function(ev, data) { if(data && data.url.indexOf('category') !== 0) { @@ -34,7 +32,7 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo categoryTools.init(cid); - enableInfiniteLoading(); + enableInfiniteLoadingOrPagination(); if (!config.usePagination) { navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count')); @@ -87,9 +85,8 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo } $('#topics-container').empty(); - loadingMoreTopics = false; - Category.loadMoreTopics(ajaxify.variables.get('category_id'), index, function() { + loadTopicsAfter(index, function() { Category.scrollToTopic(bookmark, clicked, 0); }); }); @@ -132,41 +129,11 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo } }; - function enableInfiniteLoading() { - if(!config.usePagination) { - - app.enableInfiniteLoading(function(direction) { - - if(!loadingMoreTopics && $('#topics-container').children().length) { - - var after = 0, - offset = 0, - el = null; - - if(direction > 0) { - el = $('#topics-container .category-item[data-tid]').last(); - after = parseInt(el.attr('data-index'), 10) + 1; - } else { - el = $('#topics-container .category-item[data-tid]').first(); - after = parseInt(el.attr('data-index'), 10); - if(isNaN(after)){ - after = 0; - } - after -= config.topicsPerPage; - if(after < 0) { - after = 0; - } - offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height(); - } - - Category.loadMoreTopics(ajaxify.variables.get('category_id'), after, function() { - if(direction < 0 && el) { - Category.scrollToTopic(el.attr('data-tid'), null, 0, offset); - } - }); - } - }); + function enableInfiniteLoadingOrPagination() { + if (!config.usePagination) { + infinitescroll.init(Category.loadMoreTopics); } else { + navigator.hide(); pagination.init(ajaxify.variables.get('currentPage'), ajaxify.variables.get('pageCount')); } } @@ -293,53 +260,50 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo } } + if (typeof callback === 'function') { + callback(); + } html.find('span.timeago').timeago(); app.createUserTooltips(); utils.makeNumbersHumanReadable(html.find('.human-readable-number')); - - if (typeof callback === 'function') { - callback(topics); - } }); }); }; - Category.loadMoreTopics = function(cid, after, callback) { - if (loadingMoreTopics || !$('#topics-container').length) { + Category.loadMoreTopics = function(direction) { + if (!$('#topics-container').length || !$('#topics-container').children().length) { return; } + infinitescroll.calculateAfter(direction, '#topics-container .category-item[data-tid]', config.topicsPerPage, function(after, offset, el) { + + loadTopicsAfter(after, function() { + if (direction < 0 && el) { + Category.scrollToTopic(el.attr('data-tid'), null, 0, offset); + } + }); + }); + }; + + function loadTopicsAfter(after, callback) { if(!utils.isNumber(after) || (after === 0 && $('#topics-container li.category-item[data-index="0"]').length)) { return; } $(window).trigger('action:categories.loading'); - loadingMoreTopics = true; - - socket.emit('categories.loadMore', { - cid: cid, + infinitescroll.loadMore('categories.loadMore', { + cid: ajaxify.variables.get('category_id'), after: after - }, function (err, data) { - loadingMoreTopics = false; + }, function (data) { - if(err) { - return app.alertError(err.message); - } - - if (data && data.topics.length) { + if (data.topics && data.topics.length) { Category.onTopicsLoaded(data, callback); $('#topics-container').attr('data-nextstart', data.nextStart); - } else { - - if (typeof callback === 'function') { - callback(data.topics); - } } - $(window).trigger('action:categories.loaded'); }); - }; + } return Category; }); diff --git a/public/src/forum/infinitescroll.js b/public/src/forum/infinitescroll.js new file mode 100644 index 0000000000..481b7c33c8 --- /dev/null +++ b/public/src/forum/infinitescroll.js @@ -0,0 +1,79 @@ +'use strict'; + +/* globals define, socket, ajaxify, translator, templates, app */ + +define(function() { + + var scroll = {}; + var callback; + var previousScrollTop = 0; + var loadingMore = false; + + scroll.init = function(cb) { + callback = cb; + $(window).off('scroll', onScroll).on('scroll', onScroll); + }; + + function onScroll() { + var top = $(window).height() * 0.1; + var bottom = ($(document).height() - $(window).height()) * 0.9; + var currentScrollTop = $(window).scrollTop(); + + if(currentScrollTop < top && currentScrollTop < previousScrollTop) { + callback(-1); + } else if (currentScrollTop > bottom && currentScrollTop > previousScrollTop) { + callback(1); + } + previousScrollTop = currentScrollTop; + } + + scroll.loadMore = function(method, data, callback) { + if (loadingMore) { + return; + } + loadingMore = true; + socket.emit(method, data, function(err, data) { + if (err) { + return app.alertError(err.message); + } + callback(data); + loadingMore = false; + }); + }; + + scroll.parseAndTranslate = function(template, blockName, data, callback) { + ajaxify.loadTemplate(template, function(templateHtml) { + var html = templates.parse(templates.getBlock(templateHtml, blockName), data); + + translator.translate(html, function(translatedHTML) { + callback($(translatedHTML)); + }); + }); + }; + + scroll.calculateAfter = function(direction, selector, count, callback) { + var after = 0, + offset = 0, + el = direction > 0 ? $(selector).last() : $(selector).first(); + + if (direction > 0) { + after = parseInt(el.attr('data-index'), 10) + 1; + } else { + after = parseInt(el.attr('data-index'), 10); + if (isNaN(after)) { + after = 0; + } + after -= count; + if (after < 0) { + after = 0; + } + if (el && el.offset()) { + offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height(); + } + } + + callback(after, offset, el); + }; + + return scroll; +}); \ No newline at end of file diff --git a/public/src/forum/popular.js b/public/src/forum/popular.js index fe0b9519fd..87f12ddb8d 100644 --- a/public/src/forum/popular.js +++ b/public/src/forum/popular.js @@ -1,6 +1,9 @@ -define(['forum/recent'], function(recent) { +'use strict'; + +/* globals define, app, socket*/ + +define(['forum/recent', 'forum/infinitescroll'], function(recent, infinitescroll) { var Popular = {}, - loadingMoreTopics = false, active = ''; $(window).on('action:ajaxify.start', function(ev, data) { @@ -20,34 +23,23 @@ define(['forum/recent'], function(recent) { active = recent.selectActivePill(); - app.enableInfiniteLoading(function() { - if(!loadingMoreTopics) { - loadMoreTopics(); - } - }); + infinitescroll.init(loadMoreTopics); - function loadMoreTopics() { - if(!$('#topics-container').length) { + function loadMoreTopics(direction) { + if(direction < 0 || !$('#topics-container').length) { return; } - loadingMoreTopics = true; - socket.emit('topics.loadMoreFromSet', { + infinitescroll.loadMore('topics.loadMoreFromSet', { set: 'topics:' + $('.nav-pills .active a').html().toLowerCase(), after: $('#topics-container').attr('data-nextstart') - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } - + }, function(data) { if (data.topics && data.topics.length) { recent.onTopicsLoaded('popular', data.topics, false); $('#topics-container').attr('data-nextstart', data.nextStart); } else { $('#load-more-btn').hide(); } - - loadingMoreTopics = false; }); } }; diff --git a/public/src/forum/recent.js b/public/src/forum/recent.js index 84f3f26628..660d85777c 100644 --- a/public/src/forum/recent.js +++ b/public/src/forum/recent.js @@ -1,13 +1,12 @@ 'use strict'; -/* globals define, app, socket, ajaxify, templates, translator, utils */ +/* globals define, app, socket, utils */ -define(function() { +define(['forum/infinitescroll'], function(infinitescroll) { var Recent = {}; var newTopicCount = 0, - newPostCount = 0, - loadingMoreTopics = false; + newPostCount = 0; var active = ''; @@ -36,11 +35,7 @@ define(function() { }); - app.enableInfiniteLoading(function() { - if(!loadingMoreTopics) { - Recent.loadMoreTopics(); - } - }); + infinitescroll.init(Recent.loadMoreTopics); }; Recent.selectActivePill = function() { @@ -102,42 +97,30 @@ define(function() { $('#category-no-topics').addClass('hide'); }; - Recent.loadMoreTopics = function() { - if(!$('#topics-container').length) { + Recent.loadMoreTopics = function(direction) { + if(direction < 0 || !$('#topics-container').length) { return; } - loadingMoreTopics = true; - socket.emit('topics.loadMoreRecentTopics', { + infinitescroll.loadMore('topics.loadMoreRecentTopics', { after: $('#topics-container').attr('data-nextstart'), term: active - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } - + }, function(data) { if (data.topics && data.topics.length) { Recent.onTopicsLoaded('recent', data.topics, false); $('#topics-container').attr('data-nextstart', data.nextStart); } - - loadingMoreTopics = false; }); }; Recent.onTopicsLoaded = function(templateName, topics, showSelect) { - ajaxify.loadTemplate(templateName, function(template) { - var html = templates.parse(templates.getBlock(template, 'topics'), {topics: topics, showSelect: showSelect}); - - translator.translate(html, function(translatedHTML) { - $('#category-no-topics').remove(); - - html = $(translatedHTML); - $('#topics-container').append(html); - html.find('span.timeago').timeago(); - app.createUserTooltips(); - utils.makeNumbersHumanReadable(html.find('.human-readable-number')); - }); + infinitescroll.parseAndTranslate(templateName, 'topics', {topics: topics, showSelect: showSelect}, function(html) { + $('#category-no-topics').remove(); + + $('#topics-container').append(html); + html.find('span.timeago').timeago(); + app.createUserTooltips(); + utils.makeNumbersHumanReadable(html.find('.human-readable-number')); }); }; diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 3d4775b774..86e0b4c8ac 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -3,9 +3,8 @@ /* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH, utils */ -define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', 'forum/topic/events', 'navigator'], function(pagination, threadTools, postTools, events, navigator) { +define(['forum/pagination', 'forum/infinitescroll', 'forum/topic/threadTools', 'forum/topic/postTools', 'forum/topic/events', 'navigator'], function(pagination, infinitescroll, threadTools, postTools, events, navigator) { var Topic = {}, - infiniteLoaderActive = false, scrollingToPost = false, currentUrl = ''; @@ -43,7 +42,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', hidePostToolsForDeletedPosts(); - enableInfiniteLoading(); + enableInfiniteLoadingOrPagination(); addBlockquoteEllipses($('.topic .post-content > blockquote')); @@ -114,36 +113,9 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', }); } - function enableInfiniteLoading() { + function enableInfiniteLoadingOrPagination() { if(!config.usePagination) { - - app.enableInfiniteLoading(function(direction) { - - if (!infiniteLoaderActive && $('#post-container').children().length) { - var after = 0; - var el = null; - if(direction > 0) { - el = $('#post-container .post-row').last(); - after = parseInt(el.attr('data-index'), 10) + 1; - } else { - el = $('#post-container .post-row').first(); - after = parseInt(el.attr('data-index'), 10); - after -= config.postsPerPage; - if(after < 0) { - after = 0; - } - } - - var offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height(); - - loadMorePosts(ajaxify.variables.get('topic_id'), after, function() { - hidePostToolsForDeletedPosts(); - if(direction < 0 && el) { - Topic.scrollToPost(el.attr('data-pid'), false, 0, offset); - } - }); - } - }); + infinitescroll.init(loadMorePosts); } else { navigator.hide(); @@ -224,14 +196,14 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', if(err) { return; } - var tid = $('#post-container').attr('data-tid'); + $('#post-container').empty(); var after = index - config.postsPerPage + 1; if(after < 0) { after = 0; } - loadMorePosts(tid, after, function() { + loadPostsAfter(after, function() { scrollToPid(pid); }); }); @@ -242,8 +214,8 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', tid = $('#post-container').attr('data-tid'); function animateScroll() { - $("html, body").animate({ - scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + "px" + $('html, body').animate({ + scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px' }, duration !== undefined ? duration : 400, function() { scrollingToPost = false; navigator.update(); @@ -316,7 +288,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', } }); - if(!after) { + if (!after) { var firstPost = $('#post-container .post-row').first(); if(firstPid < parseInt(firstPost.attr('data-pid'), 10)) { before = firstPost; @@ -334,35 +306,26 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', data.title = ajaxify.variables.get('topic_name'); data.viewcount = ajaxify.variables.get('viewcount'); - parseAndTranslatePosts(data, function(translatedHTML) { - var translated = $(translatedHTML); - + infinitescroll.parseAndTranslate('topic', 'posts', data, function(html) { if(after) { - translated.insertAfter(after); + html.insertAfter(after); } else if(before) { - translated.insertBefore(before); + html.insertBefore(before); } else { - $('#post-container').append(translated); + $('#post-container').append(html); } - translated.hide().fadeIn('slow'); - addBlockquoteEllipses(translated.find('.post-content > blockquote')); + html.hide().fadeIn('slow'); - onNewPostsLoaded(translated, data.posts); + addBlockquoteEllipses(html.find('.post-content > blockquote')); - if(typeof callback === 'function') { + onNewPostsLoaded(html, data.posts); + if (typeof callback === 'function') { callback(); } }); } - function parseAndTranslatePosts(data, callback) { - ajaxify.loadTemplate('topic', function(topicTemplate) { - var html = templates.parse(templates.getBlock(topicTemplate, 'posts'), data); - translator.translate(html, callback); - }); - } - function onNewPostsLoaded(html, posts) { function getPostPrivileges(pid) { socket.emit('posts.getPrivileges', pid, function(err, privileges) { @@ -377,8 +340,6 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', getPostPrivileges(posts[x].pid); } - infiniteLoaderActive = false; - app.populateOnlineUsers(); app.createUserTooltips(); utils.addCommasToNumbers(html.find('.formatted-number')); @@ -392,45 +353,47 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', function toggleModTools(postHtml, privileges) { postHtml.find('.edit, .delete').toggleClass('none', !privileges.meta.editable); postHtml.find('.move').toggleClass('none', !privileges.meta.move); - postHtml.find('.reply, .quote').toggleClass('none', !$('.post_reply').length) + postHtml.find('.reply, .quote').toggleClass('none', !$('.post_reply').length); var isSelfPost = parseInt(postHtml.attr('data-uid'), 10) === parseInt(app.uid, 10); postHtml.find('.chat, .flag').toggleClass('none', isSelfPost); } - function loadMorePosts(tid, after, callback) { - var indicatorEl = $('.loading-indicator'); - - if (infiniteLoaderActive || !$('#post-container').length) { + function loadMorePosts(direction) { + if (!$('#post-container').length) { return; } + infinitescroll.calculateAfter(direction, '#post-container .post-row', config.postsPerPage, function(after, offset, el) { + loadPostsAfter(after, function() { + if (direction < 0 && el) { + Topic.scrollToPost(el.attr('data-pid'), false, 0, offset); + } + }); + }); + } + + function loadPostsAfter(after, callback) { if (!utils.isNumber(after) || (after === 0 && $('#post-container li.post-row[data-index="0"]').length)) { return; } - infiniteLoaderActive = true; - indicatorEl.fadeIn(); + var indicatorEl = $('.loading-indicator'); + if (!indicatorEl.is(':animated')) { + indicatorEl.fadeIn(); + } - socket.emit('topics.loadMore', { - tid: tid, + infinitescroll.loadMore('topics.loadMore', { + tid: ajaxify.variables.get('topic_id'), after: after - }, function (err, data) { - - indicatorEl.fadeOut(function() { - infiniteLoaderActive = false; - }); + }, function (data) { - if(err) { - return app.alertError(err.message); - } + indicatorEl.fadeOut(); if (data && data.posts && data.posts.length) { createNewPosts(data, callback); + hidePostToolsForDeletedPosts(); } else { navigator.update(); - if (typeof callback === 'function') { - callback(data.posts); - } } }); } diff --git a/public/src/forum/unread.js b/public/src/forum/unread.js index a576a6ba23..169acf28f7 100644 --- a/public/src/forum/unread.js +++ b/public/src/forum/unread.js @@ -2,7 +2,7 @@ /* globals define, app, socket */ -define(['forum/recent', 'topicSelect'], function(recent, topicSelect) { +define(['forum/recent', 'topicSelect', 'forum/infinitescroll'], function(recent, topicSelect, infinitescroll) { var Unread = {}, loadingMoreTopics = false; @@ -86,33 +86,22 @@ define(['forum/recent', 'topicSelect'], function(recent, topicSelect) { loadMoreTopics(); }); - app.enableInfiniteLoading(function() { - if(!loadingMoreTopics) { - loadMoreTopics(); - } - }); + infinitescroll.init(loadMoreTopics); - function loadMoreTopics() { - if(!$('#topics-container').length) { + function loadMoreTopics(direction) { + if(direction < 0 || !$('#topics-container').length) { return; } - loadingMoreTopics = true; - socket.emit('topics.loadMoreUnreadTopics', { + infinitescroll.loadMore('topics.loadMoreUnreadTopics', { after: $('#topics-container').attr('data-nextstart') - }, function(err, data) { - if(err) { - return app.alertError(err.message); - } - + }, function(data) { if (data.topics && data.topics.length) { recent.onTopicsLoaded('unread', data.topics, true); $('#topics-container').attr('data-nextstart', data.nextStart); } else { $('#load-more-btn').hide(); } - - loadingMoreTopics = false; }); } }; diff --git a/public/src/translator.js b/public/src/translator.js index 59ba32cb9b..fcdecef522 100644 --- a/public/src/translator.js +++ b/public/src/translator.js @@ -67,10 +67,6 @@ }; translator.translate = function (data, language, callback) { - if (!data) { - return callback(data); - } - if (typeof language === 'function') { callback = language; if ('undefined' !== typeof window && config) { @@ -81,6 +77,10 @@ } } + if (!data) { + return callback(data); + } + function insertLanguage(text, key, value, variables) { if (value) { for (var i = 1, ii = variables.length; i < ii; i++) { From 472e6b5206b3f9e6c66a4e67ed3d3fafa59cbacd Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 17 May 2014 18:59:34 -0400 Subject: [PATCH 2/2] privilege cleanup --- src/posts.js | 4 ++-- src/privileges/categories.js | 14 ------------ src/privileges/helpers.js | 35 +++++++++++++++------------- src/privileges/posts.js | 4 ++-- src/privileges/topics.js | 44 ------------------------------------ src/socket.io/categories.js | 2 +- src/socket.io/posts.js | 2 +- src/topics.js | 2 +- src/topics/create.js | 4 ++-- src/topics/unread.js | 2 +- 10 files changed, 29 insertions(+), 84 deletions(-) diff --git a/src/posts.js b/src/posts.js index 6581882f97..f8bfc22828 100644 --- a/src/posts.js +++ b/src/posts.js @@ -164,7 +164,7 @@ var db = require('./database'), } async.filter(pids, function(pid, next) { - privileges.posts.canRead(pid, callerUid, function(err, canRead) { + privileges.posts.can('read', pid, callerUid, function(err, canRead) { next(!err && canRead); }); }, function(pids) { @@ -216,7 +216,7 @@ var db = require('./database'), } async.filter(pids, function(pid, next) { - privileges.posts.canRead(pid, uid, function(err, canRead) { + privileges.posts.can('read', pid, uid, function(err, canRead) { next(!err && canRead); }); }, function(pids) { diff --git a/src/privileges/categories.js b/src/privileges/categories.js index 8f0d25de5d..8de9702958 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -58,20 +58,6 @@ module.exports = function(privileges) { ], callback); }; - privileges.categories.canRead = function(cid, uid, callback) { - helpers.some([ - function(next) { - helpers.allowedTo('read', uid, cid, next); - }, - function(next) { - user.isModerator(uid, cid, next); - }, - function(next) { - user.isAdministrator(uid, next); - } - ], callback); - }; - privileges.categories.canMoveAllTopics = function(currentCid, targetCid, uid, callback) { async.parallel({ isAdministrator: function(next) { diff --git a/src/privileges/helpers.js b/src/privileges/helpers.js index 9c4ec535a0..642cf36b26 100644 --- a/src/privileges/helpers.js +++ b/src/privileges/helpers.js @@ -30,22 +30,8 @@ helpers.allowedTo = function(privilege, uid, cid, callback) { return callback(null, false); } - // Guests handling if (parseInt(uid, 10) === 0) { - return async.parallel([ - function(next) { - groups.exists('cid:' + cid + ':privileges:' + privilege, function(err, exists) { - next(err, !err ? !exists : false); - }); - }, - function(next) { - helpers.isMember(groups.isMember, 'cid:' + cid + ':privileges:groups:' + privilege, 'guests', function(err, isMember) { - next(err, privilege !== 'find' && privilege !== 'read' ? isMember === true : isMember !== false); - }); - } - ], function(err, results) { - callback(err, results[0] && (results[1] || results[1] === null)); - }); + return isGuestAllowedTo(privilege, cid, callback); } async.parallel({ @@ -65,6 +51,23 @@ helpers.allowedTo = function(privilege, uid, cid, callback) { }); }; +function isGuestAllowedTo(privilege, cid, callback) { + async.parallel([ + function(next) { + groups.exists('cid:' + cid + ':privileges:' + privilege, function(err, exists) { + next(err, !err ? !exists : false); + }); + }, + function(next) { + helpers.isMember(groups.isMember, 'cid:' + cid + ':privileges:groups:' + privilege, 'guests', function(err, isMember) { + next(err, privilege !== 'find' && privilege !== 'read' ? isMember === true : isMember !== false); + }); + } + ], function(err, results) { + callback(err, results[0] && (results[1] || results[1] === null)); + }); +} + helpers.isMember = function(method, group, uid, callback) { groups.exists(group, function(err, exists) { if (err) { @@ -77,7 +80,7 @@ helpers.isMember = function(method, group, uid, callback) { method(uid, group, callback); }); -} +}; helpers.hasEnoughReputationFor = function(privilege, uid, callback) { if (parseInt(meta.config['privileges:disabled'], 10)) { diff --git a/src/privileges/posts.js b/src/privileges/posts.js index d3501f218c..bd580d0144 100644 --- a/src/privileges/posts.js +++ b/src/privileges/posts.js @@ -53,13 +53,13 @@ module.exports = function(privileges) { }); }; - privileges.posts.canRead = function(pid, uid, callback) { + privileges.posts.can = function(privilege, pid, uid, callback) { posts.getCidByPid(pid, function(err, cid) { if (err) { return callback(err); } - privileges.categories.canRead(cid, uid, callback); + privileges.categories.can(privilege, cid, uid, callback); }); }; diff --git a/src/privileges/topics.js b/src/privileges/topics.js index 621baf45ca..8b36bd6c49 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -65,50 +65,6 @@ module.exports = function(privileges) { }); }; - privileges.topics.canRead = function(tid, uid, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { - if (err) { - return callback(err); - } - - privileges.categories.canRead(cid, uid, callback); - }); - }; - - privileges.topics.canCreate = function(cid, uid, callback) { - helpers.some([ - function(next) { - helpers.allowedTo('topics:create', uid, cid, next); - }, - function(next) { - user.isModerator(uid, cid, next); - }, - function(next) { - user.isAdministrator(uid, next); - } - ], callback); - }; - - privileges.topics.canReply = function(tid, uid, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { - if (err) { - return callback(err); - } - - helpers.some([ - function(next) { - helpers.allowedTo('topics:reply', uid, cid, next); - }, - function(next) { - user.isModerator(uid, cid, next); - }, - function(next) { - user.isAdministrator(uid, next); - } - ], callback); - }); - }; - privileges.topics.canEdit = function(tid, uid, callback) { helpers.some([ function(next) { diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 117640793d..e6d9ca52a6 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -9,7 +9,7 @@ var async = require('async'), SocketCategories = {}; SocketCategories.getRecentReplies = function(socket, cid, callback) { - privileges.categories.canRead(cid, socket.uid, function(err, canRead) { + privileges.categories.can('read', cid, socket.uid, function(err, canRead) { if (err) { return callback(err); } diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index f2c77678b2..cd272c758a 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -129,7 +129,7 @@ function sendNotificationToPostOwner(data, uid, notification) { SocketPosts.getRawPost = function(socket, pid, callback) { async.waterfall([ function(next) { - privileges.posts.canRead(pid, socket.uid, next); + privileges.posts.can('read', pid, socket.uid, next); }, function(canRead, next) { if (!canRead) { diff --git a/src/topics.js b/src/topics.js index 0763a13a0c..45d71dfd5c 100644 --- a/src/topics.js +++ b/src/topics.js @@ -130,7 +130,7 @@ var async = require('async'), } async.filter(tids, function(tid, next) { - privileges.topics.canRead(tid, uid, function(err, canRead) { + privileges.topics.can('read', tid, uid, function(err, canRead) { next(!err && canRead); }); }, function(tids) { diff --git a/src/topics/create.js b/src/topics/create.js index e47216de08..e23c7c18b3 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -99,7 +99,7 @@ module.exports = function(Topics) { if (!categoryExists) { return next(new Error('[[error:no-category]]')); } - privileges.topics.canCreate(cid, uid, next); + privileges.categories.can('topics:create', cid, uid, next); }, function(canCreate, next) { if(!canCreate) { @@ -173,7 +173,7 @@ module.exports = function(Topics) { return next(new Error('[[error:topic-locked]]')); } - privileges.topics.canReply(tid, uid, next); + privileges.topics.can('topics:reply', tid, uid, next); }, function(canReply, next) { if (!canReply) { diff --git a/src/topics/unread.js b/src/topics/unread.js index 4f153cf227..7000dfc1a2 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -49,7 +49,7 @@ module.exports = function(Topics) { }); async.filter(newtids, function(tid, next) { - privileges.topics.canRead(tid, uid, function(err, canRead) { + privileges.topics.can('read', tid, uid, function(err, canRead) { next(!err && canRead); }); }, function(newtids) {