<commit_before>const widgets = {};

widgets.poll = poll_widget;
widgets.tictactoe = tictactoe_widget;
widgets.todo = todo_widget;
widgets.zform = zform;

const widget_contents = {};
exports.widget_contents = widget_contents;

function set_widget_in_message(row, widget_elem) {
    const content_holder = row.find('.message_content');
    content_holder.empty().append(widget_elem);
}

exports.activate = function (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;

    events.shift();

    if (!widgets[widget_type]) {
        blueslip.warn('unknown widget_type', widget_type);
        return;
    }

    const callback = function (data) {
        post_to_server({
            msg_type: 'widget',
            data: data,
        });
    };

    if (row.attr('id').startsWith('zhome') && narrow_state.active()) {
        // Don't place widget in a home message row if we are narrowed
        // to active state
        return;
    }

    let widget_elem = widget_contents[message.id];
    if (widget_elem) {
        set_widget_in_message(row, widget_elem);
        return;
    }

    // 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');

    widgets[widget_type].activate({
        elem: widget_elem,
        callback: callback,
        message: message,
        extra_data: extra_data,
    });

    widget_contents[message.id] = widget_elem;
    set_widget_in_message(row, widget_elem);

    // 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);
    }
};<commit_msg>widgetize: Convert widgets from object to Map.<commit_after>const widgets = new Map([
    ["poll", poll_widget],
    ["tictactoe", tictactoe_widget],
    ["todo", todo_widget],
    ["zform", zform],
]);

const widget_contents = {};
exports.widget_contents = widget_contents;

function set_widget_in_message(row, widget_elem) {
    const content_holder = row.find('.message_content');
    content_holder.empty().append(widget_elem);
}

exports.activate = function (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;

    events.shift();

    if (!widgets.has(widget_type)) {
        blueslip.warn('unknown widget_type', widget_type);
        return;
    }

    const callback = function (data) {
        post_to_server({
            msg_type: 'widget',
            data: data,
        });
    };

    if (row.attr('id').startsWith('zhome') && narrow_state.active()) {
        // Don't place widget in a home message row if we are narrowed
        // to active state
        return;
    }

    let widget_elem = widget_contents[message.id];
    if (widget_elem) {
        set_widget_in_message(row, widget_elem);
        return;
    }

    // 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');

    widgets.get(widget_type).activate({
        elem: widget_elem,
        callback: callback,
        message: message,
        extra_data: extra_data,
    });

    widget_contents[message.id] = widget_elem;
    set_widget_in_message(row, widget_elem);

    // 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);
    }
};
{'fraction_non_alphanumeric': 0.11139240506329114, 'fraction_numerical': 0.0005063291139240507, 'mean_word_length': 2.8322017458777884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2322368', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1186, 'n_words': 300}