Merge branch 'master' of github.com:designcreateplay/NodeBB

v1.18.x
Julian Lam 11 years ago
commit 58b6a2e9ea

@ -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 = { var titleObj = {
active: false, active: false,
interval: undefined, interval: undefined,

@ -1,52 +1,41 @@
define(['forum/account/header'], function(header) { 'use strict';
var Favourites = {},
loadingMore = false; /* globals define, app, utils */
define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) {
var Favourites = {};
Favourites.init = function() { Favourites.init = function() {
header.init(); header.init();
$('.user-favourite-posts img').addClass('img-responsive'); $('.user-favourite-posts img').addClass('img-responsive');
app.enableInfiniteLoading(function() { infinitescroll.init(loadMore);
if(!loadingMore) {
loadMore();
}
});
}; };
function loadMore() { function loadMore(direction) {
loadingMore = true; if (direction < 0) {
socket.emit('posts.loadMoreFavourites', { return;
after: $('.user-favourite-posts').attr('data-nextstart')
}, function(err, data) {
if(err) {
return app.alertError(err.message);
} }
infinitescroll.loadMore('posts.loadMoreFavourites', {
after: $('.user-favourite-posts').attr('data-nextstart')
}, function(data) {
if (data.posts && data.posts.length) { if (data.posts && data.posts.length) {
onTopicsLoaded(data.posts); onPostsLoaded(data.posts);
$('.user-favourite-posts').attr('data-nextstart', data.nextStart); $('.user-favourite-posts').attr('data-nextstart', data.nextStart);
} }
loadingMore = false;
}); });
} }
function onTopicsLoaded(posts) { function onPostsLoaded(posts) {
ajaxify.loadTemplate('account/favourites', function(favouritesTemplate) { infinitescroll.parseAndTranslate('account/favourites', 'posts', {posts: posts}, function(html) {
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); $('.user-favourite-posts').append(html);
html.find('img').addClass('img-responsive');
html.find('span.timeago').timeago(); html.find('span.timeago').timeago();
app.createUserTooltips(); app.createUserTooltips();
utils.makeNumbersHumanReadable(html.find('.human-readable-number')); utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
}); });
});
} }
return Favourites; return Favourites;

@ -1,56 +1,42 @@
'use strict'; 'use strict';
/* globals define, app, socket, ajaxify, templates, translator, utils */ /* globals define, app, socket, utils */
define(['forum/account/header'], function(header) { define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) {
var AccountPosts = {}, var AccountPosts = {};
loadingMore = false;
AccountPosts.init = function() { AccountPosts.init = function() {
header.init(); header.init();
$('.user-favourite-posts img').addClass('img-responsive'); $('.user-favourite-posts img').addClass('img-responsive');
app.enableInfiniteLoading(function() { infinitescroll.init(loadMore);
if(!loadingMore) {
loadMore();
}
});
}; };
function loadMore() { function loadMore(direction) {
loadingMore = true; if (direction < 0) {
socket.emit('posts.loadMoreUserPosts', { return;
uid: $('.account-username-box').attr('data-uid'),
after: $('.user-favourite-posts').attr('data-nextstart')
}, function(err, data) {
if(err) {
return app.alertError(err.message);
} }
infinitescroll.loadMore('posts.loadMoreUserPosts', {
uid: $('.account-username-box').attr('data-uid'),
after: $('.user-favourite-posts').attr('data-nextstart')
}, function(data) {
if (data.posts && data.posts.length) { if (data.posts && data.posts.length) {
onPostsLoaded(data.posts); onPostsLoaded(data.posts);
$('.user-favourite-posts').attr('data-nextstart', data.nextStart); $('.user-favourite-posts').attr('data-nextstart', data.nextStart);
} }
loadingMore = false;
}); });
} }
function onPostsLoaded(posts) { function onPostsLoaded(posts) {
ajaxify.loadTemplate('account/posts', function(accountposts) { infinitescroll.parseAndTranslate('account/posts', 'posts', {posts: posts}, function(html) {
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); $('.user-favourite-posts').append(html);
html.find('img').addClass('img-responsive');
html.find('span.timeago').timeago(); html.find('span.timeago').timeago();
app.createUserTooltips(); app.createUserTooltips();
utils.makeNumbersHumanReadable(html.find('.human-readable-number')); utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
}); });
});
} }
return AccountPosts; return AccountPosts;

