2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-04-08 07:09:54 +02:00
|
|
|
import {media_breakpoints_num} from "./css_variables";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 00:59:17 +01:00
|
|
|
import * as message_scroll from "./message_scroll";
|
2021-04-07 14:08:09 +02:00
|
|
|
import * as notifications from "./notifications";
|
|
|
|
import * as overlays from "./overlays";
|
2021-02-28 00:42:30 +01:00
|
|
|
import * as rows from "./rows";
|
2021-02-28 00:42:00 +01:00
|
|
|
import * as util from "./util";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export let message_pane;
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let jwindow;
|
|
|
|
const dimensions = {};
|
|
|
|
let in_stoppable_autoscroll = false;
|
2013-05-07 17:15:20 +02:00
|
|
|
|
2021-02-28 01:44:26 +01:00
|
|
|
function make_dimen_wrapper(dimen_name, dimen_func) {
|
|
|
|
dimensions[dimen_name] = new util.CachedValue({
|
|
|
|
compute_value() {
|
2021-02-28 00:42:00 +01:00
|
|
|
return dimen_func.call(message_pane);
|
2021-02-28 01:44:26 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return function viewport_dimension_wrapper(...args) {
|
|
|
|
if (args.length !== 0) {
|
|
|
|
dimensions[dimen_name].reset();
|
2021-02-28 00:42:00 +01:00
|
|
|
return dimen_func.apply(message_pane, args);
|
2021-02-28 01:44:26 +01:00
|
|
|
}
|
|
|
|
return dimensions[dimen_name].get();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export const height = make_dimen_wrapper("height", $(message_pane).height);
|
|
|
|
export const width = make_dimen_wrapper("width", $(message_pane).width);
|
2021-02-28 01:44:26 +01:00
|
|
|
|
2016-05-25 13:02:38 +02:00
|
|
|
// Includes both scroll and arrow events. Negative means scroll up,
|
|
|
|
// positive means scroll down.
|
2021-02-28 00:42:00 +01:00
|
|
|
export let last_movement_direction = 1;
|
2016-05-25 13:02:38 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function set_last_movement_direction(value) {
|
|
|
|
last_movement_direction = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function at_top() {
|
|
|
|
return scrollTop() <= 0;
|
|
|
|
}
|
2013-05-28 22:36:48 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function message_viewport_info() {
|
2013-06-06 16:10:12 +02:00
|
|
|
// Return a structure that tells us details of the viewport
|
|
|
|
// accounting for fixed elements like the top navbar.
|
|
|
|
//
|
|
|
|
// The message_header is NOT considered to be part of the visible
|
|
|
|
// message pane, which should make sense for callers, who will
|
|
|
|
// generally be concerned about whether actual message content is
|
|
|
|
// visible.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = {};
|
2013-06-06 16:10:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const element_just_above_us = $(".floating_recipient");
|
|
|
|
const element_just_below_us = $("#compose");
|
2013-06-06 16:10:12 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
res.visible_top = element_just_above_us.offset().top + element_just_above_us.safeOuterHeight();
|
2013-06-06 16:10:12 +02:00
|
|
|
|
2019-01-06 17:29:14 +01:00
|
|
|
res.visible_bottom = element_just_below_us.position().top;
|
2013-06-06 16:10:12 +02:00
|
|
|
|
2019-01-06 17:29:14 +01:00
|
|
|
res.visible_height = res.visible_bottom - res.visible_top;
|
2013-06-06 16:10:12 +02:00
|
|
|
|
|
|
|
return res;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-06-06 16:10:12 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function at_bottom() {
|
|
|
|
const bottom = scrollTop() + height();
|
|
|
|
const full_height = message_pane.prop("scrollHeight");
|
2013-05-28 22:36:48 +02:00
|
|
|
|
2013-05-30 00:42:38 +02:00
|
|
|
// We only know within a pixel or two if we're
|
|
|
|
// exactly at the bottom, due to browser quirkiness,
|
|
|
|
// and we err on the side of saying that we are at
|
|
|
|
// the bottom.
|
2014-01-28 17:50:29 +01:00
|
|
|
return bottom + 2 >= full_height;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-05-28 22:36:48 +02:00
|
|
|
|
2013-07-26 19:14:33 +02:00
|
|
|
// This differs from at_bottom in that it only requires the bottom message to
|
|
|
|
// be visible, but you may be able to scroll down further.
|
2021-02-28 00:42:00 +01:00
|
|
|
export function bottom_message_visible() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_row = rows.last_visible();
|
2013-07-26 19:14:33 +02:00
|
|
|
if (last_row.length) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_bottom = last_row[0].getBoundingClientRect().bottom;
|
|
|
|
const bottom_of_feed = $("#compose")[0].getBoundingClientRect().top;
|
2013-07-26 19:14:33 +02:00
|
|
|
return bottom_of_feed > message_bottom;
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return false;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-07-26 19:14:33 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function is_below_visible_bottom(offset) {
|
|
|
|
return offset > scrollTop() + height() - $("#compose").height();
|
|
|
|
}
|
2013-06-06 23:52:02 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function is_scrolled_up() {
|
2019-01-06 17:44:48 +01:00
|
|
|
// Let's determine whether the user was already dealing
|
|
|
|
// with messages off the screen, which can guide auto
|
|
|
|
// scrolling decisions.
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_row = rows.last_visible();
|
2019-01-06 17:44:48 +01:00
|
|
|
if (last_row.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
const offset = offset_from_bottom(last_row);
|
2019-01-06 17:44:48 +01:00
|
|
|
|
|
|
|
return offset > 0;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2019-01-06 17:44:48 +01:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function offset_from_bottom(last_row) {
|
2019-01-06 17:44:48 +01:00
|
|
|
// A positive return value here means the last row is
|
|
|
|
// below the bottom of the feed (i.e. obscured by the compose
|
|
|
|
// box or even further below the bottom).
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_bottom = last_row.offset().top + last_row.height();
|
2021-02-28 00:42:00 +01:00
|
|
|
const info = message_viewport_info();
|
2019-01-06 17:44:48 +01:00
|
|
|
|
|
|
|
return message_bottom - info.visible_bottom;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2019-01-06 17:44:48 +01:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function set_message_position(message_top, message_height, viewport_info, ratio) {
|
2013-05-30 20:39:28 +02:00
|
|
|
// message_top = offset of the top of a message that you are positioning
|
2013-06-05 17:28:10 +02:00
|
|
|
// message_height = height of the message that you are positioning
|
2017-03-10 23:48:51 +01:00
|
|
|
// viewport_info = result of calling message_viewport.message_viewport_info
|
2013-05-30 20:39:28 +02:00
|
|
|
// ratio = fraction indicating how far down the screen the msg should be
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let how_far_down_in_visible_page = viewport_info.visible_height * ratio;
|
2013-06-05 17:28:10 +02:00
|
|
|
|
|
|
|
// special case: keep large messages fully on the screen
|
|
|
|
if (how_far_down_in_visible_page + message_height > viewport_info.visible_height) {
|
|
|
|
how_far_down_in_visible_page = viewport_info.visible_height - message_height;
|
|
|
|
|
|
|
|
// Next handle truly gigantic messages. We just say that the top of the
|
|
|
|
// message goes to the top of the viewing area. Realistically, gigantic
|
|
|
|
// messages should either be condensed, socially frowned upon, or scrolled
|
|
|
|
// with the mouse.
|
|
|
|
if (how_far_down_in_visible_page < 0) {
|
|
|
|
how_far_down_in_visible_page = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
const hidden_top = viewport_info.visible_top - scrollTop();
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const message_offset = how_far_down_in_visible_page + hidden_top;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const new_scroll_top = message_top - message_offset;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2020-06-18 08:29:08 +02:00
|
|
|
message_scroll.suppress_selection_update_on_next_scroll();
|
2021-02-28 00:42:00 +01:00
|
|
|
scrollTop(new_scroll_top);
|
|
|
|
}
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function in_viewport_or_tall(rect, top_of_feed, bottom_of_feed, require_fully_visible) {
|
2013-10-30 19:53:17 +01:00
|
|
|
if (require_fully_visible) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
rect.top > top_of_feed && // Message top is in view and
|
|
|
|
(rect.bottom < bottom_of_feed || // message is fully in view or
|
|
|
|
(rect.height > bottom_of_feed - top_of_feed && rect.top < bottom_of_feed))
|
|
|
|
); // message is tall.
|
2013-10-30 19:53:17 +01:00
|
|
|
}
|
2018-06-06 18:19:09 +02:00
|
|
|
return rect.bottom > top_of_feed && rect.top < bottom_of_feed;
|
2013-07-12 20:22:27 +02:00
|
|
|
}
|
2013-07-05 17:03:14 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function add_to_visible(
|
|
|
|
candidates,
|
|
|
|
visible,
|
|
|
|
top_of_feed,
|
|
|
|
bottom_of_feed,
|
|
|
|
require_fully_visible,
|
|
|
|
row_to_id,
|
|
|
|
) {
|
2020-02-08 05:17:57 +01:00
|
|
|
for (const row of candidates) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row_rect = row.getBoundingClientRect();
|
2013-07-12 20:22:27 +02:00
|
|
|
// Mark very tall messages as read once we've gotten past them
|
2020-07-15 00:34:28 +02:00
|
|
|
if (in_viewport_or_tall(row_rect, top_of_feed, bottom_of_feed, require_fully_visible)) {
|
2014-02-26 17:22:06 +01:00
|
|
|
visible.push(row_to_id(row));
|
2020-02-08 05:17:57 +01:00
|
|
|
} else {
|
|
|
|
break;
|
2013-07-12 20:22:27 +02:00
|
|
|
}
|
2020-02-08 05:17:57 +01:00
|
|
|
}
|
2013-07-12 20:22:27 +02:00
|
|
|
}
|
2013-07-03 21:51:59 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const top_of_feed = new util.CachedValue({
|
2020-07-20 22:18:43 +02:00
|
|
|
compute_value() {
|
2017-07-27 21:40:26 +02:00
|
|
|
return $(".floating_recipient").offset().top + $(".floating_recipient").safeOuterHeight();
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-07-31 23:28:42 +02:00
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bottom_of_feed = new util.CachedValue({
|
2020-07-20 22:18:43 +02:00
|
|
|
compute_value() {
|
2013-07-31 23:28:42 +02:00
|
|
|
return $("#compose")[0].getBoundingClientRect().top;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-07-31 23:28:42 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function _visible_divs(
|
|
|
|
selected_row,
|
|
|
|
row_min_height,
|
|
|
|
row_to_output,
|
|
|
|
div_class,
|
|
|
|
require_fully_visible,
|
|
|
|
) {
|
2013-07-12 20:22:27 +02:00
|
|
|
// Note that when using getBoundingClientRect() we are getting offsets
|
|
|
|
// relative to the visible window, but when using jQuery's offset() we are
|
|
|
|
// getting offsets relative to the full scrollable window. You can't try to
|
|
|
|
// compare heights from these two methods.
|
2019-11-02 00:06:25 +01:00
|
|
|
const height = bottom_of_feed.get() - top_of_feed.get();
|
|
|
|
const num_neighbors = Math.floor(height / row_min_height);
|
2013-07-11 17:14:11 +02:00
|
|
|
|
2013-07-12 20:22:27 +02:00
|
|
|
// We do this explicitly without merges and without recalculating
|
|
|
|
// the feed bounds to keep this computation as cheap as possible.
|
2019-11-02 00:06:25 +01:00
|
|
|
const visible = [];
|
|
|
|
const above_pointer = selected_row.prevAll("div." + div_class).slice(0, num_neighbors);
|
|
|
|
const below_pointer = selected_row.nextAll("div." + div_class).slice(0, num_neighbors);
|
2020-07-15 00:34:28 +02:00
|
|
|
add_to_visible(
|
|
|
|
selected_row,
|
|
|
|
visible,
|
|
|
|
top_of_feed.get(),
|
|
|
|
bottom_of_feed.get(),
|
|
|
|
require_fully_visible,
|
|
|
|
row_to_output,
|
|
|
|
);
|
|
|
|
add_to_visible(
|
|
|
|
above_pointer,
|
|
|
|
visible,
|
|
|
|
top_of_feed.get(),
|
|
|
|
bottom_of_feed.get(),
|
|
|
|
require_fully_visible,
|
|
|
|
row_to_output,
|
|
|
|
);
|
|
|
|
add_to_visible(
|
|
|
|
below_pointer,
|
|
|
|
visible,
|
|
|
|
top_of_feed.get(),
|
|
|
|
bottom_of_feed.get(),
|
|
|
|
require_fully_visible,
|
|
|
|
row_to_output,
|
|
|
|
);
|
2014-02-26 17:22:06 +01:00
|
|
|
|
|
|
|
return visible;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function visible_groups(require_fully_visible) {
|
2021-03-30 02:21:21 +02:00
|
|
|
const selected_row = message_lists.current.selected_row();
|
2014-02-26 17:22:06 +01:00
|
|
|
if (selected_row === undefined || selected_row.length === 0) {
|
2014-02-26 20:42:30 +01:00
|
|
|
return [];
|
2014-02-26 17:22:06 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_group = rows.get_message_recipient_row(selected_row);
|
2014-02-26 17:22:06 +01:00
|
|
|
|
|
|
|
function get_row(row) {
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Being simplistic about this, the smallest group is about 75 px high.
|
|
|
|
return _visible_divs(selected_group, 75, get_row, "recipient_row", require_fully_visible);
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2014-02-26 17:22:06 +01:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function visible_messages(require_fully_visible) {
|
2021-03-30 02:21:21 +02:00
|
|
|
const selected_row = message_lists.current.selected_row();
|
2014-02-26 17:22:06 +01:00
|
|
|
|
|
|
|
function row_to_id(row) {
|
2021-03-30 02:21:21 +02:00
|
|
|
return message_lists.current.get(rows.id($(row)));
|
2014-02-26 17:22:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Being simplistic about this, the smallest message is 25 px high.
|
|
|
|
return _visible_divs(selected_row, 25, row_to_id, "message_row", require_fully_visible);
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-07-11 17:14:11 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function scrollTop(target_scrollTop) {
|
|
|
|
const orig_scrollTop = message_pane.scrollTop();
|
2013-11-27 16:52:21 +01:00
|
|
|
if (target_scrollTop === undefined) {
|
2013-11-26 20:47:59 +01:00
|
|
|
return orig_scrollTop;
|
|
|
|
}
|
2021-02-28 00:42:00 +01:00
|
|
|
let ret = message_pane.scrollTop(target_scrollTop);
|
|
|
|
const new_scrollTop = message_pane.scrollTop();
|
|
|
|
const space_to_scroll = $("#bottom_whitespace").offset().top - height();
|
2013-11-26 20:47:59 +01:00
|
|
|
|
|
|
|
// Check whether our scrollTop didn't move even though one could have scrolled down
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
space_to_scroll > 0 &&
|
|
|
|
target_scrollTop > 0 &&
|
|
|
|
orig_scrollTop === 0 &&
|
|
|
|
new_scrollTop === 0
|
|
|
|
) {
|
2013-11-26 20:47:59 +01:00
|
|
|
// Chrome has a bug where sometimes calling
|
|
|
|
// window.scrollTop(x) has no effect, resulting in the browser
|
|
|
|
// staying at 0 -- and afterwards if you call
|
|
|
|
// window.scrollTop(x) again, it will still do nothing. To
|
|
|
|
// fix this, we need to first scroll to some other place.
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.info(
|
|
|
|
"ScrollTop did nothing when scrolling to " + target_scrollTop + ", fixing...",
|
|
|
|
);
|
2013-11-26 20:47:59 +01:00
|
|
|
// First scroll to 1 in order to clear the stuck state
|
2021-02-28 00:42:00 +01:00
|
|
|
message_pane.scrollTop(1);
|
2013-11-26 20:47:59 +01:00
|
|
|
// And then scroll where we intended to scroll to
|
2021-02-28 00:42:00 +01:00
|
|
|
ret = message_pane.scrollTop(target_scrollTop);
|
|
|
|
if (message_pane.scrollTop() === 0) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.info(
|
|
|
|
"ScrollTop fix did not work when scrolling to " +
|
|
|
|
target_scrollTop +
|
|
|
|
"! space_to_scroll was " +
|
|
|
|
space_to_scroll,
|
|
|
|
);
|
2013-11-26 20:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-27 16:52:21 +01:00
|
|
|
return ret;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-05-07 17:15:20 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function stop_auto_scrolling() {
|
2013-05-24 19:53:25 +02:00
|
|
|
if (in_stoppable_autoscroll) {
|
2021-02-28 00:42:00 +01:00
|
|
|
message_pane.stop();
|
2013-05-24 19:53:25 +02:00
|
|
|
}
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-05-24 19:53:25 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function is_narrow() {
|
2013-12-12 19:13:37 +01:00
|
|
|
// This basically returns true when we hide the right sidebar for
|
2015-09-20 08:39:54 +02:00
|
|
|
// the left_side_userlist skinny mode. It would be nice to have a less brittle
|
2021-01-25 14:58:46 +01:00
|
|
|
// test for this.
|
2021-04-08 07:09:54 +02:00
|
|
|
return window.innerWidth < media_breakpoints_num.xl;
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-12-12 19:13:37 +01:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function system_initiated_animate_scroll(scroll_amount) {
|
2020-06-18 08:29:08 +02:00
|
|
|
message_scroll.suppress_selection_update_on_next_scroll();
|
2021-02-28 00:42:00 +01:00
|
|
|
const viewport_offset = scrollTop();
|
2013-05-31 19:59:24 +02:00
|
|
|
in_stoppable_autoscroll = true;
|
2021-02-28 00:42:00 +01:00
|
|
|
message_pane.animate({
|
2013-05-24 19:53:25 +02:00
|
|
|
scrollTop: viewport_offset + scroll_amount,
|
2020-07-20 22:18:43 +02:00
|
|
|
always() {
|
2013-05-24 19:53:25 +02:00
|
|
|
in_stoppable_autoscroll = false;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-05-24 19:53:25 +02:00
|
|
|
});
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-05-24 19:53:25 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function user_initiated_animate_scroll(scroll_amount) {
|
2020-06-18 08:29:08 +02:00
|
|
|
message_scroll.suppress_selection_update_on_next_scroll();
|
2013-05-24 19:53:25 +02:00
|
|
|
in_stoppable_autoscroll = false; // defensive
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
const viewport_offset = scrollTop();
|
2013-05-28 20:06:07 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
message_pane.animate({
|
2017-01-12 00:17:43 +01:00
|
|
|
scrollTop: viewport_offset + scroll_amount,
|
2013-05-24 19:53:25 +02:00
|
|
|
});
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2013-05-24 19:53:25 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function recenter_view(message, opts) {
|
2016-05-25 13:42:20 +02:00
|
|
|
opts = opts || {};
|
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
// BarnOwl-style recentering: if the pointer is too high, move it to
|
2016-05-25 13:42:20 +02:00
|
|
|
// the 1/2 marks. If the pointer is too low, move it to the 1/7 mark.
|
2017-03-18 22:46:36 +01:00
|
|
|
// See keep_pointer_in_view() for related logic to keep the pointer onscreen.
|
2016-05-25 13:42:20 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
const viewport_info = message_viewport_info();
|
2019-11-02 00:06:25 +01:00
|
|
|
const top_threshold = viewport_info.visible_top;
|
2016-05-25 13:42:20 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bottom_threshold = viewport_info.visible_bottom;
|
2016-05-25 13:42:20 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_top = message.offset().top;
|
|
|
|
const message_height = message.safeOuterHeight(true);
|
|
|
|
const message_bottom = message_top + message_height;
|
2016-05-25 13:42:20 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_above = message_top < top_threshold;
|
|
|
|
const is_below = message_bottom > bottom_threshold;
|
2016-05-25 13:42:20 +02:00
|
|
|
|
|
|
|
if (opts.from_scroll) {
|
|
|
|
// If the message you're trying to center on is already in view AND
|
|
|
|
// you're already trying to move in the direction of that message,
|
|
|
|
// don't try to recenter. This avoids disorienting jumps when the
|
|
|
|
// pointer has gotten itself outside the threshold (e.g. by
|
|
|
|
// autoscrolling).
|
2021-02-28 00:42:00 +01:00
|
|
|
if (is_above && last_movement_direction >= 0) {
|
2016-05-25 13:42:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-28 00:42:00 +01:00
|
|
|
if (is_below && last_movement_direction <= 0) {
|
2016-05-25 13:42:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_above || opts.force_center) {
|
2021-02-28 00:42:00 +01:00
|
|
|
set_message_position(message_top, message_height, viewport_info, 1 / 2);
|
2016-05-25 13:42:20 +02:00
|
|
|
} else if (is_below) {
|
2021-02-28 00:42:00 +01:00
|
|
|
set_message_position(message_top, message_height, viewport_info, 1 / 7);
|
2016-05-25 13:42:20 +02:00
|
|
|
}
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2016-05-25 13:42:20 +02:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function keep_pointer_in_view() {
|
2017-03-18 22:46:36 +01:00
|
|
|
// See message_viewport.recenter_view() for related logic to keep the pointer onscreen.
|
|
|
|
// This function mostly comes into place for mouse scrollers, and it
|
|
|
|
// keeps the pointer in view. For people who purely scroll with the
|
|
|
|
// mouse, the pointer is kind of meaningless to them, but keyboard
|
|
|
|
// users will occasionally do big mouse scrolls, so this gives them
|
|
|
|
// a pointer reasonably close to the middle of the screen.
|
2019-11-02 00:06:25 +01:00
|
|
|
let candidate;
|
2021-03-30 02:21:21 +02:00
|
|
|
let next_row = message_lists.current.selected_row();
|
2017-03-18 22:46:36 +01:00
|
|
|
|
|
|
|
if (next_row.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
const info = message_viewport_info();
|
2020-07-15 00:34:28 +02:00
|
|
|
const top_threshold = info.visible_top + (1 / 10) * info.visible_height;
|
|
|
|
const bottom_threshold = info.visible_top + (9 / 10) * info.visible_height;
|
2017-03-18 22:46:36 +01:00
|
|
|
|
|
|
|
function message_is_far_enough_down() {
|
2021-02-28 00:42:00 +01:00
|
|
|
if (at_top()) {
|
2017-03-18 22:46:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_top = next_row.offset().top;
|
2017-03-18 22:46:36 +01:00
|
|
|
|
|
|
|
// If the message starts after the very top of the screen, we just
|
|
|
|
// leave it alone. This avoids bugs like #1608, where overzealousness
|
|
|
|
// about repositioning the pointer can cause users to miss messages.
|
|
|
|
if (message_top >= info.visible_top) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If at least part of the message is below top_threshold (10% from
|
|
|
|
// the top), then we also leave it alone.
|
2019-11-02 00:06:25 +01:00
|
|
|
const bottom_offset = message_top + next_row.safeOuterHeight(true);
|
2017-03-18 22:46:36 +01:00
|
|
|
if (bottom_offset >= top_threshold) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, the message is not "in view."
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function message_is_far_enough_up() {
|
2021-02-28 00:42:00 +01:00
|
|
|
return at_bottom() || next_row.offset().top <= bottom_threshold;
|
2017-03-18 22:46:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function adjust(in_view, get_next_row) {
|
|
|
|
// return true only if we make an actual adjustment, so
|
|
|
|
// that we know to short circuit the other direction
|
|
|
|
if (in_view(next_row)) {
|
2020-07-16 23:29:01 +02:00
|
|
|
return false; // try other side
|
2017-03-18 22:46:36 +01:00
|
|
|
}
|
|
|
|
while (!in_view(next_row)) {
|
|
|
|
candidate = get_next_row(next_row);
|
|
|
|
if (candidate.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_row = candidate;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!adjust(message_is_far_enough_down, rows.next_visible)) {
|
|
|
|
adjust(message_is_far_enough_up, rows.prev_visible);
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.select_id(rows.id(next_row), {from_scroll: true});
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2017-03-18 22:46:36 +01:00
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
export function initialize() {
|
2013-05-07 17:15:20 +02:00
|
|
|
jwindow = $(window);
|
2021-02-28 00:42:00 +01:00
|
|
|
message_pane = $(".app");
|
2013-05-07 17:15:20 +02:00
|
|
|
// This handler must be placed before all resize handlers in our application
|
2020-07-20 21:26:58 +02:00
|
|
|
jwindow.on("resize", () => {
|
2013-08-06 21:53:41 +02:00
|
|
|
dimensions.height.reset();
|
|
|
|
dimensions.width.reset();
|
2013-07-31 23:28:42 +02:00
|
|
|
top_of_feed.reset();
|
|
|
|
bottom_of_feed.reset();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(document).on("compose_started compose_canceled compose_finished", () => {
|
2013-07-31 23:28:42 +02:00
|
|
|
bottom_of_feed.reset();
|
2013-05-07 17:15:20 +02:00
|
|
|
});
|
2021-01-27 17:25:19 +01:00
|
|
|
|
|
|
|
// We stop autoscrolling when the user is clearly in the middle of
|
|
|
|
// doing something. Be careful, though, if you try to capture
|
|
|
|
// mousemove, then you will have to contend with the autoscroll
|
|
|
|
// itself generating mousemove events.
|
|
|
|
$(document).on("message_selected.zulip wheel", () => {
|
2021-02-28 00:42:00 +01:00
|
|
|
stop_auto_scrolling();
|
2021-01-27 17:25:19 +01:00
|
|
|
});
|
2021-02-28 00:42:00 +01:00
|
|
|
}
|
2021-04-07 14:08:09 +02:00
|
|
|
|
|
|
|
export function is_visible_and_focused() {
|
|
|
|
if (
|
|
|
|
overlays.is_active() ||
|
|
|
|
!notifications.is_window_focused() ||
|
|
|
|
!$("#message_feed_container").is(":visible")
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|