Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
refactor: Extract narrow_banner module.
Steve Howellc67f82b07344
diff --git a/frontend_tests/node_tests/narrow.js b/frontend_tests/node_tests/narrow.jsindex a7312c4e0fa..1b93a07c655 100644--- a/frontend_tests/node_tests/narrow.js+++ b/frontend_tests/node_tests/narrow.js@@ -14,6 +14,7 @@ const stream_topic_history = mock_esm("../../static/js/stream_topic_history");const hash_util = zrequire("hash_util");const compose_state = zrequire("compose_state");+const narrow_banner = zrequire("narrow_banner");const narrow_state = zrequire("narrow_state");const people = zrequire("people");const stream_data = zrequire("stream_data");@@ -73,7 +74,7 @@ run_test("uris", () => {run_test("show_empty_narrow_message", () => {narrow_state.reset_current_filter();- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert.equal($(".empty_feed_notice").visible(), false);assert($("#empty_narrow_message").visible());assert.equal(@@ -83,55 +84,55 @@ run_test("show_empty_narrow_message", () => {// for non-existent or private streamset_filter([["stream", "Foo"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#nonsubbed_private_nonexistent_stream_narrow_message").visible());// for non sub public streamstream_data.add_sub({name: "ROME", stream_id: 99});stream_data.update_calculated_fields(stream_data.get_sub("ROME"));set_filter([["stream", "Rome"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#nonsubbed_stream_narrow_message").visible());set_filter([["is", "starred"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_star_narrow_message").visible());set_filter([["is", "mentioned"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_narrow_all_mentioned").visible());set_filter([["is", "private"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_narrow_all_private_message").visible());set_filter([["is", "unread"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#no_unread_narrow_message").visible());set_filter([["pm-with", ["Yo"]]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#non_existing_user").visible());people.add_active_user(alice);set_filter([["pm-with", ["[email protected]", "Yo"]]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#non_existing_users").visible());set_filter([["pm-with", "[email protected]"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_narrow_private_message").visible());set_filter([["group-pm-with", "[email protected]"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_narrow_group_private_message").visible());set_filter([["sender", "[email protected]"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#silent_user").visible());set_filter([["sender", "[email protected]"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#non_existing_user").visible());const display = $("#empty_search_stop_words_string");@@ -142,7 +143,7 @@ run_test("show_empty_narrow_message", () => {};set_filter([["search", "grail"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(items.length, 2);@@ -163,7 +164,7 @@ run_test("show_search_stopwords", () => {};set_filter([["search", "what about grail"]]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(items.length, 3);@@ -176,7 +177,7 @@ run_test("show_search_stopwords", () => {["stream", "streamA"],["search", "what about grail"],]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(items.length, 4);@@ -191,7 +192,7 @@ run_test("show_search_stopwords", () => {["topic", "topicA"],["search", "what about grail"],]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(items.length, 4);@@ -212,7 +213,7 @@ run_test("show_invalid_narrow_message", () => {["stream", "streamA"],["stream", "streamB"],]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(display.text(),@@ -223,7 +224,7 @@ run_test("show_invalid_narrow_message", () => {["topic", "topicA"],["topic", "topicB"],]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(display.text(),@@ -237,7 +238,7 @@ run_test("show_invalid_narrow_message", () => {["sender", "[email protected]"],["sender", "[email protected]"],]);- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();assert($("#empty_search_narrow_message").visible());assert.equal(display.text(),diff --git a/static/js/message_fetch.js b/static/js/message_fetch.jsindex 9ffb3a8be49..528396094f6 100644--- a/static/js/message_fetch.js+++ b/static/js/message_fetch.js@@ -7,7 +7,7 @@ import * as message_list from "./message_list";import * as message_scroll from "./message_scroll";import * as message_store from "./message_store";import * as message_util from "./message_util";-import * as narrow from "./narrow";+import * as narrow_banner from "./narrow_banner";import {page_params} from "./page_params";import * as people from "./people";import * as pm_list from "./pm_list";@@ -45,7 +45,7 @@ function process_result(data, opts) {) {// Even after trying to load more messages, we have no// messages to display in this narrow.- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();}messages = messages.map((message) => {diff --git a/static/js/message_list.js b/static/js/message_list.jsindex 63a08108a75..0c96f36345b 100644--- a/static/js/message_list.js+++ b/static/js/message_list.js@@ -5,7 +5,7 @@ import * as blueslip from "./blueslip";import {i18n} from "./i18n";import {MessageListData} from "./message_list_data";import {MessageListView} from "./message_list_view";-import * as narrow from "./narrow";+import * as narrow_banner from "./narrow_banner";import * as narrow_state from "./narrow_state";import {page_params} from "./page_params";import * as stream_data from "./stream_data";@@ -74,7 +74,7 @@ export class MessageList {// If adding some new messages to the message tables caused// our current narrow to no longer be empty, hide the empty// feed placeholder text.- narrow.hide_empty_narrow_message();+ narrow_banner.hide_empty_narrow_message();}if (this === narrowed && !this.empty() && this.selected_id() === -1) {@@ -363,9 +363,9 @@ export class MessageList {if (this === narrowed) {if (this.empty()) {- narrow.show_empty_narrow_message();+ narrow_banner.show_empty_narrow_message();} else {- narrow.hide_empty_narrow_message();+ narrow_banner.hide_empty_narrow_message();}}this.rerender_view();diff --git a/static/js/message_scroll.js b/static/js/message_scroll.jsindex 76bf8a0bf81..3bafc77fcee 100644--- a/static/js/message_scroll.js+++ b/static/js/message_scroll.js@@ -6,7 +6,7 @@ import * as hash_util from "./hash_util";import * as loading from "./loading";import * as message_fetch from "./message_fetch";import * as message_viewport from "./message_viewport";-import * as narrow from "./narrow";+import * as narrow_banner from "./narrow_banner";import * as narrow_state from "./narrow_state";import * as recent_topics from "./recent_topics";import * as unread_ops from "./unread_ops";@@ -65,7 +65,7 @@ export function hide_indicators() {export function show_history_limit_notice() {$(".top-messages-logo").hide();$(".history-limited-box").show();- narrow.hide_empty_narrow_message();+ narrow_banner.hide_empty_narrow_message();}export function hide_history_limit_notice() {diff --git a/static/js/narrow.js b/static/js/narrow.jsindex 9fc0510b569..0977143bc9f 100644--- a/static/js/narrow.js+++ b/static/js/narrow.js@@ -9,7 +9,6 @@ import * as compose_state from "./compose_state";import * as condense from "./condense";import {Filter} from "./filter";import * as hashchange from "./hashchange";-import {i18n} from "./i18n";import * as message_edit from "./message_edit";import * as message_fetch from "./message_fetch";import * as message_list from "./message_list";@@ -17,6 +16,7 @@ import {MessageListData} from "./message_list_data";import * as message_scroll from "./message_scroll";import * as message_store from "./message_store";import * as message_view_header from "./message_view_header";+import * as narrow_banner from "./narrow_banner";import * as narrow_state from "./narrow_state";import * as notifications from "./notifications";import {page_params} from "./page_params";@@ -847,7 +847,7 @@ export function deactivate(coming_from_recent_topics = false) {narrow_state.reset_current_filter();- hide_empty_narrow_message();+ narrow_banner.hide_empty_narrow_message();$("body").removeClass("narrowed_view");$("#zfilt").removeClass("focused_table");@@ -902,187 +902,3 @@ export function deactivate(coming_from_recent_topics = false) {report_unnarrow_time();});}--function set_invalid_narrow_message(invalid_narrow_message) {- const search_string_display = $("#empty_search_stop_words_string");- search_string_display.text(invalid_narrow_message);-}--function show_search_query() {- // when search bar contains multiple filters, only show search queries- const current_filter = narrow_state.filter();- const search_query = current_filter.operands("search")[0];- const query_words = search_query.split(" ");-- const search_string_display = $("#empty_search_stop_words_string");- let query_contains_stop_words = false;-- // Also removes previous search_string if any- search_string_display.text(i18n.t("You searched for:"));-- // Add in stream:foo and topic:bar if present- if (current_filter.has_operator("stream") || current_filter.has_operator("topic")) {- let stream_topic_string = "";- const stream = current_filter.operands("stream")[0];- const topic = current_filter.operands("topic")[0];- if (stream) {- stream_topic_string = "stream: " + stream;- }- if (topic) {- stream_topic_string = stream_topic_string + " topic: " + topic;- }-- search_string_display.append(" ");- search_string_display.append($("<span>").text(stream_topic_string));- }-- for (const query_word of query_words) {- search_string_display.append(" ");-- // if query contains stop words, it is enclosed by a <del> tag- if (page_params.stop_words.includes(query_word)) {- // stop_words do not need sanitization so this is unnecessary but it is fail-safe.- search_string_display.append($("<del>").text(query_word));- query_contains_stop_words = true;- } else {- // We use .text("...") to sanitize the user-given query_string.- search_string_display.append($("<span>").text(query_word));- }- }-- if (query_contains_stop_words) {- search_string_display.html(- i18n.t("Some common words were excluded from your search.") +- "<br/>" +- search_string_display.html(),- );- }-}--function pick_empty_narrow_banner() {- const default_banner = $("#empty_narrow_message");-- const current_filter = narrow_state.filter();-- if (current_filter === undefined) {- return default_banner;- }-- const first_term = current_filter.operators()[0];- const first_operator = first_term.operator;- const first_operand = first_term.operand;- const num_operators = current_filter.operators().length;-- if (num_operators !== 1) {- // For invalid-multi-operator narrows, we display an invalid narrow message- const streams = current_filter.operands("stream");-- let invalid_narrow_message = "";- // No message can have multiple streams- if (streams.length > 1) {- invalid_narrow_message = i18n.t(- "You are searching for messages that belong to more than one stream, which is not possible.",- );- }- // No message can have multiple topics- if (current_filter.operands("topic").length > 1) {- invalid_narrow_message = i18n.t(- "You are searching for messages that belong to more than one topic, which is not possible.",- );- }- // No message can have multiple senders- if (current_filter.operands("sender").length > 1) {- invalid_narrow_message = i18n.t(- "You are searching for messages that are sent by more than one person, which is not possible.",- );- }- if (invalid_narrow_message !== "") {- set_invalid_narrow_message(invalid_narrow_message);- return $("#empty_search_narrow_message");- }-- // For empty stream searches within other narrows, we display the stop words- if (current_filter.operands("search").length > 0) {- show_search_query();- return $("#empty_search_narrow_message");- }- // For other multi-operator narrows, we just use the default banner- return default_banner;- } else if (first_operator === "is") {- if (first_operand === "starred") {- // You have no starred messages.- return $("#empty_star_narrow_message");- } else if (first_operand === "mentioned") {- return $("#empty_narrow_all_mentioned");- } else if (first_operand === "private") {- // You have no private messages.- return $("#empty_narrow_all_private_message");- } else if (first_operand === "unread") {- // You have no unread messages.- return $("#no_unread_narrow_message");- }- } else if (first_operator === "stream" && !stream_data.is_subscribed(first_operand)) {- // You are narrowed to a stream which does not exist or is a private stream- // in which you were never subscribed.-- function should_display_subscription_button() {- const stream_name = narrow_state.stream();-- if (!stream_name) {- return false;- }-- const stream_sub = stream_data.get_sub(first_operand);- return stream_sub && stream_sub.should_display_subscription_button;- }-- if (should_display_subscription_button()) {- return $("#nonsubbed_stream_narrow_message");- }-- return $("#nonsubbed_private_nonexistent_stream_narrow_message");- } else if (first_operator === "search") {- // You are narrowed to empty search results.- show_search_query();- return $("#empty_search_narrow_message");- } else if (first_operator === "pm-with") {- if (!people.is_valid_bulk_emails_for_compose(first_operand.split(","))) {- if (!first_operand.includes(",")) {- return $("#non_existing_user");- }- return $("#non_existing_users");- }- if (!first_operand.includes(",")) {- // You have no private messages with this person- if (people.is_current_user(first_operand)) {- return $("#empty_narrow_self_private_message");- }- return $("#empty_narrow_private_message");- }- return $("#empty_narrow_multi_private_message");- } else if (first_operator === "sender") {- if (people.get_by_email(first_operand)) {- return $("#silent_user");- }- return $("#non_existing_user");- } else if (first_operator === "group-pm-with") {- return $("#empty_narrow_group_private_message");- }- return default_banner;-}--export function show_empty_narrow_message() {- $(".empty_feed_notice").hide();- pick_empty_narrow_banner().show();- $("#left_bar_compose_reply_button_big").attr(- "title",- i18n.t("There are no messages to reply to."),- );- $("#left_bar_compose_reply_button_big").prop("disabled", true);-}--export function hide_empty_narrow_message() {- $(".empty_feed_notice").hide();- $("#left_bar_compose_reply_button_big").attr("title", i18n.t("Reply (r)"));- $("#left_bar_compose_reply_button_big").prop("disabled", false);-}diff --git a/static/js/narrow_banner.js b/static/js/narrow_banner.jsnew file mode 100644index 00000000000..dd5d6430611--- /dev/null+++ b/static/js/narrow_banner.js@@ -0,0 +1,188 @@+import $ from "jquery";++import {i18n} from "./i18n";+import * as narrow_state from "./narrow_state";+import {page_params} from "./page_params";+import * as people from "./people";+import * as stream_data from "./stream_data";++function set_invalid_narrow_message(invalid_narrow_message) {+ const search_string_display = $("#empty_search_stop_words_string");+ search_string_display.text(invalid_narrow_message);+}++function show_search_query() {+ // when search bar contains multiple filters, only show search queries+ const current_filter = narrow_state.filter();+ const search_query = current_filter.operands("search")[0];+ const query_words = search_query.split(" ");++ const search_string_display = $("#empty_search_stop_words_string");+ let query_contains_stop_words = false;++ // Also removes previous search_string if any+ search_string_display.text(i18n.t("You searched for:"));++ // Add in stream:foo and topic:bar if present+ if (current_filter.has_operator("stream") || current_filter.has_operator("topic")) {+ let stream_topic_string = "";+ const stream = current_filter.operands("stream")[0];+ const topic = current_filter.operands("topic")[0];+ if (stream) {+ stream_topic_string = "stream: " + stream;+ }+ if (topic) {+ stream_topic_string = stream_topic_string + " topic: " + topic;+ }++ search_string_display.append(" ");+ search_string_display.append($("<span>").text(stream_topic_string));+ }++ for (const query_word of query_words) {+ search_string_display.append(" ");++ // if query contains stop words, it is enclosed by a <del> tag+ if (page_params.stop_words.includes(query_word)) {+ // stop_words do not need sanitization so this is unnecessary but it is fail-safe.+ search_string_display.append($("<del>").text(query_word));+ query_contains_stop_words = true;+ } else {+ // We use .text("...") to sanitize the user-given query_string.+ search_string_display.append($("<span>").text(query_word));+ }+ }++ if (query_contains_stop_words) {+ const preamble = i18n.t("Some common words were excluded from your search.");+ search_string_display.html(preamble + "<br/>" + search_string_display.html());+ }+}++function pick_empty_narrow_banner() {+ const default_banner = $("#empty_narrow_message");++ const current_filter = narrow_state.filter();++ if (current_filter === undefined) {+ return default_banner;+ }++ const first_term = current_filter.operators()[0];+ const first_operator = first_term.operator;+ const first_operand = first_term.operand;+ const num_operators = current_filter.operators().length;++ if (num_operators !== 1) {+ // For invalid-multi-operator narrows, we display an invalid narrow message+ const streams = current_filter.operands("stream");++ let invalid_narrow_message = "";+ // No message can have multiple streams+ if (streams.length > 1) {+ invalid_narrow_message = i18n.t(+ "You are searching for messages that belong to more than one stream, which is not possible.",+ );+ }+ // No message can have multiple topics+ if (current_filter.operands("topic").length > 1) {+ invalid_narrow_message = i18n.t(+ "You are searching for messages that belong to more than one topic, which is not possible.",+ );+ }+ // No message can have multiple senders+ if (current_filter.operands("sender").length > 1) {+ invalid_narrow_message = i18n.t(+ "You are searching for messages that are sent by more than one person, which is not possible.",+ );+ }+ if (invalid_narrow_message !== "") {+ set_invalid_narrow_message(invalid_narrow_message);+ return $("#empty_search_narrow_message");+ }++ // For empty stream searches within other narrows, we display the stop words+ if (current_filter.operands("search").length > 0) {+ show_search_query();+ return $("#empty_search_narrow_message");+ }+ // For other multi-operator narrows, we just use the default banner+ return default_banner;+ } else if (first_operator === "is") {+ if (first_operand === "starred") {+ // You have no starred messages.+ return $("#empty_star_narrow_message");+ } else if (first_operand === "mentioned") {+ return $("#empty_narrow_all_mentioned");+ } else if (first_operand === "private") {+ // You have no private messages.+ return $("#empty_narrow_all_private_message");+ } else if (first_operand === "unread") {+ // You have no unread messages.+ return $("#no_unread_narrow_message");+ }+ } else if (first_operator === "stream" && !stream_data.is_subscribed(first_operand)) {+ // You are narrowed to a stream which does not exist or is a private stream+ // in which you were never subscribed.++ function should_display_subscription_button() {+ const stream_name = narrow_state.stream();++ if (!stream_name) {+ return false;+ }++ const stream_sub = stream_data.get_sub(first_operand);+ return stream_sub && stream_sub.should_display_subscription_button;+ }++ if (should_display_subscription_button()) {+ return $("#nonsubbed_stream_narrow_message");+ }++ return $("#nonsubbed_private_nonexistent_stream_narrow_message");+ } else if (first_operator === "search") {+ // You are narrowed to empty search results.+ show_search_query();+ return $("#empty_search_narrow_message");+ } else if (first_operator === "pm-with") {+ if (!people.is_valid_bulk_emails_for_compose(first_operand.split(","))) {+ if (!first_operand.includes(",")) {+ return $("#non_existing_user");+ }+ return $("#non_existing_users");+ }+ if (!first_operand.includes(",")) {+ // You have no private messages with this person+ if (people.is_current_user(first_operand)) {+ return $("#empty_narrow_self_private_message");+ }+ return $("#empty_narrow_private_message");+ }+ return $("#empty_narrow_multi_private_message");+ } else if (first_operator === "sender") {+ if (people.get_by_email(first_operand)) {+ return $("#silent_user");+ }+ return $("#non_existing_user");+ } else if (first_operator === "group-pm-with") {+ return $("#empty_narrow_group_private_message");+ }+ return default_banner;+}++export function show_empty_narrow_message() {+ $(".empty_feed_notice").hide();+ pick_empty_narrow_banner().show();+ $("#left_bar_compose_reply_button_big").attr(+ "title",+ i18n.t("There are no messages to reply to."),+ );+ $("#left_bar_compose_reply_button_big").prop("disabled", true);+}++export function hide_empty_narrow_message() {+ $(".empty_feed_notice").hide();+ $("#left_bar_compose_reply_button_big").attr("title", i18n.t("Reply (r)"));+ $("#left_bar_compose_reply_button_big").prop("disabled", false);+}diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.pyindex f86f7629bd5..425f90aa189 100644--- a/tools/linter_lib/custom_check.py+++ b/tools/linter_lib/custom_check.py@@ -132,12 +132,6 @@ js_rules = RuleList({"pattern": r"i18n\.t\(.+\).*\+","description": "Do not concatenate i18n strings",- "exclude_line": {- (- "static/js/narrow.js",- 'i18n.t("Some common words were excluded from your search.") +',- ),- },},{"pattern": r"\+.*i18n\.t\(.+\)", "description": "Do not concatenate i18n strings"},{diff --git a/tools/test-js-with-node b/tools/test-js-with-nodeindex 26fa17dd210..68158bffd65 100755--- a/tools/test-js-with-node+++ b/tools/test-js-with-node@@ -90,6 +90,7 @@ EXEMPT_FILES = {"static/js/message_viewport.js","static/js/muting_ui.js","static/js/narrow.js",+ "static/js/narrow_banner.js","static/js/navigate.js","static/js/night_mode.js","static/js/notifications.js",