mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 16:35:47 +01:00
closes #1101
This commit is contained in:
@@ -2,10 +2,6 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
||||
var Category = {},
|
||||
loadingMoreTopics = false;
|
||||
|
||||
$(window).on('action:popstate', function(ev, data) {
|
||||
|
||||
});
|
||||
|
||||
Category.init = function() {
|
||||
var cid = templates.get('category_id'),
|
||||
categoryName = templates.get('category_name'),
|
||||
@@ -42,13 +38,128 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
||||
socket.on('event:new_topic', Category.onNewTopic);
|
||||
|
||||
enableInfiniteLoading();
|
||||
|
||||
$('#topics-container').on('click', '.topic-title', function() {
|
||||
var clickedTid = $(this).parents('li.category-item[data-tid]').attr('data-tid');
|
||||
$('#topics-container li.category-item').each(function(index, el) {
|
||||
if($(el).offset().top - $(window).scrollTop() > 0) {
|
||||
tid = $(el).attr('data-tid');
|
||||
|
||||
localStorage.setItem('category:bookmark', tid);
|
||||
localStorage.setItem('category:bookmark:clicked', clickedTid);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$(window).on('action:popstate', function(ev, data) {
|
||||
if(data.url.indexOf('category/') === 0) {
|
||||
var bookmark = localStorage.getItem('category:bookmark');
|
||||
var clicked = localStorage.getItem('category:bookmark:clicked');
|
||||
|
||||
if (bookmark) {
|
||||
|
||||
if(config.usePagination) {
|
||||
socket.emit('topics.getTidPage', bookmark, function(err, page) {
|
||||
if(err) {
|
||||
return;
|
||||
}
|
||||
if(parseInt(page, 10) !== pagination.currentPage) {
|
||||
pagination.loadPage(page);
|
||||
} else {
|
||||
Category.scrollToTopic(bookmark, clicked, 400);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
||||
socket.emit('topics.getTidIndex', bookmark, function(err, index) {
|
||||
if(err) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(index === 0) {
|
||||
Category.highlightTopic(clicked);
|
||||
return;
|
||||
}
|
||||
|
||||
if (index < 0) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
$('#topics-container').empty();
|
||||
loadingMoreTopics = false;
|
||||
|
||||
Category.loadMoreTopics(templates.get('category_id'), after, function() {
|
||||
Category.scrollToTopic(bookmark, clicked, 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Category.highlightTopic = function(tid) {
|
||||
var highlight = $('#topics-container li.category-item[data-tid="' + tid + '"]');
|
||||
if(highlight.length && !highlight.hasClass('highlight')) {
|
||||
highlight.addClass('highlight');
|
||||
setTimeout(function() {
|
||||
highlight.removeClass('highlight');
|
||||
}, 5000);
|
||||
}
|
||||
};
|
||||
|
||||
Category.scrollToTopic = function(tid, clickedTid, duration, offset) {
|
||||
if(!tid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!offset) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
if($('#topics-container li.category-item[data-tid="' + tid + '"]').length) {
|
||||
var cid = templates.get('category_id');
|
||||
var scrollTo = $('#topics-container li.category-item[data-tid="' + tid + '"]');
|
||||
|
||||
if (cid && scrollTo.length) {
|
||||
$('html, body').animate({
|
||||
scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px'
|
||||
}, duration !== undefined ? duration : 400, function() {
|
||||
Category.highlightTopic(clickedTid);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function enableInfiniteLoading() {
|
||||
if(!config.usePagination) {
|
||||
app.enableInfiniteLoading(function() {
|
||||
if(!loadingMoreTopics) {
|
||||
Category.loadMoreTopics(templates.get('category_id'));
|
||||
app.enableInfiniteLoading(function(direction) {
|
||||
|
||||
if(!loadingMoreTopics && $('#topics-container').children().length) {
|
||||
|
||||
var after = 0;
|
||||
var 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);
|
||||
after -= config.topicsPerPage;
|
||||
if(after < 0) {
|
||||
after = 0;
|
||||
}
|
||||
}
|
||||
|
||||
var offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height();
|
||||
|
||||
Category.loadMoreTopics(templates.get('category_id'), after, function() {
|
||||
if(direction < 0 && el) {
|
||||
Category.scrollToTopic(el.attr('data-tid'), null, 0, offset);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -98,56 +209,112 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
||||
|
||||
$(window).trigger('action:categories.new_topic.loaded');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Category.onTopicsLoaded = function(topics, callback) {
|
||||
if(!topics || !topics.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
function removeAlreadyAddedTopics() {
|
||||
topics = topics.filter(function(topic) {
|
||||
return $('#topics-container li[data-tid="' + topic.tid +'"]').length === 0;
|
||||
});
|
||||
}
|
||||
|
||||
var after = null,
|
||||
before = null;
|
||||
|
||||
function findInsertionPoint() {
|
||||
if (!$('#topics-container .category-item[data-tid]').length) {
|
||||
return;
|
||||
}
|
||||
var last = $('#topics-container .category-item[data-tid]').last();
|
||||
var lastIndex = last.attr('data-index');
|
||||
var firstIndex = topics[topics.length - 1].index;
|
||||
if (firstIndex > lastIndex) {
|
||||
after = last;
|
||||
} else {
|
||||
before = $('#topics-container .category-item[data-tid]').first();
|
||||
}
|
||||
}
|
||||
|
||||
removeAlreadyAddedTopics();
|
||||
if(!topics.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
findInsertionPoint();
|
||||
|
||||
Category.onTopicsLoaded = function(topics) {
|
||||
var html = templates.prepare(templates['category'].blocks['topics']).parse({
|
||||
topics: topics
|
||||
});
|
||||
|
||||
translator.translate(html, function(translatedHTML) {
|
||||
var container = $('#topics-container');
|
||||
var container = $('#topics-container'),
|
||||
html = $(translatedHTML);
|
||||
|
||||
$('#topics-container, .category-sidebar').removeClass('hidden');
|
||||
$('#category-no-topics').remove();
|
||||
|
||||
html = $(translatedHTML);
|
||||
|
||||
if(config.usePagination) {
|
||||
container.empty().append(html);
|
||||
} else {
|
||||
container.append(html);
|
||||
if(after) {
|
||||
html.insertAfter(after);
|
||||
} else if(before) {
|
||||
html.insertBefore(before);
|
||||
} else {
|
||||
container.append(html);
|
||||
}
|
||||
}
|
||||
|
||||
$('#topics-container span.timeago').timeago();
|
||||
app.createUserTooltips();
|
||||
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
||||
});
|
||||
}
|
||||
|
||||
Category.loadMoreTopics = function(cid) {
|
||||
if (typeof callback === 'function') {
|
||||
callback(topics);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Category.loadMoreTopics = function(cid, after, callback) {
|
||||
if (loadingMoreTopics || !$('#topics-container').length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(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,
|
||||
after: $('#topics-container').attr('data-nextstart')
|
||||
after: after
|
||||
}, function (err, data) {
|
||||
loadingMoreTopics = false;
|
||||
|
||||
if(err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
if (data && data.topics.length) {
|
||||
Category.onTopicsLoaded(data.topics);
|
||||
Category.onTopicsLoaded(data.topics, callback);
|
||||
$('#topics-container').attr('data-nextstart', data.nextStart);
|
||||
} else {
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback(data.topics);
|
||||
}
|
||||
}
|
||||
loadingMoreTopics = false;
|
||||
|
||||
|
||||
$(window).trigger('action:categories.loaded');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return Category;
|
||||
});
|
||||
Reference in New Issue
Block a user