diff --git a/public/src/client/category.js b/public/src/client/category.js index e2dfcd9924..acbf031697 100644 --- a/public/src/client/category.js +++ b/public/src/client/category.js @@ -41,10 +41,6 @@ define('forum/category', [ sort.handleSort('categoryTopicSort', 'user.setCategorySort', 'category/' + ajaxify.data.slug); - if (!config.usePagination) { - navigator.init('[component="category/topic"]', ajaxify.data.topic_count, Category.toTop, Category.toBottom, Category.navigatorCallback); - } - enableInfiniteLoadingOrPagination(); $('[component="category"]').on('click', '[component="topic/header"]', function () { @@ -187,6 +183,7 @@ define('forum/category', [ function enableInfiniteLoadingOrPagination() { if (!config.usePagination) { + navigator.init('[component="category/topic"]', ajaxify.data.topic_count, Category.toTop, Category.toBottom, Category.navigatorCallback); infinitescroll.init($('[component="category"]'), Category.loadMoreTopics); } else { navigator.disable(); diff --git a/public/src/client/topic.js b/public/src/client/topic.js index d620da1e70..832e6e9600 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -150,14 +150,13 @@ define('forum/topic', [ return navigator.scrollToPostIndex(postIndex, true, 0); } } else if (bookmark && (!config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1)) && ajaxify.data.postcount > ajaxify.data.bookmarkThreshold) { - navigator.update(0); app.alert({ alert_id: 'bookmark', message: '[[topic:bookmark_instructions]]', timeout: 0, type: 'info', clickfn: function () { - navigator.scrollToPost(parseInt(bookmark - 1, 10), true); + navigator.scrollToIndex(parseInt(bookmark - 1, 10), true); }, closefn: function () { localStorage.removeItem('topic:' + tid + ':bookmark'); @@ -166,8 +165,6 @@ define('forum/topic', [ setTimeout(function () { app.removeAlert('bookmark'); }, 10000); - } else { - navigator.update(0); } } @@ -204,7 +201,7 @@ define('forum/topic', [ var toPost = $('[component="post"][data-pid="' + toPid + '"]'); if (toPost.length) { e.preventDefault(); - navigator.scrollToPost(toPost.attr('data-index'), true); + navigator.scrollToIndex(toPost.attr('data-index'), true); return false; } }); diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 68c0359755..9ca7e94fa9 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -1,7 +1,5 @@ - 'use strict'; - define('navigator', ['forum/pagination', 'components'], function (pagination, components) { var navigator = {}; var index = 1; @@ -55,6 +53,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co }); navigator.setCount(count); + navigator.update(0); }; function generateUrl(index) { @@ -183,7 +182,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co navigator.scrollTop = function (index) { if ($(navigator.selector + '[data-index="' + index + '"]').length) { - navigator.scrollToPost(index, true); + navigator.scrollToIndex(index, true); } else { ajaxify.go(generateUrl()); } @@ -193,44 +192,67 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co if (parseInt(index, 10) < 0) { return; } + if ($(navigator.selector + '[data-index="' + index + '"]').length) { - navigator.scrollToPost(index, true); + navigator.scrollToIndex(index, true); } else { index = parseInt(index, 10) + 1; ajaxify.go(generateUrl(index)); } }; - navigator.scrollToPost = function (postIndex, highlight, duration) { - if (!utils.isNumber(postIndex) || !components.get('topic').length) { + navigator.scrollToPost = function (index, highlight, duration) { + console.log('[navigator.scrollToPost] deprecated please use, navigator.scrollToIndex'); + navigator.scrollToIndex(index, highlight, duration); + }; + + navigator.scrollToIndex = function (index, highlight, duration) { + var inTopic = !!components.get('topic').length; + var inCategory = !!components.get('category').length; + + if (!utils.isNumber(index) || (!inTopic && !inCategory)) { return; } duration = duration !== undefined ? duration : 400; navigator.scrollActive = true; - if (components.get('post/anchor', postIndex).length) { - return navigator.scrollToPostIndex(postIndex, highlight, duration); + // if in topic and item already on page + if (inTopic && components.get('post/anchor', index).length) { + return navigator.scrollToPostIndex(index, highlight, duration); + } + + // if in category and item alreay on page + if (inCategory && $('[component="category/topic"][data-index="' + index + '"]').length) { + return navigator.scrollToTopicIndex(index, highlight, duration); + } + + if (!config.usePagination) { + navigator.scrollActive = false; + index = parseInt(index, 10) + 1; + ajaxify.go(generateUrl(index)); + return; } - if (config.usePagination) { - var index = postIndex; + var scrollMethod = inTopic ? navigator.scrollToPostIndex : navigator.scrollToTopicIndex; + if (inTopic) { if (config.topicPostSort === 'most_votes' || config.topicPostSort === 'newest_to_oldest') { index = ajaxify.data.postcount - index; } - var page = Math.max(1, Math.ceil(index / config.postsPerPage)); - - if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) { - pagination.loadPage(page, function () { - navigator.scrollToPostIndex(postIndex, highlight, duration); - }); - } else { - navigator.scrollToPostIndex(postIndex, highlight, duration); + } else if (inCategory) { + if (config.categoryTopicSort === 'most_posts' || config.categoryTopicSort === 'oldest_to_newest') { + index = ajaxify.data.ajaxify.data.topic_count - index; } + } + + var page = Math.max(1, Math.ceil(index / config.postsPerPage)); + + if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) { + pagination.loadPage(page, function () { + scrollMethod(index, highlight, duration); + }); } else { - navigator.scrollActive = false; - postIndex = parseInt(postIndex, 10) + 1; - ajaxify.go(generateUrl(postIndex)); + scrollMethod(index, highlight, duration); } }; @@ -239,6 +261,11 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co navigator.scrollToElement(scrollTo, highlight, duration); }; + navigator.scrollToTopicIndex = function (topicIndex, highlight, duration) { + var scrollTo = $('[component="category/topic"][data-index="' + topicIndex + '"]'); + navigator.scrollToElement(scrollTo, highlight, duration); + }; + navigator.scrollToElement = function (scrollTo, highlight, duration) { if (!scrollTo.length) { navigator.scrollActive = false;