org settings: Refactor the discard changes function.

This extract the logic of resetting the value of a single property element
at a time so that we can reuse this for real-time-syncing.
This commit is contained in:
Shubham Dhama 2018-03-25 18:41:25 +05:30 committed by Tim Abbott
parent a779fc6095
commit 84a5786b2e
1 changed files with 20 additions and 22 deletions

View File

@ -492,34 +492,32 @@ function _set_up() {
} }
}); });
function discard_subsection_changes(target) { function discard_property_element_changes(elem) {
_.each(get_subsection_property_elements(target), function (elem) { elem = $(elem);
elem = $(elem); var property_name = exports.extract_property_name(elem);
var property_name = exports.extract_property_name(elem); // Check whether the id refers to a property whose name we can't
// Check whether the id refers to a property whose name we can't // extract from element's id.
// extract from element's id. var property_value = property_value_element_refers(property_name);
var property_value = property_value_element_refers(property_name); if (property_value === undefined) {
if (property_value === undefined) { property_value = page_params[property_name];
property_value = page_params[property_name]; }
}
if (typeof property_value === 'boolean') { if (typeof property_value === 'boolean') {
elem.prop('checked', property_value); elem.prop('checked', property_value);
} else if (typeof property_value === 'string' || typeof property_value === 'number') { } else if (typeof property_value === 'string' || typeof property_value === 'number') {
elem.val(property_value); elem.val(property_value);
} else { } else {
blueslip.error('Element refers to unknown property ' + property_name); blueslip.error('Element refers to unknown property ' + property_name);
} }
// Triggering a change event to handle fading and showing of // Triggering a change event to handle fading and showing of
// dependent sub-settings correctly // dependent sub-settings correctly
elem.change(); elem.change();
});
} }
$('.organization').on('click', '.subsection-header .subsection-changes-discard button', function (e) { $('.organization').on('click', '.subsection-header .subsection-changes-discard button', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
discard_subsection_changes(e.target); _.each(get_subsection_property_elements(e.target), discard_property_element_changes);
var subsection = $(e.target).closest('.org-subsection-parent'); var subsection = $(e.target).closest('.org-subsection-parent');
var change_process_buttons = subsection.find('.subsection-header .button'); var change_process_buttons = subsection.find('.subsection-header .button');
change_process_buttons.removeClass('show').addClass('hide'); change_process_buttons.removeClass('show').addClass('hide');