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-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:10:03 +01:00
|
|
|
import * as message_store from "./message_store";
|
2013-08-14 20:57:29 +02:00
|
|
|
// We don't need an andSelf() here because we already know
|
|
|
|
// that our next element is *not* a message_row, so this
|
|
|
|
// isn't going to end up empty unless we're at the bottom or top.
|
2022-01-25 11:36:19 +01:00
|
|
|
export function next_visible($message_row) {
|
|
|
|
if ($message_row === undefined || $message_row.length === 0) {
|
2013-08-14 20:57:29 +02:00
|
|
|
return $();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $message_row.next(".selectable_row");
|
|
|
|
if ($row.length !== 0) {
|
|
|
|
return $row;
|
2013-08-14 20:57:29 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const $recipient_row = get_message_recipient_row($message_row);
|
|
|
|
const $next_recipient_rows = $($recipient_row).nextAll(".recipient_row");
|
|
|
|
if ($next_recipient_rows.length === 0) {
|
2014-02-05 16:55:24 +01:00
|
|
|
return $();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $(".selectable_row", $next_recipient_rows[0]).first();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function prev_visible($message_row) {
|
|
|
|
if ($message_row === undefined || $message_row.length === 0) {
|
2013-08-14 20:57:29 +02:00
|
|
|
return $();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $message_row.prev(".selectable_row");
|
|
|
|
if ($row.length !== 0) {
|
|
|
|
return $row;
|
2013-08-14 20:57:29 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const $recipient_row = get_message_recipient_row($message_row);
|
|
|
|
const $prev_recipient_rows = $($recipient_row).prevAll(".recipient_row");
|
|
|
|
if ($prev_recipient_rows.length === 0) {
|
2014-02-05 16:55:24 +01:00
|
|
|
return $();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $(".selectable_row", $prev_recipient_rows[0]).last();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function first_visible() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $(".focused_table .selectable_row").first();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function last_visible() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $(".focused_table .selectable_row").last();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function visible_range(start_id, end_id) {
|
2020-04-02 23:34:54 +02:00
|
|
|
/*
|
|
|
|
Get all visible rows between start_id
|
|
|
|
and end_in, being inclusive on both ends.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const rows = [];
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
let $row = message_lists.current.get_row(start_id);
|
|
|
|
let msg_id = id($row);
|
2020-04-02 23:34:54 +02:00
|
|
|
|
|
|
|
while (msg_id <= end_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
rows.push($row);
|
2020-04-02 23:34:54 +02:00
|
|
|
|
|
|
|
if (msg_id >= end_id) {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$row = next_visible($row);
|
|
|
|
msg_id = id($row);
|
2020-04-02 23:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rows;
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2020-04-02 23:34:54 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function is_draft_row($row) {
|
|
|
|
return $row.find(".restore-draft").length >= 1;
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2020-04-02 15:13:23 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function id($message_row) {
|
|
|
|
if (is_draft_row($message_row)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Drafts have no zid");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-04-02 15:13:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:29:23 +01:00
|
|
|
/*
|
|
|
|
For blueslip errors, don't return early, since
|
|
|
|
we may have some code now that actually relies
|
|
|
|
on the NaN behavior here. We can try to clean
|
|
|
|
that up in the future, but we mainly just want
|
|
|
|
more data now.
|
|
|
|
*/
|
2020-04-02 15:13:23 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($message_row.length !== 1) {
|
2020-02-21 17:29:23 +01:00
|
|
|
blueslip.error("Caller should pass in a single row.");
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const zid = $message_row.attr("zid");
|
2020-02-21 17:29:23 +01:00
|
|
|
|
|
|
|
if (zid === undefined) {
|
|
|
|
blueslip.error("Calling code passed rows.id a row with no zid attr.");
|
|
|
|
}
|
|
|
|
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseFloat(zid);
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-18 20:55:41 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function local_echo_id($message_row) {
|
|
|
|
const zid = $message_row.attr("zid");
|
2020-04-09 21:31:15 +02:00
|
|
|
|
|
|
|
if (zid === undefined) {
|
|
|
|
blueslip.error("Calling code passed rows.local_id a row with no zid attr.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-04-09 21:31:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!zid.includes(".0")) {
|
|
|
|
blueslip.error("Trying to get local_id from row that has reified message id: " + zid);
|
2020-04-09 21:31:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return zid;
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2020-04-09 21:31:15 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const valid_table_names = new Set(["zhome", "zfilt"]);
|
2012-10-27 02:05:54 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function get_table(table_name) {
|
2020-05-27 04:04:54 +02:00
|
|
|
if (!valid_table_names.has(table_name)) {
|
2013-08-14 20:57:29 +02:00
|
|
|
return $();
|
|
|
|
}
|
2012-10-27 03:03:06 +02:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#${CSS.escape(table_name)}`);
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2012-10-27 03:03:06 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function get_message_id(elem) {
|
2017-07-19 13:53:30 +02:00
|
|
|
// Gets the message_id for elem, where elem is a DOM
|
|
|
|
// element inside a message. This is typically used
|
|
|
|
// in click handlers for things like the reaction button.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(elem).closest(".message_row");
|
|
|
|
const message_id = id($row);
|
2017-07-19 13:53:30 +02:00
|
|
|
return message_id;
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2017-07-19 13:53:30 +02:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function get_closest_group(element) {
|
2013-08-27 17:09:27 +02:00
|
|
|
// This gets the closest message row to an element, whether it's
|
2014-02-03 22:48:25 +01:00
|
|
|
// a recipient bar or message. With our current markup,
|
2013-08-27 17:09:27 +02:00
|
|
|
// this is the most reliable way to do it.
|
2014-02-05 16:55:24 +01:00
|
|
|
return $(element).closest("div.recipient_row");
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2021-04-20 19:50:01 +02:00
|
|
|
export function get_closest_row(element) {
|
|
|
|
return $(element).closest("div.message_row");
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function first_message_in_group(message_group) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $("div.message_row", message_group).first();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function get_message_recipient_row($message_row) {
|
|
|
|
return $($message_row).parent(".recipient_row").expectOne();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function get_message_recipient_header($message_row) {
|
|
|
|
return $($message_row).parent(".recipient_row").find(".message_header").expectOne();
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|
2014-02-05 16:55:24 +01:00
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
export function recipient_from_group(message_group) {
|
|
|
|
return message_store.get(id($(message_group).children(".message_row").first().expectOne()));
|
|
|
|
}
|
2013-08-27 17:09:27 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function id_for_recipient_row($recipient_row) {
|
2014-03-13 04:25:34 +01:00
|
|
|
// A recipient row can be either a normal recipient row, or
|
|
|
|
// the FRB, which is a fake recipient row. If it's a FRB, it has
|
|
|
|
// a 'zid' property that stores the message id it is directly over
|
2022-01-25 11:36:19 +01:00
|
|
|
const $msg_row = first_message_in_group($recipient_row);
|
|
|
|
if ($msg_row.length === 0) {
|
2014-03-13 04:25:34 +01:00
|
|
|
// If we're narrowing from the FRB, take the msg id
|
|
|
|
// directly from it
|
2022-01-25 11:36:19 +01:00
|
|
|
return id($recipient_row);
|
2014-03-13 04:25:34 +01:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return id($msg_row);
|
2021-02-28 00:42:30 +01:00
|
|
|
}
|