mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 19:31:58 +00:00
settings_profile_fields: Refactor function read_field_data_from_form.
This commit is contained in:
committed by
Tim Abbott
parent
f813fdcd5e
commit
3368589df2
@@ -62,10 +62,10 @@ function delete_profile_field(e) {
|
|||||||
update_profile_fields_table_element();
|
update_profile_fields_table_element();
|
||||||
}
|
}
|
||||||
|
|
||||||
function read_field_data_from_form(selector) {
|
function read_choice_field_data_from_form(field_elem) {
|
||||||
var field_data = {};
|
var field_data = {};
|
||||||
var field_order = 1;
|
var field_order = 1;
|
||||||
selector.each(function () {
|
$(field_elem).find('div.choice-row').each(function () {
|
||||||
var text = $(this).find("input")[0].value;
|
var text = $(this).find("input")[0].value;
|
||||||
if (text) {
|
if (text) {
|
||||||
field_data[field_order - 1] = {text: text, order: field_order.toString()};
|
field_data[field_order - 1] = {text: text, order: field_order.toString()};
|
||||||
@@ -108,22 +108,24 @@ function clear_form_data() {
|
|||||||
$("#profile_field_choices_row").hide();
|
$("#profile_field_choices_row").hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function read_field_data_from_form(field_type_id, field_elem) {
|
||||||
|
// Only read field data if we are creating a choice field
|
||||||
|
if (field_type_id === field_types.CHOICE.id) {
|
||||||
|
return read_choice_field_data_from_form(field_elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function create_profile_field(e) {
|
function create_profile_field(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
var selector = $('.admin-profile-field-form div.choice-row');
|
|
||||||
var field_data = {};
|
var field_data = {};
|
||||||
var field_type = $('#profile_field_type').val();
|
var field_type = $('#profile_field_type').val();
|
||||||
|
|
||||||
if (parseInt(field_type, 10) === field_types.CHOICE.id) {
|
|
||||||
// Only read choice data if we are creating a choice field.
|
|
||||||
field_data = read_field_data_from_form(selector);
|
|
||||||
}
|
|
||||||
|
|
||||||
var opts = {
|
var opts = {
|
||||||
success_continuation: clear_form_data,
|
success_continuation: clear_form_data,
|
||||||
};
|
};
|
||||||
|
field_data = read_field_data_from_form(parseInt(field_type, 10), $('.new-profile-field-form'));
|
||||||
var form_data = {
|
var form_data = {
|
||||||
name: $("#profile_field_name").val(),
|
name: $("#profile_field_name").val(),
|
||||||
field_type: field_type,
|
field_type: field_type,
|
||||||
@@ -243,8 +245,8 @@ function open_edit_form(e) {
|
|||||||
var data = {};
|
var data = {};
|
||||||
data.name = profile_field.form.find('input[name=name]').val();
|
data.name = profile_field.form.find('input[name=name]').val();
|
||||||
data.hint = profile_field.form.find('input[name=hint]').val();
|
data.hint = profile_field.form.find('input[name=hint]').val();
|
||||||
var selector = profile_field.form.find('div.choice-row');
|
data.field_data = JSON.stringify(read_field_data_from_form(parseInt(field.type, 10),
|
||||||
data.field_data = JSON.stringify(read_field_data_from_form(selector));
|
profile_field.form));
|
||||||
|
|
||||||
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields/" + field_id,
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields/" + field_id,
|
||||||
data, profile_field_status);
|
data, profile_field_status);
|
||||||
@@ -287,14 +289,12 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
|
|||||||
_.each(profile_fields_data, function (profile_field) {
|
_.each(profile_fields_data, function (profile_field) {
|
||||||
order.push(profile_field.id);
|
order.push(profile_field.id);
|
||||||
var field_data = {};
|
var field_data = {};
|
||||||
if (profile_field.field_data !== "") {
|
if (profile_field.field_data) {
|
||||||
field_data = JSON.parse(profile_field.field_data);
|
field_data = JSON.parse(profile_field.field_data);
|
||||||
}
|
}
|
||||||
var choices = exports.parse_field_choices_from_field_data(field_data);
|
var choices = [];
|
||||||
var is_choice_field = false;
|
|
||||||
|
|
||||||
if (profile_field.type === field_types.CHOICE.id) {
|
if (profile_field.type === field_types.CHOICE.id) {
|
||||||
is_choice_field = true;
|
choices = exports.parse_field_choices_from_field_data(field_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
profile_fields_table.append(
|
profile_fields_table.append(
|
||||||
@@ -306,7 +306,7 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
|
|||||||
hint: profile_field.hint,
|
hint: profile_field.hint,
|
||||||
type: exports.field_type_id_to_string(profile_field.type),
|
type: exports.field_type_id_to_string(profile_field.type),
|
||||||
choices: choices,
|
choices: choices,
|
||||||
is_choice_field: is_choice_field,
|
is_choice_field: profile_field.type === field_types.CHOICE.id,
|
||||||
},
|
},
|
||||||
can_modify: page_params.is_admin,
|
can_modify: page_params.is_admin,
|
||||||
}
|
}
|
||||||
@@ -343,7 +343,8 @@ function set_up_choices_field() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$('#profile_field_type').on('change', function (e) {
|
$('#profile_field_type').on('change', function (e) {
|
||||||
if (parseInt($(e.target).val(), 10) === field_types.CHOICE.id) {
|
var selected_field_id = parseInt($(e.target).val(), 10);
|
||||||
|
if (selected_field_id === field_types.CHOICE.id) {
|
||||||
$("#profile_field_choices_row").show();
|
$("#profile_field_choices_row").show();
|
||||||
} else {
|
} else {
|
||||||
$("#profile_field_choices_row").hide();
|
$("#profile_field_choices_row").hide();
|
||||||
|
|||||||
Reference in New Issue
Block a user