2019-11-02 00:06:25 +01:00
|
|
|
const render_admin_user_group_list = require('../templates/admin_user_group_list.hbs');
|
|
|
|
const render_confirm_delete_user = require('../templates/confirm_delete_user.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-11-13 05:20:42 +01:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2017-11-17 06:15:19 +01:00
|
|
|
exports.reload = function () {
|
2018-03-04 17:31:22 +01:00
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_groups_section = $('#user-groups').expectOne();
|
2017-11-17 06:15:19 +01:00
|
|
|
user_groups_section.html('');
|
|
|
|
exports.populate_user_groups();
|
|
|
|
};
|
|
|
|
|
2019-11-02 17:58:55 +01:00
|
|
|
const USER_GROUP_EDIT_POLICY_MEMBERS = 1;
|
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
exports.can_edit = function (group_id) {
|
2018-06-14 09:44:07 +02:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
if (page_params.is_guest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-02 17:58:55 +01:00
|
|
|
if (page_params.realm_user_group_edit_policy !== USER_GROUP_EDIT_POLICY_MEMBERS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-14 09:44:07 +02:00
|
|
|
return user_groups.is_member_of(group_id, people.my_current_user_id());
|
|
|
|
};
|
2018-06-14 08:35:05 +02:00
|
|
|
|
2017-11-17 04:31:53 +01:00
|
|
|
exports.populate_user_groups = function () {
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_groups_section = $('#user-groups').expectOne();
|
|
|
|
const user_groups_array = user_groups.get_realm_user_groups();
|
2017-11-17 04:31:53 +01:00
|
|
|
_.each(user_groups_array, function (data) {
|
2019-07-09 21:24:00 +02:00
|
|
|
user_groups_section.append(render_admin_user_group_list({
|
2017-11-17 04:31:53 +01:00
|
|
|
user_group: {
|
|
|
|
name: data.name,
|
|
|
|
id: data.id,
|
|
|
|
description: data.description,
|
|
|
|
},
|
|
|
|
}));
|
2019-11-02 00:06:25 +01:00
|
|
|
const pill_container = $('.pill-container[data-group-pills="' + data.id + '"]');
|
|
|
|
const pills = user_pill.create_pills(pill_container);
|
2018-03-06 15:03:20 +01:00
|
|
|
|
|
|
|
function get_pill_user_ids() {
|
|
|
|
return user_pill.get_user_ids(pills);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const userg = $('div.user-group[id="' + data.id + '"]');
|
2020-01-16 20:12:06 +01:00
|
|
|
data.members.forEach(function (user_id) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const user = people.get_by_user_id(user_id);
|
2018-05-09 13:42:10 +02:00
|
|
|
user_pill.append_user(user, pills);
|
2017-11-17 04:31:53 +01:00
|
|
|
});
|
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
function update_membership(group_id) {
|
|
|
|
if (exports.can_edit(group_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-07 21:21:39 +01:00
|
|
|
userg.find('.name').attr('contenteditable', 'false');
|
|
|
|
userg.find('.description').attr('contenteditable', 'false');
|
2018-03-13 10:46:17 +01:00
|
|
|
userg.addClass('ntm');
|
2018-12-07 21:21:39 +01:00
|
|
|
pill_container.find('.input').attr('contenteditable', 'false');
|
2018-03-13 10:46:17 +01:00
|
|
|
pill_container.find('.input').css('display', 'none');
|
2018-08-18 06:23:53 +02:00
|
|
|
pill_container.addClass('not-editable');
|
2018-03-13 10:46:17 +01:00
|
|
|
pill_container.off('keydown', '.pill');
|
|
|
|
pill_container.off('keydown', '.input');
|
|
|
|
pill_container.off('click');
|
|
|
|
pill_container.on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
pill_container.find('.pill').hover(function () {
|
|
|
|
pill_container.find('.pill').find('.exit').css('opacity', '0.5');
|
2018-05-06 21:43:17 +02:00
|
|
|
}, function () {});
|
2018-03-13 10:46:17 +01:00
|
|
|
}
|
|
|
|
update_membership(data.id);
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function is_user_group_changed() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_group = get_pill_user_ids();
|
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
2020-02-04 23:46:56 +01:00
|
|
|
const original_group = Array.from(group_data.members);
|
2019-11-02 00:06:25 +01:00
|
|
|
const same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
|
|
|
const description = $('#user-groups #' + data.id + ' .description').text().trim();
|
|
|
|
const name = $('#user-groups #' + data.id + ' .name').text().trim();
|
|
|
|
const user_group_status = $('#user-groups #' + data.id + ' .user-group-status');
|
2018-08-25 00:32:13 +02:00
|
|
|
|
|
|
|
if (user_group_status.is(':visible')) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2018-06-06 18:50:09 +02:00
|
|
|
if (group_data.description === description && group_data.name === name &&
|
2018-03-15 12:10:07 +01:00
|
|
|
(!draft_group.length || same_groups)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_cancel_button() {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const cancel_button = $('#user-groups #' + data.id + ' .save-status.btn-danger');
|
|
|
|
const saved_button = $('#user-groups #' + data.id + ' .save-status.sea-green');
|
|
|
|
const save_instructions = $('#user-groups #' + data.id + ' .save-instructions');
|
2018-03-15 12:56:31 +01:00
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
if (is_user_group_changed() &&
|
|
|
|
!cancel_button.is(':visible')) {
|
|
|
|
saved_button.fadeOut(0);
|
|
|
|
cancel_button.css({display: 'inline-block', opacity: 0}).fadeTo(400, 1);
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.css({display: 'block', opacity: 0}).fadeTo(400, 1);
|
2018-03-15 12:10:07 +01:00
|
|
|
} else if (!is_user_group_changed() &&
|
|
|
|
cancel_button.is(':visible')) {
|
2018-05-06 21:43:17 +02:00
|
|
|
cancel_button.fadeOut();
|
|
|
|
save_instructions.fadeOut();
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_saved_button() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const cancel_button = $('#user-groups #' + data.id + ' .save-status.btn-danger');
|
|
|
|
const saved_button = $('#user-groups #' + data.id + ' .save-status.sea-green');
|
|
|
|
const save_instructions = $('#user-groups #' + data.id + ' .save-instructions');
|
2018-03-15 12:10:07 +01:00
|
|
|
if (!saved_button.is(':visible')) {
|
|
|
|
cancel_button.fadeOut(0);
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.fadeOut(0);
|
2018-08-22 00:31:48 +02:00
|
|
|
saved_button.css({display: 'inline-block', opacity: 0}).fadeTo(400, 1).delay(2000).fadeTo(400, 0);
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
function save_members() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_group = get_pill_user_ids();
|
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
2020-02-04 23:46:56 +01:00
|
|
|
const original_group = Array.from(group_data.members);
|
2019-11-02 00:06:25 +01:00
|
|
|
const same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
2018-03-13 23:51:53 +01:00
|
|
|
if (!draft_group.length || same_groups) {
|
|
|
|
return;
|
2017-12-18 21:38:02 +01:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const added = _.difference(draft_group, original_group);
|
|
|
|
const removed = _.difference(original_group, draft_group);
|
2018-03-13 23:51:53 +01:00
|
|
|
channel.post({
|
|
|
|
url: "/json/user_groups/" + data.id + '/members',
|
|
|
|
data: {
|
|
|
|
add: JSON.stringify(added),
|
|
|
|
delete: JSON.stringify(removed),
|
|
|
|
},
|
2018-03-15 12:10:07 +01:00
|
|
|
success: function () {
|
2018-08-22 00:31:48 +02:00
|
|
|
setTimeout(show_saved_button, 200);
|
2018-03-15 12:10:07 +01:00
|
|
|
},
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2018-03-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function save_name_desc() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group_status = $('#user-groups #' + data.id + ' .user-group-status');
|
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
|
|
|
const description = $('#user-groups #' + data.id + ' .description').text().trim();
|
|
|
|
const name = $('#user-groups #' + data.id + ' .name').text().trim();
|
2018-03-14 16:45:37 +01:00
|
|
|
|
|
|
|
if (group_data.description === description && group_data.name === name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
url: "/json/user_groups/" + data.id,
|
|
|
|
data: {
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
success: function () {
|
2018-08-25 00:32:13 +02:00
|
|
|
user_group_status.hide();
|
2018-08-22 00:31:48 +02:00
|
|
|
setTimeout(show_saved_button, 200);
|
2018-03-14 16:45:37 +01:00
|
|
|
},
|
2018-08-25 00:32:13 +02:00
|
|
|
error: function (xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const errors = JSON.parse(xhr.responseText).msg;
|
2018-08-25 00:32:13 +02:00
|
|
|
xhr.responseText = JSON.stringify({msg: errors});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, user_group_status);
|
|
|
|
update_cancel_button();
|
|
|
|
$('#user-groups #' + data.id + ' .name').text(group_data.name);
|
|
|
|
$('#user-groups #' + data.id + ' .description').text(group_data.description);
|
|
|
|
},
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_not_blur(except_class, event) {
|
|
|
|
// Event generated from or inside the typeahead.
|
|
|
|
if ($(event.relatedTarget).closest(".typeahead").length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const blur_exceptions = _.without([".pill-container", ".name", ".description", ".input", ".delete"],
|
|
|
|
except_class);
|
2018-03-15 12:10:07 +01:00
|
|
|
if ($(event.relatedTarget).closest('#user-groups #' + data.id).length) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return _.some(blur_exceptions, function (class_name) {
|
|
|
|
return $(event.relatedTarget).closest(class_name).length;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function auto_save(class_name, event) {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
if (do_not_blur(class_name, event)) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
if ($(event.relatedTarget).closest('#user-groups #' + data.id) &&
|
2018-08-22 00:31:48 +02:00
|
|
|
$(event.relatedTarget).closest('.save-status.btn-danger').length) {
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.reload();
|
2018-05-06 21:43:17 +02:00
|
|
|
return;
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
2018-03-14 16:45:37 +01:00
|
|
|
save_name_desc();
|
|
|
|
save_members();
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.input', function (event) {
|
|
|
|
auto_save('.input', event);
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.name', function (event) {
|
2018-03-15 12:10:07 +01:00
|
|
|
auto_save('.name', event);
|
|
|
|
});
|
|
|
|
$('#user-groups #' + data.id).on('input', '.name', function () {
|
|
|
|
update_cancel_button();
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#user-groups #' + data.id).on('blur', '.description', function (event) {
|
2018-03-15 12:10:07 +01:00
|
|
|
auto_save('.description', event);
|
|
|
|
});
|
|
|
|
$('#user-groups #' + data.id).on('input', '.description', function () {
|
|
|
|
update_cancel_button();
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2017-12-18 21:38:02 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const input = pill_container.children('.input');
|
2018-05-09 13:42:10 +02:00
|
|
|
if (exports.can_edit(data.id)) {
|
|
|
|
user_pill.set_up_typeahead_on_pills(input, pills, update_cancel_button);
|
|
|
|
}
|
2017-12-19 07:43:42 +01:00
|
|
|
|
2018-03-13 09:58:00 +01:00
|
|
|
(function pill_remove() {
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 09:58:00 +01:00
|
|
|
pills.onPillRemove(function () {
|
|
|
|
// onPillRemove is fired before the pill is removed from
|
|
|
|
// the DOM.
|
2018-03-15 12:10:07 +01:00
|
|
|
update_cancel_button();
|
2018-03-13 09:58:00 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
input.focus();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}());
|
2017-12-18 21:38:02 +01:00
|
|
|
});
|
2017-11-17 04:31:53 +01:00
|
|
|
};
|
|
|
|
|
2017-11-13 05:20:42 +01:00
|
|
|
exports.set_up = function () {
|
|
|
|
meta.loaded = true;
|
2017-11-17 04:31:53 +01:00
|
|
|
exports.populate_user_groups();
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2018-03-25 15:46:34 +02:00
|
|
|
$(".organization form.admin-user-group-form").off("submit").on("submit", function (e) {
|
2017-11-17 06:15:19 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group_status = $('#admin-user-group-status');
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const group = {
|
2017-11-17 06:15:19 +01:00
|
|
|
members: JSON.stringify([people.my_current_user_id()]),
|
|
|
|
};
|
|
|
|
_.each($(this).serializeArray(), function (obj) {
|
|
|
|
if (obj.value.trim() === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
group[obj.name] = obj.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/user_groups/create",
|
|
|
|
data: group,
|
|
|
|
success: function () {
|
|
|
|
user_group_status.hide();
|
|
|
|
ui_report.success(i18n.t("User group added!"), user_group_status);
|
|
|
|
$("form.admin-user-group-form input[type='text']").val("");
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
user_group_status.hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const errors = JSON.parse(xhr.responseText).msg;
|
2017-11-17 06:15:19 +01:00
|
|
|
xhr.responseText = JSON.stringify({msg: errors});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, user_group_status);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-12-06 20:00:23 +01:00
|
|
|
|
|
|
|
$('#user-groups').on('click', '.delete', function () {
|
2020-01-27 18:48:46 +01:00
|
|
|
const group_id = parseInt($(this).parents('.user-group').attr('id'), 10);
|
2018-03-13 10:46:17 +01:00
|
|
|
if (!exports.can_edit(group_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_groups.get_user_group_from_id(group_id);
|
|
|
|
const btn = $(this);
|
2018-09-12 20:45:41 +02:00
|
|
|
|
|
|
|
function delete_user_group() {
|
|
|
|
channel.del({
|
|
|
|
url: "/json/user_groups/" + group_id,
|
|
|
|
data: {
|
|
|
|
id: group_id,
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
user_groups.remove(user_group);
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.reload();
|
2018-09-12 20:45:41 +02:00
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
btn.text(i18n.t("Failed!"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:43:09 +02:00
|
|
|
// This is mostly important for styling concerns.
|
2019-11-02 00:06:25 +01:00
|
|
|
const modal_parent = $('#settings_content');
|
2018-09-12 17:43:09 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html_body = render_confirm_delete_user({
|
2018-09-12 17:43:09 +02:00
|
|
|
group_name: user_group.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
parent: modal_parent,
|
|
|
|
html_heading: i18n.t('Delete user group'),
|
|
|
|
html_body: html_body,
|
|
|
|
html_yes_button: i18n.t('Delete'),
|
|
|
|
on_click: delete_user_group,
|
|
|
|
});
|
2017-12-06 20:00:23 +01:00
|
|
|
});
|
2017-12-19 04:02:31 +01:00
|
|
|
|
|
|
|
$('#user-groups').on('keypress', '.user-group h4 > span', function (e) {
|
|
|
|
if (e.which === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2017-11-13 05:20:42 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.settings_user_groups = exports;
|