2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2022-09-13 13:15:57 +02:00
|
|
|
import _ from "lodash";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2022-03-15 17:45:35 +01:00
|
|
|
import * as pm_list_data from "./pm_list_data";
|
2021-02-28 00:44:12 +01:00
|
|
|
import * as pm_list_dom from "./pm_list_dom";
|
2022-10-04 23:33:51 +02:00
|
|
|
import * as resize from "./resize";
|
2022-11-02 05:16:11 +01:00
|
|
|
import * as topic_zoom from "./topic_zoom";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-04-09 09:20:15 +02:00
|
|
|
import * as ui_util from "./ui_util";
|
2021-02-28 00:44:12 +01:00
|
|
|
import * as vdom from "./vdom";
|
2020-08-18 19:53:22 +02:00
|
|
|
|
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;
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
// This module manages the "Private messages" section in the upper
|
2016-11-11 12:33:51 +01:00
|
|
|
// left corner of the app. This was split out from stream_list.js.
|
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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;
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
function get_private_messages_section_header() {
|
|
|
|
return $(
|
|
|
|
".private_messages_container #private_messages_section #private_messages_section_header",
|
|
|
|
);
|
2016-11-14 17:14:59 +01:00
|
|
|
}
|
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
export function set_count(count) {
|
|
|
|
ui_util.update_unread_count_in_dom(get_private_messages_section_header(), count);
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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();
|
2021-02-28 00:44:12 +01:00
|
|
|
}
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2021-02-28 00:44:12 +01:00
|
|
|
export function _build_private_messages_list() {
|
2022-04-07 00:29:10 +02:00
|
|
|
const conversations = pm_list_data.get_conversations();
|
2022-09-13 13:15:57 +02:00
|
|
|
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;
|
2021-02-28 00:44:12 +01:00
|
|
|
}
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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
|
|
|
}
|
2019-03-27 18:04:51 +01:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
function find() {
|
|
|
|
return $container.find("ul");
|
|
|
|
}
|
2020-01-31 21:58:28 +01:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
vdom.update(replace_content, find, new_dom, prior_dom);
|
|
|
|
prior_dom = new_dom;
|
|
|
|
}
|
2020-01-31 21:58:28 +01:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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;
|
2020-01-31 21:58:28 +01:00
|
|
|
}
|
2022-09-13 13:15:57 +02:00
|
|
|
} else {
|
|
|
|
const new_dom = _build_private_messages_list();
|
|
|
|
set_dom_to(new_dom);
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
2022-10-04 23:33:51 +02:00
|
|
|
// Make sure to update the left sidebar heights after updating PMs.
|
|
|
|
setTimeout(resize.resize_stream_filters_container, 0);
|
2021-02-28 00:44:12 +01:00
|
|
|
}
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2021-02-28 00:44:12 +01:00
|
|
|
export function expand() {
|
2022-09-13 13:15:57 +02:00
|
|
|
private_messages_collapsed = false;
|
|
|
|
$("#toggle_private_messages_section_icon").addClass("fa-caret-down");
|
|
|
|
$("#toggle_private_messages_section_icon").removeClass("fa-caret-right");
|
2021-02-28 00:44:12 +01:00
|
|
|
update_private_messages();
|
|
|
|
}
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2021-02-28 00:44:12 +01:00
|
|
|
export function update_dom_with_unread_counts(counts) {
|
2022-09-13 13:15:57 +02:00
|
|
|
// 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.
|
2021-02-28 00:44:12 +01:00
|
|
|
update_private_messages();
|
2022-09-13 13:15:57 +02:00
|
|
|
// This is just the global unread count.
|
2017-08-22 16:33:20 +02:00
|
|
|
set_count(counts.private_message_count);
|
2021-02-28 00:44:12 +01:00
|
|
|
}
|
2022-08-12 23:01:44 +02:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
export function highlight_all_private_messages_view() {
|
|
|
|
$(".private_messages_container").addClass("active_private_messages_section");
|
|
|
|
}
|
2022-08-12 23:01:44 +02:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
function unhighlight_all_private_messages_view() {
|
|
|
|
$(".private_messages_container").removeClass("active_private_messages_section");
|
|
|
|
}
|
2022-08-12 23:01:44 +02:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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();
|
2022-08-12 23:01:44 +02:00
|
|
|
}
|
2022-09-13 13:15:57 +02:00
|
|
|
if (narrow_to_private_messages_section) {
|
|
|
|
update_private_messages();
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 23:01:44 +02:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
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();
|
|
|
|
}
|
2022-08-12 23:01:44 +02:00
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
export function is_private_messages_collapsed() {
|
|
|
|
return private_messages_collapsed;
|
2022-08-12 23:01:44 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
export function toggle_private_messages_section() {
|
|
|
|
// change the state of PM section depending on the previous state.
|
|
|
|
if (private_messages_collapsed) {
|
2022-08-12 23:01:44 +02:00
|
|
|
expand();
|
|
|
|
} else {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:15:57 +02:00
|
|
|
function zoom_in() {
|
|
|
|
zoomed = true;
|
2022-11-02 05:16:11 +01:00
|
|
|
if (topic_zoom.is_zoomed_in()) {
|
|
|
|
topic_zoom.zoom_out();
|
|
|
|
}
|
2022-09-13 13:15:57 +02:00
|
|
|
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();
|
|
|
|
});
|
2022-08-12 23:01:44 +02:00
|
|
|
}
|