diff --git a/public/src/app.js b/public/src/app.js index 8393dfec2a..57e953a414 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -349,22 +349,10 @@ var socket, }); }; - app.scrollToTop = function () { - $('body,html').animate({ - scrollTop: 0 - }); - }; - - app.scrollToBottom = function () { - $('body,html').animate({ - scrollTop: $('html').height() - 100 - }); - }; - var previousScrollTop = 0; app.enableInfiniteLoading = function(callback) { - $(window).off('scroll').on('scroll', function() { + $(window).on('scroll', function() { var top = $(window).height() * 0.1; var bottom = ($(document).height() - $(window).height()) * 0.9; diff --git a/public/src/forum/category.js b/public/src/forum/category.js index ee19ccb7f5..14eb070a9d 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -1,10 +1,17 @@ "use strict"; /* global define, config, templates, app, ajaxify, socket, translator */ -define(['composer', 'forum/pagination', 'share'], function(composer, pagination, share) { +define(['composer', 'forum/pagination', 'share', 'navigator'], function(composer, pagination, share, navigator) { var Category = {}, loadingMoreTopics = false; + + $(window).on('action:ajaxify.start', function(ev, data) { + if(data.url.indexOf('category') !== 0) { + navigator.hide(); + } + }); + Category.init = function() { var cid = ajaxify.variables.get('category_id'); @@ -24,6 +31,10 @@ define(['composer', 'forum/pagination', 'share'], function(composer, pagination, enableInfiniteLoading(); + if (!config.usePagination) { + navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count')); + } + $('#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) { @@ -110,6 +121,7 @@ define(['composer', 'forum/pagination', 'share'], function(composer, pagination, scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px' }, duration !== undefined ? duration : 400, function() { Category.highlightTopic(clickedTid); + navigator.update(); }); } } @@ -117,6 +129,7 @@ define(['composer', 'forum/pagination', 'share'], function(composer, pagination, function enableInfiniteLoading() { if(!config.usePagination) { + app.enableInfiniteLoading(function(direction) { if(!loadingMoreTopics && $('#topics-container').children().length) { @@ -193,12 +206,22 @@ define(['composer', 'forum/pagination', 'share'], function(composer, pagination, topic.find('span.timeago').timeago(); app.createUserTooltips(); + updateTopicCount(); $(window).trigger('action:categories.new_topic.loaded'); }); }); }; + function updateTopicCount() { + socket.emit('categories.getTopicCount', ajaxify.variables.get('category_id'), function(err, topicCount) { + if(err) { + return app.alertError(err.message); + } + navigator.setCount(topicCount); + }); + } + Category.onTopicsLoaded = function(topics, callback) { if(!topics || !topics.length) { return; diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 8188654401..471967494b 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -1,9 +1,9 @@ 'use strict'; -/* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH */ +/* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH, utils */ -define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], function(pagination, threadTools, postTools) { +define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', 'navigator'], function(pagination, threadTools, postTools, navigator) { var Topic = {}, infiniteLoaderActive = false, scrollingToPost = false, @@ -17,7 +17,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], $(window).on('action:ajaxify.start', function(ev, data) { if(data.url.indexOf('topic') !== 0) { - $('.pagination-block').addClass('hidden'); + navigator.hide(); $('.header-topic-title').find('span').text('').hide(); app.removeAlert('bookmark'); } @@ -87,8 +87,8 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], }); } - if (!window.location.hash && !config.usePagination) { - updateHeader(); + if (!config.usePagination) { + navigator.init('.posts > .post-row', Topic.postCount, Topic.navigatorCallback); } $('#post-container').on('mouseenter', '.favourite-tooltip', function(e) { @@ -106,9 +106,6 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], function enableInfiniteLoading() { if(!config.usePagination) { - $('.pagination-block').removeClass('hidden'); - - updatePaginationTextAndProgressBar(1); app.enableInfiniteLoading(function(direction) { @@ -138,7 +135,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], } }); } else { - $('.pagination-block').addClass('hidden'); + navigator.hide(); pagination.init(currentPage, pageCount); } @@ -511,79 +508,44 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], }); } - $(window).on('scroll', updateHeader); + $(window).on('scroll', updateTopicTitle); + $(window).trigger('action:topic.loaded'); }; - function updateHeader() { - - $('.pagination-block a').off('click').on('click', function() { - return false; - }); - - $('.pagination-block i:first').off('click').on('click', function() { - app.scrollToTop(); - }); - - $('.pagination-block i:last').off('click').on('click', function() { - app.scrollToBottom(); - }); - + function updateTopicTitle() { if($(window).scrollTop() > 50) { $('.header-topic-title').find('span').text(ajaxify.variables.get('topic_name')).show(); } else { $('.header-topic-title').find('span').text('').hide(); } + } - $($('.posts > .post-row').get().reverse()).each(function() { - var el = $(this); - - if (elementInView(el)) { - var index = parseInt(el.attr('data-index'), 10) + 1; - if(index > Topic.postCount) { - index = Topic.postCount; - } + Topic.navigatorCallback = function(element) { - updatePaginationTextAndProgressBar(index); + var pid = element.attr('data-pid'); + console.log('derp', scrollingToPost, pid); + var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark'); - var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark'); - if (!currentBookmark || parseInt(el.attr('data-pid'), 10) >= parseInt(currentBookmark, 10)) { - localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', el.attr('data-pid')); - app.removeAlert('bookmark'); - } + if (!currentBookmark || parseInt(pid, 10) >= parseInt(currentBookmark, 10)) { + localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', pid); + app.removeAlert('bookmark'); + } - if (!scrollingToPost) { + if (!scrollingToPost) { - var newUrl = window.location.href.replace(window.location.hash, '') + '#' + el.attr('data-pid'); + var newUrl = window.location.href.replace(window.location.hash, '') + '#' + pid; - if (newUrl !== currentUrl) { - if (history.replaceState) { - history.replaceState({ - url: window.location.pathname.slice(1) + (window.location.search ? window.location.search : '' ) + '#' + el.attr('data-pid') - }, null, newUrl); - } - currentUrl = newUrl; - } + if (newUrl !== currentUrl) { + if (history.replaceState) { + history.replaceState({ + url: window.location.pathname.slice(1) + (window.location.search ? window.location.search : '' ) + '#' + pid + }, null, newUrl); } - - return false; + currentUrl = newUrl; } - }); - } - - function updatePaginationTextAndProgressBar(index) { - $('#pagination').html(index + ' out of ' + Topic.postCount); - $('.progress-bar').width((index / Topic.postCount * 100) + '%'); - } - - 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); - } + } + }; Topic.scrollToPost = function(pid, highlight, duration, offset) { if (!pid) { @@ -594,6 +556,8 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], offset = 0; } + scrollingToPost = true; + if($('#post_anchor_' + pid).length) { return scrollToPid(pid); } @@ -634,13 +598,11 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], tid = $('#post-container').attr('data-tid'); function animateScroll() { - scrollingToPost = true; - $("html, body").animate({ scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + "px" }, duration !== undefined ? duration : 400, function() { scrollingToPost = false; - updateHeader(); + navigator.update(); highlightPost(); }); } @@ -659,7 +621,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], if($('#post-container li.post-row[data-pid="' + pid + '"]').attr('data-index') !== '0') { animateScroll(); } else { - updateHeader(); + navigator.update(); highlightPost(); } } @@ -756,17 +718,17 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], function onNewPostsLoaded(html, posts) { function getPostPrivileges(pid) { - socket.emit('posts.getPrivileges', pid, function(err, privileges) { + socket.emit('posts.getPrivileges', pid, function(err, privileges) { if(err) { return app.alertError(err.message); } toggle_mod_tools(html, privileges); }); - } + } - for (var x = 0, numPosts = posts.length; x < numPosts; x++) { + for (var x = 0, numPosts = posts.length; x < numPosts; x++) { getPostPrivileges(posts[x].pid); - } + } infiniteLoaderActive = false; @@ -790,7 +752,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], if(!err) { Topic.postCount = postcount; $('#topic-post-count').html(Topic.postCount); - updateHeader(); + navigator.setCount(Topic.postCount); } }); } @@ -825,7 +787,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'], if (data && data.posts && data.posts.length) { createNewPosts(data, callback); } else { - updateHeader(); + navigator.update(); if (typeof callback === 'function') { callback(data.posts); } diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js new file mode 100644 index 0000000000..8a0aac833a --- /dev/null +++ b/public/src/modules/navigator.js @@ -0,0 +1,98 @@ + +'use strict'; + +/* globals app, define, ajaxify */ + + +define(function() { + + var navigator = {}; + var index = 1; + var count = 0; + + navigator.init = function(selector, count, callback) { + + navigator.selector = selector; + navigator.callback = callback; + + $(window).on('scroll', navigator.update); + + $('.pagination-block a').off('click').on('click', function() { + return false; + }); + + $('.pagination-block i:first').off('click').on('click', function() { + navigator.scrollToTop(); + }); + + $('.pagination-block i:last').off('click').on('click', function() { + navigator.scrollToBottom(); + }); + + navigator.setCount(count); + navigator.update(); + navigator.show(); + }; + + navigator.setCount = function(value) { + count = value; + navigator.updateTextAndProgressBar(); + }; + + navigator.show = function() { + $('.pagination-block').removeClass('hidden'); + }; + + navigator.hide = function() { + $('.pagination-block').addClass('hidden'); + }; + + navigator.update = function() { + $($(navigator.selector).get().reverse()).each(function() { + var el = $(this); + + if (elementInView(el)) { + index = parseInt(el.attr('data-index'), 10) + 1; + if(index > count) { + index = count; + } + + navigator.updateTextAndProgressBar(); + + if (typeof navigator.callback === 'function') { + navigator.callback(el); + } + + return false; + } + }); + }; + + navigator.updateTextAndProgressBar = function() { + $('#pagination').html(index + ' out of ' + count); + $('.progress-bar').width((index / count * 100) + '%'); + }; + + navigator.scrollToTop = function () { + $('body,html').animate({ + scrollTop: 0 + }); + }; + + navigator.scrollToBottom = function () { + $('body,html').animate({ + scrollTop: $('html').height() - 100 + }); + }; + + 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); + } + + return navigator; +}); \ No newline at end of file diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index eab02b41ee..0dd546ef16 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -43,4 +43,8 @@ SocketCategories.getPageCount = function(socket, cid, callback) { categories.getPageCount(cid, socket.uid, callback); }; +SocketCategories.getTopicCount = function(socket, cid, callback) { + categories.getCategoryField(cid, 'topic_count', callback); +}; + module.exports = SocketCategories; \ No newline at end of file