2017-12-14 05:51:45 +01:00
|
|
|
var settings_profile_fields = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2018-04-08 18:13:37 +02:00
|
|
|
var order = [];
|
|
|
|
|
2018-05-01 19:33:33 +02:00
|
|
|
exports.field_type_id_to_string = function (type_id) {
|
2017-12-14 05:51:45 +01:00
|
|
|
var name = _.find(page_params.custom_profile_field_types, function (type) {
|
|
|
|
return type[0] === type_id;
|
|
|
|
})[1];
|
|
|
|
return name;
|
2018-05-01 19:33:33 +02:00
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
function delete_profile_field(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-04-10 20:47:15 +02:00
|
|
|
|
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.del,
|
|
|
|
"/json/realm/profile_fields/" + encodeURIComponent($(this).attr('data-profile-field-id')),
|
|
|
|
{}, $('#admin-profile-field-status').expectOne());
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
function read_field_data_from_form(selector) {
|
|
|
|
var field_data = {};
|
2018-05-10 14:43:51 +02:00
|
|
|
var i = 0;
|
2018-04-12 11:17:52 +02:00
|
|
|
selector.each(function (ind, row) {
|
2018-05-10 14:43:51 +02:00
|
|
|
var value = i;
|
|
|
|
var text = row.children[0].value;
|
|
|
|
var order = row.children[1].value;
|
2018-04-12 11:17:52 +02:00
|
|
|
field_data[value] = {text: text, order: order};
|
2018-05-10 14:43:51 +02:00
|
|
|
i += 1;
|
2018-04-12 11:17:52 +02:00
|
|
|
});
|
|
|
|
return field_data;
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:36:09 +02:00
|
|
|
function create_choice_row(container, add_delete_button) {
|
|
|
|
var context = {};
|
|
|
|
context.add_delete_button = add_delete_button;
|
|
|
|
var row = templates.render("profile-field-choice", context);
|
|
|
|
$(container).append(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear_form_data() {
|
|
|
|
$("#profile_field_name").val("");
|
|
|
|
$("#profile_field_hint").val("");
|
|
|
|
// Set default in field type dropdown
|
|
|
|
$("#profile_field_type").val("1");
|
|
|
|
// Clear data from choice field form
|
|
|
|
$("#profile_field_choices").html("");
|
|
|
|
create_choice_row($("#profile_field_choices"), false);
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function create_profile_field(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
var selector = $('.admin-profile-field-form div.choice-row');
|
|
|
|
var field_data = {};
|
|
|
|
if ($('#profile_field_type').val() === '3') {
|
|
|
|
// Only read choice data if we are creating a choice field.
|
|
|
|
field_data = read_field_data_from_form(selector);
|
|
|
|
}
|
|
|
|
$('#profile_field_data').val(JSON.stringify(field_data));
|
2018-05-10 13:36:09 +02:00
|
|
|
var opts = {
|
|
|
|
success_continuation: clear_form_data,
|
|
|
|
};
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-04-10 20:47:15 +02:00
|
|
|
settings_ui.do_settings_change(channel.post, "/json/realm/profile_fields", $(this).serialize(),
|
2018-05-10 13:36:09 +02:00
|
|
|
$('#admin-profile-field-status').expectOne(), opts);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_choice_row(e) {
|
|
|
|
var choices_div = e.delegateTarget;
|
|
|
|
create_choice_row(choices_div, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete_choice_row(e) {
|
2018-06-03 15:45:13 +02:00
|
|
|
var row = $(e.currentTarget).parent();
|
2018-04-12 11:17:52 +02:00
|
|
|
row.remove();
|
|
|
|
}
|
|
|
|
|
2018-04-08 18:13:37 +02:00
|
|
|
function move_field(e, btn, direction) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
var button_id = parseInt(btn.attr('data-profile-field-id'), 10);
|
|
|
|
var button_index = order.indexOf(button_id);
|
|
|
|
order[button_index] = order[button_index + direction];
|
|
|
|
order[button_index + direction] = button_id;
|
|
|
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields",
|
|
|
|
{order: JSON.stringify(order)},
|
|
|
|
$('#admin-profile-field-status').expectOne());
|
|
|
|
}
|
|
|
|
|
|
|
|
function move_field_up(e) {
|
|
|
|
move_field(e, $(this), -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function move_field_down(e) {
|
|
|
|
move_field(e, $(this), 1);
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function get_profile_field_info(id) {
|
|
|
|
var info = {};
|
|
|
|
info.row = $("tr.profile-field-row[data-profile-field-id='" + id + "']");
|
|
|
|
info.form = $("tr.profile-field-form[data-profile-field-id='" + id + "']");
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:36:09 +02:00
|
|
|
function get_profile_field(id) {
|
|
|
|
var all_custom_fields = page_params.custom_profile_fields;
|
|
|
|
var field;
|
|
|
|
for (var i = 0; i < all_custom_fields.length; i += 1) {
|
|
|
|
if (all_custom_fields[i].id === id) {
|
|
|
|
field = all_custom_fields[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function open_edit_form(e) {
|
|
|
|
var field_id = $(e.currentTarget).attr("data-profile-field-id");
|
|
|
|
var profile_field = get_profile_field_info(field_id);
|
|
|
|
|
|
|
|
profile_field.row.hide();
|
|
|
|
profile_field.form.show();
|
2018-05-10 13:36:09 +02:00
|
|
|
var field = get_profile_field(parseInt(field_id,10));
|
|
|
|
// Set initial value in edit form
|
|
|
|
profile_field.form.find('input[name=name]').val(field.name);
|
|
|
|
profile_field.form.find('input[name=hint]').val(field.hint);
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
profile_field.form.find('.reset').on("click", function () {
|
|
|
|
profile_field.form.hide();
|
|
|
|
profile_field.row.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
profile_field.form.find('.submit').on("click", function () {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2018-04-10 20:47:15 +02:00
|
|
|
var profile_field_status = $('#admin-profile-field-status').expectOne();
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
// For some reason jQuery's serialize() is not working with
|
|
|
|
// channel.patch even though it is supported by $.ajax.
|
|
|
|
var data = {};
|
|
|
|
data.name = profile_field.form.find('input[name=name]').val();
|
2018-03-31 09:23:57 +02:00
|
|
|
data.hint = profile_field.form.find('input[name=hint]').val();
|
2018-04-12 11:17:52 +02:00
|
|
|
var selector = profile_field.form.find('div.choice-row');
|
|
|
|
data.field_data = JSON.stringify(read_field_data_from_form(selector));
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-04-10 20:47:15 +02:00
|
|
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields/" + field_id,
|
|
|
|
data, profile_field_status);
|
2017-12-14 05:51:45 +01:00
|
|
|
});
|
2018-04-12 11:17:52 +02:00
|
|
|
|
|
|
|
profile_field.form.find(".profile-field-choices").on("click", "button.add-choice", add_choice_row);
|
|
|
|
profile_field.form.find(".profile-field-choices").on("click", "button.delete-choice", delete_choice_row);
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.populate_profile_fields = function (profile_fields_data) {
|
2018-04-04 16:21:05 +02:00
|
|
|
$("#account-settings .custom-profile-fields-form").html("");
|
|
|
|
settings_account.add_custom_profile_fields_to_settings();
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var profile_fields_table = $("#admin_profile_fields_table").expectOne();
|
|
|
|
profile_fields_table.find("tr.profile-field-row").remove(); // Clear all rows.
|
|
|
|
profile_fields_table.find("tr.profile-field-form").remove(); // Clear all rows.
|
2018-04-08 18:13:37 +02:00
|
|
|
order = [];
|
|
|
|
_.each(profile_fields_data, function (profile_field, index) {
|
|
|
|
order.push(profile_field.id);
|
2018-04-12 11:17:52 +02:00
|
|
|
var field_data = {};
|
|
|
|
if (profile_field.field_data !== "") {
|
|
|
|
field_data = JSON.parse(profile_field.field_data);
|
|
|
|
}
|
|
|
|
var choices = [];
|
|
|
|
_.each(field_data, function (choice, value) {
|
|
|
|
choices.push({
|
|
|
|
value: value,
|
|
|
|
text: choice.text,
|
|
|
|
order: choice.order,
|
|
|
|
add_delete_button: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (choices.length > 0) {
|
|
|
|
// Remove delete button from the first choice. This makes sure that
|
|
|
|
// the user cannot delete all choices of a choice field. To delete
|
|
|
|
// all choices, just delete the field.
|
|
|
|
choices[0].add_delete_button = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var is_choice_field = false;
|
|
|
|
if (profile_field.type === 3) {
|
|
|
|
is_choice_field = true;
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
profile_fields_table.append(
|
|
|
|
templates.render(
|
|
|
|
"admin_profile_field_list", {
|
|
|
|
profile_field: {
|
|
|
|
id: profile_field.id,
|
|
|
|
name: profile_field.name,
|
2018-03-31 09:23:57 +02:00
|
|
|
hint: profile_field.hint,
|
2018-05-01 19:33:33 +02:00
|
|
|
type: exports.field_type_id_to_string(profile_field.type),
|
2018-04-12 11:17:52 +02:00
|
|
|
choices: choices,
|
|
|
|
is_choice_field: is_choice_field,
|
2017-12-14 05:51:45 +01:00
|
|
|
},
|
|
|
|
can_modify: page_params.is_admin,
|
2018-04-08 18:13:37 +02:00
|
|
|
first: index === 0,
|
|
|
|
last: index === _.size(profile_fields_data) - 1,
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
loading.destroy_indicator($('#admin_page_profile_fields_loading_indicator'));
|
|
|
|
};
|
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
function set_up_choices_field() {
|
|
|
|
create_choice_row('#profile_field_choices', false);
|
|
|
|
|
|
|
|
if ($('#profile_field_type').val() !== '3') {
|
|
|
|
// If 'Choice' type is already selected, show choice row.
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#profile_field_type').on('change', function (e) {
|
|
|
|
if ($(e.target).val() === '3') {
|
|
|
|
$("#profile_field_choices_row").show();
|
|
|
|
} else {
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#profile_field_choices").on("click", "button.add-choice", add_choice_row);
|
|
|
|
$("#profile_field_choices").on("click", "button.delete-choice", delete_choice_row);
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
exports.set_up = function () {
|
|
|
|
meta.loaded = true;
|
|
|
|
|
|
|
|
// create loading indicators
|
|
|
|
loading.make_indicator($('#admin_page_profile_fields_loading_indicator'));
|
|
|
|
// Populate profile_fields table
|
|
|
|
exports.populate_profile_fields(page_params.custom_profile_fields);
|
|
|
|
|
|
|
|
$('#admin_profile_fields_table').on('click', '.delete', delete_profile_field);
|
|
|
|
$(".organization").on("submit", "form.admin-profile-field-form", create_profile_field);
|
|
|
|
$("#admin_profile_fields_table").on("click", ".open-edit-form", open_edit_form);
|
2018-04-08 18:13:37 +02:00
|
|
|
$("#admin_profile_fields_table").on("click", ".move-field-up", move_field_up);
|
|
|
|
$("#admin_profile_fields_table").on("click", ".move-field-down", move_field_down);
|
2018-04-12 11:17:52 +02:00
|
|
|
set_up_choices_field();
|
2017-12-14 05:51:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = settings_profile_fields;
|
|
|
|
}
|