@ -1,52 +1,40 @@
'use strict'; 'use strict';
/* globals define, app, socket, ajaxify, templates, translator, utils */ /* globals define, app, socket, utils */
define(['forum/account/header'], function(header) { define(['forum/account/header', 'forum/infinitescroll'], function(header, infinitescroll) {
var AccountTopics = {}, var AccountTopics = {};
loadingMore = false;
AccountTopics.init = function() { AccountTopics.init = function() {
header.init(); header.init();
app.enableInfiniteLoading(function() { infinitescroll.init(loadMore);
if(!loadingMore) {
loadMore();
}
});
}; };
function loadMore() { function loadMore(direction) {
loadingMore = true; if (direction < 0) {
socket.emit('topics.loadMoreFromSet', { return;
}
infinitescroll.loadMore('topics.loadMoreFromSet', {
set: 'uid:' + $('.account-username-box').attr('data-uid') + ':topics', set: 'uid:' + $('.account-username-box').attr('data-uid') + ':topics',
after: $('.user-topics').attr('data-nextstart') after: $('.user-topics').attr('data-nextstart')
}, function(err, data) { }, function(data) {
if(err) {
return app.alertError(err.message);
}
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {
onTopicsLoaded(data.topics); onTopicsLoaded(data.topics);
$('.user-topics').attr('data-nextstart', data.nextStart); $('.user-topics').attr('data-nextstart', data.nextStart);
} }
loadingMore = false;
}); });
} }
function onTopicsLoaded(topics) { function onTopicsLoaded(topics) {
ajaxify.loadTemplate('account/topics', function(accounttopics) { infinitescroll.parseAndTranslate('account/topics', 'topics', {topics: topics}, function(html) {
var html = templates.parse(templates.getBlock(accounttopics, 'topics'), {topics: topics});
translator.translate(html, function(translatedHTML) {
html = $(translatedHTML);
$('#topics-container').append(html); $('#topics-container').append(html);
html.find('span.timeago').timeago(); html.find('span.timeago').timeago();
app.createUserTooltips(); app.createUserTooltips();
utils.makeNumbersHumanReadable(html.find('.human-readable-number')); utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
}); });
});
} }
return AccountTopics; return AccountTopics;

@ -1,10 +1,8 @@
"use strict"; "use strict";
/* global define, config, templates, app, utils, ajaxify, socket, translator */ /* global define, config, templates, app, utils, ajaxify, socket, translator */
define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryTools'], function(composer, pagination, share, navigator, categoryTools) { define(['composer', 'forum/pagination', 'forum/infinitescroll', 'share', 'navigator', 'forum/categoryTools'], function(composer, pagination, infinitescroll, share, navigator, categoryTools) {
var Category = {}, var Category = {};
loadingMoreTopics = false;
$(window).on('action:ajaxify.start', function(ev, data) { $(window).on('action:ajaxify.start', function(ev, data) {
if(data && data.url.indexOf('category') !== 0) { if(data && data.url.indexOf('category') !== 0) {
@ -34,7 +32,7 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo
categoryTools.init(cid); categoryTools.init(cid);
enableInfiniteLoading(); enableInfiniteLoadingOrPagination();
if (!config.usePagination) { if (!config.usePagination) {
navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count')); 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(); $('#topics-container').empty();
loadingMoreTopics = false;
Category.loadMoreTopics(ajaxify.variables.get('category_id'), index, function() { loadTopicsAfter(index, function() {
Category.scrollToTopic(bookmark, clicked, 0); Category.scrollToTopic(bookmark, clicked, 0);
}); });
}); });
@ -132,41 +129,11 @@ define(['composer', 'forum/pagination', 'share', 'navigator', 'forum/categoryToo
} }
}; };
function enableInfiniteLoading() { function enableInfiniteLoadingOrPagination() {
if(!config.usePagination) { if (!config.usePagination) {
infinitescroll.init(Category.loadMoreTopics);
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);
}
});
}
});
} else { } else {
navigator.hide();
pagination.init(ajaxify.variables.get('currentPage'), ajaxify.variables.get('pageCount')); 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(); html.find('span.timeago').timeago();
app.createUserTooltips(); app.createUserTooltips();
utils.makeNumbersHumanReadable(html.find('.human-readable-number')); utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
if (typeof callback === 'function') {
callback(topics);
}
}); });
}); });
}; };
Category.loadMoreTopics = function(cid, after, callback) { Category.loadMoreTopics = function(direction) {
if (loadingMoreTopics || !$('#topics-container').length) { if (!$('#topics-container').length || !$('#topics-container').children().length) {
return; 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)) { if(!utils.isNumber(after) || (after === 0 && $('#topics-container li.category-item[data-index="0"]').length)) {
return; return;
} }
$(window).trigger('action:categories.loading'); $(window).trigger('action:categories.loading');
loadingMoreTopics = true; infinitescroll.loadMore('categories.loadMore', {
cid: ajaxify.variables.get('category_id'),
socket.emit('categories.loadMore', {
cid: cid,
after: after after: after
}, function (err, data) { }, function (data) {
loadingMoreTopics = false;
if(err) {
return app.alertError(err.message);
}
if (data && data.topics.length) { if (data.topics && data.topics.length) {
Category.onTopicsLoaded(data, callback); Category.onTopicsLoaded(data, callback);
$('#topics-container').attr('data-nextstart', data.nextStart); $('#topics-container').attr('data-nextstart', data.nextStart);
} else {
if (typeof callback === 'function') {
callback(data.topics);
}
} }
$(window).trigger('action:categories.loaded'); $(window).trigger('action:categories.loaded');
}); });
}; }
return Category; return Category;
}); });

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

