From d5b36518a255d85349702d1400beb8cebdf9cada Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 25 Jun 2014 16:17:10 -0400 Subject: [PATCH] moved out scrollTo --- public/src/forum/topic.js | 102 +++++------------------------ public/src/forum/topic/scrollTo.js | 78 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 86 deletions(-) create mode 100644 public/src/forum/topic/scrollTo.js diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index e34ee275e2..1c500ee526 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -3,12 +3,20 @@ /* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH, utils */ -define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/threadTools', 'forum/topic/postTools', 'forum/topic/events', 'navigator'], function(pagination, infinitescroll, threadTools, postTools, events, navigator) { +var dependencies = [ + 'forum/pagination', + 'forum/infinitescroll', + 'forum/topic/threadTools', + 'forum/topic/postTools', + 'forum/topic/events', + 'forum/topic/scrollTo', + 'navigator' +]; + +define('forum/topic', dependencies, function(pagination, infinitescroll, threadTools, postTools, events, scrollTo, navigator) { var Topic = {}, - scrollingToPost = false, currentUrl = ''; - $(window).on('action:ajaxify.start', function(ev, data) { if(data.url.indexOf('topic') !== 0) { navigator.hide(); @@ -51,7 +59,7 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/ var bookmark = localStorage.getItem('topic:' + tid + ':bookmark'); var postIndex = getPostIndex(); if (postIndex) { - Topic.scrollToPost(postIndex - 1, true); + scrollTo.scrollToPost(postIndex - 1, true); } else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && postCount > 1) { app.alert({ alert_id: 'bookmark', @@ -59,7 +67,7 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/ timeout: 0, type: 'info', clickfn : function() { - Topic.scrollToPost(parseInt(bookmark, 10), true); + scrollTo.scrollToPost(parseInt(bookmark, 10), true); }, closefn : function() { localStorage.removeItem('topic:' + tid + ':bookmark'); @@ -177,7 +185,7 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/ app.removeAlert('bookmark'); } - if (!scrollingToPost) { + if (!scrollTo.active) { var parts = ajaxify.removeRelativePath(window.location.pathname.slice(1)).split('/'); var topicId = parts[1], slug = parts[2]; @@ -198,84 +206,6 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/ } }; - Topic.scrollToPost = function(postIndex, highlight, duration, offset) { - if (!utils.isNumber(postIndex)) { - return; - } - - if (!offset) { - offset = 0; - } - - scrollingToPost = true; - - if($('#post_anchor_' + postIndex).length) { - return scrollToPid(postIndex); - } - - if(config.usePagination) { - if (window.location.search.indexOf('page') !== -1) { - navigator.update(); - scrollingToPost = false; - return; - } - - var page = Math.ceil((postIndex + 1) / config.postsPerPage) - - if(parseInt(page, 10) !== pagination.currentPage) { - pagination.loadPage(page, function() { - scrollToPid(postIndex); - }); - } else { - scrollToPid(postIndex); - } - } else { - $('#post-container').empty(); - var after = postIndex - config.postsPerPage + 1; - if(after < 0) { - after = 0; - } - loadPostsAfter(after, function() { - scrollToPid(postIndex); - }); - } - - function scrollToPid(postIndex) { - var scrollTo = $('#post_anchor_' + postIndex), - tid = $('#post-container').attr('data-tid'); - - function animateScroll() { - $('html, body').animate({ - scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px' - }, duration !== undefined ? duration : 400, function() { - scrollingToPost = false; - navigator.update(); - highlightPost(); - $('body').scrollTop($('body').scrollTop() - 1); - $('html').scrollTop($('html').scrollTop() - 1); - }); - } - - function highlightPost() { - if (highlight) { - scrollTo.parent().find('.topic-item').addClass('highlight'); - setTimeout(function() { - scrollTo.parent().find('.topic-item').removeClass('highlight'); - }, 5000); - } - } - - if (tid && scrollTo.length) { - if($('#post-container li.post-row[data-index="' + postIndex + '"]').attr('data-index') !== '0') { - animateScroll(); - } else { - navigator.update(); - highlightPost(); - } - } - } - }; - function onNewPostPagination(data) { var posts = data.posts; socket.emit('topics.getPageCount', ajaxify.variables.get('topic_id'), function(err, newPageCount) { @@ -403,14 +333,14 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/ } function loadMorePosts(direction) { - if (!$('#post-container').length || scrollingToPost) { + if (!$('#post-container').length || scrollTo.active) { return; } infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, function(after, offset, el) { loadPostsAfter(after, function() { if (direction < 0 && el) { - Topic.scrollToPost(el.attr('data-index'), false, 0, offset); + scrollTo.scrollToPost(el.attr('data-index'), false, 0, offset); } }); }); diff --git a/public/src/forum/topic/scrollTo.js b/public/src/forum/topic/scrollTo.js new file mode 100644 index 0000000000..531a0ac6f3 --- /dev/null +++ b/public/src/forum/topic/scrollTo.js @@ -0,0 +1,78 @@ + +'use strict'; + +/* globals define, utils, config */ + +define('forum/topic/scrollTo', ['forum/topic/pagination', 'navigator'], function(pagination, navigator) { + + var ScrollTo = {}; + ScrollTo.active = false; + + ScrollTo.scrollToPost = function(postIndex, highlight, duration, offset) { + if (!utils.isNumber(postIndex)) { + return; + } + + offset = offset || 0; + duration = duration !== undefined ? duration : 400; + scrollTo.active = true; + + if($('#post_anchor_' + postIndex).length) { + return scrollToPid(postIndex, highlight, duration, offset); + } + + if(config.usePagination) { + if (window.location.search.indexOf('page') !== -1) { + navigator.update(); + scrollTo.active = false; + return; + } + + var page = Math.ceil((postIndex + 1) / config.postsPerPage); + + if(parseInt(page, 10) !== pagination.currentPage) { + pagination.loadPage(page, function() { + scrollToPid(postIndex, highlight, duration, offset); + }); + } else { + scrollToPid(postIndex, highlight, duration, offset); + } + } + }; + + function scrollToPid(postIndex, highlight, duration, offset) { + var scrollTo = $('#post_anchor_' + postIndex); + + function animateScroll() { + $('html, body').animate({ + scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px' + }, duration, function() { + scrollTo.active = false; + navigator.update(); + highlightPost(); + $('body').scrollTop($('body').scrollTop() - 1); + $('html').scrollTop($('html').scrollTop() - 1); + }); + } + + function highlightPost() { + if (highlight) { + scrollTo.parent().find('.topic-item').addClass('highlight'); + setTimeout(function() { + scrollTo.parent().find('.topic-item').removeClass('highlight'); + }, 5000); + } + } + + if ($('#post-container').length && scrollTo.length) { + if($('#post-container li.post-row[data-index="' + postIndex + '"]').attr('data-index') !== '0') { + animateScroll(); + } else { + navigator.update(); + highlightPost(); + } + } + } + + return ScrollTo; +}); \ No newline at end of file