2019-11-02 00:06:25 +01:00
|
|
|
const widgets = {};
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
widgets.poll = poll_widget;
|
2018-02-23 15:45:25 +01:00
|
|
|
widgets.tictactoe = tictactoe_widget;
|
2018-07-02 19:57:01 +02:00
|
|
|
widgets.todo = todo_widget;
|
2018-02-23 15:37:19 +01:00
|
|
|
widgets.zform = zform;
|
2018-02-23 15:54:07 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_contents = {};
|
2018-07-03 08:59:28 +02:00
|
|
|
exports.widget_contents = widget_contents;
|
2018-06-26 09:30:57 +02:00
|
|
|
|
|
|
|
function set_widget_in_message(row, widget_elem) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const content_holder = row.find('.message_content');
|
2018-06-26 09:30:57 +02:00
|
|
|
content_holder.empty().append(widget_elem);
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:20:33 +01:00
|
|
|
exports.activate = function (in_opts) {
|
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;
|
|
|
|
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[widget_type]) {
|
|
|
|
blueslip.warn('unknown widget_type', widget_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const callback = function (data) {
|
2018-02-23 16:20:33 +01:00
|
|
|
post_to_server({
|
|
|
|
msg_type: 'widget',
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
if (row.attr('id').startsWith('zhome') && narrow_state.active()) {
|
2018-07-03 08:59:28 +02:00
|
|
|
// Don't place widget in a home message row if we are narrowed
|
2018-06-26 09:30:57 +02:00
|
|
|
// to active state
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let widget_elem = widget_contents[message.id];
|
2018-06-26 09:30:57 +02:00
|
|
|
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.
|
2018-06-26 09:30:57 +02:00
|
|
|
widget_elem = $('<div>').addClass('widget-content');
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
widgets[widget_type].activate({
|
2018-02-23 16:20:33 +01:00
|
|
|
elem: widget_elem,
|
|
|
|
callback: callback,
|
|
|
|
message: message,
|
|
|
|
extra_data: extra_data,
|
|
|
|
});
|
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
widget_contents[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) {
|
2018-06-26 09:30:57 +02:00
|
|
|
widget_elem.handle_events(events);
|
2018-02-23 16:20:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-03 14:28:02 +02:00
|
|
|
exports.set_widgets_for_list = function () {
|
2018-06-26 09:30:57 +02:00
|
|
|
_.each(widget_contents, function (widget_elem, idx) {
|
|
|
|
if (current_msg_list.get(idx) !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = current_msg_list.get_row(idx);
|
2018-06-26 09:30:57 +02:00
|
|
|
set_widget_in_message(row, widget_elem);
|
|
|
|
}
|
|
|
|
});
|
2018-07-03 14:28:02 +02:00
|
|
|
};
|
|
|
|
|
2018-02-23 16:20:33 +01:00
|
|
|
exports.handle_event = function (widget_event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_elem = widget_contents[widget_event.message_id];
|
2018-02-23 16:20:33 +01:00
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
if (!widget_elem) {
|
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
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
widget_elem.handle_events(events);
|
2018-02-23 16:20:33 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.widgetize = exports;
|