Files
NodeBB/public/src/modules/navigator.js

233 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-04-04 12:42:41 -04:00
'use strict';
/* globals app, define, ajaxify, utils, translator, config */
2014-04-04 12:42:41 -04:00
2014-07-24 18:44:57 -04:00
define('navigator', ['forum/pagination'], function(pagination) {
2014-04-04 12:42:41 -04:00
var navigator = {};
var index = 1;
var count = 0;
navigator.scrollActive = false;
2014-04-04 12:42:41 -04:00
2014-08-16 21:33:42 -04:00
navigator.init = function(selector, count, toTop, toBottom, callback, calculateIndex) {
index = 1;
2014-04-04 12:42:41 -04:00
navigator.selector = selector;
navigator.callback = callback;
2014-06-27 15:35:53 -04:00
toTop = toTop || function() {};
toBottom = toBottom || function() {};
2014-04-04 12:42:41 -04:00
$(window).on('scroll', navigator.update);
$('.pagination-block .dropdown-menu').off('click').on('click', function(e) {
e.stopPropagation();
2014-04-04 12:42:41 -04:00
});
2014-09-06 23:57:51 -04:00
$('.pagination-block').off('shown.bs.dropdown', '.dropdown').on('shown.bs.dropdown', '.dropdown', function() {
setTimeout(function() {
$('.pagination-block input').focus();
}, 100);
});
2014-06-27 15:35:53 -04:00
$('.pagination-block .pageup').off('click').on('click', navigator.scrollUp);
$('.pagination-block .pagedown').off('click').on('click', navigator.scrollDown);
$('.pagination-block .pagetop').off('click').on('click', toTop);
$('.pagination-block .pagebottom').off('click').on('click', toBottom);
$('.pagination-block input').on('keydown', function(e) {
if (e.which === 13) {
var input = $(this);
if (!utils.isNumber(input.val())) {
input.val('');
return;
}
2014-08-16 21:33:42 -04:00
var index = parseInt(input.val(), 10);
if (typeof calculateIndex === 'function') {
index = calculateIndex(index, count);
}
var url = generateUrl(index);
input.val('');
$('.pagination-block .dropdown-toggle').trigger('click');
ajaxify.go(url);
}
});
2014-04-04 12:42:41 -04:00
navigator.setCount(count);
navigator.update();
};
function generateUrl(index) {
2015-01-12 18:52:41 -05:00
var pathname = window.location.pathname.replace(config.relative_path, '');
var parts = pathname.split('/');
return parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : '');
}
2014-04-04 12:42:41 -04:00
navigator.setCount = function(value) {
2014-04-04 16:18:51 -04:00
count = parseInt(value, 10);
2014-04-04 12:42:41 -04:00
navigator.updateTextAndProgressBar();
};
navigator.show = function() {
2014-04-04 16:18:51 -04:00
toggle(true);
2014-04-04 12:42:41 -04:00
};
navigator.hide = function() {
2014-04-04 16:18:51 -04:00
toggle(false);
2014-04-04 12:42:41 -04:00
};
2014-04-04 16:18:51 -04:00
function toggle(flag) {
2014-09-06 23:57:51 -04:00
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
if (flag && (!path.startsWith('topic') && !path.startsWith('category'))) {
return;
}
2014-04-04 16:18:51 -04:00
$('.pagination-block').toggleClass('hidden', !flag);
}
2014-04-04 12:42:41 -04:00
navigator.update = function() {
2014-04-04 16:18:51 -04:00
toggle(!!count);
2014-04-04 12:42:41 -04:00
$($(navigator.selector).get().reverse()).each(function() {
var el = $(this);
if (elementInView(el)) {
if (typeof navigator.callback === 'function') {
2014-08-16 21:33:42 -04:00
index = navigator.callback(el, count);
navigator.updateTextAndProgressBar();
2014-04-04 12:42:41 -04:00
}
return false;
}
});
};
navigator.updateTextAndProgressBar = function() {
2014-05-28 13:13:29 -04:00
index = index > count ? count : index;
2014-05-08 14:27:46 -04:00
2014-07-05 16:58:57 -04:00
$('#pagination').translateHtml('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
2014-05-07 16:11:58 -04:00
$('.pagination-block .progress-bar').width((index / count * 100) + '%');
2014-04-04 12:42:41 -04:00
};
2014-06-27 15:35:53 -04:00
navigator.scrollUp = function () {
2014-04-04 12:42:41 -04:00
$('body,html').animate({
2014-10-08 15:06:35 -04:00
scrollTop: $(window).scrollTop() - $(window).height()
2014-04-04 12:42:41 -04:00
});
};
2014-06-27 15:35:53 -04:00
navigator.scrollDown = function () {
2014-04-04 12:42:41 -04:00
$('body,html').animate({
2014-10-08 15:06:35 -04:00
scrollTop: $(window).scrollTop() + $(window).height()
2014-04-04 12:42:41 -04:00
});
};
2014-06-27 15:35:53 -04:00
navigator.scrollTop = function(index) {
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollToPost(index, true);
2014-06-27 15:35:53 -04:00
} else {
ajaxify.go(generateUrl());
}
};
navigator.scrollBottom = function(index) {
2014-10-08 15:06:35 -04:00
if (parseInt(index, 10) < 0) {
return;
}
2014-06-27 15:35:53 -04:00
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollToPost(index, true);
2014-06-27 15:35:53 -04:00
} else {
2015-02-25 17:59:59 -05:00
index = parseInt(index, 10) + 1;
2014-06-27 15:35:53 -04:00
ajaxify.go(generateUrl(index));
}
};
2014-04-04 12:42:41 -04:00
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) {
2014-10-31 17:44:06 -04:00
if (!utils.isNumber(postIndex) || !$('#post-container').length) {
return;
}
offset = offset || 0;
duration = duration !== undefined ? duration : 400;
navigator.scrollActive = true;
if($('#post_anchor_' + postIndex).length) {
return scrollToPid(postIndex, highlight, duration, offset);
}
2014-08-16 21:33:42 -04:00
if (config.usePagination) {
if (window.location.search.indexOf('page') !== -1) {
navigator.update();
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);
}
2014-08-16 21:33:42 -04:00
} else {
navigator.scrollActive = false;
2014-10-09 20:47:27 -04:00
postIndex = parseInt(postIndex, 10) + 1;
ajaxify.go(generateUrl(postIndex));
}
};
function scrollToPid(postIndex, highlight, duration, offset) {
var scrollTo = $('#post_anchor_' + postIndex);
2014-08-16 21:33:42 -04:00
if (!scrollTo) {
navigator.scrollActive = false;
return;
}
var done = false;
function animateScroll() {
$('html, body').animate({
scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px'
}, duration, function() {
if (done) {
return;
}
done = true;
navigator.scrollActive = 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');
}, 3000);
}
}
2014-08-16 21:33:42 -04:00
if ($('#post-container').length) {
animateScroll();
}
}
2014-04-04 12:42:41 -04:00
return navigator;
2014-04-10 20:31:57 +01:00
});