@ -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 = {}, var Popular = {},
loadingMoreTopics = false,
active = ''; active = '';
$(window).on('action:ajaxify.start', function(ev, data) { $(window).on('action:ajaxify.start', function(ev, data) {
@ -20,34 +23,23 @@ define(['forum/recent'], function(recent) {
active = recent.selectActivePill(); active = recent.selectActivePill();
app.enableInfiniteLoading(function() { infinitescroll.init(loadMoreTopics);
if(!loadingMoreTopics) {
loadMoreTopics();
}
});
function loadMoreTopics() { function loadMoreTopics(direction) {
if(!$('#topics-container').length) { if(direction < 0 || !$('#topics-container').length) {
return; return;
} }
loadingMoreTopics = true; infinitescroll.loadMore('topics.loadMoreFromSet', {
socket.emit('topics.loadMoreFromSet', {
set: 'topics:' + $('.nav-pills .active a').html().toLowerCase(), set: 'topics:' + $('.nav-pills .active a').html().toLowerCase(),
after: $('#topics-container').attr('data-nextstart') after: $('#topics-container').attr('data-nextstart')
}, function(err, data) { }, function(data) {
if(err) {
return app.alertError(err.message);
}
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {
recent.onTopicsLoaded('popular', data.topics, false); recent.onTopicsLoaded('popular', data.topics, false);
$('#topics-container').attr('data-nextstart', data.nextStart); $('#topics-container').attr('data-nextstart', data.nextStart);
} else { } else {
$('#load-more-btn').hide(); $('#load-more-btn').hide();
} }
loadingMoreTopics = false;
}); });
} }
}; };

@ -1,13 +1,12 @@
'use strict'; '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 Recent = {};
var newTopicCount = 0, var newTopicCount = 0,
newPostCount = 0, newPostCount = 0;
loadingMoreTopics = false;
var active = ''; var active = '';
@ -36,11 +35,7 @@ define(function() {
}); });
app.enableInfiniteLoading(function() { infinitescroll.init(Recent.loadMoreTopics);
if(!loadingMoreTopics) {
Recent.loadMoreTopics();
}
});
}; };
Recent.selectActivePill = function() { Recent.selectActivePill = function() {
@ -102,43 +97,31 @@ define(function() {
$('#category-no-topics').addClass('hide'); $('#category-no-topics').addClass('hide');
}; };
Recent.loadMoreTopics = function() { Recent.loadMoreTopics = function(direction) {
if(!$('#topics-container').length) { if(direction < 0 || !$('#topics-container').length) {
return; return;
} }
loadingMoreTopics = true; infinitescroll.loadMore('topics.loadMoreRecentTopics', {
socket.emit('topics.loadMoreRecentTopics', {
after: $('#topics-container').attr('data-nextstart'), after: $('#topics-container').attr('data-nextstart'),
term: active term: active
}, function(err, data) { }, function(data) {
if(err) {
return app.alertError(err.message);
}
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {
Recent.onTopicsLoaded('recent', data.topics, false); Recent.onTopicsLoaded('recent', data.topics, false);
$('#topics-container').attr('data-nextstart', data.nextStart); $('#topics-container').attr('data-nextstart', data.nextStart);
} }
loadingMoreTopics = false;
}); });
}; };
Recent.onTopicsLoaded = function(templateName, topics, showSelect) { Recent.onTopicsLoaded = function(templateName, topics, showSelect) {
ajaxify.loadTemplate(templateName, function(template) { infinitescroll.parseAndTranslate(templateName, 'topics', {topics: topics, showSelect: showSelect}, function(html) {
var html = templates.parse(templates.getBlock(template, 'topics'), {topics: topics, showSelect: showSelect});
translator.translate(html, function(translatedHTML) {
$('#category-no-topics').remove(); $('#category-no-topics').remove();
html = $(translatedHTML);
$('#topics-container').append(html); $('#topics-container').append(html);
html.find('span.timeago').timeago(); html.find('span.timeago').timeago();
app.createUserTooltips(); app.createUserTooltips();
utils.makeNumbersHumanReadable(html.find('.human-readable-number')); utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
}); });
});
}; };
return Recent; return Recent;

@ -3,9 +3,8 @@
/* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH, utils */ /* 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 = {}, var Topic = {},
infiniteLoaderActive = false,
scrollingToPost = false, scrollingToPost = false,
currentUrl = ''; currentUrl = '';
@ -43,7 +42,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools',
hidePostToolsForDeletedPosts(); hidePostToolsForDeletedPosts();
enableInfiniteLoading(); enableInfiniteLoadingOrPagination();
addBlockquoteEllipses($('.topic .post-content > blockquote')); 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) { if(!config.usePagination) {
infinitescroll.init(loadMorePosts);
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);
}
});
}
});
} else { } else {
navigator.hide(); navigator.hide();
@ -224,14 +196,14 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools',
if(err) { if(err) {
return; return;
} }
var tid = $('#post-container').attr('data-tid');
$('#post-container').empty(); $('#post-container').empty();
var after = index - config.postsPerPage + 1; var after = index - config.postsPerPage + 1;
if(after < 0) { if(after < 0) {
after = 0; after = 0;
} }
loadMorePosts(tid, after, function() { loadPostsAfter(after, function() {
scrollToPid(pid); scrollToPid(pid);
}); });
}); });
@ -242,8 +214,8 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools',
tid = $('#post-container').attr('data-tid'); tid = $('#post-container').attr('data-tid');
function animateScroll() { function animateScroll() {
$("html, body").animate({ $('html, body').animate({
scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + "px" scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px'
}, duration !== undefined ? duration : 400, function() { }, duration !== undefined ? duration : 400, function() {
scrollingToPost = false; scrollingToPost = false;
navigator.update(); 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(); var firstPost = $('#post-container .post-row').first();
if(firstPid < parseInt(firstPost.attr('data-pid'), 10)) { if(firstPid < parseInt(firstPost.attr('data-pid'), 10)) {
before = firstPost; before = firstPost;
@ -334,35 +306,26 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools',
data.title = ajaxify.variables.get('topic_name'); data.title = ajaxify.variables.get('topic_name');
data.viewcount = ajaxify.variables.get('viewcount'); data.viewcount = ajaxify.variables.get('viewcount');
parseAndTranslatePosts(data, function(translatedHTML) { infinitescroll.parseAndTranslate('topic', 'posts', data, function(html) {
var translated = $(translatedHTML);
if(after) { if(after) {
translated.insertAfter(after); html.insertAfter(after);
} else if(before) { } else if(before) {
translated.insertBefore(before); html.insertBefore(before);
} else { } else {
$('#post-container').append(translated); $('#post-container').append(html);
} }
translated.hide().fadeIn('slow'); html.hide().fadeIn('slow');
addBlockquoteEllipses(translated.find('.post-content > blockquote'));
onNewPostsLoaded(translated, data.posts); addBlockquoteEllipses(html.find('.post-content > blockquote'));
if(typeof callback === 'function') { onNewPostsLoaded(html, data.posts);
if (typeof callback === 'function') {
callback(); 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 onNewPostsLoaded(html, posts) {
function getPostPrivileges(pid) { function getPostPrivileges(pid) {
socket.emit('posts.getPrivileges', pid, function(err, privileges) { 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); getPostPrivileges(posts[x].pid);
} }
infiniteLoaderActive = false;
app.populateOnlineUsers(); app.populateOnlineUsers();
app.createUserTooltips(); app.createUserTooltips();
utils.addCommasToNumbers(html.find('.formatted-number')); utils.addCommasToNumbers(html.find('.formatted-number'));
@ -392,45 +353,47 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools',
function toggleModTools(postHtml, privileges) { function toggleModTools(postHtml, privileges) {
postHtml.find('.edit, .delete').toggleClass('none', !privileges.meta.editable); postHtml.find('.edit, .delete').toggleClass('none', !privileges.meta.editable);
postHtml.find('.move').toggleClass('none', !privileges.meta.move); 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); var isSelfPost = parseInt(postHtml.attr('data-uid'), 10) === parseInt(app.uid, 10);
postHtml.find('.chat, .flag').toggleClass('none', isSelfPost); postHtml.find('.chat, .flag').toggleClass('none', isSelfPost);
} }
function loadMorePosts(tid, after, callback) { function loadMorePosts(direction) {
var indicatorEl = $('.loading-indicator'); if (!$('#post-container').length) {
if (infiniteLoaderActive || !$('#post-container').length) {
return; 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)) { if (!utils.isNumber(after) || (after === 0 && $('#post-container li.post-row[data-index="0"]').length)) {
return; return;
} }
infiniteLoaderActive = true; var indicatorEl = $('.loading-indicator');
if (!indicatorEl.is(':animated')) {
indicatorEl.fadeIn(); indicatorEl.fadeIn();
}
socket.emit('topics.loadMore', { infinitescroll.loadMore('topics.loadMore', {
tid: tid, tid: ajaxify.variables.get('topic_id'),
after: after after: after
}, function (err, data) { }, function (data) {
indicatorEl.fadeOut(function() { indicatorEl.fadeOut();
infiniteLoaderActive = false;
});
if(err) {
return app.alertError(err.message);
}
if (data && data.posts && data.posts.length) { if (data && data.posts && data.posts.length) {
createNewPosts(data, callback); createNewPosts(data, callback);
hidePostToolsForDeletedPosts();
} else { } else {
navigator.update(); navigator.update();
if (typeof callback === 'function') {
callback(data.posts);
}
} }
}); });
} }

@ -2,7 +2,7 @@
/* globals define, app, socket */ /* globals define, app, socket */
define(['forum/recent', 'topicSelect'], function(recent, topicSelect) { define(['forum/recent', 'topicSelect', 'forum/infinitescroll'], function(recent, topicSelect, infinitescroll) {
var Unread = {}, var Unread = {},
loadingMoreTopics = false; loadingMoreTopics = false;
@ -86,33 +86,22 @@ define(['forum/recent', 'topicSelect'], function(recent, topicSelect) {
loadMoreTopics(); loadMoreTopics();
}); });
app.enableInfiniteLoading(function() { infinitescroll.init(loadMoreTopics);
if(!loadingMoreTopics) {
loadMoreTopics();
}
});
function loadMoreTopics() { function loadMoreTopics(direction) {
if(!$('#topics-container').length) { if(direction < 0 || !$('#topics-container').length) {
return; return;
} }
loadingMoreTopics = true; infinitescroll.loadMore('topics.loadMoreUnreadTopics', {
socket.emit('topics.loadMoreUnreadTopics', {
after: $('#topics-container').attr('data-nextstart') after: $('#topics-container').attr('data-nextstart')
}, function(err, data) { }, function(data) {
if(err) {
return app.alertError(err.message);
}
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {
recent.onTopicsLoaded('unread', data.topics, true); recent.onTopicsLoaded('unread', data.topics, true);
$('#topics-container').attr('data-nextstart', data.nextStart); $('#topics-container').attr('data-nextstart', data.nextStart);
} else { } else {
$('#load-more-btn').hide(); $('#load-more-btn').hide();
} }
loadingMoreTopics = false;
}); });
} }
}; };

