2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-24 14:09:16 +02:00
|
|
|
exports.scroll_delta = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem_top = opts.elem_top;
|
|
|
|
const container_height = opts.container_height;
|
|
|
|
const elem_bottom = opts.elem_bottom;
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let delta = 0;
|
2018-04-24 14:09:16 +02:00
|
|
|
|
|
|
|
if (elem_top < 0) {
|
2020-07-15 00:34:28 +02:00
|
|
|
delta = Math.max(elem_top, elem_bottom - container_height);
|
2018-04-24 14:09:16 +02:00
|
|
|
delta = Math.min(0, delta);
|
|
|
|
} else {
|
|
|
|
if (elem_bottom > container_height) {
|
2020-07-15 00:34:28 +02:00
|
|
|
delta = Math.min(elem_top, elem_bottom - container_height);
|
2018-04-24 14:09:16 +02:00
|
|
|
delta = Math.max(0, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.scroll_element_into_container = function (elem, container) {
|
|
|
|
// This does the minimum amount of scrolling that is needed to make
|
|
|
|
// the element visible. It doesn't try to center the element, so
|
|
|
|
// this will be non-intrusive to users when they already have
|
|
|
|
// the element visible.
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
container = ui.get_scroll_element(container);
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem_top = elem.position().top;
|
|
|
|
const elem_bottom = elem_top + elem.innerHeight();
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2020-07-20 22:18:43 +02:00
|
|
|
elem_top,
|
|
|
|
elem_bottom,
|
2018-04-24 14:09:16 +02:00
|
|
|
container_height: container.height(),
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const delta = exports.scroll_delta(opts);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
|
|
|
if (delta === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
container.scrollTop(container.scrollTop() + delta);
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.scroll_util = exports;
|