This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/offset-calculator.js.es6
2018-03-13 17:39:53 +11:00

51 lines
1.5 KiB
JavaScript

export function scrollTopFor(y) {
return y - offsetCalculator(y);
}
export default function offsetCalculator(y) {
const $header = $('header');
const $container = $('.posts-wrapper');
const containerOffset = $container.offset();
let titleHeight = 0;
const scrollTop = y || $(window).scrollTop();
if (!containerOffset || scrollTop < containerOffset.top) {
titleHeight = $('#topic-title').height() || 0;
}
const rawWinHeight = $(window).height();
const windowHeight = rawWinHeight - titleHeight;
const eyeTarget = (windowHeight / 10);
const headerHeight = $header.outerHeight(true);
const expectedOffset = titleHeight - ($header.find('.contents').height() || 0) + (eyeTarget * 2);
const ideal = headerHeight + ((expectedOffset < 0) ? 0 : expectedOffset);
if ($container.length === 0) { return expectedOffset; }
const topPos = $container.offset().top;
const docHeight = $(document).height();
let scrollPercent = Math.min((scrollTop / (docHeight-rawWinHeight)), 1.0);
let inter = topPos - scrollTop + ($container.height() * scrollPercent);
if (inter < headerHeight + eyeTarget) {
inter = headerHeight + eyeTarget;
}
if (inter > ideal) {
const bottom = $('#topic-bottom').offset().top;
const switchPos = bottom - rawWinHeight - ideal;
if (scrollTop > switchPos) {
const p = Math.max(Math.min((scrollTop + inter - switchPos) / rawWinHeight, 1.0), 0.0);
return ((1 - p) * ideal) + (p * inter);
} else {
return ideal;
}
}
return inter;
}