@ -67,10 +67,6 @@
}; };
translator.translate = function (data, language, callback) { translator.translate = function (data, language, callback) {
if (!data) {
return callback(data);
}
if (typeof language === 'function') { if (typeof language === 'function') {
callback = language; callback = language;
if ('undefined' !== typeof window && config) { if ('undefined' !== typeof window && config) {
@ -81,6 +77,10 @@
} }
} }
if (!data) {
return callback(data);
}
function insertLanguage(text, key, value, variables) { function insertLanguage(text, key, value, variables) {
if (value) { if (value) {
for (var i = 1, ii = variables.length; i < ii; i++) { for (var i = 1, ii = variables.length; i < ii; i++) {

@ -164,7 +164,7 @@ var db = require('./database'),
} }
async.filter(pids, function(pid, next) { 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); next(!err && canRead);
}); });
}, function(pids) { }, function(pids) {
@ -216,7 +216,7 @@ var db = require('./database'),
} }
async.filter(pids, function(pid, next) { 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); next(!err && canRead);
}); });
}, function(pids) { }, function(pids) {

@ -58,20 +58,6 @@ module.exports = function(privileges) {
], callback); ], 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) { privileges.categories.canMoveAllTopics = function(currentCid, targetCid, uid, callback) {
async.parallel({ async.parallel({
isAdministrator: function(next) { isAdministrator: function(next) {

@ -30,22 +30,8 @@ helpers.allowedTo = function(privilege, uid, cid, callback) {
return callback(null, false); return callback(null, false);
} }
// Guests handling
if (parseInt(uid, 10) === 0) { if (parseInt(uid, 10) === 0) {
return async.parallel([ return isGuestAllowedTo(privilege, cid, callback);
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));
});
} }
async.parallel({ 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) { helpers.isMember = function(method, group, uid, callback) {
groups.exists(group, function(err, exists) { groups.exists(group, function(err, exists) {
if (err) { if (err) {
@ -77,7 +80,7 @@ helpers.isMember = function(method, group, uid, callback) {
method(uid, group, callback); method(uid, group, callback);
}); });
} };
helpers.hasEnoughReputationFor = function(privilege, uid, callback) { helpers.hasEnoughReputationFor = function(privilege, uid, callback) {
if (parseInt(meta.config['privileges:disabled'], 10)) { if (parseInt(meta.config['privileges:disabled'], 10)) {

@ -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) { posts.getCidByPid(pid, function(err, cid) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
privileges.categories.canRead(cid, uid, callback); privileges.categories.can(privilege, cid, uid, callback);
}); });
}; };

@ -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) { privileges.topics.canEdit = function(tid, uid, callback) {
helpers.some([ helpers.some([
function(next) { function(next) {

@ -9,7 +9,7 @@ var async = require('async'),
SocketCategories = {}; SocketCategories = {};
SocketCategories.getRecentReplies = function(socket, cid, callback) { 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) { if (err) {
return callback(err); return callback(err);
} }

@ -129,7 +129,7 @@ function sendNotificationToPostOwner(data, uid, notification) {
SocketPosts.getRawPost = function(socket, pid, callback) { SocketPosts.getRawPost = function(socket, pid, callback) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
privileges.posts.canRead(pid, socket.uid, next); privileges.posts.can('read', pid, socket.uid, next);
}, },
function(canRead, next) { function(canRead, next) {
if (!canRead) { if (!canRead) {

@ -130,7 +130,7 @@ var async = require('async'),
} }
async.filter(tids, function(tid, next) { 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); next(!err && canRead);
}); });
}, function(tids) { }, function(tids) {

@ -99,7 +99,7 @@ module.exports = function(Topics) {
if (!categoryExists) { if (!categoryExists) {
return next(new Error('[[error:no-category]]')); return next(new Error('[[error:no-category]]'));
} }
privileges.topics.canCreate(cid, uid, next); privileges.categories.can('topics:create', cid, uid, next);
}, },
function(canCreate, next) { function(canCreate, next) {
if(!canCreate) { if(!canCreate) {
@ -173,7 +173,7 @@ module.exports = function(Topics) {
return next(new Error('[[error:topic-locked]]')); return next(new Error('[[error:topic-locked]]'));
} }
privileges.topics.canReply(tid, uid, next); privileges.topics.can('topics:reply', tid, uid, next);
}, },
function(canReply, next) { function(canReply, next) {
if (!canReply) { if (!canReply) {

@ -49,7 +49,7 @@ module.exports = function(Topics) {
}); });
async.filter(newtids, function(tid, next) { 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); next(!err && canRead);
}); });
}, function(newtids) { }, function(newtids) {

Loading…
Cancel
Save