zulip/web/src/widgetize.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

import $ from "jquery";
import * as blueslip from "./blueslip";
import * as message_lists from "./message_lists";
2018-02-23 15:54:07 +01:00
export const widgets = new Map();
export const widget_contents = new Map();
export function clear_for_testing() {
widget_contents.clear();
}
function set_widget_in_message($row, $widget_elem) {
const $content_holder = $row.find(".message_content");
// Avoid adding the $widget_elem if it already exists.
// This can happen when the app loads in the "Recent Conversations"
// view and the user changes the view to "All messages".
// This is important since jQuery removes all the event handlers
// on `empty()`ing an element.
if ($content_holder.find(".widget-content").length === 0) {
$content_holder.empty().append($widget_elem);
}
}
export function activate(in_opts) {
const widget_type = in_opts.widget_type;
const extra_data = in_opts.extra_data;
const events = in_opts.events;
const $row = in_opts.$row;
const message = in_opts.message;
const post_to_server = in_opts.post_to_server;
2018-02-23 16:20:33 +01:00
events.shift();
if (!widgets.has(widget_type)) {
if (widget_type === "tictactoe") {
return; // don't warn for deleted legacy widget
}
blueslip.warn("unknown widget_type", widget_type);
2018-02-23 16:20:33 +01:00
return;
}
const callback = function (data) {
2018-02-23 16:20:33 +01:00
post_to_server({
msg_type: "widget",
data,
2018-02-23 16:20:33 +01:00
});
};
if (!$row.attr("id").startsWith(`message-row-${message_lists.current?.id}-`)) {
// Don't activate widgets for messages that are not in the current view.
return;
}
let $widget_elem = widget_contents.get(message.id);
if ($widget_elem) {
set_widget_in_message($row, $widget_elem);
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.
$widget_elem = $("<div>").addClass("widget-content");
2018-02-23 16:20:33 +01:00
widgets.get(widget_type).activate({
$elem: $widget_elem,
callback,
message,
extra_data,
2018-02-23 16:20:33 +01:00
});
widget_contents.set(message.id, $widget_elem);
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) {
$widget_elem.handle_events(events);
2018-02-23 16:20:33 +01:00
}
}
2018-02-23 16:20:33 +01:00
export function set_widgets_for_list() {
for (const [idx, $widget_elem] of widget_contents) {
if (message_lists.current?.get(idx) !== undefined) {
const $row = message_lists.current.get_row(idx);
set_widget_in_message($row, $widget_elem);
}
}
}
export function handle_event(widget_event) {
const $widget_elem = widget_contents.get(widget_event.message_id);
2018-02-23 16:20:33 +01:00
if (!$widget_elem) {
// It is common for submessage events to arrive on
// messages that we don't yet have in view. We
// just ignore them completely here.
return;
}
const events = [widget_event];
2018-02-23 16:20:33 +01:00
$widget_elem.handle_events(events);
}