mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 08:25: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 = {
|
var titleObj = {
|
||||||
active: false,
|
active: false,
|
||||||
interval: undefined,
|
interval: undefined,
|
||||||
|
|||||||
@@ -1,51 +1,40 @@
|
|||||||
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});
|
$('.user-favourite-posts').append(html);
|
||||||
|
html.find('img').addClass('img-responsive');
|
||||||
translator.translate(html, function(translatedHTML) {
|
html.find('span.timeago').timeago();
|
||||||
$('#category-no-topics').remove();
|
app.createUserTooltips();
|
||||||
|
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||||
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'));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,55 +1,41 @@
|
|||||||
'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;
|
||||||
|
}
|
||||||
|
|
||||||
|
infinitescroll.loadMore('posts.loadMoreUserPosts', {
|
||||||
uid: $('.account-username-box').attr('data-uid'),
|
uid: $('.account-username-box').attr('data-uid'),
|
||||||
after: $('.user-favourite-posts').attr('data-nextstart')
|
after: $('.user-favourite-posts').attr('data-nextstart')
|
||||||
}, function(err, data) {
|
}, function(data) {
|
||||||
if(err) {
|
|
||||||
return app.alertError(err.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
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});
|
$('.user-favourite-posts').append(html);
|
||||||
|
html.find('img').addClass('img-responsive');
|
||||||
translator.translate(html, function(translatedHTML) {
|
html.find('span.timeago').timeago();
|
||||||
|
app.createUserTooltips();
|
||||||
html = $(translatedHTML);
|
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||||
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'));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,51 +1,39 @@
|
|||||||
'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});
|
$('#topics-container').append(html);
|
||||||
|
html.find('span.timeago').timeago();
|
||||||
translator.translate(html, function(translatedHTML) {
|
app.createUserTooltips();
|
||||||
html = $(translatedHTML);
|
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||||
$('#topics-container').append(html);
|
|
||||||
html.find('span.timeago').timeago();
|
|
||||||
app.createUserTooltips();
|
|
||||||
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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') {
|
Category.loadMoreTopics = function(direction) {
|
||||||
callback(topics);
|
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) {
|
function loadTopicsAfter(after, callback) {
|
||||||
if (loadingMoreTopics || !$('#topics-container').length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
if (data.topics && data.topics.length) {
|
||||||
return app.alertError(err.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data && 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;
|
||||||
});
|
});
|
||||||
|
|||||||
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 = {},
|
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,42 +97,30 @@ 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});
|
$('#category-no-topics').remove();
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
$('#topics-container').append(html);
|
||||||
$('#category-no-topics').remove();
|
html.find('span.timeago').timeago();
|
||||||
|
app.createUserTooltips();
|
||||||
html = $(translatedHTML);
|
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||||
$('#topics-container').append(html);
|
|
||||||
html.find('span.timeago').timeago();
|
|
||||||
app.createUserTooltips();
|
|
||||||
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
||||||
indicatorEl.fadeIn();
|
if (!indicatorEl.is(':animated')) {
|
||||||
|
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++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user