2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2024-02-26 20:21:39 +01:00
|
|
|
import type {Message} from "./message_store";
|
2024-08-11 18:49:43 +02:00
|
|
|
import type {Event, PollWidgetExtraData, PollWidgetOutboundData} from "./poll_widget";
|
|
|
|
import type {TodoWidgetOutboundData} from "./todo_widget";
|
2024-02-26 20:21:39 +01:00
|
|
|
|
|
|
|
// TODO: This ZFormExtraData type should be moved to web/src/zform.js when it will be migrated
|
|
|
|
type ZFormExtraData = {
|
|
|
|
type: string;
|
|
|
|
heading: string;
|
|
|
|
choices: {type: string; reply: string; long_name: string; short_name: string}[];
|
|
|
|
};
|
|
|
|
|
2022-07-29 04:25:31 +02:00
|
|
|
// TODO: This TodoWidgetExtraData type should be moved to web/src/todo_widget.js when it will be migrated
|
|
|
|
type TodoWidgetExtraData = {
|
2024-05-16 05:05:29 +02:00
|
|
|
task_list_title?: string | undefined;
|
|
|
|
tasks?: {task: string; desc: string}[] | undefined;
|
2022-07-29 04:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
type WidgetExtraData = PollWidgetExtraData | TodoWidgetExtraData | ZFormExtraData | null;
|
2024-03-28 16:31:50 +01:00
|
|
|
|
2024-02-26 20:21:39 +01:00
|
|
|
type WidgetOptions = {
|
|
|
|
widget_type: string;
|
2024-03-28 16:31:50 +01:00
|
|
|
extra_data: WidgetExtraData;
|
2024-02-26 20:21:39 +01:00
|
|
|
events: Event[];
|
|
|
|
$row: JQuery;
|
|
|
|
message: Message;
|
2024-08-11 18:49:43 +02:00
|
|
|
post_to_server: (data: {
|
|
|
|
msg_type: string;
|
|
|
|
data: string | PollWidgetOutboundData | TodoWidgetOutboundData;
|
|
|
|
}) => void;
|
2024-02-26 20:21:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type WidgetValue = Record<string, unknown> & {
|
|
|
|
activate: (data: {
|
|
|
|
$elem: JQuery;
|
2024-08-11 18:49:43 +02:00
|
|
|
callback: (data: string | PollWidgetOutboundData | TodoWidgetOutboundData) => void;
|
2024-02-26 20:21:39 +01:00
|
|
|
message: Message;
|
2024-03-28 16:31:50 +01:00
|
|
|
extra_data: WidgetExtraData;
|
2024-05-08 16:16:22 +02:00
|
|
|
}) => (events: Event[]) => void;
|
2024-02-26 20:21:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const widgets = new Map<string, WidgetValue>();
|
2024-05-08 16:16:22 +02:00
|
|
|
export const widget_event_handlers = new Map<number, (events: Event[]) => void>();
|
2024-02-26 20:21:39 +01:00
|
|
|
|
|
|
|
export function clear_for_testing(): void {
|
2024-05-08 16:16:22 +02:00
|
|
|
widget_event_handlers.clear();
|
2021-03-15 16:33:31 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 20:21:39 +01:00
|
|
|
function set_widget_in_message($row: JQuery, $widget_elem: JQuery): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $content_holder = $row.find(".message_content");
|
2024-05-07 03:50:04 +02:00
|
|
|
$content_holder.empty().append($widget_elem);
|
2018-06-26 09:30:57 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 20:21:39 +01:00
|
|
|
export function activate(in_opts: WidgetOptions): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_type = in_opts.widget_type;
|
|
|
|
const extra_data = in_opts.extra_data;
|
|
|
|
const events = in_opts.events;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = in_opts.$row;
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = in_opts.message;
|
|
|
|
const post_to_server = in_opts.post_to_server;
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2020-02-06 04:45:39 +01:00
|
|
|
if (!widgets.has(widget_type)) {
|
2020-11-09 11:22:48 +01:00
|
|
|
if (widget_type === "tictactoe") {
|
|
|
|
return; // don't warn for deleted legacy widget
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("unknown widget_type", widget_type);
|
2018-02-23 16:20:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:49:43 +02:00
|
|
|
const callback = function (
|
|
|
|
data: string | PollWidgetOutboundData | TodoWidgetOutboundData,
|
|
|
|
): void {
|
2018-02-23 16:20:33 +01:00
|
|
|
post_to_server({
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "widget",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2018-02-23 16:20:33 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-02-26 20:21:39 +01:00
|
|
|
if (!$row.attr("id")!.startsWith(`message-row-${message_lists.current?.id}-`)) {
|
2024-02-13 03:44:04 +01:00
|
|
|
// Don't activate widgets for messages that are not in the current view.
|
2018-06-26 09:30:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:20:33 +01:00
|
|
|
// We depend on our widgets to use templates to build
|
|
|
|
// the HTML that will eventually go in this div.
|
2024-05-07 03:50:04 +02:00
|
|
|
const $widget_elem = $("<div>").addClass("widget-content");
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2024-05-08 16:16:22 +02:00
|
|
|
const event_handler = widgets.get(widget_type)!.activate({
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem: $widget_elem,
|
2020-07-20 22:18:43 +02:00
|
|
|
callback,
|
|
|
|
message,
|
|
|
|
extra_data,
|
2018-02-23 16:20:33 +01:00
|
|
|
});
|
|
|
|
|
2024-05-08 16:16:22 +02:00
|
|
|
widget_event_handlers.set(message.id, event_handler);
|
2022-01-25 11:36:19 +01:00
|
|
|
set_widget_in_message($row, $widget_elem);
|
2018-02-23 16:20:33 +01:00
|
|
|
|
|
|
|
// Replay any events that already happened. (This is common
|
|
|
|
// when you narrow to a message after other users have already
|
|
|
|
// interacted with it.)
|
|
|
|
if (events.length > 0) {
|
2024-05-08 16:16:22 +02:00
|
|
|
event_handler(events);
|
2018-02-23 16:20:33 +01:00
|
|
|
}
|
2021-02-28 21:31:10 +01:00
|
|
|
}
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2024-02-26 20:21:39 +01:00
|
|
|
export function handle_event(widget_event: Event & {message_id: number}): void {
|
2024-05-08 16:16:22 +02:00
|
|
|
const event_handler = widget_event_handlers.get(widget_event.message_id);
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2024-05-08 16:16:22 +02:00
|
|
|
if (!event_handler || message_lists.current?.get_row(widget_event.message_id).length === 0) {
|
2018-05-21 20:58:32 +02:00
|
|
|
// It is common for submessage events to arrive on
|
2018-05-23 00:39:24 +02:00
|
|
|
// messages that we don't yet have in view. We
|
2018-05-21 20:58:32 +02:00
|
|
|
// just ignore them completely here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const events = [widget_event];
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2024-05-08 16:16:22 +02:00
|
|
|
event_handler(events);
|
2021-02-28 21:31:10 +01:00
|
|
|
}
|