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.

custom-profile-fields: Replace select element with the dropdown widget.

Niloth P45bbd56366ff
diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts
index 904674ca853..705ecb0d432 100644
--- a/web/src/settings_components.ts
+++ b/web/src/settings_components.ts
@@ -442,12 +442,26 @@ export const external_account_field_schema = z.object({
export type ExternalAccountFieldData = z.output<typeof external_account_field_schema>;
-function read_external_account_field_data($profile_field_form: JQuery): ExternalAccountFieldData {
- const field_data: ExternalAccountFieldData = {
- subtype: $profile_field_form
- .find<HTMLSelectOneElement>("select:not([multiple])[name=external_acc_field_type]")
- .val()!,
- };
+function read_external_account_field_data(
+ $profile_field_form: JQuery,
+ old_field_data: ExternalAccountFieldData | undefined,
+): ExternalAccountFieldData {
+ let field_data: ExternalAccountFieldData;
+ // Use dropdown widget value in "create field form".
+ if (old_field_data === undefined) {
+ const widget = get_widget_for_dropdown_list_settings("external_accounts_type");
+ assert(widget !== null);
+ const value = widget.value();
+ assert(typeof value === "string");
+ field_data = {
+ subtype: value,
+ };
+ } // Use existing subtype in "edit field form".
+ else {
+ field_data = {
+ subtype: old_field_data.subtype,
+ };
+ }
if (field_data.subtype === "custom") {
field_data.url_pattern = $profile_field_form
.find<HTMLInputElement>("input[name=url_pattern]")
@@ -469,7 +483,10 @@ export function read_field_data_from_form(
if (field_type_id === field_types.SELECT.id) {
return read_select_field_data_from_form($profile_field_form, old_field_data);
} else if (field_type_id === field_types.EXTERNAL_ACCOUNT.id) {
- return read_external_account_field_data($profile_field_form);
+ const parsed_old_field_data = old_field_data
+ ? external_account_field_schema.parse(old_field_data)
+ : undefined;
+ return read_external_account_field_data($profile_field_form, parsed_old_field_data);
}
return undefined;
}
@@ -501,6 +518,7 @@ const dropdown_widget_map = new Map<string, DropdownWidget | null>([
["realm_can_create_web_public_channel_group", null],
["folder_id", null],
["channel_privacy", null],
+ ["external_accounts_type", null],
]);
export function get_widget_for_dropdown_list_settings(
diff --git a/web/src/settings_profile_fields.ts b/web/src/settings_profile_fields.ts
index 47042e2e795..1f785414d18 100644
--- a/web/src/settings_profile_fields.ts
+++ b/web/src/settings_profile_fields.ts
@@ -1,6 +1,7 @@
import $ from "jquery";
import assert from "minimalistic-assert";
import SortableJS from "sortablejs";
+import type * as tippy from "tippy.js";
import render_confirm_delete_profile_field from "../templates/confirm_dialog/confirm_delete_profile_field.hbs";
import render_confirm_delete_profile_field_option from "../templates/confirm_dialog/confirm_delete_profile_field_option.hbs";
@@ -12,6 +13,8 @@ import render_settings_profile_field_choice from "../templates/settings/profile_
import * as channel from "./channel.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
import * as dialog_widget from "./dialog_widget.ts";
+import type {DropdownWidget, Option} from "./dropdown_widget.ts";
+import * as dropdown_widget from "./dropdown_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import * as ListWidget from "./list_widget.ts";
import * as loading from "./loading.ts";
@@ -38,6 +41,48 @@ const meta: {
loaded: false,
};
+let external_accounts_dropdown_widget: DropdownWidget;
+
+function setup_external_accounts_dropdown_widget(): void {
+ const custom_option = {
+ name: $t_html({defaultMessage: "Custom"}),
+ unique_id: "custom",
+ };
+
+ function get_options_for_external_accounts_dropdown_widget(): Option[] {
+ const options = [
+ custom_option,
+ ...Object.entries(realm.realm_default_external_accounts)
+ .toSorted(([, a], [, b]) => util.strcmp(a.text, b.text))
+ .map(([key, account]) => ({
+ name: account.text,
+ unique_id: key,
+ })),
+ ];
+
+ return options;
+ }
+
+ external_accounts_dropdown_widget = new dropdown_widget.DropdownWidget({
+ widget_name: "external_accounts_type",
+ get_options: get_options_for_external_accounts_dropdown_widget,
+ item_click_callback: external_account_item_click_callback,
+ $events_container: $("#profile_field_external_accounts"),
+ text_if_current_value_not_in_options: $t({
+ defaultMessage: "Select an account type",
+ }),
+ unique_id_type: "string",
+ on_show_callback(_dropdown: tippy.Instance, widget: dropdown_widget.DropdownWidget) {
+ external_accounts_dropdown_widget = widget;
+ },
+ });
+ external_accounts_dropdown_widget.setup();
+ settings_components.set_dropdown_setting_widget(
+ "external_accounts_type",
+ external_accounts_dropdown_widget,
+ );
+}
+
function display_success_status(): void {
const $spinner = $("#admin-profile-field-status").expectOne();
const success_msg_html = settings_ui.strings.success_html;
@@ -157,11 +202,37 @@ function initialize_form_to_defaults(): void {
$("#custom_field_url_pattern").val("");
$("#custom_external_account_url_pattern").hide();
$("#profile_field_external_accounts").hide();
- $("#profile_field_external_accounts_type").val(
- $<HTMLSelectOneElement>(
- "select:not([multiple])#profile_field_external_accounts_type option:first-child",
- ).val()!,
- );
+
+ external_accounts_dropdown_widget.render();
+}
+
+function external_account_item_click_callback(
+ event: JQuery.ClickEvent,
+ dropdown: tippy.Instance,
+ widget: dropdown_widget.DropdownWidget,
+): void {
+ $("#dialog_error").hide();
+ dropdown.hide();
+ event.preventDefault();
+ event.stopPropagation();
+
+ const external_account_type = external_accounts_dropdown_widget.value();
+ assert(external_account_type !== undefined);
+
+ if (external_account_type === "custom") {
+ $("#custom_external_account_url_pattern").show();
+ $("#profile_field_name").val("").prop("disabled", false);
+ $("#profile_field_hint").val("").prop("disabled", false);
+ widget.render();
+ return;
+ }
+
+ $("#custom_external_account_url_pattern").hide();
+ const external_account = realm.realm_default_external_accounts[external_account_type];
+ assert(external_account !== undefined);
+ $("#profile_field_name").val(external_account.name).prop("disabled", true);
+ $("#profile_field_hint").val("").prop("disabled", true);
+ widget.render();
}
function update_form_for_field_type_selection(): void {
@@ -183,20 +254,9 @@ function update_form_for_field_type_selection(): void {
switch (profile_field_type) {
case field_types.EXTERNAL_ACCOUNT.id: {
$("#profile_field_external_accounts").show();
- const profile_field_external_account_type = $<HTMLSelectOneElement>(
- "select:not([multiple])#profile_field_external_accounts_type",
- ).val()!;
- if (profile_field_external_account_type === "custom") {
- $("#custom_external_account_url_pattern").show();
- } else {
- $("#custom_external_account_url_pattern").hide();
- const external_account =
- realm.realm_default_external_accounts[profile_field_external_account_type];
- assert(external_account !== undefined);
- const profile_field_name = external_account.name;
- $("#profile_field_name").val(profile_field_name).prop("disabled", true);
- $("#profile_field_hint").val("").prop("disabled", true);
- }
+ external_accounts_dropdown_widget.render();
+ $("#profile_field_name").val("").prop("disabled", true);
+ $("#profile_field_hint").val("").prop("disabled", true);
break;
}
case field_types.PRONOUNS.id: {
@@ -267,8 +327,9 @@ function open_custom_profile_field_creation_form_modal(): void {
function initialize_custom_profile_field_form(): void {
set_up_select_field();
+ setup_external_accounts_dropdown_widget();
- $("#profile_field_type, #profile_field_external_accounts_type").on("change", () => {
+ $("#profile_field_type").on("change", () => {
update_form_for_field_type_selection();
});
diff --git a/web/templates/settings/add_new_custom_profile_field_form.hbs b/web/templates/settings/add_new_custom_profile_field_form.hbs
index c5e9c6cf64b..dbe531b30c9 100644
--- a/web/templates/settings/add_new_custom_profile_field_form.hbs
+++ b/web/templates/settings/add_new_custom_profile_field_form.hbs
@@ -9,13 +9,7 @@
</select>
</div>
<div class="input-group" id="profile_field_external_accounts">
- <label for="profile_field_external_accounts_type" class="modal-field-label">{{t "External account type" }}</label>
- <select id="profile_field_external_accounts_type" name="external_acc_field_type" class="modal_select bootstrap-focus-style">
- {{#each realm_default_external_accounts}}
- <option value='{{@key}}'>{{this.text}}</option>
- {{/each}}
- <option value="custom">{{t 'Custom' }}</option>
- </select>
+ {{> ../dropdown_widget_with_label widget_name="external_accounts_type" label=(t "External accounts")}}
</div>
<div class="input-group">
<label for="profile_field_name" class="modal-field-label">{{t "Label" }}</label>