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_list_widget: Refactor dropdown_list_widget to constructor function.

aryanshridhar5c2bafc6b02f
diff --git a/frontend_tests/node_tests/dropdown_list_widget.js b/frontend_tests/node_tests/dropdown_list_widget.js
index efa68cc5bb4..0504a95e4e6 100644
--- a/frontend_tests/node_tests/dropdown_list_widget.js
+++ b/frontend_tests/node_tests/dropdown_list_widget.js
@@ -12,7 +12,7 @@ const noop = () => {};
mock_esm("../../static/js/list_widget", {
create: () => ({init: noop}),
});
-const {DropdownListWidget: dropdown_list_widget} = zrequire("dropdown_list_widget");
+const {DropdownListWidget} = zrequire("dropdown_list_widget");
const setup_zjquery_data = (name) => {
const input_group = $(".input_group");
@@ -38,7 +38,7 @@ run_test("basic_functions", () => {
const {reset_button, $widget} = setup_zjquery_data(opts.widget_name);
- const widget = dropdown_list_widget(opts);
+ const widget = new DropdownListWidget(opts);
assert.equal(widget.value(), "one");
assert.equal(updated_value, undefined); // We haven't 'updated' the widget yet.
@@ -77,6 +77,6 @@ run_test("no_default_value", () => {
"dropdown-list-widget: Called without a default value; using null value",
);
setup_zjquery_data(opts.widget_name);
- const widget = dropdown_list_widget(opts);
+ const widget = new DropdownListWidget(opts);
assert.equal(widget.value(), "null-value");
});
diff --git a/static/js/dropdown_list_widget.js b/static/js/dropdown_list_widget.js
index adf3d550948..24ad45cdb7e 100644
--- a/static/js/dropdown_list_widget.js
+++ b/static/js/dropdown_list_widget.js
@@ -5,7 +5,7 @@ import render_dropdown_list from "../templates/settings/dropdown_list.hbs";
import * as blueslip from "./blueslip";
import * as ListWidget from "./list_widget";
-export const DropdownListWidget = function ({
+export function DropdownListWidget({
widget_name,
data,
default_text,
@@ -15,153 +15,179 @@ export const DropdownListWidget = function ({
value,
on_update = () => {},
}) {
- const container_id = `${widget_name}_widget`;
- const value_id = `id_${widget_name}`;
+ // Initializing values
+ this.widget_name = widget_name;
+ this.data = data;
+ this.default_text = default_text;
+ this.render_text = render_text;
+ this.null_value = null_value;
+ this.include_current_item = include_current_item;
+ this.initial_value = value;
+ this.on_update = on_update;
+
+ this.container_id = `${widget_name}_widget`;
+ this.value_id = `id_${widget_name}`;
+
if (value === undefined) {
- value = null_value;
+ this.initial_value = null_value;
blueslip.warn("dropdown-list-widget: Called without a default value; using null value");
}
- const render_default_text = (elem) => {
- elem.text(default_text);
- elem.addClass("text-warning");
- elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").hide();
- };
+ // Setting up dropdown_list_widget
+ this.setup();
+}
- const render = (value) => {
- $(`#${CSS.escape(container_id)} #${CSS.escape(value_id)}`).data("value", value);
+DropdownListWidget.prototype.render_default_text = function (elem) {
+ elem.text(this.default_text);
+ elem.addClass("text-warning");
+ elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").hide();
+};
- const elem = $(`#${CSS.escape(container_id)} #${CSS.escape(widget_name)}_name`);
+DropdownListWidget.prototype.render = function (value) {
+ $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.value_id)}`).data("value", value);
- if (!value || value === null_value) {
- render_default_text(elem);
- return;
- }
+ const elem = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.widget_name)}_name`);
- // Happy path
- const item = data.find((x) => x.value === value.toString());
+ if (!value || value === this.null_value) {
+ this.render_default_text(elem);
+ return;
+ }
- if (item === undefined) {
- render_default_text(elem);
- return;
- }
+ // Happy path
+ const item = this.data.find((x) => x.value === value.toString());
- const text = render_text(item.name);
- elem.text(text);
- elem.removeClass("text-warning");
- elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").show();
- };
+ if (item === undefined) {
+ this.render_default_text(elem);
+ return;
+ }
- const update = (value) => {
- render(value);
- on_update(value);
- };
+ const text = this.render_text(item.name);
+ elem.text(text);
+ elem.removeClass("text-warning");
+ elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").show();
+};
- const register_event_handlers = () => {
- $(`#${CSS.escape(container_id)} .dropdown-list-body`).on(
- "click keypress",
- ".list_item",
- function (e) {
- const setting_elem = $(this).closest(`.${CSS.escape(widget_name)}_setting`);
- if (e.type === "keypress") {
- if (e.key === "Enter") {
- setting_elem.find(".dropdown-menu").dropdown("toggle");
- } else {
- return;
- }
- }
- const value = $(this).attr("data-value");
- update(value);
- },
- );
- $(`#${CSS.escape(container_id)} .dropdown_list_reset_button`).on("click", (e) => {
- update(null_value);
- e.preventDefault();
- });
- };
+DropdownListWidget.prototype.update = function (value) {
+ this.render(value);
+ this.on_update(value);
+};
- const setup = () => {
- // populate the dropdown
- const dropdown_list_body = $(
- `#${CSS.escape(container_id)} .dropdown-list-body`,
- ).expectOne();
- const search_input = $(`#${CSS.escape(container_id)} .dropdown-search > input[type=text]`);
- const dropdown_toggle = $(`#${CSS.escape(container_id)} .dropdown-toggle`);
- const get_data = (data) => {
- if (include_current_item) {
- return data;
+DropdownListWidget.prototype.register_event_handlers = function () {
+ $(`#${CSS.escape(this.container_id)} .dropdown-list-body`).on(
+ "click keypress",
+ ".list_item",
+ (e) => {
+ const setting_elem = $(e.currentTarget).closest(
+ `.${CSS.escape(this.widget_name)}_setting`,
+ );
+ if (e.type === "keypress") {
+ if (e.key === "Enter") {
+ setting_elem.find(".dropdown-menu").dropdown("toggle");
+ } else {
+ return;
+ }
}
- return data.filter((x) => x.value !== value.toString());
- };
+ const value = $(e.currentTarget).attr("data-value");
+ this.update(value);
+ },
+ );
+ $(`#${CSS.escape(this.container_id)} .dropdown_list_reset_button`).on("click", (e) => {
+ this.update(this.null_value);
+ e.preventDefault();
+ });
+};
- ListWidget.create(dropdown_list_body, get_data(data), {
- name: `${CSS.escape(widget_name)}_list`,
- modifier(item) {
- return render_dropdown_list({item});
- },
- filter: {
- element: search_input,
- predicate(item, value) {
- return item.name.toLowerCase().includes(value);
- },
+DropdownListWidget.prototype.setup_dropdown_widget = function (data) {
+ const dropdown_list_body = $(
+ `#${CSS.escape(this.container_id)} .dropdown-list-body`,
+ ).expectOne();
+ const search_input = $(`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`);
+ const get_data = () => {
+ if (this.include_current_item) {
+ return data;
+ }
+ return data.filter((x) => x.value !== this.value.toString());
+ };
+
+ ListWidget.create(dropdown_list_body, get_data(data), {
+ name: `${CSS.escape(this.widget_name)}_list`,
+ modifier(item) {
+ return render_dropdown_list({item});
+ },
+ filter: {
+ element: search_input,
+ predicate(item, value) {
+ return item.name.toLowerCase().includes(value);
},
- simplebar_container: $(`#${CSS.escape(container_id)} .dropdown-list-wrapper`),
- });
- $(`#${CSS.escape(container_id)} .dropdown-search`).on("click", (e) => {
- e.stopPropagation();
- });
-
- dropdown_toggle.on("click", () => {
- search_input.val("").trigger("input");
- });
-
- dropdown_toggle.on("focus", (e) => {
- // On opening a Bootstrap Dropdown, the parent element receives focus.
- // Here, we want our search input to have focus instead.
- e.preventDefault();
- // This function gets called twice when focusing the
- // dropdown, and only in the second call is the input
- // field visible in the DOM; so the following visibility
- // check ensures we wait for the second call to focus.
- if (dropdown_list_body.is(":visible")) {
- search_input.trigger("focus");
- }
- });
+ },
+ simplebar_container: $(`#${CSS.escape(this.container_id)} .dropdown-list-wrapper`),
+ });
+};
- search_input.on("keydown", (e) => {
- const {key, keyCode, which} = e;
- const navigation_keys = ["ArrowUp", "ArrowDown", "Escape"];
- if (!navigation_keys.includes(key)) {
- return;
- }
- e.preventDefault();
- e.stopPropagation();
-
- // We pass keyCode instead of key here because the outdated
- // bootstrap library we have at static/third/ still uses the
- // deprecated keyCode & which properties.
- const custom_event = new $.Event("keydown.dropdown.data-api", {keyCode, which});
- dropdown_toggle.trigger(custom_event);
- });
-
- render(value);
- register_event_handlers();
- };
+// Sets the focus to the ListWidget input once the dropdown button is clicked.
+DropdownListWidget.prototype.dropdown_toggle_click_handler = function () {
+ const dropdown_toggle = $(`#${CSS.escape(this.container_id)} .dropdown-toggle`);
+ const search_input = $(`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`);
- const get_value = () => {
- let val = $(`#${CSS.escape(container_id)} #${CSS.escape(value_id)}`).data("value");
- if (val === null) {
- val = "";
+ dropdown_toggle.on("click", () => {
+ search_input.val("").trigger("input");
+ });
+};
+
+DropdownListWidget.prototype.setup = function () {
+ // populate the dropdown
+ const dropdown_list_body = $(
+ `#${CSS.escape(this.container_id)} .dropdown-list-body`,
+ ).expectOne();
+ const search_input = $(`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`);
+ const dropdown_toggle = $(`#${CSS.escape(this.container_id)} .dropdown-toggle`);
+
+ this.setup_dropdown_widget(this.data);
+
+ $(`#${CSS.escape(this.container_id)} .dropdown-search`).on("click", (e) => {
+ e.stopPropagation();
+ });
+
+ this.dropdown_toggle_click_handler();
+
+ dropdown_toggle.on("focus", (e) => {
+ // On opening a Bootstrap Dropdown, the parent element receives focus.
+ // Here, we want our search input to have focus instead.
+ e.preventDefault();
+ // This function gets called twice when focusing the
+ // dropdown, and only in the second call is the input
+ // field visible in the DOM; so the following visibility
+ // check ensures we wait for the second call to focus.
+ if (dropdown_list_body.is(":visible")) {
+ search_input.trigger("focus");
}
- return val;
- };
+ });
- // Run setup() automatically on initialization.
- setup();
+ search_input.on("keydown", (e) => {
+ const {key, keyCode, which} = e;
+ const navigation_keys = ["ArrowUp", "ArrowDown", "Escape"];
+ if (!navigation_keys.includes(key)) {
+ return;
+ }
+ e.preventDefault();
+ e.stopPropagation();
+
+ // We pass keyCode instead of key here because the outdated
+ // bootstrap library we have at static/third/ still uses the
+ // deprecated keyCode & which properties.
+ const custom_event = new $.Event("keydown.dropdown.data-api", {keyCode, which});
+ dropdown_toggle.trigger(custom_event);
+ });
+
+ this.render(this.initial_value);
+ this.register_event_handlers();
+};
- return {
- render,
- value: get_value,
- update,
- };
+// Returns the updated value
+DropdownListWidget.prototype.value = function () {
+ let val = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.value_id)}`).data("value");
+ if (val === null) {
+ val = "";
+ }
+ return val;
};
diff --git a/static/js/message_edit.js b/static/js/message_edit.js
index 9ff7471ebea..27eef9ca786 100644
--- a/static/js/message_edit.js
+++ b/static/js/message_edit.js
@@ -15,7 +15,7 @@ import * as composebox_typeahead from "./composebox_typeahead";
import * as condense from "./condense";
import * as confirm_dialog from "./confirm_dialog";
import * as dialog_widget from "./dialog_widget";
-import {DropdownListWidget as dropdown_list_widget} from "./dropdown_list_widget";
+import {DropdownListWidget} from "./dropdown_list_widget";
import * as echo from "./echo";
import * as giphy from "./giphy";
import {$t, $t_html} from "./i18n";
@@ -441,7 +441,7 @@ function edit_message(row, raw_content) {
const copy_message = row.find(".copy_message");
if (is_stream_editable) {
- stream_widget = dropdown_list_widget(opts);
+ stream_widget = new DropdownListWidget(opts);
}
stream_bar.decorate(message.stream, stream_header_colorblock, false);
diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js
index 24fd61d42c1..c35e12706ee 100644
--- a/static/js/settings_bots.js
+++ b/static/js/settings_bots.js
@@ -10,7 +10,7 @@ import * as avatar from "./avatar";
import * as bot_data from "./bot_data";
import * as channel from "./channel";
import {csrf_token} from "./csrf";
-import {DropdownListWidget as dropdown_list_widget} from "./dropdown_list_widget";
+import {DropdownListWidget} from "./dropdown_list_widget";
import {$t} from "./i18n";
import * as loading from "./loading";
import * as overlays from "./overlays";
@@ -440,7 +440,7 @@ export function set_up() {
default_text: $t({defaultMessage: "No owner"}),
value: bot.owner_id,
};
- const owner_widget = dropdown_list_widget(opts);
+ const owner_widget = new DropdownListWidget(opts);
const service = bot_data.get_services(bot_id)[0];
if (bot.bot_type.toString() === OUTGOING_WEBHOOK_BOT_TYPE) {
diff --git a/static/js/settings_org.js b/static/js/settings_org.js
index 816e63256f5..52a6e344446 100644
--- a/static/js/settings_org.js
+++ b/static/js/settings_org.js
@@ -9,7 +9,7 @@ import * as blueslip from "./blueslip";
import * as channel from "./channel";
import * as confirm_dialog from "./confirm_dialog";
import {csrf_token} from "./csrf";
-import {DropdownListWidget as dropdown_list_widget} from "./dropdown_list_widget";
+import {DropdownListWidget} from "./dropdown_list_widget";
import {$t, $t_html} from "./i18n";
import * as loading from "./loading";
import * as overlays from "./overlays";
@@ -638,17 +638,17 @@ export function init_dropdown_widgets() {
render_text: (x) => `#${x}`,
null_value: -1,
};
- notifications_stream_widget = dropdown_list_widget({
+ notifications_stream_widget = new DropdownListWidget({
widget_name: "realm_notifications_stream_id",
value: page_params.realm_notifications_stream_id,
...notification_stream_options,
});
- signup_notifications_stream_widget = dropdown_list_widget({
+ signup_notifications_stream_widget = new DropdownListWidget({
widget_name: "realm_signup_notifications_stream_id",
value: page_params.realm_signup_notifications_stream_id,
...notification_stream_options,
});
- default_code_language_widget = dropdown_list_widget({
+ default_code_language_widget = new DropdownListWidget({
widget_name: "realm_default_code_block_language",
data: Object.keys(pygments_data.langs).map((x) => ({
name: x,
diff --git a/static/js/settings_users.js b/static/js/settings_users.js
index 4131e5f146e..97c0b6989c6 100644
--- a/static/js/settings_users.js
+++ b/static/js/settings_users.js
@@ -10,7 +10,7 @@ import * as bot_data from "./bot_data";
import * as channel from "./channel";
import * as confirm_dialog from "./confirm_dialog";
import * as dialog_widget from "./dialog_widget";
-import {DropdownListWidget as dropdown_list_widget} from "./dropdown_list_widget";
+import {DropdownListWidget} from "./dropdown_list_widget";
import {$t, $t_html} from "./i18n";
import * as ListWidget from "./list_widget";
import * as loading from "./loading";
@@ -652,7 +652,7 @@ function handle_bot_form(tbody, status_field) {
};
// Note: Rendering this is quite expensive in
// organizations with 10Ks of users.
- owner_widget = dropdown_list_widget(opts);
+ owner_widget = new DropdownListWidget(opts);
}
dialog_widget.launch({
diff --git a/static/js/stream_popover.js b/static/js/stream_popover.js
index d610e290a2e..e80345ac072 100644
--- a/static/js/stream_popover.js
+++ b/static/js/stream_popover.js
@@ -13,7 +13,7 @@ import * as browser_history from "./browser_history";
import * as channel from "./channel";
import * as compose_actions from "./compose_actions";
import * as confirm_dialog from "./confirm_dialog";
-import {DropdownListWidget as dropdown_list_widget} from "./dropdown_list_widget";
+import {DropdownListWidget} from "./dropdown_list_widget";
import * as hash_util from "./hash_util";
import {$t, $t_html} from "./i18n";
import * as message_edit from "./message_edit";
@@ -379,7 +379,7 @@ function build_move_topic_to_stream_popover(e, current_stream_id, topic_name) {
$("#move-a-topic-modal-holder").html(render_move_topic_to_stream(args));
- stream_widget = dropdown_list_widget(opts);
+ stream_widget = new DropdownListWidget(opts);
stream_header_colorblock = $("#move_topic_modal .topic_stream_edit_header").find(
".stream_header_colorblock",
);