Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
scroll_to_bottom_button: Move the click handler into the module.
Aman Agrawalbf1ab61d9ef9
diff --git a/web/src/click_handlers.ts b/web/src/click_handlers.tsindex e57e20e0159..643db5caef8 100644--- a/web/src/click_handlers.ts+++ b/web/src/click_handlers.ts@@ -24,7 +24,6 @@ import * as message_store from "./message_store.ts";import * as message_view from "./message_view.ts";import * as mouse_drag from "./mouse_drag.ts";import * as narrow_state from "./narrow_state.ts";-import * as navigate from "./navigate.ts";import {page_params} from "./page_params.ts";import * as pm_list from "./pm_list.ts";import * as popover_menus from "./popover_menus.ts";@@ -327,17 +326,6 @@ export function initialize(): void {});});- $("body").on("click", "#scroll-to-bottom-button-clickable-area", (e) => {- e.preventDefault();- e.stopPropagation();-- // Since it take a few milliseconds for this button complete disappear transition,- // it is possible for user to click it before it hides when switching narrows.- if (narrow_state.is_message_feed_visible()) {- navigate.to_end();- }- });-$("body").on("click", ".message_row", function () {$(".selected_msg_for_touchscreen").removeClass("selected_msg_for_touchscreen");$(this).addClass("selected_msg_for_touchscreen");diff --git a/web/src/scroll_to_bottom_button.ts b/web/src/scroll_to_bottom_button.tsindex 3b898b0b474..49ba97b3b44 100644--- a/web/src/scroll_to_bottom_button.ts+++ b/web/src/scroll_to_bottom_button.ts@@ -4,6 +4,7 @@ import type * as tippy from "tippy.js";import * as message_lists from "./message_lists.ts";import * as message_viewport from "./message_viewport.ts";+import * as narrow_state from "./narrow_state.ts";import {the} from "./util.ts";let hide_scroll_to_bottom_timer: ReturnType<typeof setInterval> | undefined;@@ -56,7 +57,18 @@ export function show_scroll_to_bottom_button(): void {$("#scroll-to-bottom-button-container").addClass("show");}-export function initialize(): void {+export function initialize(on_click: {scroll_to_bottom: () => void}): void {+ $("body").on("click", "#scroll-to-bottom-button-clickable-area", (e) => {+ e.preventDefault();+ e.stopPropagation();++ // Since it take a few milliseconds for this button complete disappear transition,+ // it is possible for user to click it before it hides when switching narrows.+ if (narrow_state.is_message_feed_visible()) {+ on_click.scroll_to_bottom();+ }+ });+$(document).on("keydown", (e) => {if (e.shiftKey || e.ctrlKey || e.metaKey) {return;diff --git a/web/src/ui_init.js b/web/src/ui_init.jsindex a722dfb36b1..f6b51e7e11c 100644--- a/web/src/ui_init.js+++ b/web/src/ui_init.js@@ -679,7 +679,7 @@ export async function initialize_everything(state_data) {server_events.finished_initial_fetch();});message_scroll.initialize();- scroll_to_bottom_button.initialize();+ scroll_to_bottom_button.initialize({scroll_to_bottom: navigate.to_end});markdown.initialize(markdown_config.get_helpers());linkifiers.initialize(realm.realm_linkifiers);realm_playground.initialize({diff --git a/web/tests/scroll_to_bottom_button.test.cjs b/web/tests/scroll_to_bottom_button.test.cjsindex 1769d14352f..95985d92902 100644--- a/web/tests/scroll_to_bottom_button.test.cjs+++ b/web/tests/scroll_to_bottom_button.test.cjs@@ -16,6 +16,7 @@ set_global("TransitionEvent", TransitionEvent);const message_lists = mock_esm("../src/message_lists", {current: undefined});const message_viewport = mock_esm("../src/message_viewport");+const narrow_state = mock_esm("../src/narrow_state");const scroll_to_bottom_button = zrequire("scroll_to_bottom_button");@@ -56,11 +57,34 @@ function keydown(modifiers = {}) {});}+let clicks;++function click() {+ const event_calls = [];+ $("body").get_on_handler(+ "click",+ "#scroll-to-bottom-button-clickable-area",+ )({+ preventDefault() {+ event_calls.push("preventDefault");+ },+ stopPropagation() {+ event_calls.push("stopPropagation");+ },+ });+ assert.deepEqual(event_calls, ["preventDefault", "stopPropagation"]);+}+function test(label, f) {run_test(label, (helpers) => {clock.reset();$container().set_matches(":hover", false);- scroll_to_bottom_button.initialize();+ clicks = [];+ scroll_to_bottom_button.initialize({+ scroll_to_bottom() {+ clicks.push("scroll_to_bottom");+ },+ });f(helpers);});}@@ -187,6 +211,20 @@ test("a keyboard scroll does not make a later hide come early", ({override}) =>assert.ok(!is_shown());});+test("a click scrolls to the bottom", ({override}) => {+ override(narrow_state, "is_message_feed_visible", () => true);++ click();+ assert.deepEqual(clicks, ["scroll_to_bottom"]);+});++test("a click does nothing once the message feed is hidden", ({override}) => {+ override(narrow_state, "is_message_feed_visible", () => false);++ click();+ assert.deepEqual(clicks, []);+});+test("hidden by a keypress without modifiers", () => {for (const modifier of ["shiftKey", "ctrlKey", "metaKey"]) {$container().addClass("show");