pm_list: Handle narrow functions separately for pm_section.

This commit is a preparatory commit for #20870, it introduces
`handle_narrow_deactivated` and `handle_narrow_activated`
functions in pm_list.js, separately from top_left_corner.js,
to reduce the complexity of handling private messages section
separately.
This commit is contained in:
jai2201 2022-08-13 02:31:44 +05:30 committed by Tim Abbott
parent 48d2783559
commit aa700ff59d
4 changed files with 40 additions and 87 deletions

View File

@ -6,69 +6,18 @@ const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery"); const $ = require("../zjsunit/zjquery");
const pm_list = mock_esm("../../static/js/pm_list");
mock_esm("../../static/js/resize", { mock_esm("../../static/js/resize", {
resize_stream_filters_container: () => {}, resize_stream_filters_container: () => {},
}); });
const {Filter} = zrequire("../js/filter"); const {Filter} = zrequire("../js/filter");
const people = zrequire("people");
const top_left_corner = zrequire("top_left_corner"); const top_left_corner = zrequire("top_left_corner");
run_test("narrowing", ({override}) => { run_test("narrowing", () => {
let filter = new Filter([{operator: "is", operand: "mentioned"}]);
// activating narrow // activating narrow
let pm_expanded;
let pm_closed;
override(pm_list, "close", () => {
pm_closed = true;
});
override(pm_list, "expand", () => {
pm_expanded = true;
});
assert.ok(!pm_expanded);
let filter = new Filter([{operator: "is", operand: "private"}]);
top_left_corner.handle_narrow_activated(filter);
assert.ok(pm_expanded);
const alice = {
email: "alice@example.com",
user_id: 1,
full_name: "Alice Smith",
};
const bob = {
email: "bob@example.com",
user_id: 2,
full_name: "Bob Patel",
};
people.add_active_user(alice);
people.add_active_user(bob);
pm_expanded = false;
filter = new Filter([{operator: "pm-with", operand: "alice@example.com"}]);
top_left_corner.handle_narrow_activated(filter);
assert.ok(pm_expanded);
pm_expanded = false;
filter = new Filter([{operator: "pm-with", operand: "bob@example.com,alice@example.com"}]);
top_left_corner.handle_narrow_activated(filter);
assert.ok(pm_expanded);
pm_expanded = false;
filter = new Filter([{operator: "pm-with", operand: "not@valid.com"}]);
top_left_corner.handle_narrow_activated(filter);
assert.ok(!pm_expanded);
pm_expanded = false;
people.deactivate(alice);
filter = new Filter([{operator: "pm-with", operand: "alice@example.com"}]);
top_left_corner.handle_narrow_activated(filter);
assert.ok(pm_expanded);
filter = new Filter([{operator: "is", operand: "mentioned"}]);
top_left_corner.handle_narrow_activated(filter); top_left_corner.handle_narrow_activated(filter);
assert.ok($(".top_left_mentions").hasClass("active-filter")); assert.ok($(".top_left_mentions").hasClass("active-filter"));
@ -82,15 +31,12 @@ run_test("narrowing", ({override}) => {
// deactivating narrow // deactivating narrow
pm_closed = false;
top_left_corner.handle_narrow_deactivated(); top_left_corner.handle_narrow_deactivated();
assert.ok($(".top_left_all_messages").hasClass("active-filter")); assert.ok($(".top_left_all_messages").hasClass("active-filter"));
assert.ok(!$(".top_left_mentions").hasClass("active-filter")); assert.ok(!$(".top_left_mentions").hasClass("active-filter"));
assert.ok(!$(".top_left_private_messages").hasClass("active-filter"));
assert.ok(!$(".top_left_starred_messages").hasClass("active-filter")); assert.ok(!$(".top_left_starred_messages").hasClass("active-filter"));
assert.ok(!$(".top_left_recent_topics").hasClass("active-filter")); assert.ok(!$(".top_left_recent_topics").hasClass("active-filter"));
assert.ok(pm_closed);
set_global("setTimeout", (f) => { set_global("setTimeout", (f) => {
f(); f();
@ -98,7 +44,6 @@ run_test("narrowing", ({override}) => {
top_left_corner.narrow_to_recent_topics(); top_left_corner.narrow_to_recent_topics();
assert.ok(!$(".top_left_all_messages").hasClass("active-filter")); assert.ok(!$(".top_left_all_messages").hasClass("active-filter"));
assert.ok(!$(".top_left_mentions").hasClass("active-filter")); assert.ok(!$(".top_left_mentions").hasClass("active-filter"));
assert.ok(!$(".top_left_private_messages").hasClass("active-filter"));
assert.ok(!$(".top_left_starred_messages").hasClass("active-filter")); assert.ok(!$(".top_left_starred_messages").hasClass("active-filter"));
assert.ok($(".top_left_recent_topics").hasClass("active-filter")); assert.ok($(".top_left_recent_topics").hasClass("active-filter"));
}); });

View File

@ -25,6 +25,7 @@ import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications"; import * as notifications from "./notifications";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as pm_list from "./pm_list";
import * as recent_topics_ui from "./recent_topics_ui"; import * as recent_topics_ui from "./recent_topics_ui";
import * as recent_topics_util from "./recent_topics_util"; import * as recent_topics_util from "./recent_topics_util";
import * as resize from "./resize"; import * as resize from "./resize";
@ -570,6 +571,7 @@ export function activate(raw_operators, opts) {
const current_filter = narrow_state.filter(); const current_filter = narrow_state.filter();
top_left_corner.handle_narrow_activated(current_filter); top_left_corner.handle_narrow_activated(current_filter);
pm_list.handle_narrow_activated(current_filter);
stream_list.handle_narrow_activated(current_filter); stream_list.handle_narrow_activated(current_filter);
typing_events.render_notifications_for_narrow(); typing_events.render_notifications_for_narrow();
message_view_header.initialize(); message_view_header.initialize();
@ -980,6 +982,7 @@ function handle_post_narrow_deactivate_processes() {
} }
top_left_corner.handle_narrow_deactivated(); top_left_corner.handle_narrow_deactivated();
pm_list.handle_narrow_deactivated();
stream_list.handle_narrow_deactivated(); stream_list.handle_narrow_deactivated();
compose_closed_ui.update_buttons_for_stream(); compose_closed_ui.update_buttons_for_stream();
message_edit.handle_narrow_deactivated(); message_edit.handle_narrow_deactivated();

View File

@ -1,6 +1,7 @@
import $ from "jquery"; import $ from "jquery";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";
import * as people from "./people";
import * as pm_list_data from "./pm_list_data"; import * as pm_list_data from "./pm_list_data";
import * as pm_list_dom from "./pm_list_dom"; import * as pm_list_dom from "./pm_list_dom";
import * as stream_popover from "./stream_popover"; import * as stream_popover from "./stream_popover";
@ -74,3 +75,36 @@ export function update_dom_with_unread_counts(counts) {
update_private_messages(); update_private_messages();
set_count(counts.private_message_count); set_count(counts.private_message_count);
} }
function should_expand_pm_list(filter) {
const op_is = filter.operands("is");
if (op_is.length >= 1 && op_is.includes("private")) {
return true;
}
const op_pm = filter.operands("pm-with");
if (op_pm.length !== 1) {
return false;
}
const emails_strings = op_pm[0];
const emails = emails_strings.split(",");
const has_valid_emails = people.is_valid_bulk_emails_for_compose(emails);
return has_valid_emails;
}
export function handle_narrow_activated(filter) {
if (should_expand_pm_list(filter)) {
expand();
} else {
close();
}
}
export function handle_narrow_deactivated() {
close();
}

View File

@ -1,6 +1,5 @@
import $ from "jquery"; import $ from "jquery";
import * as people from "./people";
import * as pm_list from "./pm_list"; import * as pm_list from "./pm_list";
import * as resize from "./resize"; import * as resize from "./resize";
import * as ui_util from "./ui_util"; import * as ui_util from "./ui_util";
@ -36,27 +35,6 @@ function deselect_top_left_corner_items() {
remove($(".top_left_recent_topics")); remove($(".top_left_recent_topics"));
} }
function should_expand_pm_list(filter) {
const op_is = filter.operands("is");
if (op_is.length >= 1 && op_is.includes("private")) {
return true;
}
const op_pm = filter.operands("pm-with");
if (op_pm.length !== 1) {
return false;
}
const emails_strings = op_pm[0];
const emails = emails_strings.split(",");
const has_valid_emails = people.is_valid_bulk_emails_for_compose(emails);
return has_valid_emails;
}
export function handle_narrow_activated(filter) { export function handle_narrow_activated(filter) {
deselect_top_left_corner_items(); deselect_top_left_corner_items();
@ -84,17 +62,10 @@ export function handle_narrow_activated(filter) {
$filter_li.addClass("active-filter"); $filter_li.addClass("active-filter");
} }
} }
if (should_expand_pm_list(filter)) {
pm_list.expand();
} else {
pm_list.close();
}
} }
export function handle_narrow_deactivated() { export function handle_narrow_deactivated() {
deselect_top_left_corner_items(); deselect_top_left_corner_items();
pm_list.close();
const $filter_li = $(".top_left_all_messages"); const $filter_li = $(".top_left_all_messages");
$filter_li.addClass("active-filter"); $filter_li.addClass("active-filter");