js: Convert static/js/recent_topics.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-27 16:27:48 -08:00
committed by Tim Abbott
parent 934d4e4e10
commit 1a8422b152
26 changed files with 105 additions and 100 deletions

View File

@@ -147,7 +147,6 @@
"page_params": false,
"pointer": false,
"realm_night_logo": false,
"recent_topics": false,
"search_pill_widget": false,
"settings_profile_fields": false,
"stream_list": false,

View File

@@ -52,8 +52,8 @@ const hashchange = zrequire("hashchange");
const stream_data = zrequire("stream_data");
const recent_topics = zrequire("recent_topics");
recent_topics.show = () => {};
recent_topics.is_visible = () => false;
recent_topics.__Rewire__("show", () => {});
recent_topics.__Rewire__("is_visible", () => false);
run_test("operators_round_trip", () => {
let operators;

View File

@@ -122,7 +122,7 @@ set_global("current_msg_list", {
return 101;
},
});
set_global("recent_topics", {
rewiremock("../../static/js/recent_topics").with({
is_visible: () => false,
});

View File

@@ -20,7 +20,7 @@ rewiremock("../../static/js/message_list_view").with({
MessageListView,
});
set_global("recent_topics", {
rewiremock("../../static/js/recent_topics").with({
process_messages: noop,
});
// Still required for page_params.initial_pointer

View File

@@ -30,7 +30,7 @@ const narrow_state = {__esModule: true};
rewiremock("../../static/js/narrow_state").with(narrow_state);
const stream_data = {__esModule: true};
rewiremock("../../static/js/stream_data").with(stream_data);
set_global("recent_topics", {
rewiremock("../../static/js/recent_topics").with({
is_visible: () => false,
});

View File

@@ -55,7 +55,7 @@ set_global("search_pill_widget", {
},
},
});
set_global("recent_topics", {
rewiremock("../../static/js/recent_topics").with({
hide: () => {},
is_visible: () => {},
});

View File

@@ -394,7 +394,7 @@ run_test("test_filter_all", () => {
i = row_data.length;
rt = reset_module("recent_topics");
stub_out_filter_buttons();
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
rt.set_filter("all");
rt.process_messages([messages[0]]);
@@ -435,7 +435,7 @@ run_test("test_filter_unread", () => {
let i = 0;
rt = reset_module("recent_topics");
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
rt.set_default_focus();
stub_templates(() => "<recent_topics table stub>");
@@ -502,7 +502,7 @@ run_test("test_filter_participated", () => {
let i = 0;
rt = reset_module("recent_topics");
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
rt.set_default_focus();
stub_templates(() => "<recent_topics table stub>");
stub_out_filter_buttons();
@@ -561,7 +561,7 @@ stub_templates(() => "<recent_topics table stub>");
run_test("basic assertions", () => {
rt = reset_module("recent_topics");
stub_out_filter_buttons();
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
rt.set_default_focus();
rt.set_filter("all");
rt.process_messages(messages);
@@ -642,7 +642,7 @@ run_test("basic assertions", () => {
run_test("test_reify_local_echo_message", () => {
rt = reset_module("recent_topics");
stub_out_filter_buttons();
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
rt.set_filter("all");
rt.process_messages(messages);
@@ -807,7 +807,7 @@ run_test("test_topic_edit", () => {
run_test("test_search", () => {
rt = reset_module("recent_topics");
rt.is_visible = () => true;
rt.__Rewire__("is_visible", () => true);
assert.equal(rt.topic_in_search_results("t", "general", "Recent Topic"), true);
assert.equal(rt.topic_in_search_results("T", "general", "Recent Topic"), true);
assert.equal(rt.topic_in_search_results("to", "general", "Recent Topic"), true);

View File

@@ -27,7 +27,7 @@ const stream_list = set_global("stream_list", {});
const stream_muting = {__esModule: true};
rewiremock("../../static/js/stream_muting").with(stream_muting);
let subs = set_global("subs", {});
set_global("recent_topics", {
rewiremock("../../static/js/recent_topics").with({
complete_rerender: () => {},
});

View File

@@ -120,7 +120,6 @@ const compose = zrequire("compose");
zrequire("composebox_typeahead");
zrequire("narrow");
zrequire("stream_list");
zrequire("recent_topics");
run_test("initialize_everything", () => {
util.is_mobile = () => false;

View File

@@ -38,7 +38,6 @@ import "../dropdown_list_widget";
import "../settings_profile_fields";
import "../settings";
import "../ui_init";
import "../recent_topics";
import "../search_pill_widget";
import "../desktop_integration";

View File

@@ -24,6 +24,7 @@ import * as notifications from "./notifications";
import * as overlays from "./overlays";
import * as popovers from "./popovers";
import * as reactions from "./reactions";
import * as recent_topics from "./recent_topics";
import * as rows from "./rows";
import * as server_events from "./server_events";
import * as settings_panel_menu from "./settings_panel_menu";

View File

@@ -8,6 +8,7 @@ import * as notifications from "./notifications";
import * as people from "./people";
import * as pm_list from "./pm_list";
import * as popovers from "./popovers";
import * as recent_topics from "./recent_topics";
import * as rows from "./rows";
import * as sent_messages from "./sent_messages";
import * as stream_topic_history from "./stream_topic_history";

View File

@@ -19,7 +19,6 @@ declare let message_list: any;
declare let narrow: any;
declare let page_params: any;
declare let pointer: any;
declare let recent_topics: any;
declare let search_pill_widget: any;
declare let settings_profile_fields: any;
declare let stream_list: any;

View File

@@ -7,6 +7,7 @@ import * as invite from "./invite";
import * as message_viewport from "./message_viewport";
import * as navigate from "./navigate";
import * as overlays from "./overlays";
import * as recent_topics from "./recent_topics";
import * as search from "./search";
import * as settings from "./settings";
import * as settings_panel_menu from "./settings_panel_menu";

View File

@@ -22,6 +22,7 @@ import * as navigate from "./navigate";
import * as overlays from "./overlays";
import * as popovers from "./popovers";
import * as reactions from "./reactions";
import * as recent_topics from "./recent_topics";
import * as search from "./search";
import * as stream_popover from "./stream_popover";
import * as topic_zoom from "./topic_zoom";

View File

@@ -14,6 +14,7 @@ const narrow_state = require("./narrow_state");
const notifications = require("./notifications");
const pm_list = require("./pm_list");
const recent_senders = require("./recent_senders");
const recent_topics = require("./recent_topics");
const resize = require("./resize");
const stream_data = require("./stream_data");
const stream_topic_history = require("./stream_topic_history");

View File

@@ -6,6 +6,7 @@ import * as message_store from "./message_store";
import * as message_util from "./message_util";
import * as people from "./people";
import * as pm_list from "./pm_list";
import * as recent_topics from "./recent_topics";
import * as server_events from "./server_events";
import * as stream_data from "./stream_data";
import * as ui_report from "./ui_report";

View File

@@ -18,6 +18,7 @@ import * as narrow_state from "./narrow_state";
import * as people from "./people";
import * as popovers from "./popovers";
import * as reactions from "./reactions";
import * as recent_topics from "./recent_topics";
import * as rendered_markdown from "./rendered_markdown";
import * as rows from "./rows";
import * as stream_color from "./stream_color";

View File

@@ -6,6 +6,7 @@ import * as loading from "./loading";
import * as message_fetch from "./message_fetch";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
import * as recent_topics from "./recent_topics";
let actively_scrolling = false;

View File

@@ -2,6 +2,7 @@ import render_message_view_header from "../templates/message_view_header.hbs";
import * as narrow_state from "./narrow_state";
import * as peer_data from "./peer_data";
import * as recent_topics from "./recent_topics";
import * as rendered_markdown from "./rendered_markdown";
import * as search from "./search";

View File

@@ -6,6 +6,7 @@ import * as feedback_widget from "./feedback_widget";
import * as ListWidget from "./list_widget";
import * as muting from "./muting";
import * as overlays from "./overlays";
import * as recent_topics from "./recent_topics";
import * as settings_muting from "./settings_muting";
import * as stream_data from "./stream_data";
import * as stream_popover from "./stream_popover";

View File

@@ -16,6 +16,7 @@ const message_view_header = require("./message_view_header");
const narrow_state = require("./narrow_state");
const notifications = require("./notifications");
const people = require("./people");
const recent_topics = require("./recent_topics");
const resize = require("./resize");
const search = require("./search");
const search_pill = require("./search_pill");

View File

@@ -1,26 +1,24 @@
"use strict";
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";
const render_recent_topic_row = require("../templates/recent_topic_row.hbs");
const render_recent_topics_filters = require("../templates/recent_topics_filters.hbs");
const render_recent_topics_body = require("../templates/recent_topics_table.hbs");
const drafts = require("./drafts");
const hash_util = require("./hash_util");
const ListWidget = require("./list_widget");
const {localstorage} = require("./localstorage");
const message_store = require("./message_store");
const message_util = require("./message_util");
const message_view_header = require("./message_view_header");
const muting = require("./muting");
const narrow_state = require("./narrow_state");
const navigate = require("./navigate");
const notifications = require("./notifications");
const panels = require("./panels");
const people = require("./people");
const recent_senders = require("./recent_senders");
const stream_data = require("./stream_data");
const timerender = require("./timerender");
const top_left_corner = require("./top_left_corner");
import * as drafts from "./drafts";
import * as hash_util from "./hash_util";
import * as ListWidget from "./list_widget";
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";
import * as muting from "./muting";
import * as narrow_state from "./narrow_state";
import * as navigate from "./navigate";
import * as notifications from "./notifications";
import * as panels from "./panels";
import * as people from "./people";
import * as recent_senders from "./recent_senders";
import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
import * as top_left_corner from "./top_left_corner";
const topics = new Map(); // Key is stream-id:topic.
let topics_widget;
@@ -55,23 +53,22 @@ const ls_key = "recent_topic_filters";
const ls = localstorage();
let filters = new Set();
exports.save_filters = function () {
export function save_filters() {
ls.set(ls_key, Array.from(filters));
};
}
exports.load_filters = function () {
export function load_filters() {
filters = new Set(ls.get(ls_key));
};
}
function set_default_focus() {
export function set_default_focus() {
// If at any point we are confused about the currently
// focused element, we switch focus to search.
current_focus_elem = $("#recent_topics_search");
current_focus_elem.trigger("focus");
}
exports.set_default_focus = set_default_focus;
function set_table_focus(row, col) {
const topic_rows = $("#recent_topics_table table tbody tr");
if (topic_rows.length === 0 || row < 0 || row >= topic_rows.length) {
@@ -125,7 +122,7 @@ function get_topic_key(stream_id, topic) {
return stream_id + ":" + topic.toLowerCase();
}
exports.process_messages = function (messages) {
export function process_messages(messages) {
// FIX: Currently, we do a complete_rerender every time
// we process a new message.
// While this is inexpensive and handles all the cases itself,
@@ -133,12 +130,12 @@ exports.process_messages = function (messages) {
// the UI will be returned to the beginning of the list on every
// update.
for (const msg of messages) {
exports.process_message(msg);
process_message(msg);
}
complete_rerender();
}
exports.complete_rerender();
};
exports.process_message = function (msg) {
export function process_message(msg) {
if (msg.type !== "stream") {
return false;
}
@@ -167,9 +164,9 @@ exports.process_message = function (msg) {
// to topic info fetched from backend, which is currently not a thing.
topic_data.participated = is_ours || topic_data.participated;
return true;
};
}
exports.reify_message_id_if_available = function (opts) {
export function reify_message_id_if_available(opts) {
// We don't need to reify the message_id of the topic
// if a new message arrives in the topic from another user,
// since it replaces the last_msg_id of the topic which
@@ -181,7 +178,7 @@ exports.reify_message_id_if_available = function (opts) {
}
}
return false;
};
}
function get_sorted_topics() {
// Sort all recent topics by last message time.
@@ -190,9 +187,9 @@ function get_sorted_topics() {
);
}
exports.get = function () {
export function get() {
return get_sorted_topics();
};
}
function format_topic(topic_data) {
const last_msg = message_store.get(topic_data.last_msg_id);
@@ -251,39 +248,39 @@ function get_topic_row(topic_data) {
return $(`#${CSS.escape("recent_topic:" + topic_key)}`);
}
exports.process_topic_edit = function (old_stream_id, old_topic, new_topic, new_stream_id) {
export function process_topic_edit(old_stream_id, old_topic, new_topic, new_stream_id) {
// See `recent_senders.process_topic_edit` for
// logic behind this and important notes on use of this function.
topics.delete(get_topic_key(old_stream_id, old_topic));
const old_topic_msgs = message_util.get_messages_in_topic(old_stream_id, old_topic);
exports.process_messages(old_topic_msgs);
process_messages(old_topic_msgs);
new_stream_id = new_stream_id || old_stream_id;
const new_topic_msgs = message_util.get_messages_in_topic(new_stream_id, new_topic);
exports.process_messages(new_topic_msgs);
};
process_messages(new_topic_msgs);
}
exports.topic_in_search_results = function (keyword, stream, topic) {
export function topic_in_search_results(keyword, stream, topic) {
if (keyword === "") {
return true;
}
const text = (stream + " " + topic).toLowerCase();
const search_words = keyword.toLowerCase().split(/\s+/);
return search_words.every((word) => text.includes(word));
};
}
exports.update_topics_of_deleted_message_ids = function (message_ids) {
export function update_topics_of_deleted_message_ids(message_ids) {
const topics_to_rerender = message_util.get_topics_for_message_ids(message_ids);
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);
exports.process_messages(msgs);
process_messages(msgs);
}
}
};
exports.filters_should_hide_topic = function (topic_data) {
export function filters_should_hide_topic(topic_data) {
const msg = message_store.get(topic_data.last_msg_id);
const sub = stream_data.get_sub_by_id(msg.stream_id);
@@ -312,15 +309,15 @@ exports.filters_should_hide_topic = function (topic_data) {
}
const search_keyword = $("#recent_topics_search").val();
if (!recent_topics.topic_in_search_results(search_keyword, msg.stream, msg.topic)) {
if (!topic_in_search_results(search_keyword, msg.stream, msg.topic)) {
return true;
}
return false;
};
}
exports.inplace_rerender = function (topic_key) {
if (!exports.is_visible()) {
export function inplace_rerender(topic_key) {
if (!is_visible()) {
return false;
}
if (!topics.has(topic_key)) {
@@ -331,16 +328,16 @@ exports.inplace_rerender = function (topic_key) {
topics_widget.render_item(topic_data);
const topic_row = get_topic_row(topic_data);
if (exports.filters_should_hide_topic(topic_data)) {
if (filters_should_hide_topic(topic_data)) {
topic_row.hide();
} else {
topic_row.show();
}
revive_current_focus();
return true;
};
}
exports.update_topic_is_muted = function (stream_id, topic) {
export function update_topic_is_muted(stream_id, topic) {
const key = get_topic_key(stream_id, topic);
if (!topics.has(key)) {
// we receive mute request for a topic we are
@@ -348,16 +345,16 @@ exports.update_topic_is_muted = function (stream_id, topic) {
return false;
}
exports.inplace_rerender(key);
inplace_rerender(key);
return true;
};
}
exports.update_topic_unread_count = function (message) {
export function update_topic_unread_count(message) {
const topic_key = get_topic_key(message.stream_id, message.topic);
exports.inplace_rerender(topic_key);
};
inplace_rerender(topic_key);
}
exports.set_filter = function (filter) {
export function set_filter(filter) {
// This function updates the `filters` variable
// after user clicks on one of the filter buttons
// based on `btn-recent-selected` class and current
@@ -379,13 +376,13 @@ exports.set_filter = function (filter) {
filters.add(filter);
}
exports.save_filters();
};
save_filters();
}
function show_selected_filters() {
// Add `btn-selected-filter` to the buttons to show
// which filters are applied.
exports.load_filters();
load_filters();
if (filters.size === 0) {
$("#recent_topics_filter_buttons")
.find('[data-filter="all"]')
@@ -399,7 +396,7 @@ function show_selected_filters() {
}
}
exports.update_filters_view = function () {
export function update_filters_view() {
const rendered_filters = render_recent_topics_filters({
filter_participated: filters.has("participated"),
filter_unread: filters.has("unread"),
@@ -409,7 +406,7 @@ exports.update_filters_view = function () {
show_selected_filters();
topics_widget.hard_redraw();
};
}
function stream_sort(a, b) {
const a_stream = message_store.get(a.last_msg_id).stream;
@@ -433,8 +430,8 @@ function topic_sort(a, b) {
return -1;
}
exports.complete_rerender = function () {
if (!exports.is_visible()) {
export function complete_rerender() {
if (!is_visible()) {
return;
}
// Prepare header
@@ -450,7 +447,7 @@ exports.complete_rerender = function () {
// Show topics list
const container = $("#recent_topics_table table tbody");
container.empty();
const mapped_topic_values = Array.from(exports.get().values()).map((value) => value);
const mapped_topic_values = Array.from(get().values()).map((value) => value);
topics_widget = ListWidget.create(container, mapped_topic_values, {
name: "recent_topics_table",
@@ -462,7 +459,7 @@ exports.complete_rerender = function () {
// We use update_filters_view & filters_should_hide_topic to do all the
// filtering for us, which is called using click_handlers.
predicate(topic_data) {
return !exports.filters_should_hide_topic(topic_data);
return !filters_should_hide_topic(topic_data);
},
},
sort_fields: {
@@ -473,13 +470,13 @@ exports.complete_rerender = function () {
simplebar_container: $("#recent_topics_table .table_fix_head"),
callback_after_render: revive_current_focus,
});
};
}
exports.is_visible = function () {
export function is_visible() {
return $("#recent_topics_view").is(":visible");
};
}
exports.show = function () {
export function show() {
// Hide selected elements in the left sidebar.
top_left_corner.narrow_to_recent_topics();
stream_list.handle_narrow_deactivated();
@@ -504,14 +501,14 @@ exports.show = function () {
notifications.redraw_title();
message_view_header.render_title_area();
exports.complete_rerender();
};
complete_rerender();
}
function filter_buttons() {
return $("#recent_filters_group").children();
}
exports.hide = function () {
export function hide() {
$("#message_feed_container").show();
$("#recent_topics_view").hide();
// On firefox (and flaky on other browsers), focus
@@ -536,9 +533,9 @@ exports.hide = function () {
// This makes sure user lands on the selected message
// and not always at the top of the narrow.
navigate.plan_scroll_to_selected();
};
}
exports.change_focused_element = function (e, input_key) {
export function change_focused_element(e, input_key) {
// 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
@@ -663,6 +660,4 @@ exports.change_focused_element = function (e, input_key) {
}
return false;
};
window.recent_topics = exports;
}

View File

@@ -4,6 +4,7 @@ import * as message_view_header from "./message_view_header";
import * as narrow_state from "./narrow_state";
import * as overlays from "./overlays";
import * as peer_data from "./peer_data";
import * as recent_topics from "./recent_topics";
import * as settings_notifications from "./settings_notifications";
import * as stream_color from "./stream_color";
import * as stream_data from "./stream_data";

View File

@@ -39,6 +39,7 @@ import * as people from "./people";
import * as pm_conversations from "./pm_conversations";
import * as pm_list from "./pm_list";
import * as presence from "./presence";
import * as recent_topics from "./recent_topics";
import * as reload from "./reload";
import * as resize from "./resize";
import * as rows from "./rows";

View File

@@ -6,6 +6,7 @@ const message_store = require("./message_store");
const message_viewport = require("./message_viewport");
const notifications = require("./notifications");
const overlays = require("./overlays");
const recent_topics = require("./recent_topics");
const reload = require("./reload");
const unread_ui = require("./unread_ui");