Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

dropdown-widget: Handle button click in a separate click handlers.

Sahil Batrada53d5b97882
diff --git a/web/src/dropdown_widget.ts b/web/src/dropdown_widget.ts
index b639f06ff5d..41fe95935f1 100644
--- a/web/src/dropdown_widget.ts
+++ b/web/src/dropdown_widget.ts
@@ -51,6 +51,7 @@ export type DropdownWidgetOptions = {
widget: DropdownWidget,
is_sticky_bottom_option_clicked: boolean,
) => void;
+ item_button_click_callback?: (event: JQuery.ClickEvent) => void;
// Provide an parent element to widget which will be re-rendered if the widget is setup again.
// It is important to not pass `$("body")` here for widgets that would be `setup()`
// multiple times, so that we don't have duplicate event handlers.
@@ -96,6 +97,7 @@ export class DropdownWidget {
widget: DropdownWidget,
is_sticky_bottom_option_clicked: boolean,
) => void;
+ item_button_click_callback: (event: JQuery.ClickEvent) => void;
focus_target_on_hidden: boolean;
on_show_callback: (instance: tippy.Instance, widget: DropdownWidget) => void;
on_mount_callback: (instance: tippy.Instance) => void;
@@ -132,6 +134,7 @@ export class DropdownWidget {
this.widget_value_selector = `${this.widget_selector} .dropdown_widget_value`;
this.get_options = options.get_options;
this.item_click_callback = options.item_click_callback;
+ this.item_button_click_callback = options.item_button_click_callback ?? noop;
this.focus_target_on_hidden = options.focus_target_on_hidden ?? true;
this.on_show_callback = options.on_show_callback ?? noop;
this.on_mount_callback = options.on_mount_callback ?? noop;
@@ -616,6 +619,13 @@ export class DropdownWidget {
this.current_hover_index = 0;
});
+ $popper.on("click", ".dropdown-list-control-button", (event) => {
+ event.preventDefault();
+ event.stopPropagation();
+ this.item_button_click_callback(event);
+ this.current_hover_index = 0;
+ });
+
// Click on $sticky_bottom_option.
$popper.on("click", ".sticky-bottom-option", (event) => {
this.item_click_callback(event, instance, this, true);
diff --git a/web/src/saved_snippets_ui.ts b/web/src/saved_snippets_ui.ts
index babcc6519cd..687de38a415 100644
--- a/web/src/saved_snippets_ui.ts
+++ b/web/src/saved_snippets_ui.ts
@@ -101,15 +101,7 @@ function delete_saved_snippet(saved_snippet_id: string): void {
});
}
-function item_click_callback(
- event: JQuery.ClickEvent,
- dropdown: tippy.Instance,
- widget: dropdown_widget.DropdownWidget,
- is_sticky_bottom_option_clicked: boolean,
-): void {
- event.preventDefault();
- event.stopPropagation();
-
+function item_button_click_callback(event: JQuery.ClickEvent): void {
if (
$(event.target).closest(".saved_snippets-dropdown-list-container .dropdown-list-delete")
.length > 0
@@ -118,7 +110,9 @@ function item_click_callback(
html_heading: $t_html({defaultMessage: "Delete saved snippet?"}),
html_body: render_confirm_delete_saved_snippet(),
on_click() {
- const saved_snippet_id = $(event.currentTarget).attr("data-unique-id");
+ const saved_snippet_id = $(event.target)
+ .closest(".list-item")
+ .attr("data-unique-id");
assert(saved_snippet_id !== undefined);
delete_saved_snippet(saved_snippet_id);
},
@@ -130,7 +124,7 @@ function item_click_callback(
$(event.target).closest(".saved_snippets-dropdown-list-container .dropdown-list-edit")
.length > 0
) {
- const saved_snippet_id = $(event.currentTarget).attr("data-unique-id");
+ const saved_snippet_id = $(event.target).closest(".list-item").attr("data-unique-id");
assert(saved_snippet_id !== undefined);
const saved_snippet = saved_snippets.get_saved_snippet_by_id(
@@ -157,6 +151,16 @@ function item_click_callback(
});
return;
}
+}
+
+function item_click_callback(
+ event: JQuery.ClickEvent,
+ dropdown: tippy.Instance,
+ widget: dropdown_widget.DropdownWidget,
+ is_sticky_bottom_option_clicked: boolean,
+): void {
+ event.preventDefault();
+ event.stopPropagation();
dropdown.hide();
// Get target textarea where the "Add saved snippet" button is clicked.
@@ -199,6 +203,7 @@ export function setup_saved_snippets_dropdown_widget(widget_selector: string): v
widget_selector,
get_options: saved_snippets.get_options_for_dropdown_widget,
item_click_callback,
+ item_button_click_callback,
$events_container: $("body"),
unique_id_type: "number",
sticky_bottom_option: $t({
diff --git a/web/templates/dropdown_list.hbs b/web/templates/dropdown_list.hbs
index e55b94c4e14..b913de657e9 100644
--- a/web/templates/dropdown_list.hbs
+++ b/web/templates/dropdown_list.hbs
@@ -6,10 +6,10 @@
{{#if bold_current_selection}}
<span class="dropdown-list-bold-selected">{{name}}</span>
{{#if has_edit_icon}}
- {{> components/icon_button custom_classes="dropdown-list-edit" intent="neutral" icon="edit" aria-label=edit_icon_label }}
+ {{> components/icon_button custom_classes="dropdown-list-edit dropdown-list-control-button" intent="neutral" icon="edit" aria-label=edit_icon_label }}
{{/if}}
{{#if has_delete_icon}}
- {{> components/icon_button custom_classes="dropdown-list-delete" intent="danger" icon="trash" aria-label=delete_icon_label }}
+ {{> components/icon_button custom_classes="dropdown-list-delete dropdown-list-control-button" intent="danger" icon="trash" aria-label=delete_icon_label }}
{{/if}}
{{else}}
{{name}}