You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/public/src/modules/navigator.js

232 lines
5.8 KiB
JavaScript

11 years ago
'use strict';
/* globals app, define, ajaxify, utils, translator, config */
11 years ago
11 years ago
define('navigator', ['forum/pagination'], function(pagination) {
11 years ago
var navigator = {};
var index = 1;
var count = 0;
navigator.scrollActive = false;
11 years ago
navigator.init = function(selector, count, toTop, toBottom, callback, calculateIndex) {
index = 1;
11 years ago
navigator.selector = selector;
navigator.callback = callback;
11 years ago
toTop = toTop || function() {};
toBottom = toBottom || function() {};
11 years ago
$(window).on('scroll', navigator.update);
$('.pagination-block .dropdown-menu').off('click').on('click', function(e) {
e.stopPropagation();
11 years ago
});
11 years ago
$('.pagination-block').off('shown.bs.dropdown', '.dropdown').on('shown.bs.dropdown', '.dropdown', function() {
setTimeout(function() {
$('.pagination-block input').focus();
}, 100);
});
11 years ago
$('.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;
}
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);
}
});
11 years ago
navigator.setCount(count);
navigator.update();
};
function generateUrl(index) {
var parts = window.location.pathname.split('/');
return parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : '');
}
11 years ago
navigator.setCount = function(value) {
11 years ago
count = parseInt(value, 10);
11 years ago
navigator.updateTextAndProgressBar();
};
navigator.show = function() {
11 years ago
toggle(true);
11 years ago
};
navigator.hide = function() {
11 years ago
toggle(false);
11 years ago
};
11 years ago
function toggle(flag) {
11 years ago
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
if (flag && (!path.startsWith('topic') && !path.startsWith('category'))) {
return;
}
11 years ago
$('.pagination-block').toggleClass('hidden', !flag);
}
11 years ago
navigator.update = function() {
11 years ago
toggle(!!count);
11 years ago
$($(navigator.selector).get().reverse()).each(function() {
var el = $(this);
if (elementInView(el)) {
if (typeof navigator.callback === 'function') {
index = navigator.callback(el, count);
navigator.updateTextAndProgressBar();
11 years ago
}
return false;
}
});
};
navigator.updateTextAndProgressBar = function() {
index = index > count ? count : index;
11 years ago
11 years ago
$('#pagination').translateHtml('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
11 years ago
$('.pagination-block .progress-bar').width((index / count * 100) + '%');
11 years ago
};
11 years ago
navigator.scrollUp = function () {
11 years ago
$('body,html').animate({
11 years ago
scrollTop: $(window).scrollTop() - $(window).height()
11 years ago
});
};
11 years ago
navigator.scrollDown = function () {
11 years ago
$('body,html').animate({
11 years ago
scrollTop: $(window).scrollTop() + $(window).height()
11 years ago
});
};
11 years ago
navigator.scrollTop = function(index) {
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollToPost(index, true);
11 years ago
} else {
ajaxify.go(generateUrl());
}
};
navigator.scrollBottom = function(index) {
11 years ago
if (parseInt(index, 10) < 0) {
return;
}
11 years ago
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollToPost(index, true);
11 years ago
} else {
10 years ago
index = parseInt(index, 10);
11 years ago
ajaxify.go(generateUrl(index));
}
};
11 years ago
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) {
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);
}
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);
}
} else {
navigator.scrollActive = false;
11 years ago
postIndex = parseInt(postIndex, 10) + 1;
ajaxify.go(generateUrl(postIndex));
}
};
function scrollToPid(postIndex, highlight, duration, offset) {
var scrollTo = $('#post_anchor_' + postIndex);
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);
}
}
if ($('#post-container').length) {
animateScroll();
}
}
11 years ago
return navigator;
});