2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:05:26 +01:00
|
|
|
import * as message_viewport from "./message_viewport";
|
|
|
|
import * as rows from "./rows";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as unread_ops from "./unread_ops";
|
2021-02-28 00:42:00 +01:00
|
|
|
|
2018-05-26 12:36:24 +02:00
|
|
|
function go_to_row(msg_id) {
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.select_id(msg_id, {then_scroll: true, from_scroll: true});
|
2013-07-24 23:41:49 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function up() {
|
2018-08-04 08:33:00 +02:00
|
|
|
message_viewport.set_last_movement_direction(-1);
|
2021-03-30 02:21:21 +02:00
|
|
|
const msg_id = message_lists.current.prev();
|
2018-05-26 12:36:24 +02:00
|
|
|
if (msg_id === undefined) {
|
|
|
|
return;
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
2018-05-26 12:36:24 +02:00
|
|
|
go_to_row(msg_id);
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function down(with_centering) {
|
2018-08-04 08:33:00 +02:00
|
|
|
message_viewport.set_last_movement_direction(1);
|
2018-05-29 00:21:13 +02:00
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current.is_at_end()) {
|
2018-05-29 00:21:13 +02:00
|
|
|
if (with_centering) {
|
|
|
|
// At the last message, scroll to the bottom so we have
|
|
|
|
// lots of nice whitespace for new messages coming in.
|
2021-03-30 02:21:21 +02:00
|
|
|
const current_msg_table = rows.get_table(message_lists.current.table_name);
|
2020-07-15 00:34:28 +02:00
|
|
|
message_viewport.scrollTop(
|
|
|
|
current_msg_table.safeOuterHeight(true) - message_viewport.height() * 0.1,
|
|
|
|
);
|
2022-02-15 02:23:46 +01:00
|
|
|
unread_ops.process_scrolled_to_bottom();
|
2018-05-29 00:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal path starts here.
|
2021-03-30 02:21:21 +02:00
|
|
|
const msg_id = message_lists.current.next();
|
2018-05-26 12:36:24 +02:00
|
|
|
if (msg_id === undefined) {
|
|
|
|
return;
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
2018-05-26 12:36:24 +02:00
|
|
|
go_to_row(msg_id);
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function to_home() {
|
2018-08-04 08:33:00 +02:00
|
|
|
message_viewport.set_last_movement_direction(-1);
|
2021-03-30 02:21:21 +02:00
|
|
|
const first_id = message_lists.current.first().id;
|
|
|
|
message_lists.current.select_id(first_id, {then_scroll: true, from_scroll: true});
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function to_end() {
|
2021-03-30 02:21:21 +02:00
|
|
|
const next_id = message_lists.current.last().id;
|
2018-08-04 08:33:00 +02:00
|
|
|
message_viewport.set_last_movement_direction(1);
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.select_id(next_id, {then_scroll: true, from_scroll: true});
|
2022-02-15 02:23:46 +01:00
|
|
|
unread_ops.process_scrolled_to_bottom();
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2017-03-18 23:23:39 +01:00
|
|
|
function amount_to_paginate() {
|
|
|
|
// Some day we might have separate versions of this function
|
|
|
|
// for Page Up vs. Page Down, but for now it's the same
|
|
|
|
// strategy in either direction.
|
2019-11-02 00:06:25 +01:00
|
|
|
const info = message_viewport.message_viewport_info();
|
|
|
|
const page_size = info.visible_height;
|
2017-03-18 23:23:39 +01:00
|
|
|
|
|
|
|
// We don't want to page up a full page, because Zulip users
|
|
|
|
// are especially worried about missing messages, so we want
|
|
|
|
// a little bit of the old page to stay on the screen. The
|
|
|
|
// value chosen here is roughly 2 or 3 lines of text, but there
|
|
|
|
// is nothing sacred about it, and somebody more anal than me
|
|
|
|
// might wish to tie this to the size of some particular DOM
|
|
|
|
// element.
|
2019-11-02 00:06:25 +01:00
|
|
|
const overlap_amount = 55;
|
2017-03-18 23:23:39 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let delta = page_size - overlap_amount;
|
2017-03-18 23:23:39 +01:00
|
|
|
|
|
|
|
// If the user has shrunk their browser a whole lot, pagination
|
|
|
|
// is not going to be very pleasant, but we can at least
|
|
|
|
// ensure they go in the right direction.
|
|
|
|
if (delta < 1) {
|
|
|
|
delta = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function page_up_the_right_amount() {
|
2017-03-18 23:23:39 +01:00
|
|
|
// This function's job is to scroll up the right amount,
|
|
|
|
// after the user hits Page Up. We do this ourselves
|
|
|
|
// because we can't rely on the browser to account for certain
|
|
|
|
// page elements, like the compose box, that sit in fixed
|
|
|
|
// positions above the message pane. For other scrolling
|
2020-03-28 01:25:56 +01:00
|
|
|
// related adjustments, try to make those happen in the
|
2017-03-18 23:23:39 +01:00
|
|
|
// scroll handlers, not here.
|
2019-11-02 00:06:25 +01:00
|
|
|
const delta = amount_to_paginate();
|
2017-03-18 23:23:39 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() - delta);
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2017-03-18 23:23:39 +01:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function page_down_the_right_amount() {
|
2017-03-18 23:23:39 +01:00
|
|
|
// see also: page_up_the_right_amount
|
2019-11-02 00:06:25 +01:00
|
|
|
const delta = amount_to_paginate();
|
2017-03-18 23:23:39 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + delta);
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2017-03-18 23:23:39 +01:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function page_up() {
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_viewport.at_top() && !message_lists.current.empty()) {
|
|
|
|
message_lists.current.select_id(message_lists.current.first().id, {then_scroll: false});
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2021-02-28 01:05:26 +01:00
|
|
|
page_up_the_right_amount();
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function page_down() {
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_viewport.at_bottom() && !message_lists.current.empty()) {
|
|
|
|
message_lists.current.select_id(message_lists.current.last().id, {then_scroll: false});
|
2022-02-15 02:23:46 +01:00
|
|
|
unread_ops.process_scrolled_to_bottom();
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2021-02-28 01:05:26 +01:00
|
|
|
page_down_the_right_amount();
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2013-07-24 22:51:13 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function scroll_to_selected() {
|
2021-03-30 02:21:21 +02:00
|
|
|
const selected_row = message_lists.current.selected_row();
|
2018-06-06 18:50:09 +02:00
|
|
|
if (selected_row && selected_row.length !== 0) {
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.recenter_view(selected_row);
|
2016-05-25 13:26:57 +02:00
|
|
|
}
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2016-05-25 13:26:57 +02:00
|
|
|
|
2020-06-18 08:35:18 +02:00
|
|
|
let scroll_to_selected_planned = false;
|
2021-02-28 01:05:26 +01:00
|
|
|
|
|
|
|
export function plan_scroll_to_selected() {
|
2020-06-18 08:35:18 +02:00
|
|
|
scroll_to_selected_planned = true;
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|
2016-05-25 13:24:33 +02:00
|
|
|
|
2021-02-28 01:05:26 +01:00
|
|
|
export function maybe_scroll_to_selected() {
|
2020-06-18 08:35:18 +02:00
|
|
|
// If we have made a plan to scroll to the selected message but
|
|
|
|
// deferred doing so, do so here.
|
|
|
|
if (scroll_to_selected_planned) {
|
2021-02-28 01:05:26 +01:00
|
|
|
scroll_to_selected();
|
2020-06-18 08:35:18 +02:00
|
|
|
scroll_to_selected_planned = false;
|
2016-05-25 13:24:33 +02:00
|
|
|
}
|
2021-02-28 01:05:26 +01:00
|
|
|
}
|