dialog_widget: Extract `get_current_values` function from `launch`.

This commit separates the `get_current_values` function from the
`launch` code, allowing it to be exported and utilized independently.

Fixes part of #26692.

Co-authored-by: Kunal Sharma <v.shm.kunal@gmail.com>.
Co-authored-by: Angelica <angelica.ferlin@gmail.com>.
This commit is contained in:
sanchi-t 2024-03-07 15:52:56 +05:30 committed by Tim Abbott
parent d2516607bb
commit 2ad886f427
1 changed files with 18 additions and 22 deletions

View File

@ -120,6 +120,24 @@ export function close(on_hidden_callback?: () => void): void {
modals.close(current_dialog_widget_id(), {on_hidden: on_hidden_callback});
}
export function get_current_values($inputs: JQuery): Record<string, unknown> {
const current_values: Record<string, unknown> = {};
$inputs.each(function () {
const property_name = $(this).attr("name")!;
if (property_name) {
if (this instanceof HTMLInputElement && this.type === "file" && this.files?.length) {
// If the input is a file input and a file has been selected, set value to file object
current_values[property_name] = this.files[0];
} else if (property_name === "edit_bot_owner") {
current_values[property_name] = $(this).find(".dropdown_widget_value").text();
} else {
current_values[property_name] = $(this).val();
}
}
});
return current_values;
}
export function launch(conf: DialogWidgetConfig): string {
// Mandatory fields:
// * html_heading
@ -176,28 +194,6 @@ export function launch(conf: DialogWidgetConfig): string {
const $submit_button = $dialog.find(".dialog_submit_button");
function get_current_values($inputs: JQuery): Record<string, unknown> {
const current_values: Record<string, unknown> = {};
$inputs.each(function () {
const property_name = $(this).attr("name")!;
if (property_name) {
if (
this instanceof HTMLInputElement &&
this.type === "file" &&
this.files?.length
) {
// If the input is a file input and a file has been selected, set value to file object
current_values[property_name] = this.files[0];
} else if (property_name === "edit_bot_owner") {
current_values[property_name] = $(this).find(".dropdown_widget_value").text();
} else {
current_values[property_name] = $(this).val();
}
}
});
return current_values;
}
if (conf.update_submit_disabled_state_on_change) {
const $inputs = $dialog.find(".modal__content").find("input,select,textarea,button");