|
|
@ -38,13 +38,128 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
|
|
|
socket.on('event:new_topic', Category.onNewTopic);
|
|
|
|
socket.on('event:new_topic', Category.onNewTopic);
|
|
|
|
|
|
|
|
|
|
|
|
enableInfiniteLoading();
|
|
|
|
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'), index, 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() {
|
|
|
|
function enableInfiniteLoading() {
|
|
|
|
if(!config.usePagination) {
|
|
|
|
if(!config.usePagination) {
|
|
|
|
app.enableInfiniteLoading(function() {
|
|
|
|
app.enableInfiniteLoading(function(direction) {
|
|
|
|
if(!loadingMoreTopics) {
|
|
|
|
|
|
|
|
Category.loadMoreTopics(templates.get('category_id'));
|
|
|
|
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 {
|
|
|
|
} else {
|
|
|
@ -94,56 +209,112 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
|
|
|
|
|
|
|
|
|
|
|
$(window).trigger('action:categories.new_topic.loaded');
|
|
|
|
$(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({
|
|
|
|
var html = templates.prepare(templates['category'].blocks['topics']).parse({
|
|
|
|
topics: topics
|
|
|
|
topics: topics
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
translator.translate(html, function(translatedHTML) {
|
|
|
|
translator.translate(html, function(translatedHTML) {
|
|
|
|
var container = $('#topics-container');
|
|
|
|
var container = $('#topics-container'),
|
|
|
|
|
|
|
|
html = $(translatedHTML);
|
|
|
|
|
|
|
|
|
|
|
|
$('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
$('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
$('#category-no-topics').remove();
|
|
|
|
$('#category-no-topics').remove();
|
|
|
|
|
|
|
|
|
|
|
|
html = $(translatedHTML);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(config.usePagination) {
|
|
|
|
if(config.usePagination) {
|
|
|
|
container.empty().append(html);
|
|
|
|
container.empty().append(html);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
container.append(html);
|
|
|
|
if(after) {
|
|
|
|
|
|
|
|
html.insertAfter(after);
|
|
|
|
|
|
|
|
} else if(before) {
|
|
|
|
|
|
|
|
html.insertBefore(before);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
container.append(html);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$('#topics-container span.timeago').timeago();
|
|
|
|
$('#topics-container span.timeago').timeago();
|
|
|
|
app.createUserTooltips();
|
|
|
|
app.createUserTooltips();
|
|
|
|
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
|
|
|
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
|
|
|
callback(topics);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Category.loadMoreTopics = function(cid) {
|
|
|
|
Category.loadMoreTopics = function(cid, after, callback) {
|
|
|
|
if (loadingMoreTopics || !$('#topics-container').length) {
|
|
|
|
if (loadingMoreTopics || !$('#topics-container').length) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(after === 0 && $('#topics-container li.category-item[data-index="0"]').length) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$(window).trigger('action:categories.loading');
|
|
|
|
$(window).trigger('action:categories.loading');
|
|
|
|
loadingMoreTopics = true;
|
|
|
|
loadingMoreTopics = true;
|
|
|
|
|
|
|
|
|
|
|
|
socket.emit('categories.loadMore', {
|
|
|
|
socket.emit('categories.loadMore', {
|
|
|
|
cid: cid,
|
|
|
|
cid: cid,
|
|
|
|
after: $('#topics-container').attr('data-nextstart')
|
|
|
|
after: after
|
|
|
|
}, function (err, data) {
|
|
|
|
}, function (err, data) {
|
|
|
|
|
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
if(err) {
|
|
|
|
return app.alertError(err.message);
|
|
|
|
return app.alertError(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data && data.topics.length) {
|
|
|
|
if (data && data.topics.length) {
|
|
|
|
Category.onTopicsLoaded(data.topics);
|
|
|
|
Category.onTopicsLoaded(data.topics, callback);
|
|
|
|
$('#topics-container').attr('data-nextstart', data.nextStart);
|
|
|
|
$('#topics-container').attr('data-nextstart', data.nextStart);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
|
|
|
callback(data.topics);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$(window).trigger('action:categories.loaded');
|
|
|
|
$(window).trigger('action:categories.loaded');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return Category;
|
|
|
|
return Category;
|
|
|
|
});
|
|
|
|
});
|