settings: Improve enable_or_disable_save_button method code.

This commit reorganizes the code in enable_or_disable_save_button
method, and improves its readability. It also replaces the previous
tooltip logic for the disabled save button in the save/discard widget
with the more standardized methods to handle the same from ui_util.ts.
This commit is contained in:
Sayam Samal
2025-05-14 13:27:28 +05:30
committed by Tim Abbott
parent 30bfabb2eb
commit 0914732387
2 changed files with 34 additions and 33 deletions

View File

@@ -35,6 +35,7 @@ import {stream_subscription_schema} from "./sub_store.ts";
import type {GroupSettingPillContainer} from "./typeahead_helper.ts";
import {group_setting_value_schema} from "./types.ts";
import type {HTMLSelectOneElement} from "./types.ts";
import * as ui_util from "./ui_util.ts";
import * as user_group_pill from "./user_group_pill.ts";
import * as user_groups from "./user_groups.ts";
import type {UserGroup} from "./user_groups.ts";
@@ -1445,45 +1446,40 @@ function should_disable_save_button_for_group_settings(settings: string[]): bool
}
function enable_or_disable_save_button($subsection_elem: JQuery): void {
const $save_button = $subsection_elem.find(".save-button");
const time_limit_settings = [...$subsection_elem.find(".time-limit-setting")];
if (
time_limit_settings.length > 0 &&
should_disable_save_button_for_time_limit_settings(time_limit_settings)
) {
$save_button.prop("disabled", true);
return;
}
let disable_save_button = false;
if (time_limit_settings.length > 0) {
disable_save_button =
should_disable_save_button_for_time_limit_settings(time_limit_settings);
} else if ($subsection_elem.attr("id") === "org-compose-settings") {
disable_save_button = should_disable_save_button_for_jitsi_server_url_setting();
const $button_wrapper = $subsection_elem.find<tippy.PopperElement>(
".subsection-changes-save",
);
const tippy_instance = util.the($button_wrapper)._tippy;
if (disable_save_button) {
// avoid duplication of tippy
if (!tippy_instance) {
const opts: Partial<tippy.Props> = {placement: "top"};
initialize_disable_button_hint_popover(
$button_wrapper,
if (
$subsection_elem.attr("id") === "org-compose-settings" &&
should_disable_save_button_for_jitsi_server_url_setting()
) {
ui_util.disable_element_and_add_tooltip(
$save_button,
$t({defaultMessage: "Cannot save invalid Jitsi server URL."}),
opts,
);
}
} else {
if (tippy_instance) {
tippy_instance.destroy();
}
}
return;
}
if (!disable_save_button) {
const group_settings = [...$subsection_elem.find(".pill-container")].map((elem) =>
extract_property_name($(elem)),
);
if (group_settings.length > 0) {
disable_save_button = should_disable_save_button_for_group_settings(group_settings);
}
if (
group_settings.length > 0 &&
should_disable_save_button_for_group_settings(group_settings)
) {
$save_button.prop("disabled", true);
return;
}
$subsection_elem.find(".subsection-changes-save button").prop("disabled", disable_save_button);
ui_util.enable_element_and_remove_tooltip($save_button);
}
export function initialize_disable_button_hint_popover(

View File

@@ -22,6 +22,10 @@ mock_esm("../src/buttons", {
modify_action_button_style: noop,
});
mock_esm("../src/scroll_util", {scroll_element_into_container: noop});
mock_esm("../src/ui_util", {
disable_element_and_add_tooltip: noop,
enable_element_and_remove_tooltip: noop,
});
set_global("document", "document-stub");
set_global("requestAnimationFrame", (func) => func());
@@ -95,6 +99,7 @@ function createSaveButtons(subsection) {
$stub_save_button_header.set_find_results(".time-limit-setting", []);
$stub_save_button_header.set_find_results(".pill-container", []);
$stub_save_button_header.set_find_results(".subsection-changes-save button", $stub_save_button);
$stub_save_button_header.set_find_results(".save-button", $stub_save_button);
return {
props,