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

141 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-04-04 12:42:41 -04:00
'use strict';
/* globals app, define, ajaxify, utils, translator */
2014-04-04 12:42:41 -04:00
define('navigator', function() {
2014-04-04 12:42:41 -04:00
var navigator = {};
var index = 1;
var count = 0;
2014-06-27 15:35:53 -04:00
navigator.init = function(selector, count, callback, toTop, toBottom) {
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-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;
}
var url = generateUrl(input.val());
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) {
var parts = window.location.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) {
$('.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)) {
index = parseInt(el.attr('data-index'), 10) + 1;
navigator.updateTextAndProgressBar();
if (typeof navigator.callback === 'function') {
navigator.callback(el);
}
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
translator.translate('[[global:pagination.out_of, ' + index + ', ' + count + ']]', function(translated) {
$('#pagination').html(translated);
});
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({
scrollTop: 0
});
};
2014-06-27 15:35:53 -04:00
navigator.scrollDown = function () {
2014-04-04 12:42:41 -04:00
$('body,html').animate({
scrollTop: $('html').height() - 100
});
};
2014-06-27 15:35:53 -04:00
navigator.scrollTop = function(index) {
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollUp();
} else {
ajaxify.go(generateUrl());
}
};
navigator.scrollBottom = function(index) {
if ($('li[data-index="' + index + '"]').length) {
navigator.scrollDown();
} else {
index = parseInt(index, 10) + 1;
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);
}
return navigator;
2014-04-10 20:31:57 +01:00
});