mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 00:15:46 +01:00
infinite scroll refactor
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -1,52 +1,41 @@
|
||||
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');
|
||||
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'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return Favourites;
|
||||
|
||||
@@ -1,56 +1,42 @@
|
||||
'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', {
|
||||
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 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(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');
|
||||
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'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return AccountPosts;
|
||||
|
||||
@@ -1,52 +1,40 @@
|
||||
'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);
|
||||
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'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return AccountTopics;
|
||||
|
||||
@@ -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() {
|
||||
function enableInfiniteLoadingOrPagination() {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
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(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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Category.loadMoreTopics = function(cid, after, callback) {
|
||||
if (loadingMoreTopics || !$('#topics-container').length) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
79
public/src/forum/infinitescroll.js
Normal file
79
public/src/forum/infinitescroll.js
Normal file
@@ -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 = {},
|
||||
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;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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,43 +97,31 @@ 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) {
|
||||
infinitescroll.parseAndTranslate(templateName, 'topics', {topics: topics, showSelect: showSelect}, function(html) {
|
||||
$('#category-no-topics').remove();
|
||||
|
||||
html = $(translatedHTML);
|
||||
$('#topics-container').append(html);
|
||||
html.find('span.timeago').timeago();
|
||||
app.createUserTooltips();
|
||||
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return Recent;
|
||||
|
||||
@@ -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();
|
||||
@@ -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'));
|
||||
|
||||
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;
|
||||
var indicatorEl = $('.loading-indicator');
|
||||
if (!indicatorEl.is(':animated')) {
|
||||
indicatorEl.fadeIn();
|
||||
|
||||
socket.emit('topics.loadMore', {
|
||||
tid: tid,
|
||||
after: after
|
||||
}, function (err, data) {
|
||||
|
||||
indicatorEl.fadeOut(function() {
|
||||
infiniteLoaderActive = false;
|
||||
});
|
||||
|
||||
if(err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
infinitescroll.loadMore('topics.loadMore', {
|
||||
tid: ajaxify.variables.get('topic_id'),
|
||||
after: after
|
||||
}, function (data) {
|
||||
|
||||
indicatorEl.fadeOut();
|
||||
|
||||
if (data && data.posts && data.posts.length) {
|
||||
createNewPosts(data, callback);
|
||||
hidePostToolsForDeletedPosts();
|
||||
} else {
|
||||
navigator.update();
|
||||
if (typeof callback === 'function') {
|
||||
callback(data.posts);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
Reference in New Issue
Block a user