widgets: Avoid adding the widget_elem if it already exists.

Fixes #18631
This commit is contained in:
Ganesh Pawar 2021-07-24 13:12:04 +05:30 committed by Tim Abbott
parent ab2ef76baa
commit 43a3a73a0d
2 changed files with 12 additions and 1 deletions

View File

@ -111,6 +111,7 @@ test("activate", ({override}) => {
is_event_handled = false;
assert.ok(!widgetize.widget_contents.has(opts.message.id));
message_content.set_find_results(".widget-content", false);
widgetize.activate(opts);
assert.ok(is_widget_elem_inserted);
@ -122,6 +123,7 @@ test("activate", ({override}) => {
is_widget_activated = false;
is_event_handled = false;
message_content.set_find_results(".widget-content", false);
widgetize.activate(opts);
assert.ok(is_widget_elem_inserted);
@ -133,6 +135,7 @@ test("activate", ({override}) => {
is_widget_activated = false;
is_event_handled = false;
message_content.set_find_results(".widget-content", false);
widgetize.activate(opts);
assert.ok(!is_widget_elem_inserted);

View File

@ -21,7 +21,15 @@ export function clear_for_testing() {
function set_widget_in_message(row, widget_elem) {
const content_holder = row.find(".message_content");
content_holder.empty().append(widget_elem);
// Avoid adding the widget_elem if it already exists.
// This can happen when the app loads in the "Recent topics"
// 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) {