2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
import render_recent_topic_row from "../templates/recent_topic_row.hbs";
|
|
|
|
import render_recent_topics_filters from "../templates/recent_topics_filters.hbs";
|
|
|
|
import render_recent_topics_body from "../templates/recent_topics_table.hbs";
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
import * as compose_closed_ui from "./compose_closed_ui";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as hash_util from "./hash_util";
|
2021-06-03 21:51:44 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
2022-02-25 06:08:08 +01:00
|
|
|
import * as loading from "./loading";
|
2021-02-28 01:27:48 +01:00
|
|
|
import {localstorage} from "./localstorage";
|
|
|
|
import * as message_store from "./message_store";
|
|
|
|
import * as message_util from "./message_util";
|
|
|
|
import * as message_view_header from "./message_view_header";
|
2021-06-28 20:39:04 +02:00
|
|
|
import * as muted_topics from "./muted_topics";
|
2021-02-28 21:31:57 +01:00
|
|
|
import * as narrow from "./narrow";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-05-17 10:01:02 +02:00
|
|
|
import * as navbar_alerts from "./navbar_alerts";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as navigate from "./navigate";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as recent_senders from "./recent_senders";
|
2021-06-10 14:18:46 +02:00
|
|
|
import {get, process_message, topics} from "./recent_topics_data";
|
recent_topics: Don't rely on ":visible" to avoid forced reflow.
Previously, navigating from any stream to the recent topics view would
cause a forced reflow every time we checked `is_visible()` because it
would call `$("#recent_topics_view").is(":visible")`.
The reason for this is related to how browsers ship frames, the
process follows these steps:
JavaScript > style calculations > layout > paint > composite.
(The layout step is called Reflow in firefox.)
Typically, the browser will handle these steps in the most optimal
manner possible, delaying expensive operations until they're needed.
However, it is possible to cause the browser to perform a layout
earlier than necessary. An example of this is what we previously did:
When we call `top_left_corner.narrow_to_recent_topics()`, we ask to
add a class via `.addClass()`, this schedules a Style Recalculation,
then, when we call `message_view_header.make_message_view_header()` it
calls `recent_topics_util.is_visible()` which calls
`$("#recent_topics_view").is(":visible")`.
Before the browser can get this value, it realizes that our dom was
invalidated by `.addClass()` and so it must execute the scheduled
Style Recalculation and cause a layout.
This is called a forced synchronous layout.
This commit adds a JavaScript variable representing the visible state,
in order to prevent the above behavior.
This commit reduces the main thread run time of
`build_message_view_header` from 131.81 ms to 5.20 ms.
Unfortunately we still have the case where
`recent_topics_ui.revive_current_focus()` calls
`recent_topics_ui.set_table_focus()` which causes a reflow.
However, by eliminating this reflow we still save ~100ms.
(It's important to note that we only save this sometimes, as other
things can still cost us a reflow.)
Further reading: https://developers.google.com/web/fundamentals/
performance/rendering/avoid-large-complex-layouts-and-layout-thrashing
2021-11-05 21:21:43 +01:00
|
|
|
import {get_topic_key, is_in_focus, is_visible, set_visible} from "./recent_topics_util";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as timerender from "./timerender";
|
|
|
|
import * as top_left_corner from "./top_left_corner";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
let topics_widget;
|
2020-05-22 21:04:03 +02:00
|
|
|
// Sets the number of avatars to display.
|
|
|
|
// Rest of the avatars, if present, are displayed as {+x}
|
|
|
|
const MAX_AVATAR = 4;
|
2021-06-03 21:51:44 +02:00
|
|
|
const MAX_EXTRA_SENDERS = 10;
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-06-20 10:17:44 +02:00
|
|
|
// Use this to set the focused element.
|
|
|
|
//
|
|
|
|
// We set it's value to `table` in case the
|
|
|
|
// focus in one of the table rows, since the
|
|
|
|
// table rows are constantly updated and tracking
|
|
|
|
// the selected element in them would be tedious via
|
|
|
|
// jquery.
|
|
|
|
//
|
|
|
|
// So, we use table as a grid system and
|
|
|
|
// track the coordinates of the focus element via
|
|
|
|
// `row_focus` and `col_focus`.
|
2021-06-10 14:18:46 +02:00
|
|
|
export let current_focus_elem = "table";
|
|
|
|
|
2021-09-06 17:43:52 +02:00
|
|
|
// If user clicks a topic in recent topics, then
|
|
|
|
// we store that topic here so that we can restore focus
|
|
|
|
// to that topic when user revisits.
|
|
|
|
let last_visited_topic = "";
|
2020-06-20 10:17:44 +02:00
|
|
|
let row_focus = 0;
|
|
|
|
// Start focus on the topic column, so Down+Enter works to visit a topic.
|
|
|
|
let col_focus = 1;
|
|
|
|
|
2021-03-13 16:45:47 +01:00
|
|
|
export const COLUMNS = {
|
|
|
|
stream: 0,
|
|
|
|
topic: 1,
|
|
|
|
mute: 2,
|
|
|
|
read: 3,
|
|
|
|
};
|
|
|
|
|
2020-06-20 10:17:44 +02:00
|
|
|
// The number of selectable actions in a recent_topics. Used to
|
|
|
|
// implement wraparound of elements with the right/left keys. Must be
|
|
|
|
// increased when we add new actions, or rethought if we add optional
|
|
|
|
// actions that only appear in some rows.
|
|
|
|
const MAX_SELECTABLE_COLS = 4;
|
|
|
|
|
2020-09-21 01:43:18 +02:00
|
|
|
// we use localstorage to persist the recent topic filters
|
|
|
|
const ls_key = "recent_topic_filters";
|
|
|
|
const ls = localstorage();
|
|
|
|
|
|
|
|
let filters = new Set();
|
2021-02-28 01:27:48 +01:00
|
|
|
|
2021-03-01 21:08:50 +01:00
|
|
|
export function clear_for_tests() {
|
|
|
|
filters.clear();
|
|
|
|
topics.clear();
|
|
|
|
topics_widget = undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function save_filters() {
|
2020-09-21 01:43:18 +02:00
|
|
|
ls.set(ls_key, Array.from(filters));
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-09-21 01:43:18 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function load_filters() {
|
2020-09-21 01:43:18 +02:00
|
|
|
filters = new Set(ls.get(ls_key));
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-09-21 01:43:18 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function set_default_focus() {
|
2020-06-20 10:17:44 +02:00
|
|
|
// If at any point we are confused about the currently
|
|
|
|
// focused element, we switch focus to search.
|
|
|
|
current_focus_elem = $("#recent_topics_search");
|
2020-07-20 21:24:26 +02:00
|
|
|
current_focus_elem.trigger("focus");
|
2021-05-10 14:05:30 +02:00
|
|
|
compose_closed_ui.set_standard_text_for_reply_button();
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:31:56 +02:00
|
|
|
function get_min_load_count(already_rendered_count, load_count) {
|
|
|
|
const extra_rows_for_viewing_pleasure = 15;
|
|
|
|
if (row_focus > already_rendered_count + load_count) {
|
|
|
|
return row_focus + extra_rows_for_viewing_pleasure - already_rendered_count;
|
|
|
|
}
|
|
|
|
return load_count;
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:18:46 +02:00
|
|
|
function is_table_focused() {
|
|
|
|
return current_focus_elem === "table";
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:36:29 +02:00
|
|
|
function set_table_focus(row, col, using_keyboard) {
|
2020-06-20 10:17:44 +02:00
|
|
|
const topic_rows = $("#recent_topics_table table tbody tr");
|
|
|
|
if (topic_rows.length === 0 || row < 0 || row >= topic_rows.length) {
|
|
|
|
row_focus = 0;
|
|
|
|
// return focus back to filters if we cannot focus on the table.
|
|
|
|
set_default_focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:34:36 +02:00
|
|
|
const topic_row = topic_rows.eq(row);
|
2021-05-17 02:36:29 +02:00
|
|
|
// We need to allow table to render first before setting focus.
|
|
|
|
setTimeout(
|
|
|
|
() => topic_row.find(".recent_topics_focusable").eq(col).children().trigger("focus"),
|
|
|
|
0,
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
current_focus_elem = "table";
|
2021-03-18 13:57:28 +01:00
|
|
|
|
2021-05-17 02:36:29 +02:00
|
|
|
if (using_keyboard) {
|
|
|
|
const scroll_element = document.querySelector(
|
|
|
|
"#recent_topics_table .table_fix_head .simplebar-content-wrapper",
|
|
|
|
);
|
|
|
|
const half_height_of_visible_area = scroll_element.offsetHeight / 2;
|
|
|
|
const topic_offset = topic_offset_to_visible_area(topic_row);
|
|
|
|
|
|
|
|
if (topic_offset === "above") {
|
|
|
|
scroll_element.scrollBy({top: -1 * half_height_of_visible_area});
|
|
|
|
} else if (topic_offset === "below") {
|
|
|
|
scroll_element.scrollBy({top: half_height_of_visible_area});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
const message = {
|
|
|
|
stream: topic_row.find(".recent_topic_stream a").text(),
|
|
|
|
topic: topic_row.find(".recent_topic_name a").text(),
|
|
|
|
};
|
|
|
|
compose_closed_ui.update_reply_recipient_label(message);
|
2020-06-20 10:17:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
export function get_focused_row_message() {
|
|
|
|
if (is_table_focused()) {
|
|
|
|
const recent_topic_id_prefix_len = "recent_topic:".length;
|
|
|
|
const topic_rows = $("#recent_topics_table table tbody tr");
|
2021-09-08 08:50:23 +02:00
|
|
|
if (topic_rows.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
const topic_row = topic_rows.eq(row_focus);
|
|
|
|
const topic_id = topic_row.attr("id").slice(recent_topic_id_prefix_len);
|
|
|
|
const topic_last_msg_id = topics.get(topic_id).last_msg_id;
|
|
|
|
return message_store.get(topic_last_msg_id);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:19:37 +02:00
|
|
|
export function revive_current_focus() {
|
2020-06-20 10:17:44 +02:00
|
|
|
// After re-render, the current_focus_elem is no longer linked
|
|
|
|
// to the focused element, this function attempts to revive the
|
|
|
|
// link and focus to the element prior to the rerender.
|
2021-02-15 15:33:37 +01:00
|
|
|
|
2021-03-30 10:38:06 +02:00
|
|
|
// We try to avoid setting focus when user
|
|
|
|
// is not focused on recent topics.
|
|
|
|
if (!is_in_focus()) {
|
2021-02-15 15:33:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:17:44 +02:00
|
|
|
if (!current_focus_elem) {
|
|
|
|
set_default_focus();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-02 12:36:02 +02:00
|
|
|
if (is_table_focused()) {
|
2021-09-06 17:43:52 +02:00
|
|
|
if (last_visited_topic) {
|
|
|
|
const topic_last_msg_id = topics.get(last_visited_topic).last_msg_id;
|
|
|
|
const current_list = topics_widget.get_current_list();
|
|
|
|
const last_visited_topic_index = current_list.findIndex(
|
|
|
|
(topic) => topic.last_msg_id === topic_last_msg_id,
|
|
|
|
);
|
|
|
|
if (last_visited_topic_index >= 0) {
|
|
|
|
row_focus = last_visited_topic_index;
|
|
|
|
}
|
|
|
|
last_visited_topic = "";
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:17:44 +02:00
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const filter_button = current_focus_elem.data("filter");
|
2020-06-20 10:17:44 +02:00
|
|
|
if (!filter_button) {
|
|
|
|
set_default_focus();
|
|
|
|
} else {
|
2020-07-15 00:34:28 +02:00
|
|
|
current_focus_elem = $("#recent_topics_filter_buttons").find(
|
2021-02-03 23:23:32 +01:00
|
|
|
`[data-filter='${CSS.escape(filter_button)}']`,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2020-07-20 21:24:26 +02:00
|
|
|
current_focus_elem.trigger("focus");
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-25 06:08:08 +01:00
|
|
|
export function show_loading_indicator() {
|
|
|
|
loading.make_indicator($("#recent_topics_loading_messages_indicator"));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hide_loading_indicator() {
|
|
|
|
$("#recent_topics_bottom_whitespace").hide();
|
|
|
|
loading.destroy_indicator($("#recent_topics_loading_messages_indicator"), {
|
|
|
|
abs_positioned: false,
|
|
|
|
});
|
|
|
|
// Show empty table text if there are no messages fetched.
|
|
|
|
$("#recent_topics_table tbody").addClass("required-text");
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function process_messages(messages) {
|
2020-06-01 17:17:41 +02:00
|
|
|
// While this is inexpensive and handles all the cases itself,
|
|
|
|
// the UX can be bad if user wants to scroll down the list as
|
|
|
|
// the UI will be returned to the beginning of the list on every
|
|
|
|
// update.
|
2021-05-06 19:47:28 +02:00
|
|
|
//
|
|
|
|
// Only rerender if topic_data actually
|
|
|
|
// changed.
|
|
|
|
let topic_data_changed = false;
|
2020-03-21 14:42:10 +01:00
|
|
|
for (const msg of messages) {
|
2021-05-06 19:47:28 +02:00
|
|
|
if (process_message(msg)) {
|
|
|
|
topic_data_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (topic_data_changed) {
|
|
|
|
complete_rerender();
|
2020-03-21 14:42:10 +01:00
|
|
|
}
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
function format_topic(topic_data) {
|
|
|
|
const last_msg = message_store.get(topic_data.last_msg_id);
|
|
|
|
const stream = last_msg.stream;
|
|
|
|
const stream_id = last_msg.stream_id;
|
2021-04-15 17:02:54 +02:00
|
|
|
const stream_info = sub_store.get(stream_id);
|
2020-07-12 16:45:42 +02:00
|
|
|
if (stream_info === undefined) {
|
|
|
|
// stream was deleted
|
|
|
|
return {};
|
|
|
|
}
|
2020-05-22 08:16:08 +02:00
|
|
|
const topic = last_msg.topic;
|
2021-02-05 21:20:14 +01:00
|
|
|
const time = new Date(last_msg.timestamp * 1000);
|
2020-05-22 08:16:08 +02:00
|
|
|
const last_msg_time = timerender.last_seen_status_from_date(time);
|
2020-06-26 12:25:35 +02:00
|
|
|
const full_datetime = timerender.get_full_datetime(time);
|
2020-05-23 09:04:51 +02:00
|
|
|
|
|
|
|
// We hide the row according to filters or if it's muted.
|
2020-05-29 11:56:19 +02:00
|
|
|
// We only supply the data to the topic rows and let jquery
|
|
|
|
// display / hide them according to filters instead of
|
|
|
|
// doing complete re-render.
|
2021-06-28 20:39:04 +02:00
|
|
|
const topic_muted = Boolean(muted_topics.is_topic_muted(stream_id, topic));
|
2020-05-29 17:22:53 +02:00
|
|
|
const stream_muted = stream_data.is_muted(stream_id);
|
|
|
|
const muted = topic_muted || stream_muted;
|
2021-03-09 14:51:01 +01:00
|
|
|
const unread_count = unread.num_unread_for_topic(stream_id, topic);
|
2020-05-22 21:04:03 +02:00
|
|
|
|
|
|
|
// Display in most recent sender first order
|
|
|
|
const all_senders = recent_senders.get_topic_recent_senders(stream_id, topic);
|
|
|
|
const senders = all_senders.slice(-MAX_AVATAR);
|
2021-05-12 08:00:54 +02:00
|
|
|
const senders_info = people.sender_info_for_recent_topics_row(senders);
|
2020-05-22 21:04:03 +02:00
|
|
|
|
2021-06-03 21:51:44 +02:00
|
|
|
// Collect extra senders fullname for tooltip.
|
|
|
|
const extra_sender_ids = all_senders.slice(0, -MAX_AVATAR);
|
|
|
|
const displayed_other_senders = extra_sender_ids.slice(-MAX_EXTRA_SENDERS);
|
|
|
|
const displayed_other_names = people.get_display_full_names(displayed_other_senders.reverse());
|
|
|
|
|
|
|
|
if (extra_sender_ids.length > MAX_EXTRA_SENDERS) {
|
|
|
|
// We display only 10 extra senders in tooltips,
|
|
|
|
// and just display remaining number of senders.
|
|
|
|
const remaining_senders = extra_sender_ids.length - MAX_EXTRA_SENDERS;
|
2021-09-08 09:26:34 +02:00
|
|
|
// Pluralization syntax from:
|
|
|
|
// https://formatjs.io/docs/core-concepts/icu-syntax/#plural-format
|
2021-06-03 21:51:44 +02:00
|
|
|
displayed_other_names.push(
|
2021-09-08 09:26:34 +02:00
|
|
|
$t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"and {remaining_senders, plural, one {1 other} other {# others}}.",
|
|
|
|
},
|
|
|
|
{remaining_senders},
|
|
|
|
),
|
2021-06-03 21:51:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const other_sender_names = displayed_other_names.join("<br/>");
|
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
return {
|
2020-05-26 10:33:35 +02:00
|
|
|
// stream info
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
|
|
|
stream,
|
2020-05-26 10:33:35 +02:00
|
|
|
stream_color: stream_info.color,
|
|
|
|
invite_only: stream_info.invite_only,
|
|
|
|
is_web_public: stream_info.is_web_public,
|
|
|
|
stream_url: hash_util.by_stream_uri(stream_id),
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
topic,
|
2020-06-12 12:46:30 +02:00
|
|
|
topic_key: get_topic_key(stream_id, topic),
|
2020-07-20 22:18:43 +02:00
|
|
|
unread_count,
|
|
|
|
last_msg_time,
|
2020-05-22 08:16:08 +02:00
|
|
|
topic_url: hash_util.by_stream_topic_uri(stream_id, topic),
|
2020-05-22 21:04:03 +02:00
|
|
|
senders: senders_info,
|
2020-06-12 12:07:48 +02:00
|
|
|
other_senders_count: Math.max(0, all_senders.length - MAX_AVATAR),
|
2021-06-03 21:51:44 +02:00
|
|
|
other_sender_names,
|
2020-07-20 22:18:43 +02:00
|
|
|
muted,
|
|
|
|
topic_muted,
|
2020-05-29 11:56:19 +02:00
|
|
|
participated: topic_data.participated,
|
2021-07-03 08:53:32 +02:00
|
|
|
full_last_msg_date_time: full_datetime,
|
2020-05-22 08:16:08 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
function get_topic_row(topic_data) {
|
|
|
|
const msg = message_store.get(topic_data.last_msg_id);
|
2020-06-12 12:46:30 +02:00
|
|
|
const topic_key = get_topic_key(msg.stream_id, msg.topic);
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#${CSS.escape("recent_topic:" + topic_key)}`);
|
2020-05-22 08:16:08 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function process_topic_edit(old_stream_id, old_topic, new_topic, new_stream_id) {
|
2020-05-26 12:16:05 +02:00
|
|
|
// See `recent_senders.process_topic_edit` for
|
|
|
|
// logic behind this and important notes on use of this function.
|
2020-06-12 12:46:30 +02:00
|
|
|
topics.delete(get_topic_key(old_stream_id, old_topic));
|
2020-05-26 12:16:05 +02:00
|
|
|
|
|
|
|
const old_topic_msgs = message_util.get_messages_in_topic(old_stream_id, old_topic);
|
2021-02-28 01:27:48 +01:00
|
|
|
process_messages(old_topic_msgs);
|
2020-05-26 12:16:05 +02:00
|
|
|
|
|
|
|
new_stream_id = new_stream_id || old_stream_id;
|
|
|
|
const new_topic_msgs = message_util.get_messages_in_topic(new_stream_id, new_topic);
|
2021-02-28 01:27:48 +01:00
|
|
|
process_messages(new_topic_msgs);
|
|
|
|
}
|
2020-05-26 12:16:05 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function topic_in_search_results(keyword, stream, topic) {
|
2020-06-01 17:17:41 +02:00
|
|
|
if (keyword === "") {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-29 22:22:29 +02:00
|
|
|
const text = (stream + " " + topic).toLowerCase();
|
|
|
|
const search_words = keyword.toLowerCase().split(/\s+/);
|
|
|
|
return search_words.every((word) => text.includes(word));
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-06-01 17:17:41 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function update_topics_of_deleted_message_ids(message_ids) {
|
2020-08-07 09:15:47 +02:00
|
|
|
const topics_to_rerender = message_util.get_topics_for_message_ids(message_ids);
|
2020-07-15 09:36:03 +02:00
|
|
|
|
|
|
|
for (const [stream_id, topic] of topics_to_rerender.values()) {
|
|
|
|
topics.delete(get_topic_key(stream_id, topic));
|
|
|
|
const msgs = message_util.get_messages_in_topic(stream_id, topic);
|
2021-02-28 01:27:48 +01:00
|
|
|
process_messages(msgs);
|
2020-07-15 09:36:03 +02:00
|
|
|
}
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-07-15 09:36:03 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function filters_should_hide_topic(topic_data) {
|
2020-06-01 17:17:41 +02:00
|
|
|
const msg = message_store.get(topic_data.last_msg_id);
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(msg.stream_id);
|
2020-06-01 17:17:41 +02:00
|
|
|
|
2021-01-06 15:23:07 +01:00
|
|
|
if (sub === undefined || !sub.subscribed) {
|
|
|
|
// Never try to process deactivated & unsubscribed stream msgs.
|
2020-07-12 16:45:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (filters.has("unread")) {
|
2021-03-09 14:51:01 +01:00
|
|
|
const unreadCount = unread.num_unread_for_topic(msg.stream_id, msg.topic);
|
2020-06-14 09:29:29 +02:00
|
|
|
if (unreadCount === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-12 12:21:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!topic_data.participated && filters.has("participated")) {
|
2020-05-29 11:56:19 +02:00
|
|
|
return true;
|
2020-06-12 12:21:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!filters.has("include_muted")) {
|
2021-06-28 20:39:04 +02:00
|
|
|
const topic_muted = Boolean(muted_topics.is_topic_muted(msg.stream_id, msg.topic));
|
2020-06-14 09:29:29 +02:00
|
|
|
const stream_muted = stream_data.is_muted(msg.stream_id);
|
|
|
|
if (topic_muted || stream_muted) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-12 12:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const search_keyword = $("#recent_topics_search").val();
|
2021-02-28 01:27:48 +01:00
|
|
|
if (!topic_in_search_results(search_keyword, msg.stream, msg.topic)) {
|
2020-06-01 17:17:41 +02:00
|
|
|
return true;
|
2020-05-29 11:56:19 +02:00
|
|
|
}
|
2020-06-12 12:21:34 +02:00
|
|
|
|
2020-05-29 11:56:19 +02:00
|
|
|
return false;
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-05-29 11:56:19 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function inplace_rerender(topic_key) {
|
|
|
|
if (!is_visible()) {
|
2020-05-22 08:16:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-01 17:17:41 +02:00
|
|
|
if (!topics.has(topic_key)) {
|
|
|
|
return false;
|
2020-05-22 08:16:08 +02:00
|
|
|
}
|
2020-05-29 11:56:19 +02:00
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
const topic_data = topics.get(topic_key);
|
|
|
|
topics_widget.render_item(topic_data);
|
|
|
|
const topic_row = get_topic_row(topic_data);
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
if (filters_should_hide_topic(topic_data)) {
|
2020-05-29 11:56:19 +02:00
|
|
|
topic_row.hide();
|
|
|
|
} else {
|
|
|
|
topic_row.show();
|
|
|
|
}
|
2020-06-20 10:17:44 +02:00
|
|
|
revive_current_focus();
|
2020-06-01 17:17:41 +02:00
|
|
|
return true;
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-05-22 08:16:08 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function update_topic_is_muted(stream_id, topic) {
|
2020-06-12 12:46:30 +02:00
|
|
|
const key = get_topic_key(stream_id, topic);
|
2020-05-22 08:16:08 +02:00
|
|
|
if (!topics.has(key)) {
|
|
|
|
// we receive mute request for a topic we are
|
|
|
|
// not tracking currently
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
inplace_rerender(key);
|
2020-05-22 08:16:08 +02:00
|
|
|
return true;
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-05-22 08:16:08 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function update_topic_unread_count(message) {
|
2020-06-12 12:46:30 +02:00
|
|
|
const topic_key = get_topic_key(message.stream_id, message.topic);
|
2021-02-28 01:27:48 +01:00
|
|
|
inplace_rerender(topic_key);
|
|
|
|
}
|
2020-05-22 17:11:19 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function set_filter(filter) {
|
2020-06-12 13:07:10 +02:00
|
|
|
// This function updates the `filters` variable
|
|
|
|
// after user clicks on one of the filter buttons
|
|
|
|
// based on `btn-recent-selected` class and current
|
|
|
|
// set `filters`.
|
|
|
|
|
|
|
|
// Get the button which was clicked.
|
2021-02-03 23:23:32 +01:00
|
|
|
const filter_elem = $("#recent_topics_filter_buttons").find(
|
|
|
|
`[data-filter="${CSS.escape(filter)}"]`,
|
|
|
|
);
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-06-12 13:07:10 +02:00
|
|
|
// If user clicks `All`, we clear all filters.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (filter === "all" && filters.size !== 0) {
|
2020-05-23 09:04:51 +02:00
|
|
|
filters = new Set();
|
2020-07-15 00:34:28 +02:00
|
|
|
// If the button was already selected, remove the filter.
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (filter_elem.hasClass("btn-recent-selected")) {
|
2020-05-23 09:04:51 +02:00
|
|
|
filters.delete(filter);
|
2020-07-15 00:34:28 +02:00
|
|
|
// If the button was not selected, we add the filter.
|
2020-05-23 09:04:51 +02:00
|
|
|
} else {
|
|
|
|
filters.add(filter);
|
|
|
|
}
|
2020-09-21 01:43:18 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
save_filters();
|
|
|
|
}
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-05-29 11:56:19 +02:00
|
|
|
function show_selected_filters() {
|
2020-06-12 13:07:10 +02:00
|
|
|
// Add `btn-selected-filter` to the buttons to show
|
|
|
|
// which filters are applied.
|
2020-05-23 09:04:51 +02:00
|
|
|
if (filters.size === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons")
|
2020-05-23 09:04:51 +02:00
|
|
|
.find('[data-filter="all"]')
|
2021-02-18 06:39:15 +01:00
|
|
|
.addClass("btn-recent-selected")
|
|
|
|
.attr("aria-checked", "true");
|
2020-05-23 09:04:51 +02:00
|
|
|
} else {
|
|
|
|
for (const filter of filters) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons")
|
2021-02-03 23:23:32 +01:00
|
|
|
.find(`[data-filter="${CSS.escape(filter)}"]`)
|
2021-02-18 06:39:15 +01:00
|
|
|
.addClass("btn-recent-selected")
|
|
|
|
.attr("aria-checked", "true");
|
2020-05-23 09:04:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 11:56:19 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function update_filters_view() {
|
2020-05-29 11:56:19 +02:00
|
|
|
const rendered_filters = render_recent_topics_filters({
|
2020-07-15 01:29:15 +02:00
|
|
|
filter_participated: filters.has("participated"),
|
|
|
|
filter_unread: filters.has("unread"),
|
|
|
|
filter_muted: filters.has("include_muted"),
|
2020-05-29 11:56:19 +02:00
|
|
|
});
|
|
|
|
$("#recent_filters_group").html(rendered_filters);
|
|
|
|
show_selected_filters();
|
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
topics_widget.hard_redraw();
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-05-29 11:56:19 +02:00
|
|
|
|
2020-06-01 13:24:29 +02:00
|
|
|
function stream_sort(a, b) {
|
|
|
|
const a_stream = message_store.get(a.last_msg_id).stream;
|
|
|
|
const b_stream = message_store.get(b.last_msg_id).stream;
|
|
|
|
if (a_stream > b_stream) {
|
|
|
|
return 1;
|
|
|
|
} else if (a_stream === b_stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function topic_sort(a, b) {
|
|
|
|
const a_topic = message_store.get(a.last_msg_id).topic;
|
|
|
|
const b_topic = message_store.get(b.last_msg_id).topic;
|
|
|
|
if (a_topic > b_topic) {
|
|
|
|
return 1;
|
|
|
|
} else if (a_topic === b_topic) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:36:29 +02:00
|
|
|
function topic_offset_to_visible_area(topic_row) {
|
2021-05-06 06:34:36 +02:00
|
|
|
const scroll_container = $("#recent_topics_table .table_fix_head");
|
|
|
|
const thead_height = 30;
|
|
|
|
const under_closed_compose_region_height = 50;
|
|
|
|
|
|
|
|
const scroll_container_top = $(scroll_container).offset().top + thead_height;
|
|
|
|
const scroll_container_bottom =
|
|
|
|
scroll_container_top + $(scroll_container).height() - under_closed_compose_region_height;
|
|
|
|
|
|
|
|
const topic_row_top = $(topic_row).offset().top;
|
|
|
|
const topic_row_bottom = topic_row_top + $(topic_row).height();
|
|
|
|
|
2021-05-17 02:36:29 +02:00
|
|
|
// Topic is above the visible scroll region.
|
|
|
|
if (topic_row_top < scroll_container_top) {
|
|
|
|
return "above";
|
|
|
|
// Topic is below the visible scroll region.
|
|
|
|
} else if (topic_row_bottom > scroll_container_bottom) {
|
|
|
|
return "below";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Topic is visible
|
|
|
|
return "visible";
|
2021-05-06 06:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function set_focus_to_element_in_center() {
|
2021-05-17 02:36:29 +02:00
|
|
|
const table_wrapper_element = document.querySelector("#recent_topics_table .table_fix_head");
|
2021-05-06 06:34:36 +02:00
|
|
|
const topic_rows = $("#recent_topics_table table tbody tr");
|
2021-05-17 02:36:29 +02:00
|
|
|
|
2021-05-06 06:34:36 +02:00
|
|
|
if (row_focus > topic_rows.length) {
|
|
|
|
// User used a filter which reduced
|
|
|
|
// the number of visible rows.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let topic_row = topic_rows.eq(row_focus);
|
2021-05-17 02:36:29 +02:00
|
|
|
const topic_offset = topic_offset_to_visible_area(topic_row);
|
|
|
|
if (topic_offset !== "visible") {
|
2021-05-06 06:34:36 +02:00
|
|
|
// Get the element at the center of the table.
|
2021-05-17 02:36:29 +02:00
|
|
|
const position = table_wrapper_element.getBoundingClientRect();
|
2021-05-06 06:34:36 +02:00
|
|
|
const topic_center_x = (position.left + position.right) / 2;
|
|
|
|
const topic_center_y = (position.top + position.bottom) / 2;
|
|
|
|
|
|
|
|
topic_row = $(document.elementFromPoint(topic_center_x, topic_center_y)).closest("tr");
|
|
|
|
|
|
|
|
row_focus = topic_rows.index(topic_row);
|
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_scroll_position_for_render(scroll_container) {
|
|
|
|
const table_bottom_margin = 100; // Extra margin at the bottom of table.
|
|
|
|
const table_row_height = 50;
|
|
|
|
return (
|
|
|
|
scroll_container.scrollTop +
|
|
|
|
scroll_container.clientHeight +
|
|
|
|
table_bottom_margin +
|
|
|
|
table_row_height >
|
|
|
|
scroll_container.scrollHeight
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function complete_rerender() {
|
|
|
|
if (!is_visible()) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return;
|
2020-06-01 17:17:41 +02:00
|
|
|
}
|
2021-05-12 17:49:58 +02:00
|
|
|
|
|
|
|
// Update header
|
2021-03-25 06:33:02 +01:00
|
|
|
load_filters();
|
2021-05-12 17:49:58 +02:00
|
|
|
show_selected_filters();
|
|
|
|
|
|
|
|
// Show topics list
|
|
|
|
const mapped_topic_values = Array.from(get().values()).map((value) => value);
|
|
|
|
|
|
|
|
if (topics_widget) {
|
|
|
|
topics_widget.replace_list_data(mapped_topic_values);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-29 11:56:19 +02:00
|
|
|
const rendered_body = render_recent_topics_body({
|
2020-07-15 01:29:15 +02:00
|
|
|
filter_participated: filters.has("participated"),
|
|
|
|
filter_unread: filters.has("unread"),
|
|
|
|
filter_muted: filters.has("include_muted"),
|
2020-06-01 17:17:41 +02:00
|
|
|
search_val: $("#recent_topics_search").val() || "",
|
2020-05-29 11:56:19 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_table").html(rendered_body);
|
2020-07-05 12:19:09 +02:00
|
|
|
const container = $("#recent_topics_table table tbody");
|
2020-06-01 17:17:41 +02:00
|
|
|
container.empty();
|
2021-01-29 10:27:56 +01:00
|
|
|
topics_widget = ListWidget.create(container, mapped_topic_values, {
|
2020-06-01 17:17:41 +02:00
|
|
|
name: "recent_topics_table",
|
2020-06-01 13:24:29 +02:00
|
|
|
parent_container: $("#recent_topics_table"),
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(item) {
|
2020-06-01 17:17:41 +02:00
|
|
|
return render_recent_topic_row(format_topic(item));
|
|
|
|
},
|
|
|
|
filter: {
|
2020-06-12 12:21:34 +02:00
|
|
|
// We use update_filters_view & filters_should_hide_topic to do all the
|
2020-06-01 17:17:41 +02:00
|
|
|
// filtering for us, which is called using click_handlers.
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(topic_data) {
|
2021-02-28 01:27:48 +01:00
|
|
|
return !filters_should_hide_topic(topic_data);
|
2020-06-01 17:17:41 +02:00
|
|
|
},
|
|
|
|
},
|
2020-06-01 13:24:29 +02:00
|
|
|
sort_fields: {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_sort,
|
|
|
|
topic_sort,
|
2020-06-01 13:24:29 +02:00
|
|
|
},
|
2020-06-01 17:17:41 +02:00
|
|
|
html_selector: get_topic_row,
|
2020-07-15 01:29:15 +02:00
|
|
|
simplebar_container: $("#recent_topics_table .table_fix_head"),
|
2020-09-23 09:37:29 +02:00
|
|
|
callback_after_render: revive_current_focus,
|
2021-05-06 06:34:36 +02:00
|
|
|
is_scroll_position_for_render,
|
|
|
|
post_scroll__pre_render_callback: set_focus_to_element_in_center,
|
2021-05-06 06:31:56 +02:00
|
|
|
get_min_load_count,
|
2020-06-01 17:17:41 +02:00
|
|
|
});
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-04-08 13:59:56 +02:00
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function show() {
|
2021-05-12 17:20:41 +02:00
|
|
|
if (is_visible()) {
|
|
|
|
// If we're already visible, E.g. because the user hit Esc
|
|
|
|
// while already in the recent topics view, do nothing.
|
|
|
|
return;
|
|
|
|
}
|
2020-07-05 12:19:09 +02:00
|
|
|
// Hide selected elements in the left sidebar.
|
|
|
|
top_left_corner.narrow_to_recent_topics();
|
|
|
|
stream_list.handle_narrow_deactivated();
|
|
|
|
|
|
|
|
// Hide "middle-column" which has html for rendering
|
|
|
|
// a messages narrow. We hide it and show recent topics.
|
|
|
|
$("#message_feed_container").hide();
|
|
|
|
$("#recent_topics_view").show();
|
recent_topics: Don't rely on ":visible" to avoid forced reflow.
Previously, navigating from any stream to the recent topics view would
cause a forced reflow every time we checked `is_visible()` because it
would call `$("#recent_topics_view").is(":visible")`.
The reason for this is related to how browsers ship frames, the
process follows these steps:
JavaScript > style calculations > layout > paint > composite.
(The layout step is called Reflow in firefox.)
Typically, the browser will handle these steps in the most optimal
manner possible, delaying expensive operations until they're needed.
However, it is possible to cause the browser to perform a layout
earlier than necessary. An example of this is what we previously did:
When we call `top_left_corner.narrow_to_recent_topics()`, we ask to
add a class via `.addClass()`, this schedules a Style Recalculation,
then, when we call `message_view_header.make_message_view_header()` it
calls `recent_topics_util.is_visible()` which calls
`$("#recent_topics_view").is(":visible")`.
Before the browser can get this value, it realizes that our dom was
invalidated by `.addClass()` and so it must execute the scheduled
Style Recalculation and cause a layout.
This is called a forced synchronous layout.
This commit adds a JavaScript variable representing the visible state,
in order to prevent the above behavior.
This commit reduces the main thread run time of
`build_message_view_header` from 131.81 ms to 5.20 ms.
Unfortunately we still have the case where
`recent_topics_ui.revive_current_focus()` calls
`recent_topics_ui.set_table_focus()` which causes a reflow.
However, by eliminating this reflow we still save ~100ms.
(It's important to note that we only save this sometimes, as other
things can still cost us a reflow.)
Further reading: https://developers.google.com/web/fundamentals/
performance/rendering/avoid-large-complex-layouts-and-layout-thrashing
2021-11-05 21:21:43 +01:00
|
|
|
set_visible(true);
|
2020-07-30 16:18:47 +02:00
|
|
|
$("#message_view_header_underpadding").hide();
|
|
|
|
$(".header").css("padding-bottom", "0px");
|
2020-07-05 12:19:09 +02:00
|
|
|
|
2021-08-02 20:09:56 +02:00
|
|
|
narrow.hide_mark_as_read_turned_off_banner();
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
// We want to show `new stream message` instead of
|
|
|
|
// `new topic`, which we are already doing in this
|
|
|
|
// function. So, we reuse it here.
|
2021-05-11 22:04:35 +02:00
|
|
|
compose_closed_ui.update_buttons_for_recent_topics();
|
2020-07-05 12:19:09 +02:00
|
|
|
|
2021-02-28 20:21:01 +01:00
|
|
|
narrow_state.reset_current_filter();
|
|
|
|
narrow.set_narrow_title("Recent topics");
|
2020-07-05 12:19:09 +02:00
|
|
|
message_view_header.render_title_area();
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
complete_rerender();
|
|
|
|
}
|
2020-06-20 10:17:44 +02:00
|
|
|
|
2020-07-02 13:47:28 +02:00
|
|
|
function filter_buttons() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $("#recent_filters_group").children();
|
2020-07-02 13:47:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:48 +01:00
|
|
|
export function hide() {
|
2021-12-03 15:40:13 +01:00
|
|
|
// On firefox (and flaky on other browsers), focus
|
|
|
|
// remains on the focused element even after it is hidden. We
|
|
|
|
// forcefully blur it so that focus returns to the visible
|
|
|
|
// focused element.
|
|
|
|
const focused_element = $(document.activeElement);
|
|
|
|
if ($("#recent_topics_view").has(focused_element)) {
|
|
|
|
focused_element.trigger("blur");
|
|
|
|
}
|
|
|
|
|
recent_topics: Show message list underpadding before container.
Going through the description of commit
a150b9b0aed2f95c607f13accc2b08b08fb5f29d is highly recommended since
this is a related issue.
We had received a complaint on chat.zulip.org about navigation with
the keyboard `n` key being significantly slow (~5 seconds), the first
time `n` was pressed when starting from the recent topics view.
It was difficult to reproduce the amount of lag that was reported, but
running chrome with the profile tab set to 4x slowdown helped get
close to it.
Based on profiling from the original report, as well as locally with
chrome set to 4x slowdown, led to the realisation that recent topics
to stream navigation involved a lot of dom thrashing, and so this
series of commits aims to prevent this path from causing forced
reflows.
In this commit, we reorder the calls to $(...).show() in
recent_topics_ui.hide(), this prevents the first reflow in this path,
most likely because displaying message_feed_container before
message_view_header_underpadding was guaranteed to cause style
recalculations since the underpadding is visually above the message
container.
This causes a net 60 ms decrease in the first renarrow, an ~ 70 ms
increase in the second renarrow and an ~ 5 ms increase in the third
renarrow but, more importantly, it eliminates one reflow and sets on a
path where we can achieve strong gains in subsequent commits.
2021-11-14 11:22:44 +01:00
|
|
|
$("#message_view_header_underpadding").show();
|
2020-07-05 12:19:09 +02:00
|
|
|
$("#message_feed_container").show();
|
|
|
|
$("#recent_topics_view").hide();
|
recent_topics: Don't rely on ":visible" to avoid forced reflow.
Previously, navigating from any stream to the recent topics view would
cause a forced reflow every time we checked `is_visible()` because it
would call `$("#recent_topics_view").is(":visible")`.
The reason for this is related to how browsers ship frames, the
process follows these steps:
JavaScript > style calculations > layout > paint > composite.
(The layout step is called Reflow in firefox.)
Typically, the browser will handle these steps in the most optimal
manner possible, delaying expensive operations until they're needed.
However, it is possible to cause the browser to perform a layout
earlier than necessary. An example of this is what we previously did:
When we call `top_left_corner.narrow_to_recent_topics()`, we ask to
add a class via `.addClass()`, this schedules a Style Recalculation,
then, when we call `message_view_header.make_message_view_header()` it
calls `recent_topics_util.is_visible()` which calls
`$("#recent_topics_view").is(":visible")`.
Before the browser can get this value, it realizes that our dom was
invalidated by `.addClass()` and so it must execute the scheduled
Style Recalculation and cause a layout.
This is called a forced synchronous layout.
This commit adds a JavaScript variable representing the visible state,
in order to prevent the above behavior.
This commit reduces the main thread run time of
`build_message_view_header` from 131.81 ms to 5.20 ms.
Unfortunately we still have the case where
`recent_topics_ui.revive_current_focus()` calls
`recent_topics_ui.set_table_focus()` which causes a reflow.
However, by eliminating this reflow we still save ~100ms.
(It's important to note that we only save this sometimes, as other
things can still cost us a reflow.)
Further reading: https://developers.google.com/web/fundamentals/
performance/rendering/avoid-large-complex-layouts-and-layout-thrashing
2021-11-05 21:21:43 +01:00
|
|
|
set_visible(false);
|
2020-07-05 12:19:09 +02:00
|
|
|
|
2020-07-30 16:18:47 +02:00
|
|
|
$(".header").css("padding-bottom", "10px");
|
|
|
|
|
2020-07-05 12:19:09 +02:00
|
|
|
// This solves a bug with message_view_header
|
|
|
|
// being broken sometimes when we narrow
|
|
|
|
// to a filter and back to recent topics
|
|
|
|
// before it completely re-rerenders.
|
|
|
|
message_view_header.render_title_area();
|
|
|
|
|
|
|
|
// Fixes misaligned message_view and hidden
|
|
|
|
// floating_recipient_bar.
|
2021-05-17 10:01:02 +02:00
|
|
|
navbar_alerts.resize_app();
|
2020-07-05 12:19:09 +02:00
|
|
|
|
|
|
|
// This makes sure user lands on the selected message
|
|
|
|
// and not always at the top of the narrow.
|
|
|
|
navigate.plan_scroll_to_selected();
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|
2020-07-05 12:19:09 +02:00
|
|
|
|
2021-03-12 08:08:48 +01:00
|
|
|
function is_focus_at_last_table_row() {
|
|
|
|
const topic_rows = $("#recent_topics_table table tbody tr");
|
|
|
|
return row_focus === topic_rows.length - 1;
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:43:52 +02:00
|
|
|
export function focus_clicked_element(topic_row_index, col, topic_key) {
|
2021-03-13 16:45:47 +01:00
|
|
|
current_focus_elem = "table";
|
|
|
|
col_focus = col;
|
2021-09-06 15:22:32 +02:00
|
|
|
row_focus = topic_row_index;
|
2021-09-06 17:43:52 +02:00
|
|
|
|
|
|
|
if (col === COLUMNS.topic) {
|
|
|
|
last_visited_topic = topic_key;
|
|
|
|
}
|
2021-05-10 14:21:37 +02:00
|
|
|
// Set compose_closed_ui reply button text. The rest of the table
|
|
|
|
// focus logic should be a noop.
|
|
|
|
set_table_focus(row_focus, col_focus);
|
2021-03-13 16:45:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 15:15:33 +01:00
|
|
|
export function change_focused_element($elt, input_key) {
|
2020-06-20 10:17:44 +02:00
|
|
|
// Called from hotkeys.js; like all logic in that module,
|
|
|
|
// returning true will cause the caller to do
|
|
|
|
// preventDefault/stopPropagation; false will let the browser
|
|
|
|
// handle the key.
|
|
|
|
|
2021-03-16 15:15:33 +01:00
|
|
|
if ($elt.attr("id") === "recent_topics_search") {
|
2020-06-20 10:17:44 +02:00
|
|
|
// Since the search box a text area, we want the browser to handle
|
|
|
|
// Left/Right and selection within the widget; but if the user
|
|
|
|
// arrows off the edges, we should move focus to the adjacent widgets..
|
|
|
|
const textInput = $("#recent_topics_search").get(0);
|
|
|
|
const start = textInput.selectionStart;
|
|
|
|
const end = textInput.selectionEnd;
|
|
|
|
const text_length = textInput.value.length;
|
|
|
|
let is_selected = false;
|
|
|
|
if (end - start > 0) {
|
|
|
|
is_selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (input_key) {
|
2021-04-25 22:54:23 +02:00
|
|
|
// Allow browser to handle all
|
2021-03-10 13:56:10 +01:00
|
|
|
// character keypresses.
|
2020-07-02 14:02:51 +02:00
|
|
|
case "vim_left":
|
|
|
|
case "vim_right":
|
|
|
|
case "vim_down":
|
|
|
|
case "vim_up":
|
2021-03-10 13:56:10 +01:00
|
|
|
case "open_recent_topics":
|
2020-07-02 14:02:51 +02:00
|
|
|
return false;
|
|
|
|
case "shift_tab":
|
|
|
|
current_focus_elem = filter_buttons().last();
|
|
|
|
break;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "left_arrow":
|
|
|
|
if (start !== 0 || is_selected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
current_focus_elem = filter_buttons().last();
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "tab":
|
|
|
|
current_focus_elem = filter_buttons().first();
|
|
|
|
break;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "right_arrow":
|
|
|
|
if (end !== text_length || is_selected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
current_focus_elem = filter_buttons().first();
|
|
|
|
break;
|
|
|
|
case "down_arrow":
|
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
return true;
|
|
|
|
case "click":
|
2020-07-15 00:34:28 +02:00
|
|
|
// Note: current_focus_elem can be different here, so we just
|
2020-07-20 21:24:26 +02:00
|
|
|
// set current_focus_elem to the input box, we don't want .trigger("focus") on
|
2020-07-15 00:34:28 +02:00
|
|
|
// it since it is already focused.
|
2020-08-11 01:47:44 +02:00
|
|
|
// We only do this for search because we don't want the focus to
|
2020-07-15 00:34:28 +02:00
|
|
|
// go away from the input box when `revive_current_focus` is called
|
|
|
|
// on rerender when user is typing.
|
2020-07-15 02:14:03 +02:00
|
|
|
current_focus_elem = $("#recent_topics_search");
|
2021-05-10 14:05:30 +02:00
|
|
|
compose_closed_ui.set_standard_text_for_reply_button();
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
2021-01-04 12:02:59 +01:00
|
|
|
case "escape":
|
2021-04-02 12:36:02 +02:00
|
|
|
if (is_table_focused()) {
|
2021-03-09 13:21:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-04 12:02:59 +01:00
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
return true;
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
2021-03-16 15:15:33 +01:00
|
|
|
} else if ($elt.hasClass("btn-recent-filters")) {
|
2020-06-20 10:17:44 +02:00
|
|
|
switch (input_key) {
|
2021-03-13 16:45:47 +01:00
|
|
|
case "click":
|
2021-03-16 15:15:33 +01:00
|
|
|
current_focus_elem = $elt;
|
2021-03-13 16:45:47 +01:00
|
|
|
return true;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "shift_tab":
|
|
|
|
case "vim_left":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "left_arrow":
|
2021-03-16 15:15:33 +01:00
|
|
|
if (filter_buttons().first()[0] === $elt[0]) {
|
2020-07-15 02:14:03 +02:00
|
|
|
current_focus_elem = $("#recent_topics_search");
|
|
|
|
} else {
|
2021-03-16 15:15:33 +01:00
|
|
|
current_focus_elem = $elt.prev();
|
2020-07-15 02:14:03 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "tab":
|
|
|
|
case "vim_right":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "right_arrow":
|
2021-03-16 15:15:33 +01:00
|
|
|
if (filter_buttons().last()[0] === $elt[0]) {
|
2020-07-15 02:14:03 +02:00
|
|
|
current_focus_elem = $("#recent_topics_search");
|
|
|
|
} else {
|
2021-03-16 15:15:33 +01:00
|
|
|
current_focus_elem = $elt.next();
|
2020-07-15 02:14:03 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "vim_down":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "down_arrow":
|
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
return true;
|
2021-03-09 13:21:32 +01:00
|
|
|
case "escape":
|
2021-04-02 12:36:02 +02:00
|
|
|
if (is_table_focused()) {
|
2021-03-09 13:21:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_table_focus(row_focus, col_focus);
|
|
|
|
return true;
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
2021-04-02 12:36:02 +02:00
|
|
|
} else if (is_table_focused()) {
|
2020-06-20 10:17:44 +02:00
|
|
|
// For arrowing around the table of topics, we implement left/right
|
|
|
|
// wraparound. Going off the top or the bottom takes one
|
|
|
|
// to the navigation at the top (see set_table_focus).
|
|
|
|
switch (input_key) {
|
2021-03-09 13:21:32 +01:00
|
|
|
case "escape":
|
|
|
|
return false;
|
2020-09-23 09:43:59 +02:00
|
|
|
case "open_recent_topics":
|
|
|
|
set_default_focus();
|
|
|
|
return true;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "shift_tab":
|
|
|
|
case "vim_left":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "left_arrow":
|
|
|
|
col_focus -= 1;
|
|
|
|
if (col_focus < 0) {
|
|
|
|
col_focus = MAX_SELECTABLE_COLS - 1;
|
|
|
|
}
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "tab":
|
|
|
|
case "vim_right":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "right_arrow":
|
|
|
|
col_focus += 1;
|
|
|
|
if (col_focus >= MAX_SELECTABLE_COLS) {
|
|
|
|
col_focus = 0;
|
|
|
|
}
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "vim_down":
|
2021-03-12 08:08:48 +01:00
|
|
|
// We stop user at last table row
|
|
|
|
// so that user doesn't end up in
|
|
|
|
// input box where it is impossible to
|
|
|
|
// get out of using vim_up / vim_down
|
|
|
|
// keys. This also blocks the user from
|
|
|
|
// having `jjjj` typed in the input box
|
|
|
|
// when continuously pressing `j`.
|
|
|
|
if (is_focus_at_last_table_row()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
row_focus += 1;
|
|
|
|
break;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "down_arrow":
|
|
|
|
row_focus += 1;
|
|
|
|
break;
|
2020-07-02 14:02:51 +02:00
|
|
|
case "vim_up":
|
2021-03-12 08:08:48 +01:00
|
|
|
// See comment on vim_down.
|
|
|
|
// Similarly, blocks the user from
|
|
|
|
// having `kkkk` typed in the input box
|
|
|
|
// when continuously pressing `k`.
|
|
|
|
if (row_focus === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
row_focus -= 1;
|
|
|
|
break;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "up_arrow":
|
|
|
|
row_focus -= 1;
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
2021-05-17 02:36:29 +02:00
|
|
|
set_table_focus(row_focus, col_focus, true);
|
2020-06-20 10:17:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-09 13:21:32 +01:00
|
|
|
if (current_focus_elem && input_key !== "escape") {
|
2020-07-20 21:24:26 +02:00
|
|
|
current_focus_elem.trigger("focus");
|
2021-05-07 18:38:01 +02:00
|
|
|
if (current_focus_elem.hasClass("btn-recent-filters")) {
|
2021-05-10 14:05:30 +02:00
|
|
|
compose_closed_ui.set_standard_text_for_reply_button();
|
2021-05-07 18:38:01 +02:00
|
|
|
}
|
2021-03-09 13:21:32 +01:00
|
|
|
return true;
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 12:33:12 +02:00
|
|
|
return false;
|
2021-02-28 01:27:48 +01:00
|
|
|
}
|