2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
|
|
|
|
2021-02-28 00:37:30 +01:00
|
|
|
export function scroll_delta(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;
|
2021-02-28 00:37:30 +01:00
|
|
|
}
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function scroll_element_into_container($elem, $container) {
|
2018-04-24 14:09:16 +02:00
|
|
|
// 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.
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container = ui.get_scroll_element($container);
|
|
|
|
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,
|
2022-01-25 11:36:19 +01:00
|
|
|
container_height: $container.height(),
|
2018-04-24 14:09:16 +02:00
|
|
|
};
|
|
|
|
|
2021-02-28 00:37:30 +01:00
|
|
|
const delta = scroll_delta(opts);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
|
|
|
if (delta === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.scrollTop($container.scrollTop() + delta);
|
2021-02-28 00:37:30 +01:00
|
|
|
}
|