mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 08:55:47 +01:00
IS improvements
* simplify calculation (scrollTop vs elementInView) * use post anchor rather than create big $ objs from post html * pressing f5 repeatedly now always takes you to the correct pid
This commit is contained in:
@@ -61,7 +61,7 @@ define('forum/topic', [
|
|||||||
|
|
||||||
handleKeys();
|
handleKeys();
|
||||||
|
|
||||||
navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex);
|
navigator.init('[component="post/anchor"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex);
|
||||||
|
|
||||||
$(window).on('scroll', updateTopicTitle);
|
$(window).on('scroll', updateTopicTitle);
|
||||||
|
|
||||||
@@ -219,43 +219,30 @@ define('forum/topic', [
|
|||||||
return index;
|
return index;
|
||||||
};
|
};
|
||||||
|
|
||||||
Topic.navigatorCallback = function(topPostIndex, bottomPostIndex, elementCount) {
|
Topic.navigatorCallback = function(index, elementCount) {
|
||||||
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
|
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
|
||||||
if (!path.startsWith('topic')) {
|
if (!path.startsWith('topic')) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
var postIndex = topPostIndex;
|
|
||||||
var index = bottomPostIndex;
|
|
||||||
if (config.topicPostSort !== 'oldest_to_newest') {
|
|
||||||
if (bottomPostIndex === 0) {
|
|
||||||
index = 1;
|
|
||||||
} else {
|
|
||||||
index = Math.max(elementCount - bottomPostIndex + 2, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
|
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
|
||||||
var currentBookmark = ajaxify.data.bookmark || localStorage.getItem(bookmarkKey);
|
var currentBookmark = ajaxify.data.bookmark || localStorage.getItem(bookmarkKey);
|
||||||
|
|
||||||
if (!currentBookmark || parseInt(postIndex, 10) > parseInt(currentBookmark, 10)) {
|
if (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10)) {
|
||||||
if (app.user.uid) {
|
if (app.user.uid) {
|
||||||
var payload = {
|
socket.emit('topics.bookmark', {
|
||||||
'tid': ajaxify.data.tid,
|
'tid': ajaxify.data.tid,
|
||||||
'index': postIndex
|
'index': index
|
||||||
};
|
}, function(err) {
|
||||||
socket.emit('topics.bookmark', payload, function(err) {
|
ajaxify.data.bookmark = index;
|
||||||
if (err) {
|
|
||||||
console.warn('Error saving bookmark:', err);
|
|
||||||
}
|
|
||||||
ajaxify.data.bookmark = postIndex;
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem(bookmarkKey, postIndex);
|
localStorage.setItem(bookmarkKey, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// removes the bookmark alert when we get to / past the bookmark
|
// removes the bookmark alert when we get to / past the bookmark
|
||||||
if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) {
|
if (!currentBookmark || parseInt(index, 10) >= parseInt(currentBookmark, 10)) {
|
||||||
app.removeAlert('bookmark');
|
app.removeAlert('bookmark');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,14 +251,15 @@ define('forum/topic', [
|
|||||||
var topicId = parts[1],
|
var topicId = parts[1],
|
||||||
slug = parts[2];
|
slug = parts[2];
|
||||||
var newUrl = 'topic/' + topicId + '/' + (slug ? slug : '');
|
var newUrl = 'topic/' + topicId + '/' + (slug ? slug : '');
|
||||||
if (postIndex > 1) {
|
if (index > 1) {
|
||||||
newUrl += '/' + postIndex;
|
newUrl += '/' + index;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newUrl !== currentUrl) {
|
if (newUrl !== currentUrl) {
|
||||||
if (Topic.replaceURLTimeout) {
|
if (Topic.replaceURLTimeout) {
|
||||||
clearTimeout(Topic.replaceURLTimeout);
|
clearTimeout(Topic.replaceURLTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
Topic.replaceURLTimeout = setTimeout(function() {
|
Topic.replaceURLTimeout = setTimeout(function() {
|
||||||
Topic.replaceURLTimeout = 0;
|
Topic.replaceURLTimeout = 0;
|
||||||
if (history.replaceState) {
|
if (history.replaceState) {
|
||||||
@@ -284,7 +272,6 @@ define('forum/topic', [
|
|||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -90,28 +90,21 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com
|
|||||||
navigator.update = function() {
|
navigator.update = function() {
|
||||||
toggle(!!count);
|
toggle(!!count);
|
||||||
|
|
||||||
var topIndex = 0;
|
var middleOfViewport = $(window).scrollTop() + $(window).height() / 2;
|
||||||
var bottomIndex = 0;
|
|
||||||
|
index = 0;
|
||||||
|
|
||||||
$(navigator.selector).each(function() {
|
$(navigator.selector).each(function() {
|
||||||
var el = $(this);
|
var el = $(this);
|
||||||
|
|
||||||
if (elementInView(el)) {
|
index++;
|
||||||
if (!topIndex) {
|
if (el.offset().top > middleOfViewport) {
|
||||||
topIndex = parseInt(el.attr('data-index'), 10) + 1;
|
|
||||||
} else {
|
|
||||||
bottomIndex = parseInt(el.attr('data-index'), 10) + 1;
|
|
||||||
}
|
|
||||||
} else if (topIndex && bottomIndex) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (topIndex && !bottomIndex) {
|
if (typeof navigator.callback === 'function') {
|
||||||
bottomIndex = topIndex;
|
navigator.callback(index, count);
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof navigator.callback === 'function' && topIndex && bottomIndex) {
|
|
||||||
index = navigator.callback(topIndex, bottomIndex, count);
|
|
||||||
navigator.updateTextAndProgressBar();
|
navigator.updateTextAndProgressBar();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -155,16 +148,6 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function elementInView(el) {
|
|
||||||
var scrollTop = $(window).scrollTop() + $('#header-menu').height();
|
|
||||||
var scrollBottom = scrollTop + $(window).height();
|
|
||||||
|
|
||||||
var elTop = el.offset().top;
|
|
||||||
var elBottom = elTop + Math.floor(el.height());
|
|
||||||
|
|
||||||
return (elTop >= scrollTop && elBottom < scrollBottom) || (elTop < scrollTop && elBottom > scrollTop);
|
|
||||||
}
|
|
||||||
|
|
||||||
navigator.scrollToPost = function(postIndex, highlight, duration, offset) {
|
navigator.scrollToPost = function(postIndex, highlight, duration, offset) {
|
||||||
if (!utils.isNumber(postIndex) || !components.get('topic').length) {
|
if (!utils.isNumber(postIndex) || !components.get('topic').length) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user