settings_org: Update populate_data_for_request to have module-level scope.

This commit changes populate_data_for_request to have module-level scope
and not be defined in register_save_discard_widget_handlers, so that we
can use this function for stream settings too in further commits.
This commit is contained in:
Sahil Batra 2022-11-09 22:24:47 +05:30 committed by Tim Abbott
parent c96dd02084
commit 59b7a68e71
1 changed files with 32 additions and 37 deletions

View File

@ -924,6 +924,37 @@ function enable_or_disable_save_button($subsection_elem) {
$subsection_elem.find(".subsection-changes-save button").prop("disabled", disable_save_btn);
}
function populate_data_for_request(subsection, for_realm_default_settings) {
const data = {};
const properties_elements = get_subsection_property_elements(subsection);
for (const input_elem of properties_elements) {
const $input_elem = $(input_elem);
if (check_property_changed($input_elem, for_realm_default_settings)) {
const input_value = get_input_element_value($input_elem);
if (input_value !== undefined) {
let property_name;
if (for_realm_default_settings) {
property_name = extract_property_name($input_elem, for_realm_default_settings);
} else if ($input_elem.attr("id").startsWith("id_authmethod")) {
// Authentication Method component IDs include authentication method name
// for uniqueness, anchored to "id_authmethod" prefix, e.g. "id_authmethodapple_<property_name>".
// We need to strip that whole construct down to extract the actual property name.
// The [\da-z]+ part of the regexp covers the auth method name itself.
// We assume it's not an empty string and can contain only digits and lowercase ASCII letters,
// this is ensured by a respective allowlist-based filter in populate_auth_methods().
[, property_name] = /^id_authmethod[\da-z]+_(.*)$/.exec($input_elem.attr("id"));
} else {
[, property_name] = /^id_realm_(.*)$/.exec($input_elem.attr("id"));
}
data[property_name] = input_value;
}
}
}
return data;
}
export function register_save_discard_widget_handlers(
$container,
patch_url,
@ -1035,42 +1066,6 @@ export function register_save_discard_widget_handlers(
return data;
}
function populate_data_for_request(subsection) {
const data = {};
const properties_elements = get_subsection_property_elements(subsection);
for (const input_elem of properties_elements) {
const $input_elem = $(input_elem);
if (check_property_changed($input_elem, for_realm_default_settings)) {
const input_value = get_input_element_value($input_elem);
if (input_value !== undefined) {
let property_name;
if (for_realm_default_settings) {
property_name = extract_property_name(
$input_elem,
for_realm_default_settings,
);
} else if ($input_elem.attr("id").startsWith("id_authmethod")) {
// Authentication Method component IDs include authentication method name
// for uniqueness, anchored to "id_authmethod" prefix, e.g. "id_authmethodapple_<property_name>".
// We need to strip that whole construct down to extract the actual property name.
// The [\da-z]+ part of the regexp covers the auth method name itself.
// We assume it's not an empty string and can contain only digits and lowercase ASCII letters,
// this is ensured by a respective allowlist-based filter in populate_auth_methods().
[, property_name] = /^id_authmethod[\da-z]+_(.*)$/.exec(
$input_elem.attr("id"),
);
} else {
[, property_name] = /^id_realm_(.*)$/.exec($input_elem.attr("id"));
}
data[property_name] = input_value;
}
}
}
return data;
}
$container.on("click", ".subsection-header .subsection-changes-save button", (e) => {
e.preventDefault();
e.stopPropagation();
@ -1088,7 +1083,7 @@ export function register_save_discard_widget_handlers(
}
const data = {
...populate_data_for_request($subsection_elem),
...populate_data_for_request($subsection_elem, for_realm_default_settings),
...extra_data,
};
save_organization_settings(data, $save_button, patch_url);