Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
widgets: Split out generic_widget code.
Steve Howell1f4138c65e6e
diff --git a/web/src/generic_widget.ts b/web/src/generic_widget.tsnew file mode 100644index 00000000000..11d131367ab--- /dev/null+++ b/web/src/generic_widget.ts@@ -0,0 +1,91 @@+import * as blueslip from "./blueslip.ts";+import type {Message} from "./message_store.ts";+import type {PollWidgetExtraData, PollWidgetOutboundData} from "./poll_data.ts";+import type {TodoWidgetExtraData, TodoWidgetOutboundData} from "./todo_widget.ts";+import type {Event} from "./widget_data.ts";+import type {ZFormExtraData} from "./zform_data.ts";++export type WidgetExtraData = PollWidgetExtraData | TodoWidgetExtraData | ZFormExtraData | null;++type HandleInboundEventsFunction = (events: Event[]) => void;++export type PostToServerFunction = (data: {msg_type: string; data: WidgetOutboundData}) => void;++type WidgetOutboundData = PollWidgetOutboundData | TodoWidgetOutboundData;++// These are poll, todo, and zform implementations.+// They are currently injected into us from another module+// for historical reasons. (as of January 2026)+type WidgetImplementation = Record<string, unknown> & {+ activate: (data: {+ $elem: JQuery;+ callback: (data: WidgetOutboundData) => void;+ message: Message;+ extra_data: WidgetExtraData;+ }) => HandleInboundEventsFunction;+};++export const widgets = new Map<string, WidgetImplementation>();++export function is_supported_widget_type(widget_type: string): boolean {+ if (widgets.has(widget_type)) {+ return true;+ }++ if (widget_type === "tictactoe") {+ return false; // don't warn for deleted legacy widget+ }++ blueslip.warn("unknown widget_type", {widget_type});+ return false;+}++export class GenericWidget {+ // Eventually we will have concrete classes for PollWidget,+ // TodoWidget, and ZformWidget, but for now we need this+ // wrapper class.+ inbound_events_handler: HandleInboundEventsFunction;++ constructor(inbound_events_handler: HandleInboundEventsFunction) {+ this.inbound_events_handler = inbound_events_handler;+ }++ handle_inbound_events(events: Event[]): void {+ this.inbound_events_handler(events);+ }+}++export function create_widget_instance(info: {+ widget_type: string;+ post_to_server: PostToServerFunction;+ $widget_elem: JQuery;+ message: Message;+ extra_data: WidgetExtraData;+}): GenericWidget {+ const {widget_type, post_to_server, $widget_elem, message, extra_data} = info;++ // For historical reasons, we don't directly import the+ // modules that handle poll, todo, and zform.+ const widget_implementation = widgets.get(widget_type)!;++ // We pass this is into the widgets to provide them a black-box+ // service that sends any events **outbound** to the other active+ // users. For example, if I vote on a poll, the widget from my+ // client will send data to the server using this callback, and+ // then the server will broadcast my poll vote to other users.+ function post_to_server_callback(data: WidgetOutboundData): void {+ post_to_server({+ msg_type: "widget",+ data,+ });+ }++ const inbound_events_handler = widget_implementation.activate({+ $elem: $widget_elem,+ callback: post_to_server_callback,+ message,+ extra_data,+ });++ return new GenericWidget(inbound_events_handler);+}diff --git a/web/src/poll_widget.ts b/web/src/poll_widget.tsindex fd8b0a95928..be83de4a38f 100644--- a/web/src/poll_widget.ts+++ b/web/src/poll_widget.ts@@ -6,6 +6,7 @@ import render_widgets_poll_widget from "../templates/widgets/poll_widget.hbs";import render_widgets_poll_widget_results from "../templates/widgets/poll_widget_results.hbs";import * as blueslip from "./blueslip.ts";+import type {WidgetExtraData} from "./generic_widget.ts";import {$t} from "./i18n.ts";import * as keydown_util from "./keydown_util.ts";import * as message_lists from "./message_lists.ts";@@ -20,7 +21,6 @@ import {vote_schema,} from "./poll_data.ts";import type {Event} from "./widget_data.ts";-import type {WidgetExtraData} from "./widgetize.ts";export function activate({$elem,diff --git a/web/src/widgetize.ts b/web/src/widgetize.tsindex 5fc7d0af493..58f9724e4a5 100644--- a/web/src/widgetize.ts+++ b/web/src/widgetize.ts@@ -1,19 +1,10 @@import $ from "jquery";-import * as blueslip from "./blueslip.ts";+import type {GenericWidget, PostToServerFunction, WidgetExtraData} from "./generic_widget.ts";+import {create_widget_instance, is_supported_widget_type} from "./generic_widget.ts";import * as message_lists from "./message_lists.ts";import type {Message} from "./message_store.ts";-import type {PollWidgetExtraData, PollWidgetOutboundData} from "./poll_data.ts";-import type {TodoWidgetExtraData, TodoWidgetOutboundData} from "./todo_widget.ts";import type {Event} from "./widget_data.ts";-import type {ZFormExtraData} from "./zform_data.ts";--export type WidgetExtraData = PollWidgetExtraData | TodoWidgetExtraData | ZFormExtraData | null;--type WidgetOutboundData = PollWidgetOutboundData | TodoWidgetOutboundData;--type PostToServerFunction = (data: {msg_type: string; data: WidgetOutboundData}) => void;-type HandleInboundEventsFunction = (events: Event[]) => void;// These are the arguments that get passed in to us from the// submessage system, which is essentially the transport layer@@ -32,19 +23,6 @@ type ActivateArguments = {post_to_server: PostToServerFunction;};-// These are poll, todo, and zform implementations.-// They are currently injected into us from another module-// for historical reasons. (as of January 2026)-type WidgetImplementation = Record<string, unknown> & {- activate: (data: {- $elem: JQuery;- callback: (data: WidgetOutboundData) => void;- message: Message;- extra_data: WidgetExtraData;- }) => HandleInboundEventsFunction;-};--export const widgets = new Map<string, WidgetImplementation>();const generic_widget_map = new Map<number, GenericWidget>();export function clear_for_testing(): void {@@ -64,69 +42,6 @@ function set_widget_in_message($row: JQuery, $widget_elem: JQuery): void {$content_holder.empty().append($widget_elem);}-function is_supported_widget_type(widget_type: string): boolean {- if (widgets.has(widget_type)) {- return true;- }-- if (widget_type === "tictactoe") {- return false; // don't warn for deleted legacy widget- }-- blueslip.warn("unknown widget_type", {widget_type});- return false;-}--export class GenericWidget {- // Eventually we will have concrete classes for PollWidget,- // TodoWidget, and ZformWidget, but for now we need this- // wrapper class.- inbound_events_handler: HandleInboundEventsFunction;-- constructor(inbound_events_handler: HandleInboundEventsFunction) {- this.inbound_events_handler = inbound_events_handler;- }-- handle_inbound_events(events: Event[]): void {- this.inbound_events_handler(events);- }-}--function create_widget_instance(info: {- widget_type: string;- post_to_server: PostToServerFunction;- $widget_elem: JQuery;- message: Message;- extra_data: WidgetExtraData;-}): GenericWidget {- const {widget_type, post_to_server, $widget_elem, message, extra_data} = info;-- // For historical reasons, we don't directly import the- // modules that handle poll, todo, and zform.- const widget_implementation = widgets.get(widget_type)!;-- // We pass this is into the widgets to provide them a black-box- // service that sends any events **outbound** to the other active- // users. For example, if I vote on a poll, the widget from my- // client will send data to the server using this callback, and- // then the server will broadcast my poll vote to other users.- function post_to_server_callback(data: WidgetOutboundData): void {- post_to_server({- msg_type: "widget",- data,- });- }-- const inbound_events_handler = widget_implementation.activate({- $elem: $widget_elem,- callback: post_to_server_callback,- message,- extra_data,- });-- return new GenericWidget(inbound_events_handler);-}-export function activate(in_opts: ActivateArguments): void {const widget_type = in_opts.widget_type;const extra_data = in_opts.extra_data;diff --git a/web/src/widgets.ts b/web/src/widgets.tsindex a48587d85c3..172e95c9173 100644--- a/web/src/widgets.ts+++ b/web/src/widgets.ts@@ -1,10 +1,10 @@+import * as generic_widget from "./generic_widget.ts";import * as poll_widget from "./poll_widget.ts";import * as todo_widget from "./todo_widget.ts";-import * as widgetize from "./widgetize.ts";import * as zform from "./zform.ts";export function initialize(): void {- widgetize.widgets.set("poll", poll_widget);- widgetize.widgets.set("todo", todo_widget);- widgetize.widgets.set("zform", zform);+ generic_widget.widgets.set("poll", poll_widget);+ generic_widget.widgets.set("todo", todo_widget);+ generic_widget.widgets.set("zform", zform);}diff --git a/web/src/zform.ts b/web/src/zform.tsindex 7fe68e95c1e..ca29797ebdd 100644--- a/web/src/zform.ts+++ b/web/src/zform.ts@@ -3,10 +3,10 @@ import $ from "jquery";import render_widgets_zform_choices from "../templates/widgets/zform_choices.hbs";import * as blueslip from "./blueslip.ts";+import type {WidgetExtraData} from "./generic_widget.ts";import type {Message} from "./message_store.ts";import * as transmit from "./transmit.ts";import type {Event} from "./widget_data.ts";-import type {WidgetExtraData} from "./widgetize.ts";import {zform_widget_extra_data_schema} from "./zform_data.ts";import type {ZFormExtraData} from "./zform_data.ts";diff --git a/web/tests/widgetize.test.cjs b/web/tests/widgetize.test.cjsindex eabf874ce1e..99107c574f4 100644--- a/web/tests/widgetize.test.cjs+++ b/web/tests/widgetize.test.cjs@@ -59,6 +59,7 @@ mock_esm("../src/poll_widget", fake_poll_widget);set_global("document", "document-stub");+const {GenericWidget} = zrequire("generic_widget");const widgetize = zrequire("widgetize");const widgets = zrequire("widgets");@@ -149,7 +150,7 @@ test("activate", ({override}) => {assert.ok(!is_widget_activated);assert.ok(!is_event_handled);- /* Testing widgetize.GenericWidget */+ /* Testing GenericWidget */message_lists.current = {id: 2};const post_activate_event = {data: {@@ -163,7 +164,7 @@ test("activate", ({override}) => {is_event_handled = true;assert.deepEqual(e, [post_activate_event]);};- widgetize.set_widget_for_tests(2001, new widgetize.GenericWidget(handle_events));+ widgetize.set_widget_for_tests(2001, new GenericWidget(handle_events));override(message_lists.current, "get_row", (idx) => {assert.equal(idx, 2001);return $row;