zulip/static/js/pm_list.js

197 lines
6.2 KiB
JavaScript
Raw Normal View History

import $ from "jquery";
import _ from "lodash";
import * as pm_list_data from "./pm_list_data";
import * as pm_list_dom from "./pm_list_dom";
import * as resize from "./resize";
import * as topic_zoom from "./topic_zoom";
import * as ui from "./ui";
import * as ui_util from "./ui_util";
import * as vdom from "./vdom";
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
let prior_dom;
// This module manages the "Private messages" section in the upper
// left corner of the app. This was split out from stream_list.js.
let private_messages_collapsed = false;
// The private messages section can be zoomed in to view more messages.
// This keeps track of if we're zoomed in or not.
let zoomed = false;
function get_private_messages_section_header() {
return $(
".private_messages_container #private_messages_section #private_messages_section_header",
);
}
export function set_count(count) {
ui_util.update_unread_count_in_dom(get_private_messages_section_header(), count);
}
function close() {
private_messages_collapsed = true;
$("#toggle_private_messages_section_icon").removeClass("fa-caret-down");
$("#toggle_private_messages_section_icon").addClass("fa-caret-right");
update_private_messages();
}
export function _build_private_messages_list() {
const conversations = pm_list_data.get_conversations();
const pm_list_info = pm_list_data.get_list_info(zoomed);
const conversations_to_be_shown = pm_list_info.conversations_to_be_shown;
const more_conversations_unread_count = pm_list_info.more_conversations_unread_count;
const pm_list_nodes = conversations_to_be_shown.map((conversation) =>
pm_list_dom.keyed_pm_li(conversation),
);
const all_conversations_shown = conversations_to_be_shown.length === conversations.length;
if (!all_conversations_shown) {
pm_list_nodes.push(
pm_list_dom.more_private_conversations_li(more_conversations_unread_count),
);
}
const dom_ast = pm_list_dom.pm_ul(pm_list_nodes);
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
return dom_ast;
}
function set_dom_to(new_dom) {
const $container = ui.get_content_element($("#private_messages_list"));
function replace_content(html) {
$container.html(html);
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
}
function find() {
return $container.find("ul");
}
vdom.update(replace_content, find, new_dom, prior_dom);
prior_dom = new_dom;
}
export function update_private_messages() {
if (private_messages_collapsed) {
// In the collapsed state, we will still display the current
// conversation, to preserve the UI invariant that there's
// always something highlighted in the left sidebar.
const conversations = pm_list_data.get_conversations();
const active_conversation = conversations.find((conversation) => conversation.is_active);
if (active_conversation) {
const node = [pm_list_dom.keyed_pm_li(active_conversation)];
const new_dom = pm_list_dom.pm_ul(node);
set_dom_to(new_dom);
} else {
// Otherwise, empty the section.
$(".pm-list").empty();
prior_dom = undefined;
}
} else {
const new_dom = _build_private_messages_list();
set_dom_to(new_dom);
}
// Make sure to update the left sidebar heights after updating PMs.
setTimeout(resize.resize_stream_filters_container, 0);
}
export function expand() {
private_messages_collapsed = false;
$("#toggle_private_messages_section_icon").addClass("fa-caret-down");
$("#toggle_private_messages_section_icon").removeClass("fa-caret-right");
update_private_messages();
}
export function update_dom_with_unread_counts(counts) {
// In theory, we could support passing the counts object through
// to pm_list_data, rather than fetching it directly there. But
// it's not an important optimization, because it's unlikely a
// user would have 10,000s of unread PMs where it could matter.
update_private_messages();
// This is just the global unread count.
2017-08-22 16:33:20 +02:00
set_count(counts.private_message_count);
}
export function highlight_all_private_messages_view() {
$(".private_messages_container").addClass("active_private_messages_section");
}
function unhighlight_all_private_messages_view() {
$(".private_messages_container").removeClass("active_private_messages_section");
}
export function handle_narrow_activated(filter) {
const active_filter = filter;
const is_all_private_message_view = _.isEqual(active_filter.sorted_term_types(), [
"is-private",
]);
const narrow_to_private_messages_section = active_filter.operands("pm-with").length !== 0;
if (is_all_private_message_view) {
highlight_all_private_messages_view();
} else {
unhighlight_all_private_messages_view();
}
if (narrow_to_private_messages_section) {
update_private_messages();
}
}
export function handle_narrow_deactivated() {
// Since one can renarrow via the keyboard shortcut or similar, we
// avoid disturbing the zoomed state here.
unhighlight_all_private_messages_view();
update_private_messages();
}
export function is_private_messages_collapsed() {
return private_messages_collapsed;
}
export function toggle_private_messages_section() {
// change the state of PM section depending on the previous state.
if (private_messages_collapsed) {
expand();
} else {
close();
}
}
function zoom_in() {
zoomed = true;
if (topic_zoom.is_zoomed_in()) {
topic_zoom.zoom_out();
}
update_private_messages();
$(".private_messages_container").removeClass("zoom-out").addClass("zoom-in");
$("#streams_list").hide();
$(".left-sidebar .right-sidebar-items").hide();
}
function zoom_out() {
zoomed = false;
update_private_messages();
$(".private_messages_container").removeClass("zoom-in").addClass("zoom-out");
$("#streams_list").show();
$(".left-sidebar .right-sidebar-items").show();
}
export function initialize() {
$(".private_messages_container").on("click", "#show_more_private_messages", (e) => {
e.stopPropagation();
e.preventDefault();
zoom_in();
});
$(".private_messages_container").on("click", "#hide_more_private_messages", (e) => {
e.stopPropagation();
e.preventDefault();
zoom_out();
});
}