2017-12-14 05:51:45 +01:00
|
|
|
var settings_profile_fields = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2018-12-08 17:37:57 +01:00
|
|
|
exports.maybe_disable_widgets = function () {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".organization-box [data-name='profile-field-settings']")
|
|
|
|
.find("input, button, select").attr("disabled", true);
|
|
|
|
};
|
|
|
|
|
2018-04-08 18:13:37 +02:00
|
|
|
var order = [];
|
2018-08-15 13:17:15 +02:00
|
|
|
var field_types = page_params.custom_profile_field_types;
|
2018-04-08 18:13:37 +02:00
|
|
|
|
2018-05-01 19:33:33 +02:00
|
|
|
exports.field_type_id_to_string = function (type_id) {
|
2018-08-15 11:35:18 +02:00
|
|
|
var field_type_str;
|
|
|
|
|
|
|
|
_.every(field_types, function (field_type) {
|
|
|
|
if (field_type.id === type_id) {
|
2018-08-15 13:28:51 +02:00
|
|
|
// Few necessary modifications in field-type-name for
|
|
|
|
// table-list view of custom fields UI in org settings
|
|
|
|
if (field_type.name === "Date picker") {
|
|
|
|
field_type_str = "Date";
|
|
|
|
} else if (field_type.name === "Person picker") {
|
|
|
|
field_type_str = "Person";
|
|
|
|
} else {
|
|
|
|
field_type_str = field_type.name;
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:35:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return field_type_str;
|
2018-05-01 19:33:33 +02:00
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-08-15 13:37:40 +02:00
|
|
|
function update_profile_fields_table_element() {
|
|
|
|
var profile_fields_table = $("#admin_profile_fields_table").expectOne();
|
|
|
|
|
|
|
|
// If there are no custom fields, hide the table headers at the top
|
|
|
|
if (page_params.custom_profile_fields.length < 1) {
|
|
|
|
profile_fields_table.hide();
|
|
|
|
} else {
|
|
|
|
profile_fields_table.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2018-08-15 13:37:40 +02:00
|
|
|
update_profile_fields_table_element();
|
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-08-02 19:31:17 +02:00
|
|
|
var field_order = 1;
|
|
|
|
selector.each(function () {
|
|
|
|
var text = $(this).find("input")[0].value;
|
2018-08-12 14:57:26 +02:00
|
|
|
if (text) {
|
|
|
|
field_data[field_order - 1] = {text: text, order: field_order.toString()};
|
|
|
|
field_order += 1;
|
|
|
|
}
|
2018-04-12 11:17:52 +02:00
|
|
|
});
|
2018-08-02 19:31:17 +02:00
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
return field_data;
|
|
|
|
}
|
|
|
|
|
2018-08-11 19:47:58 +02:00
|
|
|
function update_choice_delete_btn(container, display_flag) {
|
|
|
|
var no_of_choice_row = container.find(".choice-row").length;
|
|
|
|
|
|
|
|
// Disable delete button if there only one choice row
|
|
|
|
// Enable choice delete button more one than once choice
|
|
|
|
if (no_of_choice_row === 1) {
|
|
|
|
if (display_flag === true) {
|
|
|
|
container.find(".choice-row .delete-choice").show();
|
|
|
|
} else {
|
|
|
|
container.find(".choice-row .delete-choice").hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 17:02:46 +02:00
|
|
|
function create_choice_row(container) {
|
2018-05-10 13:36:09 +02:00
|
|
|
var context = {};
|
|
|
|
var row = templates.render("profile-field-choice", context);
|
|
|
|
$(container).append(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear_form_data() {
|
|
|
|
$("#profile_field_name").val("");
|
|
|
|
$("#profile_field_hint").val("");
|
2018-08-15 13:17:15 +02:00
|
|
|
// Set default type "Short Text" in field type dropdown
|
|
|
|
$("#profile_field_type").val(field_types.SHORT_TEXT.id);
|
2018-05-10 13:36:09 +02:00
|
|
|
// Clear data from choice field form
|
|
|
|
$("#profile_field_choices").html("");
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row($("#profile_field_choices"));
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn($("#profile_field_choices"), false);
|
2018-05-10 13:36:09 +02:00
|
|
|
$("#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 = {};
|
2018-08-15 13:17:15 +02:00
|
|
|
var field_type = $('#profile_field_type').val();
|
2018-08-02 18:35:33 +02:00
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt(field_type, 10) === field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
// Only read choice data if we are creating a choice field.
|
|
|
|
field_data = read_field_data_from_form(selector);
|
|
|
|
}
|
2018-08-02 18:35:33 +02:00
|
|
|
|
2018-05-10 13:36:09 +02:00
|
|
|
var opts = {
|
|
|
|
success_continuation: clear_form_data,
|
|
|
|
};
|
2018-08-02 18:35:33 +02:00
|
|
|
var form_data = {
|
|
|
|
name: $("#profile_field_name").val(),
|
2018-08-15 13:17:15 +02:00
|
|
|
field_type: field_type,
|
2018-08-02 18:35:33 +02:00
|
|
|
hint: $("#profile_field_hint").val(),
|
|
|
|
field_data: JSON.stringify(field_data),
|
|
|
|
};
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-08-02 18:35:33 +02:00
|
|
|
settings_ui.do_settings_change(channel.post, "/json/realm/profile_fields", form_data,
|
2018-05-10 13:36:09 +02:00
|
|
|
$('#admin-profile-field-status').expectOne(), opts);
|
2018-08-15 13:37:40 +02:00
|
|
|
update_profile_fields_table_element();
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_choice_row(e) {
|
2018-08-12 14:50:16 +02:00
|
|
|
if ($(e.target).parent().next().hasClass("choice-row")) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-12 11:17:52 +02:00
|
|
|
var choices_div = e.delegateTarget;
|
2018-08-12 14:50:16 +02:00
|
|
|
update_choice_delete_btn($(choices_div), true);
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row(choices_div);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function delete_choice_row(e) {
|
2018-06-03 15:45:13 +02:00
|
|
|
var row = $(e.currentTarget).parent();
|
2018-08-11 19:47:58 +02:00
|
|
|
var container = row.parent();
|
2018-04-12 11:17:52 +02:00
|
|
|
row.remove();
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn(container, false);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-06 20:35:50 +02:00
|
|
|
exports.parse_field_choices_from_field_data = function (field_data) {
|
|
|
|
var choices = [];
|
|
|
|
_.each(field_data, function (choice, value) {
|
|
|
|
choices.push({
|
|
|
|
value: value,
|
|
|
|
text: choice.text,
|
|
|
|
order: choice.order,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return choices;
|
|
|
|
};
|
|
|
|
|
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-12-07 21:21:39 +01:00
|
|
|
var field = get_profile_field(parseInt(field_id, 10));
|
2018-05-10 13:36:09 +02:00
|
|
|
// 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
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt(field.type, 10) === field_types.CHOICE.id) {
|
2018-08-06 20:35:50 +02:00
|
|
|
// Re-render field choices in edit form to load initial choice data
|
|
|
|
var choice_list = profile_field.form.find('.edit_profile_field_choices_container');
|
|
|
|
choice_list.off();
|
|
|
|
choice_list.html("");
|
|
|
|
|
|
|
|
var field_data = {};
|
|
|
|
if (field.field_data !== "") {
|
|
|
|
field_data = JSON.parse(field.field_data);
|
|
|
|
}
|
|
|
|
var choices_data = exports.parse_field_choices_from_field_data(field_data);
|
|
|
|
|
|
|
|
_.each(choices_data, function (choice) {
|
|
|
|
choice_list.append(
|
|
|
|
templates.render("profile-field-choice", {
|
|
|
|
text: choice.text,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:50:16 +02:00
|
|
|
// Add blank choice at last
|
|
|
|
create_choice_row(choice_list);
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn(choice_list, false);
|
2018-08-06 20:35:50 +02:00
|
|
|
Sortable.create(choice_list[0], {
|
2018-08-11 17:02:46 +02:00
|
|
|
onUpdate: function () {},
|
2018-08-02 19:31:17 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-08-12 14:50:16 +02:00
|
|
|
profile_field.form.find(".edit_profile_field_choices_container").on("input", ".choice-row input", add_choice_row);
|
2018-08-02 19:31:17 +02:00
|
|
|
profile_field.form.find(".edit_profile_field_choices_container").on("click", "button.delete-choice", delete_choice_row);
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2018-06-09 09:37:59 +02:00
|
|
|
function update_field_order() {
|
|
|
|
order = [];
|
|
|
|
$('.profile-field-row').each(function () {
|
|
|
|
order.push(parseInt($(this).attr('data-profile-field-id'), 10));
|
|
|
|
});
|
|
|
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields",
|
|
|
|
{order: JSON.stringify(order)},
|
|
|
|
$('#admin-profile-field-status').expectOne());
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
exports.populate_profile_fields = function (profile_fields_data) {
|
|
|
|
if (!meta.loaded) {
|
2018-06-11 21:02:00 +02:00
|
|
|
// If outside callers call us when we're not loaded, just
|
|
|
|
// exit and we'll draw the widgets again during set_up().
|
2017-12-14 05:51:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields(profile_fields_data);
|
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields = function (profile_fields_data) {
|
|
|
|
// We should only call this internally or from tests.
|
2017-12-14 05:51:45 +01:00
|
|
|
var profile_fields_table = $("#admin_profile_fields_table").expectOne();
|
2018-06-09 09:37:59 +02:00
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
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 = [];
|
2018-06-09 09:37:59 +02:00
|
|
|
_.each(profile_fields_data, function (profile_field) {
|
2018-04-08 18:13:37 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-08-06 20:35:50 +02:00
|
|
|
var choices = exports.parse_field_choices_from_field_data(field_data);
|
2018-04-12 11:17:52 +02:00
|
|
|
var is_choice_field = false;
|
2018-08-06 20:35:50 +02:00
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (profile_field.type === field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
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-06-09 09:37:59 +02:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
var field_list = $("#admin_profile_fields_table")[0];
|
|
|
|
Sortable.create(field_list, {
|
|
|
|
onUpdate: update_field_order,
|
|
|
|
});
|
|
|
|
}
|
2018-08-15 13:37:40 +02:00
|
|
|
|
|
|
|
update_profile_fields_table_element();
|
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() {
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row('#profile_field_choices');
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn($("#profile_field_choices"), false);
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-08-24 06:40:40 +02:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
var choice_list = $("#profile_field_choices")[0];
|
|
|
|
Sortable.create(choice_list, {
|
|
|
|
onUpdate: function () {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
var field_type = $('#profile_field_type').val();
|
2018-08-02 19:31:17 +02:00
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt(field_type, 10) !== field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
// If 'Choice' type is already selected, show choice row.
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#profile_field_type').on('change', function (e) {
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt($(e.target).val(), 10) === field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
$("#profile_field_choices_row").show();
|
|
|
|
} else {
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:50:16 +02:00
|
|
|
$("#profile_field_choices").on("input", ".choice-row input", add_choice_row);
|
2018-04-12 11:17:52 +02:00
|
|
|
$("#profile_field_choices").on("click", "button.delete-choice", delete_choice_row);
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
exports.set_up = function () {
|
2018-12-08 18:16:37 +01:00
|
|
|
exports.build_page();
|
|
|
|
exports.maybe_disable_widgets();
|
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-12-08 18:16:37 +01:00
|
|
|
exports.build_page = function () {
|
2017-12-14 05:51:45 +01:00
|
|
|
// create loading indicators
|
|
|
|
loading.make_indicator($('#admin_page_profile_fields_loading_indicator'));
|
|
|
|
// Populate profile_fields table
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields(page_params.custom_profile_fields);
|
|
|
|
meta.loaded = true;
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
$('#admin_profile_fields_table').on('click', '.delete', delete_profile_field);
|
2018-08-02 18:35:33 +02:00
|
|
|
$("#profile-field-settings").on("click", "#add-custom-profile-field-btn", create_profile_field);
|
2017-12-14 05:51:45 +01:00
|
|
|
$("#admin_profile_fields_table").on("click", ".open-edit-form", open_edit_form);
|
2018-04-12 11:17:52 +02:00
|
|
|
set_up_choices_field();
|
2018-08-15 13:38:18 +02:00
|
|
|
clear_form_data();
|
2017-12-14 05:51:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = settings_profile_fields;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.settings_profile_fields = settings_profile_fields;
|